import 'package:cutdex_ui/theme.dart'; import 'package:cutdex_ui/widgets.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { testWidgets( 'native menu waits for right-button release and dismissal keeps left clicks working', (tester) async { debugDefaultTargetPlatformOverride = TargetPlatform.macOS; addTearDown(() => debugDefaultTargetPlatformOverride = null); const channel = MethodChannel('cutdex_native/context_menu'); var opened = 0; var clicked = 0; tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, ( _, ) async { opened++; return null; }); addTearDown( () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( channel, null, ), ); await tester.pumpWidget( MaterialApp( theme: AppTheme.penLight, home: Scaffold( body: Column( children: [ AppContextMenu( itemsBuilder: (_) => [ AppMenuItem(label: 'Action', onPressed: () {}), ], child: const SizedBox(width: 200, height: 200), ), TextButton( onPressed: () => clicked++, child: const Text('Other control'), ), ], ), ), ), ); for (var i = 0; i < 2; i++) { final pointer = await tester.startGesture( tester.getCenter(find.byType(AppContextMenu)), kind: PointerDeviceKind.mouse, buttons: kSecondaryMouseButton, ); // Hold past the tap-down deadline: native tracking must not start yet. await tester.pump(const Duration(milliseconds: 200)); expect(opened, i); await pointer.up(); await tester.pumpAndSettle(); expect(opened, i + 1); expect(find.text('Action'), findsNothing); await tester.tap(find.text('Other control')); await tester.pumpAndSettle(); expect(clicked, i + 1); } expect(tester.takeException(), isNull); debugDefaultTargetPlatformOverride = null; }, ); testWidgets( 'searchable shared select filters languages and resets after selection', (tester) async { var value = 'ja'; await tester.pumpWidget( MaterialApp( theme: AppTheme.penLight, home: Scaffold( body: SizedBox( width: 280, child: StatefulBuilder( builder: (context, setState) => AppSelect( emptyLabel: '没有匹配的选项', label: '源语言', value: value, searchable: true, searchHint: '搜索选项', onChanged: (next) => setState(() => value = next), options: const [ AppSelectOption('ja', '日语 (ja)'), AppSelectOption('zh', '中文 (zh)'), AppSelectOption('en', '英语 (en)', enabled: false), ], ), ), ), ), ), ); await tester.tap(find.byType(TextField)); await tester.pumpAndSettle(); await tester.enterText(find.byType(TextField), 'zh'); await tester.pumpAndSettle(); expect(find.widgetWithText(MenuItemButton, '日语 (ja)'), findsNothing); await tester.tap(find.widgetWithText(MenuItemButton, '中文 (zh)')); await tester.pumpAndSettle(); expect(value, 'zh'); await tester.tap(find.byType(TextField)); await tester.pumpAndSettle(); expect(find.widgetWithText(MenuItemButton, '日语 (ja)'), findsOneWidget); expect( tester .widget( find.widgetWithText(MenuItemButton, '英语 (en)'), ) .onPressed, isNull, ); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(find.byType(MenuItemButton), findsNothing); expect( tester.widget(find.byType(TextField)).controller!.text, '中文 (zh)', ); await tester.tap(find.byType(TextField)); await tester.enterText(find.byType(TextField), 'unknown'); await tester.pumpAndSettle(); expect(find.text('没有匹配的选项'), findsOneWidget); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(value, 'zh'); expect( tester.widget(find.byType(TextField)).controller!.text, '中文 (zh)', ); await tester.tap(find.byType(TextField)); await tester.enterText(find.byType(TextField), 'ja'); await tester.pumpAndSettle(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(value, 'ja'); expect( tester.widget(find.byType(TextField)).controller!.text, '日语 (ja)', ); expect(tester.takeException(), isNull); }, ); testWidgets( 'stationary mouse scroll releases old hover focus without shifting the menu', (tester) async { await tester.pumpWidget( MaterialApp( theme: AppTheme.penDark, home: Scaffold( body: SizedBox( width: 280, child: AppSelect( emptyLabel: 'No matching options', label: 'Language', value: 0, searchable: true, searchHint: '搜索选项', onChanged: (_) {}, options: [ for (var i = 0; i < 40; i++) AppSelectOption(i, 'Language $i'), ], ), ), ), ), ); await tester.tap(find.byType(TextField)); await tester.pumpAndSettle(); final previous = find.widgetWithText(MenuItemButton, 'Language 3'); final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse); await mouse.addPointer(location: Offset.zero); await mouse.moveTo(tester.getCenter(previous)); await tester.pumpAndSettle(); expect( tester.widget(previous).focusNode!.hasFocus, isFalse, ); final scrollable = tester .stateList(find.byType(Scrollable)) .firstWhere((state) => state.axisDirection == AxisDirection.down); scrollable.position.jumpTo(24); // Do not move the pointer: allow the normal transition to finish. await tester.pumpAndSettle(); expect( tester.widget(previous).focusNode!.hasFocus, isFalse, ); expect(scrollable.position.pixels, 24); final highlight = const Color(0xFFFCFCFC).withValues(alpha: 0.078); final backgrounds = tester.widgetList( find.descendant( of: find.byType(MenuItemButton), matching: find.byType(Material), ), ); expect( backgrounds.where((material) => material.color == highlight), hasLength(1), ); await mouse.removePointer(); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); }, ); testWidgets('select works inside a dialog in both themes', (tester) async { for (final theme in [AppTheme.penLight, AppTheme.penDark]) { var value = 1; await tester.pumpWidget( MaterialApp( theme: theme, home: StatefulBuilder( builder: (context, setState) => Scaffold( body: AlertDialog( content: SizedBox( width: 360, child: AppSelect( emptyLabel: 'No matching options', value: value, label: 'Theme', options: const [ AppSelectOption(1, 'System'), AppSelectOption(2, 'Dark'), AppSelectOption(3, 'Disabled', enabled: false), ], onChanged: (next) => setState(() => value = next), ), ), ), ), ), ), ); await tester.tap(find.byType(TextButton)); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); expect(find.byIcon(Icons.check), findsOneWidget); expect( tester.getSize(find.widgetWithText(MenuItemButton, 'Dark')).height, 28, ); expect( tester .widget( find.widgetWithText(MenuItemButton, 'Disabled'), ) .onPressed, isNull, ); await tester.tap(find.widgetWithText(MenuItemButton, 'Dark')); await tester.pumpAndSettle(); expect(value, 2); expect(find.byType(MenuItemButton), findsNothing); } }); testWidgets('dropdown supports keyboard selection and escape', ( tester, ) async { var selected = false; late FocusNode triggerFocus; await tester.pumpWidget( MaterialApp( theme: AppTheme.penLight, home: Scaffold( body: AppDropdownMenu( items: [ AppMenuItem(label: 'Action', onPressed: () => selected = true), ], builder: (context, toggle, focus) { triggerFocus = focus; return TextButton( focusNode: focus, onPressed: toggle, child: const Text('Open'), ); }, ), ), ), ); triggerFocus.requestFocus(); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); await tester.pumpAndSettle(); expect(find.text('Action'), findsOneWidget); await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(selected, isTrue); expect(triggerFocus.hasFocus, isTrue); await tester.tap(find.text('Open')); await tester.pumpAndSettle(); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(find.text('Action'), findsNothing); expect(triggerFocus.hasFocus, isTrue); }); testWidgets('context menu clamps at edges and opens submenu', (tester) async { var selected = false; await tester.pumpWidget( MaterialApp( theme: AppTheme.penDark, home: Scaffold( body: AppContextMenu( itemsBuilder: (_) => [ AppSubmenu( label: 'More', items: [ AppMenuItem( label: 'Action', onPressed: () => selected = true, ), ], ), ], child: const SizedBox.expand(), ), ), ), ); await tester.tapAt(const Offset(795, 595), buttons: kSecondaryMouseButton); await tester.pumpAndSettle(); final rect = tester.getRect(find.byType(SubmenuButton)); expect(rect.right, lessThanOrEqualTo(800)); expect(rect.bottom, lessThanOrEqualTo(600)); await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight); await tester.pumpAndSettle(); await tester.tap(find.text('Action')); await tester.pumpAndSettle(); expect(selected, isTrue); expect(find.text('More'), findsNothing); expect(tester.takeException(), isNull); }); testWidgets( 'context menu stays beside a bottom pointer in an offset region', (tester) async { await tester.pumpWidget( MaterialApp( theme: AppTheme.penDark, home: Scaffold( body: Padding( padding: const EdgeInsets.only(left: 180, top: 120), child: Align( alignment: Alignment.topLeft, child: SizedBox( width: 400, height: 460, child: AppContextMenu( itemsBuilder: (_) => const [ AppMenuItem(label: 'Action 1'), AppMenuItem(label: 'Action 2'), AppMenuItem(label: 'Action 3'), AppMenuItem(label: 'Action 4'), AppMenuItem(label: 'Action 5'), ], child: const ColoredBox(color: Colors.black), ), ), ), ), ), ), ); const pointer = Offset(340, 560); await tester.tapAt(pointer, buttons: kSecondaryMouseButton); await tester.pumpAndSettle(); final items = find.byType(MenuItemButton); expect(items, findsNWidgets(5)); final first = tester.getRect(items.first); final last = tester.getRect(items.last); expect(first.left, closeTo(pointer.dx + 6, 1)); expect(first.top, greaterThan(380)); expect(last.bottom, lessThanOrEqualTo(pointer.dy)); }, ); testWidgets( 'context menu only focuses its first item when opened by keyboard', (tester) async { await tester.pumpWidget( MaterialApp( theme: AppTheme.penDark, home: Scaffold( body: AppContextMenu( itemsBuilder: (_) => const [AppMenuItem(label: 'Action')], child: const SizedBox.expand(), ), ), ), ); await tester.tapAt( const Offset(300, 300), buttons: kSecondaryMouseButton, ); await tester.pumpAndSettle(); expect(find.text('Action'), findsOneWidget); expect(FocusManager.instance.primaryFocus, isA()); expect(FocusManager.instance.primaryFocus, isNot(isA())); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); await tester.sendKeyEvent(LogicalKeyboardKey.contextMenu); await tester.pumpAndSettle(); expect(find.text('Action'), findsOneWidget); expect(FocusManager.instance.primaryFocus, isA()); }, ); }