cleanup and added unit tests

This commit is contained in:
verboomp
2026-01-27 14:09:12 +01:00
parent 3d456128b1
commit e4b2dd0462
42 changed files with 1467 additions and 977 deletions

View File

@@ -0,0 +1,48 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fotodocumentation/utils/global_router.dart';
import 'package:fotodocumentation/utils/login_credentials.dart';
import 'package:mockito/mockito.dart';
import 'package:fotodocumentation/controller/customer_controller.dart';
import 'package:fotodocumentation/dto/customer_dto.dart';
import 'package:fotodocumentation/utils/di_container.dart';
import '../testing/test_utils.dart';
import '../testing/test_utils.mocks.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
DiContainer.instance.initState();
DiContainer.instance.put(LoginCredentials, getDefaultLoginCredentials());
group('Customer List Test', () {
testWidgets('Customer list screen search test', (WidgetTester tester) async {
await _searchtest(tester);
});
});
}
List<CustomerListDto> _list = [CustomerListDto(id: 1, customerNumber: "CODE1", name: "Customer 1"), CustomerListDto(id: 2, customerNumber: "CODE2", name: "Customer 2")];
Future<void> _searchtest(WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
String searchText = 'Henk';
var controller = MockCustomerController();
DiContainer.instance.put(CustomerController, controller);
when(controller.getAll("", "")).thenAnswer((_) async => _list);
when(controller.getAll(searchText, "")).thenAnswer((_) async => [_list.first]);
await pumpAppConfig(tester, GlobalRouter.pathHome);
verify(controller.getAll(argThat(equals("")), argThat(equals("")))).called(1);
await tester.enterText(find.byKey(Key("Search_text_field")), searchText);
await tester.testTextInput.receiveAction(TextInputAction.done);
await tester.pumpAndSettle();
verify(controller.getAll(searchText, "")).called(1);
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,200 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fotodocumentation/controller/login_controller.dart';
import 'package:fotodocumentation/dto/jwt_token_pair_dto.dart';
import 'package:fotodocumentation/pages/login/login_widget.dart';
import 'package:fotodocumentation/utils/di_container.dart';
import 'package:fotodocumentation/utils/global_router.dart';
import 'package:fotodocumentation/utils/login_credentials.dart';
import 'package:mockito/mockito.dart';
import '../testing/test_utils.dart';
import '../testing/test_utils.mocks.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
DiContainer.instance.initState();
late MockLoginController mockLoginController;
late MockLoginCredentials mockLoginCredentials;
setUp(() {
mockLoginController = MockLoginController();
mockLoginCredentials = MockLoginCredentials();
when(mockLoginCredentials.isLoggedIn).thenReturn(false);
DiContainer.instance.put(LoginController, mockLoginController);
DiContainer.instance.put(LoginCredentials, mockLoginCredentials);
});
group('LoginWidget', () {
testWidgets('displays login title', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(tester, const LoginWidget());
await tester.pumpAndSettle();
// Verify the login title is displayed (German localization)
expect(find.text('BILDERUPLOAD'), findsOneWidget);
});
testWidgets('displays username and password fields', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(tester, const LoginWidget());
await tester.pumpAndSettle();
// Verify username field exists
expect(find.byKey(const Key("username")), findsOneWidget);
// Verify password field exists
expect(find.byKey(const Key("password")), findsOneWidget);
});
testWidgets('displays login button', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(tester, const LoginWidget());
await tester.pumpAndSettle();
// Verify login button exists
expect(find.byKey(const Key("SubmitWidgetButton")), findsOneWidget);
});
testWidgets('can enter username and password', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(tester, const LoginWidget());
await tester.pumpAndSettle();
// Enter username
await tester.enterText(find.byKey(const Key("username")), 'testuser');
await tester.pumpAndSettle();
// Enter password
await tester.enterText(find.byKey(const Key("password")), 'testpassword');
await tester.pumpAndSettle();
// Verify text was entered
expect(find.text('testuser'), findsOneWidget);
});
testWidgets('calls authenticate on login button tap', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
final jwtTokenPairDto = JwtTokenPairDto(
accessToken: 'test_access_token',
refreshToken: 'test_refresh_token',
);
when(mockLoginController.authenticate('testuser', 'testpassword'))
.thenAnswer((_) async => (jwtTokenPairDto: jwtTokenPairDto));
await pumpAppConfig(tester, GlobalRouter.pathLogin);
await tester.pumpAndSettle();
// Enter credentials
await tester.enterText(find.byKey(const Key("username")), 'testuser');
await tester.enterText(find.byKey(const Key("password")), 'testpassword');
await tester.pumpAndSettle();
// Tap login button
await tester.tap(find.byKey(const Key("SubmitWidgetButton")));
await tester.pumpAndSettle();
// Verify authenticate was called with correct credentials
verify(mockLoginController.authenticate('testuser', 'testpassword')).called(1);
});
testWidgets('sets logged in on successful authentication', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
final jwtTokenPairDto = JwtTokenPairDto(
accessToken: 'test_access_token',
refreshToken: 'test_refresh_token',
);
when(mockLoginController.authenticate('testuser', 'testpassword'))
.thenAnswer((_) async => (jwtTokenPairDto: jwtTokenPairDto));
await pumpAppConfig(tester, GlobalRouter.pathLogin);
await tester.pumpAndSettle();
// Enter credentials
await tester.enterText(find.byKey(const Key("username")), 'testuser');
await tester.enterText(find.byKey(const Key("password")), 'testpassword');
await tester.pumpAndSettle();
// Tap login button
await tester.tap(find.byKey(const Key("SubmitWidgetButton")));
await tester.pumpAndSettle();
// Verify setLoggedIn was called
verify(mockLoginCredentials.setLoggedIn(true)).called(1);
});
testWidgets('displays error message on failed authentication', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
when(mockLoginController.authenticate('testuser', 'wrongpassword'))
.thenAnswer((_) async => (jwtTokenPairDto: null));
await pumpAppConfig(tester, GlobalRouter.pathLogin);
await tester.pumpAndSettle();
// Enter credentials
await tester.enterText(find.byKey(const Key("username")), 'testuser');
await tester.enterText(find.byKey(const Key("password")), 'wrongpassword');
await tester.pumpAndSettle();
// Tap login button
await tester.tap(find.byKey(const Key("SubmitWidgetButton")));
await tester.pumpAndSettle();
// Verify error message is displayed (German localization)
expect(find.text('Falscher Benutzername oder Passwort'), findsOneWidget);
});
testWidgets('does not call setLoggedIn on failed authentication', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
when(mockLoginController.authenticate('testuser', 'wrongpassword'))
.thenAnswer((_) async => (jwtTokenPairDto: null));
await pumpAppConfig(tester, GlobalRouter.pathLogin);
await tester.pumpAndSettle();
// Enter credentials
await tester.enterText(find.byKey(const Key("username")), 'testuser');
await tester.enterText(find.byKey(const Key("password")), 'wrongpassword');
await tester.pumpAndSettle();
// Tap login button
await tester.tap(find.byKey(const Key("SubmitWidgetButton")));
await tester.pumpAndSettle();
// Verify setLoggedIn was NOT called
verifyNever(mockLoginCredentials.setLoggedIn(any));
});
testWidgets('password field obscures text', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(tester, const LoginWidget());
await tester.pumpAndSettle();
// Find the EditableText descendant of the password field
// TextFormField wraps TextField which contains EditableText
final passwordFieldFinder = find.byKey(const Key("password"));
final editableTextFinder = find.descendant(
of: passwordFieldFinder,
matching: find.byType(EditableText),
);
final editableText = tester.widget<EditableText>(editableTextFinder);
// Verify obscureText is true
expect(editableText.obscureText, isTrue);
});
});
}

View File

@@ -0,0 +1,258 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fotodocumentation/dto/customer_dto.dart' show CustomerDto;
import 'package:fotodocumentation/dto/picture_dto.dart';
import 'package:fotodocumentation/pages/customer/picture_widget.dart';
import 'package:fotodocumentation/pages/customer/picture_fullscreen_dialog.dart';
import 'package:fotodocumentation/utils/di_container.dart';
import 'package:fotodocumentation/utils/login_credentials.dart';
import '../testing/test_utils.dart';
// Minimal valid base64 encoded 1x1 pixel PNG image
const String _testImage =
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
DiContainer.instance.initState();
DiContainer.instance.put(LoginCredentials, getDefaultLoginCredentials());
late CustomerDto customerDto;
late PictureDto pictureDto1;
late PictureDto pictureDto2;
late PictureDto pictureDto3;
setUp(() {
pictureDto1 = PictureDto(
id: 1,
comment: 'First picture comment',
category: 'category1',
image: _testImage,
pictureDate: DateTime(2024, 1, 15),
username: 'user1',
);
pictureDto2 = PictureDto(
id: 2,
comment: 'Second picture comment',
category: 'category2',
image: _testImage,
pictureDate: DateTime(2024, 2, 20),
username: 'user2',
);
pictureDto3 = PictureDto(
id: 3,
comment: null,
category: 'category3',
image: _testImage,
pictureDate: DateTime(2024, 3, 25),
username: 'user3',
);
customerDto = CustomerDto(
id: 1,
name: 'Test Apotheke',
customerNumber: 'CUST001',
pictures: [pictureDto1, pictureDto2, pictureDto3],
);
});
group('PictureWidget', () {
testWidgets('displays customer information correctly', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
PictureWidget(customerDto: customerDto, pictureDto: pictureDto1),
);
await tester.pumpAndSettle();
// Verify that the header is displayed
expect(find.text('INFORMATIONEN'), findsOneWidget);
// Verify customer name is displayed
expect(find.text('Test Apotheke'), findsOneWidget);
// Verify customer number is displayed
expect(find.text('CUST001'), findsOneWidget);
// Verify labels are displayed
expect(find.text('APOTHEKE'), findsOneWidget);
expect(find.text('KUNDENNUMMER'), findsOneWidget);
expect(find.text('DATUM'), findsOneWidget);
expect(find.text('KOMMENTAR'), findsOneWidget);
});
testWidgets('displays picture comment', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
PictureWidget(customerDto: customerDto, pictureDto: pictureDto1),
);
await tester.pumpAndSettle();
// Verify the comment is displayed
expect(find.text('First picture comment'), findsOneWidget);
});
testWidgets('displays empty string when comment is null', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
PictureWidget(customerDto: customerDto, pictureDto: pictureDto3),
);
await tester.pumpAndSettle();
// The comment field should be empty but the label should exist
expect(find.text('KOMMENTAR'), findsOneWidget);
});
testWidgets('shows close button', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
PictureWidget(customerDto: customerDto, pictureDto: pictureDto1),
);
await tester.pumpAndSettle();
// Verify close button icon exists
expect(find.byIcon(Icons.close), findsOneWidget);
});
testWidgets('shows right navigation button when there are more pictures', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
PictureWidget(customerDto: customerDto, pictureDto: pictureDto1),
);
await tester.pumpAndSettle();
// First picture should have right navigation (chevron_right), no left navigation
expect(find.byIcon(Icons.chevron_right), findsOneWidget);
expect(find.byIcon(Icons.chevron_left), findsNothing);
});
testWidgets('shows left navigation button when not on first picture', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
PictureWidget(customerDto: customerDto, pictureDto: pictureDto2),
);
await tester.pumpAndSettle();
// Middle picture should have both navigation buttons
expect(find.byIcon(Icons.chevron_left), findsOneWidget);
expect(find.byIcon(Icons.chevron_right), findsOneWidget);
});
testWidgets('shows only left navigation on last picture', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
PictureWidget(customerDto: customerDto, pictureDto: pictureDto3),
);
await tester.pumpAndSettle();
// Last picture should have left navigation, no right navigation
expect(find.byIcon(Icons.chevron_left), findsOneWidget);
expect(find.byIcon(Icons.chevron_right), findsNothing);
});
testWidgets('navigates to next picture when right button is tapped', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
PictureWidget(customerDto: customerDto, pictureDto: pictureDto1),
);
await tester.pumpAndSettle();
// Verify first picture comment is shown
expect(find.text('First picture comment'), findsOneWidget);
// Tap right navigation button
await tester.tap(find.byIcon(Icons.chevron_right));
await tester.pumpAndSettle();
// Verify second picture comment is now shown
expect(find.text('Second picture comment'), findsOneWidget);
expect(find.text('First picture comment'), findsNothing);
});
testWidgets('navigates to previous picture when left button is tapped', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
PictureWidget(customerDto: customerDto, pictureDto: pictureDto2),
);
await tester.pumpAndSettle();
// Verify second picture comment is shown
expect(find.text('Second picture comment'), findsOneWidget);
// Tap left navigation button
await tester.tap(find.byIcon(Icons.chevron_left));
await tester.pumpAndSettle();
// Verify first picture comment is now shown
expect(find.text('First picture comment'), findsOneWidget);
expect(find.text('Second picture comment'), findsNothing);
});
testWidgets('shows no navigation buttons when only one picture', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
final singlePictureCustomer = CustomerDto(
id: 1,
name: 'Test Apotheke',
customerNumber: 'CUST001',
pictures: [pictureDto1],
);
await pumpApp(
tester,
PictureWidget(customerDto: singlePictureCustomer, pictureDto: pictureDto1),
);
await tester.pumpAndSettle();
// No navigation buttons should be shown
expect(find.byIcon(Icons.chevron_left), findsNothing);
expect(find.byIcon(Icons.chevron_right), findsNothing);
});
testWidgets('opens fullscreen dialog when image is tapped', (WidgetTester tester) async {
setScreenSize(tester, 2048, 2048);
await pumpApp(
tester,
PictureWidget(customerDto: customerDto, pictureDto: pictureDto1),
);
await tester.pumpAndSettle();
// Find the GestureDetector with the image key
final imageFinder = find.byKey(const Key("image"));
expect(imageFinder, findsOneWidget);
// Ensure the widget is visible first
await tester.ensureVisible(imageFinder);
await tester.pumpAndSettle();
// Tap the image - use warnIfMissed: false since the Image.memory may have
// rendering issues in test environment but the GestureDetector should still work
await tester.tap(imageFinder, warnIfMissed: false);
await tester.pumpAndSettle();
// Verify fullscreen dialog is shown
expect(find.byType(PictureFullscreenDialog), findsOneWidget);
});
});
}

View File

@@ -0,0 +1,211 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fotodocumentation/controller/base_controller.dart';
import 'package:fotodocumentation/pages/ui_utils/component/general_error_widget.dart';
import 'package:fotodocumentation/utils/di_container.dart';
import 'package:fotodocumentation/utils/login_credentials.dart';
import '../../testing/test_utils.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
DiContainer.instance.initState();
DiContainer.instance.put(LoginCredentials, getDefaultLoginCredentials());
group('GeneralErrorWidget', () {
testWidgets('displays error icon', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
const GeneralErrorWidget(error: 'Test error'),
);
await tester.pumpAndSettle();
// Verify error icon is displayed
expect(find.byIcon(Icons.error_outline), findsOneWidget);
});
testWidgets('displays error message with error text', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
const GeneralErrorWidget(error: 'Something went wrong'),
);
await tester.pumpAndSettle();
// Verify error message is displayed (German localization: "Fehler: {name}")
expect(find.text('Fehler: Something went wrong'), findsOneWidget);
});
testWidgets('displays status code message when statusCode is provided', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
const GeneralErrorWidget(error: '', statusCode: 404),
);
await tester.pumpAndSettle();
// Verify status code message is displayed (German localization: "Statuscode {statusCode}")
expect(find.text('Statuscode 404'), findsOneWidget);
});
testWidgets('displays status code message for 500 error', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
const GeneralErrorWidget(error: '', statusCode: 500),
);
await tester.pumpAndSettle();
// Verify status code message is displayed
expect(find.text('Statuscode 500'), findsOneWidget);
});
testWidgets('does not display retry button when reload is null', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
const GeneralErrorWidget(error: 'Test error'),
);
await tester.pumpAndSettle();
// Verify retry button is not displayed
expect(find.byType(ElevatedButton), findsNothing);
expect(find.text('Wiederholen'), findsNothing);
});
testWidgets('displays retry button when reload is provided', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
GeneralErrorWidget(error: 'Test error', reload: () {}),
);
await tester.pumpAndSettle();
// Verify retry button is displayed (German localization: "Wiederholen")
expect(find.byType(ElevatedButton), findsOneWidget);
expect(find.text('Wiederholen'), findsOneWidget);
});
testWidgets('calls reload callback when retry button is tapped', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
bool reloadCalled = false;
await pumpApp(
tester,
GeneralErrorWidget(
error: 'Test error',
reload: () {
reloadCalled = true;
},
),
);
await tester.pumpAndSettle();
// Tap retry button
await tester.tap(find.byType(ElevatedButton));
await tester.pumpAndSettle();
// Verify reload was called
expect(reloadCalled, isTrue);
});
testWidgets('fromServerError factory creates widget with status code', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
final serverError = ServerError(403);
await pumpApp(
tester,
GeneralErrorWidget.fromServerError(serverError),
);
await tester.pumpAndSettle();
// Verify status code message is displayed
expect(find.text('Statuscode 403'), findsOneWidget);
});
testWidgets('fromServerError factory with reload callback', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
bool reloadCalled = false;
final serverError = ServerError(401);
await pumpApp(
tester,
GeneralErrorWidget.fromServerError(
serverError,
reload: () {
reloadCalled = true;
},
),
);
await tester.pumpAndSettle();
// Verify status code is displayed
expect(find.text('Statuscode 401'), findsOneWidget);
// Verify retry button is displayed and functional
expect(find.text('Wiederholen'), findsOneWidget);
await tester.tap(find.byType(ElevatedButton));
await tester.pumpAndSettle();
expect(reloadCalled, isTrue);
});
testWidgets('widget renders correctly', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
const GeneralErrorWidget(error: 'Test error'),
);
await tester.pumpAndSettle();
// Verify GeneralErrorWidget is rendered
expect(find.byType(GeneralErrorWidget), findsOneWidget);
// Verify it contains a Column for layout
final columnFinder = find.descendant(
of: find.byType(GeneralErrorWidget),
matching: find.byType(Column),
);
expect(columnFinder, findsOneWidget);
});
testWidgets('error icon has correct color', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
const GeneralErrorWidget(error: 'Test error'),
);
await tester.pumpAndSettle();
// Find the Icon widget and verify its color
final icon = tester.widget<Icon>(find.byIcon(Icons.error_outline));
expect(icon.color, equals(Colors.red[300]));
});
testWidgets('error icon has correct size', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
const GeneralErrorWidget(error: 'Test error'),
);
await tester.pumpAndSettle();
// Find the Icon widget and verify its size
final icon = tester.widget<Icon>(find.byIcon(Icons.error_outline));
expect(icon.size, equals(60));
});
});
}