import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/ui/adaptive/app_menu_anchor.dart'; void main() { testWidgets('Escape closes the menu and the keyboard returns to the field', ( tester, ) async { var picked = 0; await tester.pumpWidget( MaterialApp( home: Scaffold( body: Column( children: [ const TextField(key: ValueKey('field')), AppMenuAnchor( menuChildren: [ MenuItemButton( onPressed: () => picked++, child: const Text('First'), ), MenuItemButton( onPressed: () => picked += 10, child: const Text('Second'), ), ], builder: (context, menu, _) => IconButton( tooltip: 'More', onPressed: () => menu.isOpen ? menu.close() : menu.open(), icon: const Icon(Icons.more_vert), ), ), ], ), ), ), ); await tester.tap(find.byKey(const ValueKey('field'))); await tester.pump(); final field = FocusManager.instance.primaryFocus; await tester.tap(find.byTooltip('More')); await tester.pumpAndSettle(); expect(find.text('First'), findsOneWidget); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(find.text('First'), findsNothing); expect(FocusManager.instance.primaryFocus, same(field)); // The arrow keys and Enter work from wherever the keyboard was. await tester.tap(find.byTooltip('More')); await tester.pumpAndSettle(); await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(picked, 10); expect(find.text('First'), findsNothing); }); }