import 'dart:ui' as ui; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/shell/windows/window_manager.dart'; import 'package:pipelines_app/ui/motion/surface_visibility.dart'; import 'window_test_harness.dart'; /// Layering acceptance (docs/plans/floating-windows.md, Phase 1.4): every /// kind of popup opened from inside a window is above every window, over the /// Chat destination and over another one; a pushed page stays inside its /// window. "Above" is a hit test at the popup reaching the popup /// ([Finder.hitTestable]), or its pixels for a tooltip, which takes no input. void main() { late WindowManager manager; /// Two overlapping windows; `a` is pressed, so it is on top, and `b` /// covers the area to its lower right. Future open( WidgetTester tester, int destination, { GlobalKey? boundary, }) async { manager = probeManager(); manager.open('probe', 'b', frame: const Rect.fromLTWH(300, 200, 600, 500)); manager.open('probe', 'a', frame: const Rect.fromLTWH(100, 80, 360, 640)); await pumpProbe( tester, manager, destination: destination, boundary: boundary, ); } String? picked(WidgetTester tester, String id) => tester .state(find.byKey(ValueKey('content-$id'))) .picked; NavigatorState root(WidgetTester tester) => tester.state(find.byType(Navigator).first); for (final (destination, where) in [ (0, 'Chat'), (1, 'another destination'), ]) { group('over $where:', () { testWidgets('bottom sheet', (tester) async { await open(tester, destination); await tester.tap(find.byKey(const ValueKey('a-sheet'))); await tester.pumpAndSettle(); expect(find.text('Sheet item').hitTestable(), findsOneWidget); // On the root navigator: full width, not clipped to the window. expect( tester.getSize(find.byType(BottomSheet)).width, greaterThan(600), ); expect(root(tester).canPop(), isTrue); // The sheet closes itself with its own navigator. await tester.tap(find.text('Sheet item')); await tester.pumpAndSettle(); expect(find.text('Sheet item'), findsNothing); expect(find.byKey(const ValueKey('a-sheet')), findsOneWidget); }); testWidgets('dialog', (tester) async { await open(tester, destination); await tester.tap(find.byKey(const ValueKey('a-dialog'))); await tester.pumpAndSettle(); expect(find.text('Dialog title').hitTestable(), findsOneWidget); // Its barrier covers the windows. expect( find.byKey(const ValueKey('b-dialog')).hitTestable(), findsNothing, ); }); testWidgets('app choice menu and details dialog', (tester) async { await open(tester, destination); final button = tester.getRect(find.byKey(const ValueKey('a-choice'))); await tester.tap(find.byKey(const ValueKey('a-choice'))); await tester.pumpAndSettle(); // A menu at its button, not a sheet, though the window is narrow. expect(find.byType(BottomSheet), findsNothing); expect(find.text('Choice two').hitTestable(), findsOneWidget); final menu = tester.getRect( find.byKey(const ValueKey('app-choice-menu')), ); expect((menu.top - button.bottom).abs(), lessThan(40)); expect(menu.left, button.left); await tester.tap(find.text('Choice two')); await tester.pumpAndSettle(); expect(picked(tester, 'a'), 'Choice two'); await tester.tap(find.byKey(const ValueKey('a-details'))); await tester.pumpAndSettle(); expect(find.byType(Dialog), findsOneWidget); expect(find.text('Details body').hitTestable(), findsOneWidget); }); testWidgets('MenuAnchor', (tester) async { await open(tester, destination); await tester.tap(find.byKey(const ValueKey('a-anchor'))); await tester.pumpAndSettle(); expect(find.text('Anchor item').hitTestable(), findsOneWidget); await tester.tap(find.text('Anchor item')); await tester.pumpAndSettle(); expect(picked(tester, 'a'), 'anchor'); }); testWidgets('PopupMenuButton opens at its button', (tester) async { await open(tester, destination); final button = tester.getRect(find.byKey(const ValueKey('a-popup'))); await tester.tap(find.byKey(const ValueKey('a-popup'))); await tester.pumpAndSettle(); expect(find.text('Popup item').hitTestable(), findsOneWidget); final item = tester.getRect(find.text('Popup item')); expect((item.top - button.top).abs(), lessThan(80)); expect((item.left - button.left).abs(), lessThan(200)); await tester.tap(find.text('Popup item')); await tester.pumpAndSettle(); expect(picked(tester, 'a'), 'popup'); }); testWidgets('DropdownButton opens at its button', (tester) async { await open(tester, destination); final button = tester.getRect(find.byKey(const ValueKey('a-dropdown'))); await tester.tap(find.byKey(const ValueKey('a-dropdown'))); await tester.pumpAndSettle(); expect(find.text('Drop two').hitTestable(), findsOneWidget); final item = tester.getRect(find.text('Drop two').last); expect((item.top - button.top).abs(), lessThan(120)); expect((item.left - button.left).abs(), lessThan(40)); await tester.tap(find.text('Drop two').last); await tester.pumpAndSettle(); expect(picked(tester, 'a'), 'two'); }); testWidgets('pushed page opens inside its window, with Back inside', ( tester, ) async { await open(tester, destination); final window = windowRect(tester, 'Probe a'); await tester.tap(find.byKey(const ValueKey('a-page'))); await tester.pumpAndSettle(); expect(find.text('Page body').hitTestable(), findsOneWidget); final page = tester.getRect(find.text('Page of a')); expect(window.contains(page.topLeft), isTrue); expect(window.contains(page.bottomRight), isTrue); // The window keeps its title bar; the app has no new page. expect(find.text('Probe a').hitTestable(), findsOneWidget); expect(root(tester).canPop(), isFalse); // The other window is untouched. expect( find.byKey(const ValueKey('b-snack')).hitTestable(), findsOneWidget, ); await tester.tap(find.byType(BackButton)); await tester.pumpAndSettle(); expect(find.text('Page body'), findsNothing); expect( find.byKey(const ValueKey('a-page')).hitTestable(), findsOneWidget, ); }); testWidgets('SnackBar shows inside its window', (tester) async { await open(tester, destination); await tester.tap(find.byKey(const ValueKey('a-snack'))); await tester.pumpAndSettle(); expect(find.text('Snack text').hitTestable(), findsOneWidget); final window = windowRect(tester, 'Probe a'); final snack = tester.getRect(find.byType(SnackBar)); expect(window.contains(snack.topLeft), isTrue); expect(window.contains(snack.bottomRight - const Offset(1, 1)), isTrue); }); testWidgets('text selection menu', (tester) async { await open(tester, destination); // On the text itself, which selects a word. await tester.longPressAt( tester.getRect(find.byKey(const ValueKey('a-field'))).centerLeft + const Offset(30, 0), ); await tester.pumpAndSettle(); expect(find.text('Select all').hitTestable(), findsOneWidget); }); testWidgets('tooltip of content and of the title bar', (tester) async { final boundary = GlobalKey(); await open(tester, destination, boundary: boundary); final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse); addTearDown(mouse.removePointer); await mouse.addPointer(location: Offset.zero); await mouse.moveTo( tester.getCenter(find.byKey(const ValueKey('a-tip'))), ); await tester.pumpAndSettle(const Duration(seconds: 1)); expect(find.text('Tip text'), findsOneWidget); // Its own pixels show: nothing is painted over it. final tip = tester.getRect(find.text('Tip text')); final image = await _capture(tester, boundary); expect( await _pixel(tester, image, tip.topLeft - const Offset(3, 2)), probeTooltipColor, ); await mouse.moveTo(tester.getCenter(find.byTooltip('Close').last)); await tester.pumpAndSettle(const Duration(seconds: 1)); expect(find.text('Close'), findsOneWidget); // Not clipped to the window: tooltips paint in an overlay as large // as the app. final overlay = tester.renderObject( find .ancestor(of: find.text('Close'), matching: find.byType(Overlay)) .first, ); expect(overlay.size, const Size(1280, 800)); }); }); } testWidgets('a popup from a window under another window is above both', ( tester, ) async { await open(tester, 0); // b is under a; its sheet button is outside a. await tester.tap(find.byKey(const ValueKey('b-sheet'))); await tester.pumpAndSettle(); expect(manager.active!.contentId, 'b'); expect(find.text('Sheet item').hitTestable(), findsOneWidget); }); testWidgets('a popup opened by the press that focuses its window keeps the ' 'keyboard', (tester) async { await open(tester, 0); // The keyboard is in the app behind, as after using another tab. FocusManager.instance.primaryFocus?.unfocus(); await tester.pump(); await tester.tap(find.byKey(const ValueKey('b-sheet'))); await tester.pumpAndSettle(); expect(find.text('Sheet item').hitTestable(), findsOneWidget); final focus = FocusManager.instance.primaryFocus!.context!; expect(ModalRoute.of(focus), isA>()); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(find.text('Sheet item'), findsNothing); // The window content is not the current surface under a popup. await tester.tap(find.byKey(const ValueKey('b-dialog'))); await tester.pumpAndSettle(); expect( SurfaceVisibility.currentOf( tester.element(find.byKey(const ValueKey('content-b'))), ), isFalse, ); }); testWidgets('Escape closes the popup, not the window', (tester) async { await open(tester, 0); await tester.tap(find.byKey(const ValueKey('a-dialog'))); await tester.pumpAndSettle(); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(find.text('Dialog title'), findsNothing); expect(manager.windows, hasLength(2)); expect(find.text('Probe a').hitTestable(), findsOneWidget); }); } Future _capture(WidgetTester tester, GlobalKey boundary) async { final render = boundary.currentContext!.findRenderObject()! as RenderRepaintBoundary; return (await tester.runAsync(() => render.toImage()))!; } Future _pixel(WidgetTester tester, ui.Image image, Offset at) async { final data = (await tester.runAsync( () => image.toByteData(format: ui.ImageByteFormat.rawRgba), ))!; final i = (at.dy.round() * image.width + at.dx.round()) * 4; return Color.fromARGB( data.getUint8(i + 3), data.getUint8(i), data.getUint8(i + 1), data.getUint8(i + 2), ); }