|
| 1 | +import 'package:auto_route/auto_route.dart'; |
| 2 | +import 'package:flutter/foundation.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:integration_test/integration_test.dart'; |
| 5 | +import 'package:material_ui/material_ui.dart'; |
| 6 | + |
| 7 | +import 'support/app.dart'; |
| 8 | +import 'support/autocomplete_state_field.dart'; |
| 9 | +import 'support/browser_focus.dart' |
| 10 | + if (dart.library.js_interop) 'support/browser_focus_web.dart'; |
| 11 | + |
| 12 | +// A text field in a nested router keeps focus while its Autocomplete options |
| 13 | +// open and close, with semantics on. |
| 14 | +// Reproduces https://github.com/flutter/flutter/issues/194873 in auto_route. |
| 15 | +// Headless test: auto_route/test/nested_router_semantics_test.dart |
| 16 | +void main() { |
| 17 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 18 | + |
| 19 | + group('nested router text field focus', () { |
| 20 | + // Steps: with semantics on, open a page beside a semantics container |
| 21 | + // that shows an AutoRouter, which shows another AutoRouter, which shows |
| 22 | + // an Autocomplete field. Tap the field (its options open), type a letter |
| 23 | + // no option holds (they close), then delete it (they open again). |
| 24 | + // Should: the field keeps its semantics parents, so on the web the |
| 25 | + // browser's focus stays on the field's <input>. |
| 26 | + testWidgets('a nested text field keeps focus', (tester) async { |
| 27 | + final semantics = tester.ensureSemantics(); |
| 28 | + final router = RootStackRouter.build( |
| 29 | + routes: [ |
| 30 | + AutoRoute( |
| 31 | + path: '/', |
| 32 | + page: PageInfo( |
| 33 | + 'OuterShellRoute', |
| 34 | + builder: (_) => Scaffold( |
| 35 | + body: Row( |
| 36 | + children: [ |
| 37 | + Semantics( |
| 38 | + container: true, |
| 39 | + label: 'Shell navigation', |
| 40 | + child: const SizedBox(width: 80, child: Text('Nav')), |
| 41 | + ), |
| 42 | + const Expanded(child: AutoRouter()), |
| 43 | + ], |
| 44 | + ), |
| 45 | + ), |
| 46 | + ), |
| 47 | + children: [ |
| 48 | + AutoRoute( |
| 49 | + path: '', |
| 50 | + page: PageInfo( |
| 51 | + 'InnerShellRoute', |
| 52 | + builder: (_) => const AutoRouter(), |
| 53 | + ), |
| 54 | + children: [ |
| 55 | + AutoRoute( |
| 56 | + path: '', |
| 57 | + page: PageInfo( |
| 58 | + 'StateFieldRoute', |
| 59 | + builder: (_) => const AutocompleteStateField(), |
| 60 | + ), |
| 61 | + ), |
| 62 | + ], |
| 63 | + ), |
| 64 | + ], |
| 65 | + ), |
| 66 | + ], |
| 67 | + ); |
| 68 | + await pumpRouterApp(tester, router); |
| 69 | + |
| 70 | + List<int> fieldParents() { |
| 71 | + final parents = <int>[]; |
| 72 | + for ( |
| 73 | + var node = tester.getSemantics(find.byType(EditableText)).parent; |
| 74 | + node != null; |
| 75 | + node = node.parent |
| 76 | + ) { |
| 77 | + parents.add(node.id); |
| 78 | + } |
| 79 | + return parents; |
| 80 | + } |
| 81 | + |
| 82 | + final unfocused = fieldParents(); |
| 83 | + // Off the web there is no DOM, so only the semantics parents count. |
| 84 | + Future<void> expectStillFocused( |
| 85 | + String step, { |
| 86 | + required bool areOptionsShown, |
| 87 | + }) async { |
| 88 | + await tester.pumpAndSettle(); |
| 89 | + expect( |
| 90 | + find.text('Texas'), |
| 91 | + areOptionsShown ? findsOneWidget : findsNothing, |
| 92 | + ); |
| 93 | + expect(fieldParents(), unfocused, reason: step); |
| 94 | + if (kIsWeb) expect(browserFocusedTag(), 'INPUT', reason: step); |
| 95 | + } |
| 96 | + |
| 97 | + await tester.tap(find.byType(TextField)); |
| 98 | + await expectStillFocused('options opened', areOptionsShown: true); |
| 99 | + await tester.enterText(find.byType(TextField), 'q'); |
| 100 | + await expectStillFocused('options closed', areOptionsShown: false); |
| 101 | + await tester.enterText(find.byType(TextField), ''); |
| 102 | + await expectStillFocused('options opened again', areOptionsShown: true); |
| 103 | + semantics.dispose(); |
| 104 | + }); |
| 105 | + }); |
| 106 | +} |
0 commit comments