import 'package:cutdex_ui/theme.dart'; import 'package:cutdex_ui/widgets.dart'; import 'package:flutter/material.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { testWidgets('popover clears pointer focus and retains keyboard focus', ( tester, ) async { final focus = FocusNode(); final states = WidgetStatesController(); addTearDown(focus.dispose); addTearDown(states.dispose); await tester.pumpWidget( MaterialApp( theme: AppTheme.penDark, home: Scaffold( body: Align( alignment: Alignment.topLeft, child: AppPopover( builder: (_, toggle) => TextButton( focusNode: focus, statesController: states, onPressed: toggle, child: const Text('Open'), ), child: const Text('Content'), ), ), ), ), ); final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse); await mouse.addPointer(location: const Offset(400, 400)); for (final outside in [false, true]) { final trigger = tester.getCenter(find.text('Open')); await mouse.moveTo(trigger); await mouse.down(trigger); await mouse.up(); await tester.pumpAndSettle(); expect(find.text('Content'), findsOneWidget); final closingPoint = outside ? const Offset(400, 400) : trigger; await mouse.moveTo(closingPoint); await mouse.down(closingPoint); await mouse.up(); await mouse.moveTo(const Offset(450, 450)); await tester.pumpAndSettle(); expect(find.text('Content'), findsNothing); expect(focus.hasFocus, isFalse); expect(states.value, isNot(contains(WidgetState.focused))); expect(states.value, isNot(contains(WidgetState.hovered))); } await mouse.removePointer(); focus.requestFocus(); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(find.text('Content'), findsOneWidget); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(find.text('Content'), findsNothing); expect(focus.hasFocus, isTrue); expect(tester.takeException(), isNull); }); testWidgets( 'popover anchors below its trigger and dismisses outside and with Escape', (tester) async { var outsideClicks = 0; await tester.pumpWidget( MaterialApp( theme: AppTheme.penLight, home: Scaffold( body: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ AppPopover( builder: (_, toggle) => IconButton( key: const Key('trigger'), onPressed: toggle, icon: const Icon(Icons.task_alt), ), child: const Text('Tasks'), ), const SizedBox(height: 200), TextButton( onPressed: () => outsideClicks++, child: const Text('Outside'), ), ], ), ), ), ); await tester.tap(find.byKey(const Key('trigger'))); await tester.pumpAndSettle(); expect(find.text('Tasks'), findsOneWidget); // The menu host must not add a second outline around the inner surface. final menuSurface = tester .widgetList( find.ancestor( of: find.text('Tasks'), matching: find.byType(Material), ), ) .firstWhere((material) => material.shape is OutlinedBorder); expect( (menuSurface.shape! as OutlinedBorder).side.style, BorderStyle.none, ); expect( tester.getTopLeft(find.text('Tasks')).dy, greaterThan(tester.getBottomLeft(find.byKey(const Key('trigger'))).dy), ); await tester.tap(find.text('Outside')); await tester.pumpAndSettle(); expect(find.text('Tasks'), findsNothing); expect(outsideClicks, 1); await tester.tap(find.byKey(const Key('trigger'))); await tester.pumpAndSettle(); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(find.text('Tasks'), findsNothing); expect(tester.takeException(), isNull); }, ); testWidgets('end-aligned popover stays inside the viewport', (tester) async { await tester.pumpWidget( MaterialApp( theme: AppTheme.penLight, home: Scaffold( body: Align( alignment: Alignment.topLeft, child: AppPopover( width: 288, alignment: AlignmentDirectional.bottomEnd, surfaceKey: const Key('surface'), builder: (_, toggle) => TextButton( key: const Key('trigger'), onPressed: toggle, child: const Text('Open'), ), child: const Text('Content'), ), ), ), ), ); await tester.tap(find.byKey(const Key('trigger'))); await tester.pumpAndSettle(); expect( tester.getRect(find.byKey(const Key('surface'))).left, greaterThanOrEqualTo(0), ); expect(tester.takeException(), isNull); }); }