diff --git a/lib/screens/flight_log/flight_log_modal.dart b/lib/screens/flight_log/flight_log_modal.dart index b1c7d69..365f494 100644 --- a/lib/screens/flight_log/flight_log_modal.dart +++ b/lib/screens/flight_log/flight_log_modal.dart @@ -196,7 +196,7 @@ class _FlightLogModalState extends State { // fuel + fuel price CupertinoFormSection(children: [ CupertinoTextFormFieldRow( - key: const Key("flight_log_modal_form_fuel_price"), + key: const Key("input_flightLogModal_fuel"), controller: _fuelController, prefix: Text(AppLocalizations.of(context)! .flightLogModal_label_fuel_cupertino), @@ -209,6 +209,7 @@ class _FlightLogModalState extends State { ), // TODO convert to standalone form row widget (using a controller? Though material widget doesn't support it...) CupertinoTextFormFieldRow( + key: const Key("input_flightLogModal_fuelPrice"), controller: _fuelPriceController, prefix: Text(AppLocalizations.of(context)! .flightLogModal_label_fuel_cost_cupertino( @@ -368,7 +369,7 @@ class _FlightLogModalState extends State { contentPadding: const EdgeInsets.fromLTRB(20, 5, 20, 5), leading: const Icon(Icons.local_gas_station), title: TextFormField( - key: const Key("flight_log_modal_form_fuel_price"), + key: const Key("input_flightLogModal_fuel"), controller: _fuelController, // TODO cursorColor: widget.model.backgroundColor, keyboardType: const TextInputType.numberWithOptions(decimal: true), @@ -389,6 +390,7 @@ class _FlightLogModalState extends State { trailing: SizedBox( width: MediaQuery.of(context).size.width * 0.4, child: _MaterialFuelPriceSelector.totalCost( + key: const Key("input_flightLogModal_fuelPrice"), textController: _fuelPriceController, currencySymbol: _appConfig.fuelPriceCurrency, ), @@ -442,6 +444,7 @@ class _FlightLogModalState extends State { ); trailingActions = [ PlatformTextButton( + key: const Key('button_flightLogModal_save'), onPressed: () => _onSave(context), cupertino: (_, __) => CupertinoTextButtonData( // workaround for https://github.com/flutter/flutter/issues/32701 @@ -454,6 +457,7 @@ class _FlightLogModalState extends State { leadingAction = null; trailingActions = [ PlatformIconButton( + key: const Key('button_flightLogModal_save'), onPressed: () => _onSave(context), icon: const Icon(Icons.check_sharp), material: (_, __) => MaterialIconButtonData( diff --git a/test/screens/flight_log/flight_log_modal_test.dart b/test/screens/flight_log/flight_log_modal_test.dart index 750d95b..84f223d 100644 --- a/test/screens/flight_log/flight_log_modal_test.dart +++ b/test/screens/flight_log/flight_log_modal_test.dart @@ -5,6 +5,7 @@ import 'package:airborne/services/flight_log_services.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:flutter_platform_widgets/flutter_platform_widgets.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; @@ -13,7 +14,10 @@ import 'package:provider/provider.dart'; import 'flight_log_modal_test.mocks.dart'; @GenerateMocks([AppConfig, FlightLogBookService]) -void main() { +void main() async { + const locale = Locale('en'); + final lang = await AppLocalizations.delegate.load(locale); + Widget createSkeletonApp(FlightLogItem model) => MaterialApp( localizationsDelegates: const [ AppLocalizations.delegate, @@ -21,7 +25,7 @@ void main() { GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], - locale: const Locale('en'), + locale: locale, home: MultiProvider( providers: [ _provideAppConfigForSampleAircraft(), @@ -48,39 +52,139 @@ void main() { await tester.pumpWidget(createSkeletonApp(item)); await tester.enterText( - find.byKey(const Key("flight_log_modal_form_fuel_price")), "42"); + find.byKey(const Key("input_flightLogModal_fuelPrice")), "42"); await tester.pump(); expect(tester.state(find.byType(Form)).validate(), true); await tester.enterText( - find.byKey(const Key("flight_log_modal_form_fuel_price")), "ABC"); + find.byKey(const Key("input_flightLogModal_fuelPrice")), "ABC"); await tester.pump(); expect(tester.state(find.byType(Form)).validate(), false); await tester.enterText( - find.byKey(const Key("flight_log_modal_form_fuel_price")), "43.2"); + find.byKey(const Key("input_flightLogModal_fuelPrice")), "43.2"); + await tester.pump(); + expect(tester.state(find.byType(Form)).validate(), true); + + await tester.enterText( + find.byKey(const Key("input_flightLogModal_fuelPrice")), "43.28293"); + await tester.pump(); + expect(tester.state(find.byType(Form)).validate(), true); + + await tester.enterText( + find.byKey(const Key("input_flightLogModal_fuelPrice")), "43.28"); await tester.pump(); expect(tester.state(find.byType(Form)).validate(), true); // FIXME this should fail on the locale of the test await tester.enterText( - find.byKey(const Key("flight_log_modal_form_fuel_price")), "43,2"); + find.byKey(const Key("input_flightLogModal_fuelPrice")), "43,2"); await tester.pump(); expect(tester.state(find.byType(Form)).validate(), true); }); testWidgets('Fuel amount validation', (tester) async { - // TODO + FlightLogItem item = FlightLogItem( + null, + DateTime.now(), + 'Sara', + 'Fly@localhost', + 'Fly@localhost', + 1238, + 1238, + null, + null, + null, + ); + await tester.pumpWidget(createSkeletonApp(item)); + + await tester.enterText( + find.byKey(const Key("input_flightLogModal_fuel")), "42"); + await tester.pump(); + expect(tester.state(find.byType(Form)).validate(), true); + + await tester.enterText( + find.byKey(const Key("input_flightLogModal_fuel")), "ABC"); + await tester.pump(); + expect(tester.state(find.byType(Form)).validate(), false); + + await tester.enterText( + find.byKey(const Key("input_flightLogModal_fuel")), "43.2"); + await tester.pump(); + expect(tester.state(find.byType(Form)).validate(), true); + + await tester.enterText( + find.byKey(const Key("input_flightLogModal_fuel")), "43.28"); + await tester.pump(); + expect(tester.state(find.byType(Form)).validate(), true); + + await tester.enterText( + find.byKey(const Key("input_flightLogModal_fuel")), "43.28"); + await tester.pump(); + expect(tester.state(find.byType(Form)).validate(), true); + + await tester.enterText( + find.byKey(const Key("input_flightLogModal_fuel")), "43.2802"); + await tester.pump(); + expect(tester.state(find.byType(Form)).validate(), true); + + // FIXME this should fail on the locale of the test + await tester.enterText( + find.byKey(const Key("input_flightLogModal_fuel")), "43,2"); + await tester.pump(); + expect(tester.state(find.byType(Form)).validate(), true); }); testWidgets('Fuel amount mandatory when fuel cost is greater than zero', (tester) async { - // TODO + FlightLogItem item = FlightLogItem( + null, + DateTime.now(), + 'Sara', + 'Fly@localhost', + 'Fly@localhost', + 1238, + 1238, + null, + null, + null, + ); + await tester.pumpWidget(createSkeletonApp(item)); + + await tester.enterText( + find.byKey(const Key("input_flightLogModal_fuelPrice")), "102.38"); + await tester.pump(); + await tester.tap(find.byKey(const Key('button_flightLogModal_save'))); + await tester.pump(); + expect(find.byType(PlatformAlertDialog), findsOneWidget); + expect(find.text(lang.flightLogModal_error_invalid_fuel_empty), + findsOneWidget); }); testWidgets('Fuel cost mandatory when fuel amount is greater than zero', (tester) async { - // TODO + FlightLogItem item = FlightLogItem( + null, + DateTime.now(), + 'Sara', + 'Fly@localhost', + 'Fly@localhost', + 1238, + 1238, + null, + null, + null, + ); + await tester.pumpWidget(createSkeletonApp(item)); + + await tester.enterText( + find.byKey(const Key("input_flightLogModal_fuel")), "102.38"); + await tester.pump(); + await tester.tap(find.byKey(const Key('button_flightLogModal_save'))); + await tester.pump(); + expect(find.byType(PlatformAlertDialog), findsOneWidget); + expect(find.text(lang.flightLogModal_error_invalid_fuelCost_empty), + findsOneWidget); }); }); }