import 'dart:async'; import 'package:cutdex/app/editor_theme.dart'; import 'package:cutdex/features/properties/property_number_input.dart'; import 'package:cutdex/l10n/app_localizations.dart'; import 'package:flutter/material.dart'; import 'package:cutdex_ui/theme.dart'; import 'package:cutdex_ui/widgets.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { testWidgets('top parameter label retains drag, submit and escape', ( tester, ) async { var starts = 0; var ends = 0; var distance = 0.0; final submitted = []; await tester.pumpWidget( MaterialApp( localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, theme: AppTheme.propertiesPanelTheme( EditorTheme.desktop(EditorTheme.penDark), ), home: Scaffold( body: Center( child: SizedBox( width: 256, child: PropertyNumberInput( label: 'Rotation', unit: '°', value: 0, enabled: true, readOnly: false, scrubbing: false, editContext: 0, submit: (value) async { submitted.add(value); return true; }, onScrubStart: () { starts++; return true; }, onScrubUpdate: (value) => distance = value, onScrubEnd: () => ends++, onScrubCancel: () {}, ), ), ), ), ), ); await tester.pumpAndSettle(); expect(tester.getSize(find.byType(TextField)).height, 28); expect( tester .getRect( find .ancestor( of: find.byType(AppScrubLabel), matching: find.byType(Row), ) .first, ) .bottom, tester.getTopLeft(find.byType(TextField)).dy - 4, ); await tester.drag(find.byType(AppScrubLabel), const Offset(30, 0)); await tester.pump(); expect(starts, 1); expect(ends, 1); expect(distance, greaterThan(0)); await tester.enterText(find.byType(TextField), '45'); await tester.testTextInput.receiveAction(TextInputAction.done); await tester.pumpAndSettle(); expect(submitted, [45]); await tester.enterText(find.byType(TextField), '90'); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect( tester.widget(find.byType(TextField)).controller!.text, '0', ); expect(submitted, [45]); expect(tester.takeException(), isNull); }); testWidgets('old submission cannot overwrite input in a new edit context', ( tester, ) async { final input = _Input(); await input.mount(tester); await input.submit(tester, '11'); input.update(() { input.editContext = 1; input.value = 2; }); await tester.pump(); await tester.enterText(find.byType(TextField), '33'); input.pending.single.complete(false); await tester.pump(); expect(input.field(tester).controller!.text, '33'); expect(input.field(tester).decoration!.errorText, isNull); }); testWidgets('old submission cannot overwrite new input in the same context', ( tester, ) async { final input = _Input(); await input.mount(tester); await input.submit(tester, '11'); await tester.enterText(find.byType(TextField), '33'); input.pending.single.complete(false); await tester.pump(); expect(input.field(tester).controller!.text, '33'); expect(input.field(tester).decoration!.errorText, isNull); }); testWidgets('returning to the old context does not revive its submission', ( tester, ) async { final input = _Input(); await input.mount(tester); await input.submit(tester, '11'); input.update(() => input.editContext = 1); await tester.pump(); input.update(() => input.editContext = 0); await tester.pump(); input.pending.single.complete(false); await tester.pump(); expect(input.field(tester).controller!.text, '1'); expect(input.field(tester).decoration!.errorText, isNull); }); testWidgets( 'out of order completion preserves the latest submission result', (tester) async { final input = _Input(); await input.mount(tester); await input.submit(tester, '11'); await input.submit(tester, '33'); expect(input.values, [11, 33]); input.pending[1].complete(false); await tester.pump(); final error = input.field(tester).decoration!.errorText; expect(error, isNotEmpty); input.pending[0].complete(true); await tester.pump(); expect(input.field(tester).decoration!.errorText, error); }, ); testWidgets( 'normal save still completes when the parent disables the input', (tester) async { final input = _Input(); await input.mount(tester); await input.submit(tester, '11'); input.update(() => input.enabled = false); await tester.pump(); input.update(() { input.enabled = true; input.value = 11; }); await tester.pump(); input.pending.single.complete(true); await tester.pump(); expect(input.field(tester).controller!.text, '11'); expect(input.field(tester).decoration!.errorText, isNull); }, ); testWidgets('escape discards an outstanding submission result', ( tester, ) async { final input = _Input(); await input.mount(tester); await input.submit(tester, '11'); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pump(); input.pending.single.complete(false); await tester.pump(); expect(input.field(tester).controller!.text, '1'); expect(input.field(tester).decoration!.errorText, isNull); }); testWidgets('submission completion ignores a disposed input', (tester) async { final input = _Input(); await input.mount(tester); await input.submit(tester, '11'); await tester.pumpWidget(const SizedBox()); input.pending.single.complete(false); await tester.pump(); expect(tester.takeException(), isNull); }); } class _Input { int editContext = 0; double value = 1; bool enabled = true; final pending = >[]; final values = []; late StateSetter update; TextField field(WidgetTester tester) => tester.widget(find.byType(TextField)); Future submit(WidgetTester tester, String text) async { await tester.enterText(find.byType(TextField), text); await tester.testTextInput.receiveAction(TextInputAction.done); await tester.pump(); } Future mount(WidgetTester tester) => tester.pumpWidget( MaterialApp( theme: EditorTheme.penLight, localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, locale: const Locale('zh'), home: Scaffold( body: StatefulBuilder( builder: (_, setState) { update = setState; return PropertyNumberInput( label: 'X', value: value, enabled: enabled, readOnly: false, submit: (value) { values.add(value); final result = Completer(); pending.add(result); return result.future; }, onScrubUpdate: (_) {}, onScrubEnd: () {}, onScrubCancel: () {}, scrubbing: false, editContext: editContext, ); }, ), ), ), ); }