ANDREAS_LATANOWSKY_CPD_1910877/test/widget_test.dart

126 lines
4.8 KiB
Dart
Raw Normal View History

2024-01-10 23:39:33 +01:00
import 'dart:io';
import 'package:cpd_app/session_list.dart';
2023-11-21 02:29:46 +01:00
import 'package:flutter_test/flutter_test.dart';
2024-01-10 23:39:33 +01:00
import 'package:cpd_app/main.dart';
import 'package:cpd_app/ocr_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
Future<Uint8List> loadImageBytesFromAssets(String imagePath) async {
final ByteData data = await rootBundle.load(imagePath);
return data.buffer.asUint8List();
}
2023-11-21 02:29:46 +01:00
void main() {
2024-01-10 23:39:33 +01:00
setUpAll(() {
HttpOverrides.global = null;
});
testWidgets('Check ocr UI', (WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
final buttonsFinder = find.byType(ElevatedButton);
expect(buttonsFinder, findsWidgets);
await tester.tap(find.widgetWithText(ElevatedButton, "Image Tools"));
await tester.pumpAndSettle();
expect(find.text('OCR'), findsOneWidget);
var ocrbtn = find.widgetWithText(ElevatedButton, "OCR");
expect(ocrbtn, findsOneWidget);
var imgtrans = find.widgetWithText(ElevatedButton, "Image Translation");
expect(imgtrans, findsOneWidget);
var prev = find.widgetWithText(ElevatedButton, "Previous Images");
expect(prev, findsOneWidget);
await tester.tap(find.widgetWithText(ElevatedButton, "OCR"));
await tester.pumpAndSettle();
expect(find.text('OCR'), findsOneWidget);
expect(find.text('Select Image'), findsOneWidget);
expect(find.widgetWithText(DropdownButton<String>, 'Auto'), findsOneWidget);
await tester.tap(find.widgetWithText(DropdownButton<String>, 'Auto'));
await tester.pumpAndSettle();
expect(find.text('German'), findsOneWidget);
expect(find.text('English'), findsOneWidget);
expect(find.text('French'), findsOneWidget);
expect(find.text('Russian'), findsOneWidget);
await tester.tap(find.widgetWithText(DropdownMenuItem<String>, 'German'));
await tester.tap(find.widgetWithText(ElevatedButton, "Select Image"));
await tester.pump();
final ocrFinder = find.byType(OCRPage);
final ocrState = tester.state<OCRPageState>(ocrFinder);
Uint8List bytes = await loadImageBytesFromAssets('assets/schild.png');
ocrState.setState(() {
var image = XFile.fromData(bytes);
ocrState.selectedImage = image;
});
expect(ocrState.selectedImage, isNotNull);
await tester.pumpAndSettle();
expect(find.widgetWithText(ElevatedButton, "Extract Text"), findsOneWidget);
});
testWidgets('Check imageTranslateUI', (WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
final buttonsFinder = find.byType(ElevatedButton);
expect(buttonsFinder, findsWidgets);
await tester.tap(find.widgetWithText(ElevatedButton, "Image Tools"));
await tester.pumpAndSettle();
expect(find.text('Image Translation'), findsOneWidget);
var btn1 = find.widgetWithText(ElevatedButton, "Image Translation");
expect(btn1, findsOneWidget);
await tester.tap(btn1);
await tester.pumpAndSettle();
expect(find.text('Image Translation'), findsOneWidget);
expect(
find.widgetWithText(DropdownButton<String>, 'English'), findsOneWidget);
await tester.tap(find.widgetWithText(DropdownButton<String>, 'English'));
await tester.pumpAndSettle();
expect(find.text('German'), findsExactly(2));
expect(find.text('English'), findsExactly(2));
expect(find.text('French'), findsExactly(1));
expect(find.text('Russian'), findsExactly(1));
await tester.pumpAndSettle();
await tester.tap(find.widgetWithText(DropdownButton<String>, 'German'));
expect(find.text('German'), findsExactly(2));
expect(find.text('English'), findsExactly(2));
expect(find.text('French'), findsExactly(1));
expect(find.text('Russian'), findsExactly(1));
//
});
testWidgets('Check listPreview', (WidgetTester tester) async {
ImageList.addImage(
"test1", await loadImageBytesFromAssets('assets/schild.png'));
ImageList.addImage(
"test2", await loadImageBytesFromAssets('assets/schild.png'));
ImageList.addImage(
"test3", await loadImageBytesFromAssets('assets/schild.png'));
ImageList.addImage(
"test3", await loadImageBytesFromAssets('assets/schild.png'));
await tester.pumpWidget(const MyApp());
final buttonsFinder = find.byType(ElevatedButton);
expect(buttonsFinder, findsWidgets);
2023-11-21 19:48:58 +01:00
2024-01-10 23:39:33 +01:00
await tester.tap(find.widgetWithText(ElevatedButton, "Image Tools"));
await tester.pumpAndSettle();
2023-11-21 19:48:58 +01:00
2024-01-10 23:39:33 +01:00
var btn1 = find.widgetWithText(ElevatedButton, "Previous Images");
expect(btn1, findsOneWidget);
await tester.tap(btn1);
await tester.pumpAndSettle();
expect(find.text('Image Gallery'), findsOneWidget);
2023-11-21 19:48:58 +01:00
2024-01-10 23:39:33 +01:00
final gridViewFinder = find.byType(GridView);
expect(gridViewFinder, findsOneWidget);
2023-11-21 19:48:58 +01:00
2024-01-10 23:39:33 +01:00
const numberOfEntries = 4;
expect(find.byType(GestureDetector), findsNWidgets(numberOfEntries));
2023-11-21 02:29:46 +01:00
});
}