diff --git a/lib/screens/metering/components/shared/readings_container/components/shared/animated_dialog_picker/components/dialog_picker/widget_picker_dialog.dart b/lib/screens/metering/components/shared/readings_container/components/shared/animated_dialog_picker/components/dialog_picker/widget_picker_dialog.dart index 3c253b1..9fdb8fa 100644 --- a/lib/screens/metering/components/shared/readings_container/components/shared/animated_dialog_picker/components/dialog_picker/widget_picker_dialog.dart +++ b/lib/screens/metering/components/shared/readings_container/components/shared/animated_dialog_picker/components/dialog_picker/widget_picker_dialog.dart @@ -118,7 +118,11 @@ class _DialogPickerState extends State> { ), const SizedBox(width: Dimens.grid16), TextButton( - onPressed: () => widget.onSelect(_selectedValue), + onPressed: () { + if (widget.initialValue != _selectedValue) { + widget.onSelect(_selectedValue); + } + }, child: Text(S.of(context).select), ), ], diff --git a/test/screens/metering/components/shared/readings_container/shared/animated_dialog_picker_test.dart b/test/screens/metering/components/shared/readings_container/shared/animated_dialog_test.dart similarity index 100% rename from test/screens/metering/components/shared/readings_container/shared/animated_dialog_picker_test.dart rename to test/screens/metering/components/shared/readings_container/shared/animated_dialog_test.dart diff --git a/test/screens/metering/components/shared/readings_container/shared/dialog_picker_test.dart b/test/screens/metering/components/shared/readings_container/shared/dialog_picker_test.dart new file mode 100644 index 0000000..56a05d8 --- /dev/null +++ b/test/screens/metering/components/shared/readings_container/shared/dialog_picker_test.dart @@ -0,0 +1,89 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:lightmeter/generated/l10n.dart'; +import 'package:lightmeter/res/dimens.dart'; +import 'package:lightmeter/screens/metering/components/shared/readings_container/components/iso_picker/widget_picker_iso.dart'; +import 'package:lightmeter/screens/metering/components/shared/readings_container/components/shared/animated_dialog_picker/components/dialog_picker/widget_picker_dialog.dart'; +import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; +import 'package:mocktail/mocktail.dart'; + +import '../../../../../../application_mock.dart'; + +class _ValueChanged { + void onChanged(T value) {} +} + +class _MockValueChanged extends Mock implements _ValueChanged {} + +void main() { + final functions = _MockValueChanged(); + + group( + 'onChanged', + () { + testWidgets( + 'other', + (tester) async { + await tester.pumpApplication(functions.onChanged); + await tester.openAnimatedPicker(); + expect(find.byType(DialogPicker), findsOneWidget); + await tester.tapListTile(500); + await tester.tapSelectButton(); + verify(() => functions.onChanged(const IsoValue(500, StopType.third))).called(1); + }, + ); + + testWidgets( + 'same', + (tester) async { + await tester.pumpApplication(functions.onChanged); + await tester.openAnimatedPicker(); + expect(find.byType(DialogPicker), findsOneWidget); + await tester.tapListTile(400); + await tester.tapSelectButton(); + verifyNever(() => functions.onChanged(const IsoValue(400, StopType.full))); + }, + ); + }, + ); +} + +extension WidgetTesterActions on WidgetTester { + Future pumpApplication(ValueChanged onChanged) async { + await pumpWidget( + WidgetTestApplicationMock( + child: Row( + children: [ + Expanded( + child: IsoValuePicker( + selectedValue: const IsoValue(400, StopType.full), + values: IsoValue.values, + onChanged: onChanged, + ), + ), + ], + ), + ), + ); + await pumpAndSettle(); + } + + Future openAnimatedPicker() async { + await tap(find.byType(T)); + await pumpAndSettle(Dimens.durationL); + } + + Future tapListTile(int iso) async { + expect(find.descendant(of: find.byType(RadioListTile), matching: find.text('$iso')), findsOneWidget); + await tap(find.descendant(of: find.byType(RadioListTile), matching: find.text('$iso'))); + } + + Future tapSelectButton() async { + final cancelButton = find.byWidgetPredicate( + (widget) => widget is TextButton && widget.child is Text && (widget.child as Text?)?.data == S.current.select, + ); + expect(cancelButton, findsOneWidget); + await tap(cancelButton); + await pumpAndSettle(); + } +}