import 'package:cutdex_ui/icons.dart'; 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() { for (final dismissal in ['trigger', 'selection', 'outside']) { testWidgets( 'pointer $dismissal dismissal releases menu trigger highlight', (tester) async { late FocusNode triggerFocus; final states = WidgetStatesController(); addTearDown(states.dispose); var closed = 0; await tester.pumpWidget( MaterialApp( theme: AppTheme.penDark, home: Scaffold( body: Align( alignment: Alignment.topLeft, child: AppDropdownMenu( onClose: () => closed++, items: [AppMenuItem(label: 'Action', onPressed: () {})], builder: (_, toggle, focus) { triggerFocus = focus; return TextButton( focusNode: focus, statesController: states, onPressed: toggle, child: const Text('Open'), ); }, ), ), ), ), ); final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse); await mouse.addPointer(location: const Offset(400, 400)); Future click(Offset point) async { await mouse.moveTo(point); await mouse.down(point); await mouse.up(); } await click(tester.getCenter(find.text('Open'))); await tester.pumpAndSettle(); expect(find.text('Action'), findsOneWidget); switch (dismissal) { case 'trigger': await click(tester.getCenter(find.text('Open'))); case 'selection': await click(tester.getCenter(find.text('Action'))); case 'outside': await click(const Offset(400, 400)); } await mouse.moveTo(const Offset(450, 450)); await tester.pumpAndSettle(); expect(find.text('Action'), findsNothing); expect(closed, 1); expect(triggerFocus.hasFocus, isFalse); expect(states.value, isNot(contains(WidgetState.focused))); expect(states.value, isNot(contains(WidgetState.hovered))); await mouse.removePointer(); expect(tester.takeException(), isNull); }, ); } for (final kind in [PointerDeviceKind.mouse, PointerDeviceKind.touch]) { testWidgets('dropdown outside $kind click activates buttons and inputs', ( tester, ) async { var outsideClicks = 0; var menuActions = 0; final inputFocus = FocusNode(); addTearDown(inputFocus.dispose); await tester.pumpWidget( MaterialApp( theme: AppTheme.penLight, home: Scaffold( body: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ AppDropdownMenu( items: [ AppMenuItem( label: 'Menu action', onPressed: () => menuActions++, ), ], builder: (_, toggle, focus) => TextButton( focusNode: focus, onPressed: toggle, child: const Text('Open menu'), ), ), const SizedBox(height: 200), TextButton( onPressed: () => outsideClicks++, child: const Text('Outside button'), ), SizedBox(width: 240, child: TextField(focusNode: inputFocus)), ], ), ), ), ); await tester.tap(find.text('Open menu'), kind: kind); await tester.pumpAndSettle(); expect(find.text('Menu action'), findsOneWidget); await tester.tap(find.text('Outside button'), kind: kind); await tester.pumpAndSettle(); expect(find.text('Menu action'), findsNothing); expect(outsideClicks, 1); await tester.tap(find.text('Open menu'), kind: kind); await tester.pumpAndSettle(); await tester.tap(find.byType(TextField), kind: kind); await tester.pumpAndSettle(); expect(find.text('Menu action'), findsNothing); expect(inputFocus.hasFocus, isTrue); expect(menuActions, 0); expect(tester.takeException(), isNull); }); testWidgets('dropdowns and selects switch on the first $kind click', ( tester, ) async { var selected = 0; var actions = 0; Widget dropdown(String name) => AppDropdownMenu( items: [AppMenuItem(label: '$name action', onPressed: () => actions++)], builder: (_, toggle, focus) => TextButton(focusNode: focus, onPressed: toggle, child: Text(name)), ); await tester.pumpWidget( MaterialApp( theme: AppTheme.penDark, home: Scaffold( body: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.start, children: [ dropdown('First'), dropdown('Second'), SizedBox( width: 200, child: AppSelect( value: selected, options: const [ AppSelectOption(0, 'Automatic'), AppSelectOption(1, 'Manual'), ], onChanged: (next) => selected = next, label: 'Mode', emptyLabel: 'No options', ), ), ], ), ), ), ); await tester.tap(find.text('First'), kind: kind); await tester.pumpAndSettle(); expect(find.text('First action'), findsOneWidget); await tester.tap(find.text('Second'), kind: kind); await tester.pumpAndSettle(); expect(find.text('First action'), findsNothing); expect(find.text('Second action'), findsOneWidget); // Clicking the open menu's own trigger still closes it. await tester.tap(find.text('Second'), kind: kind); await tester.pumpAndSettle(); expect(find.text('Second action'), findsNothing); await tester.tap(find.text('First'), kind: kind); await tester.pumpAndSettle(); await tester.tap( find.widgetWithText(TextButton, 'Automatic'), kind: kind, ); await tester.pumpAndSettle(); expect(find.text('First action'), findsNothing); expect(find.widgetWithText(MenuItemButton, 'Manual'), findsOneWidget); await tester.tap(find.text('First'), kind: kind); await tester.pumpAndSettle(); expect(find.widgetWithText(MenuItemButton, 'Manual'), findsNothing); expect(find.text('First action'), findsOneWidget); expect(selected, 0); await tester.tap(find.text('First action'), kind: kind); await tester.pumpAndSettle(); expect(actions, 1); expect(find.text('First action'), findsNothing); expect(tester.takeException(), isNull); }); } testWidgets('Flutter context menu outside click also activates its target', ( tester, ) async { debugDefaultTargetPlatformOverride = TargetPlatform.windows; addTearDown(() => debugDefaultTargetPlatformOverride = null); var outsideClicks = 0; await tester.pumpWidget( MaterialApp( theme: AppTheme.penDark, home: Scaffold( body: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ AppContextMenu( itemsBuilder: (_) => [ AppMenuItem(label: 'Context action', onPressed: () {}), ], child: const SizedBox(width: 100, height: 100), ), const SizedBox(height: 200), TextButton( onPressed: () => outsideClicks++, child: const Text('Outside button'), ), ], ), ), ), ); await tester.tap( find.byType(AppContextMenu), kind: PointerDeviceKind.mouse, buttons: kSecondaryMouseButton, ); await tester.pumpAndSettle(); expect(find.text('Context action'), findsOneWidget); await tester.tap( find.text('Outside button'), kind: PointerDeviceKind.mouse, ); await tester.pumpAndSettle(); expect(find.text('Context action'), findsNothing); expect(outsideClicks, 1); expect(tester.takeException(), isNull); debugDefaultTargetPlatformOverride = null; }); testWidgets('dropdown offsets do not inherit or overwrite expansion state', ( tester, ) async { final bucket = PageStorageBucket(); const ancestorKey = PageStorageKey('expanded-property'); int? selected; await tester.pumpWidget( MaterialApp( theme: AppTheme.penDark, home: Scaffold( body: PageStorage( bucket: bucket, child: Builder( key: ancestorKey, builder: (context) { bucket.writeState(context, true); return SizedBox( width: 220, child: AppSelect( label: 'Color space', emptyLabel: 'No options', value: 0, options: const [ AppSelectOption(0, 'Automatic'), AppSelectOption(1, 'Manual'), ], onChanged: (value) => selected = value, ), ); }, ), ), ), ), ); await tester.pumpAndSettle(); for (var i = 0; i < 2; i++) { await tester.tap(find.text('Automatic')); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); await tester.tap(find.text('Manual')); await tester.pumpAndSettle(); expect(selected, 1); expect(bucket.readState(tester.element(find.byKey(ancestorKey))), isTrue); expect(tester.takeException(), isNull); } }); 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(AppIcons.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()); }, ); }