Files
hartmann-foto_documentation/hartmann-foto-documentation-frontend/test/pages/picture_widget_test.dart
2026-01-29 07:08:44 +01:00

249 lines
8.0 KiB
Dart

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',
evaluation: 1,
);
pictureDto2 = PictureDto(
id: 2,
comment: 'Second picture comment',
category: 'category2',
image: _testImage,
pictureDate: DateTime(2024, 2, 20),
username: 'user2',
evaluation: 1,
);
pictureDto3 = PictureDto(
id: 3,
comment: null,
category: 'category3',
image: _testImage,
pictureDate: DateTime(2024, 3, 25),
username: 'user3',
evaluation: 1,
);
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 both navigation buttons at bottom', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
PictureWidget(customerDto: customerDto, pictureDto: pictureDto1),
);
await tester.pumpAndSettle();
// Both navigation buttons should always be visible at the bottom
expect(find.byIcon(Icons.chevron_left), findsOneWidget);
expect(find.byIcon(Icons.chevron_right), findsOneWidget);
});
testWidgets('shows both navigation buttons when on middle picture', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
PictureWidget(customerDto: customerDto, pictureDto: pictureDto2),
);
await tester.pumpAndSettle();
// Both navigation buttons should be visible
expect(find.byIcon(Icons.chevron_left), findsOneWidget);
expect(find.byIcon(Icons.chevron_right), findsOneWidget);
});
testWidgets('shows both navigation buttons on last picture', (WidgetTester tester) async {
setScreenSize(tester, 1024, 1024);
await pumpApp(
tester,
PictureWidget(customerDto: customerDto, pictureDto: pictureDto3),
);
await tester.pumpAndSettle();
// Both navigation buttons should be visible (right one is disabled)
expect(find.byIcon(Icons.chevron_left), findsOneWidget);
expect(find.byIcon(Icons.chevron_right), findsOneWidget);
});
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 disabled 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();
// Both navigation buttons should be shown but disabled
expect(find.byIcon(Icons.chevron_left), findsOneWidget);
expect(find.byIcon(Icons.chevron_right), findsOneWidget);
});
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);
});
});
}