import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/shell/windows/window_frame.dart'; import 'package:pipelines_app/shell/windows/window_layer.dart'; import 'package:pipelines_app/shell/windows/window_manager.dart'; import 'window_test_harness.dart'; void main() { late WindowManager manager; setUp(() => manager = probeManager()); Finder window(String title) => find.byWidgetPredicate( (w) => w is WindowLayer && 'Probe ${w.entry.contentId}' == title, ); Finder inWindow(String title, Finder what) => find.descendant(of: window(title), matching: what); testWidgets('a window shows its title, actions and content', (tester) async { manager.open('probe', 'a'); await pumpProbe(tester, manager); expect( windowRect(tester, 'Probe a'), const Rect.fromLTWH(100, 80, 360, 420), ); expect(find.byTooltip('Probe action'), findsOneWidget); for (final tip in ['Minimise', 'Maximise', 'Close']) { expect(find.byTooltip(tip), findsOneWidget); } expect(find.text('sheet').hitTestable(), findsOneWidget); // The title bar, the content and the app behind all keep their // semantics (a page route's barrier hides what is behind it). final handle = tester.ensureSemantics(); expect(find.bySemanticsLabel('Window: Probe a'), findsOneWidget); expect(tester.getSemantics(find.byTooltip('Minimise')).tooltip, 'Minimise'); expect(find.bySemanticsLabel('sheet'), findsOneWidget); expect(find.bySemanticsLabel('Chat'), findsOneWidget); expect( tester.getSemantics(find.bySemanticsLabel('Window: Probe a')).rect.size, const Size(360, 420), ); handle.dispose(); // The content is laid out for the window, not the app. final content = tester.element(find.byType(ProbeContent)); expect(MediaQuery.sizeOf(content).width, 360); expect(tester.getRect(find.byType(ProbeContent)).top, greaterThan(80)); }); testWidgets('the outline covers the full window and uses the theme palette', ( tester, ) async { manager.open('probe', 'a'); await pumpProbe(tester, manager); final outline = find.byType(WindowOutline); expect(outline, findsOneWidget); expect(tester.getSize(outline), const Size(360, 420)); final decoration = tester .widget( find.descendant( of: outline, matching: find.byType(DecoratedBox), ), ) .decoration as BoxDecoration; expect( decoration.border, Border.all( color: Theme.of( tester.element(find.byType(WindowOutline)), ).colorScheme.outlineVariant, ), ); expect(decoration.borderRadius, BorderRadius.circular(windowCornerRadius)); }); testWidgets('the app behind takes input everywhere outside a window', ( tester, ) async { manager.open('probe', 'a'); await pumpProbe(tester, manager, destination: 1); expect(find.text('Runs content').hitTestable(), findsOneWidget); await tester.tap(find.byKey(const ValueKey('a-page'))); await tester.pumpAndSettle(); // A page pushed inside the window still leaves the app usable. expect(find.text('Runs content').hitTestable(), findsOneWidget); }); testWidgets('drag the title to move, the corner to resize, inside the app', ( tester, ) async { final entry = manager.open('probe', 'a'); await pumpProbe(tester, manager); await tester.drag( find.byKey(const ValueKey('window-title')), const Offset(50, 30), ); await tester.pumpAndSettle(); expect( windowRect(tester, 'Probe a'), const Rect.fromLTWH(150, 110, 360, 420), ); await tester.drag( find.byKey(const ValueKey('window-resize')), const Offset(40, -500), ); await tester.pumpAndSettle(); // Never smaller than the kind's minimum. expect( windowRect(tester, 'Probe a'), const Rect.fromLTWH(150, 110, 400, 160), ); await tester.drag( find.byKey(const ValueKey('window-title')), const Offset(600, 5000), ); await tester.pumpAndSettle(); expect( windowRect(tester, 'Probe a'), const Rect.fromLTWH(750, 640, 400, 160), ); expect( manager.byId(entry.id)!.frame, const Rect.fromLTWH(750, 640, 400, 160), ); }); testWidgets('maximise fills the app and restores; double click toggles', ( tester, ) async { manager.open('probe', 'a'); await pumpProbe(tester, manager); await tester.tap(find.byTooltip('Maximise')); await tester.pumpAndSettle(); expect(windowRect(tester, 'Probe a'), const Rect.fromLTWH(8, 8, 1264, 784)); expect(find.byKey(const ValueKey('window-resize')), findsNothing); await tester.tap(find.byTooltip('Restore size')); await tester.pumpAndSettle(); expect( windowRect(tester, 'Probe a'), const Rect.fromLTWH(100, 80, 360, 420), ); final title = tester.getCenter(find.byKey(const ValueKey('window-title'))); await tester.tapAt(title); await tester.pump(kDoubleTapMinTime); await tester.tapAt(title); await tester.pumpAndSettle(); expect(manager.windows.single.maximized, isTrue); }); testWidgets('minimise hides the window and keeps its state; the group ' 'brings it back', (tester) async { final entry = manager.open('probe', 'a'); await pumpProbe(tester, manager); await tester.enterText(find.byKey(const ValueKey('a-field')), 'typed'); final state = tester.state(find.byType(ProbeContent)); await tester.tap(find.byTooltip('Minimise')); await tester.pumpAndSettle(); expect(manager.byId(entry.id)!.minimized, isTrue); expect(find.text('Probe a').hitTestable(), findsNothing); expect(find.byType(WindowFrame, skipOffstage: false), findsOneWidget); manager.toggleGroup('probe'); await tester.pumpAndSettle(); expect(find.text('Probe a').hitTestable(), findsOneWidget); expect( tester.state(find.byType(ProbeContent)), same(state), ); expect(find.text('typed'), findsOneWidget); }); testWidgets('X closes the window', (tester) async { final entry = manager.open('probe', 'a'); await pumpProbe(tester, manager); await tester.tap(find.byTooltip('Close')); await tester.pumpAndSettle(); expect(manager.windows, isEmpty); expect(find.byType(ProbeContent, skipOffstage: false), findsNothing); expect((manager.kinds['probe']! as ProbeWindowKind).closed_, [entry.id]); }); testWidgets('pressing a window brings it to the top and keeps both alive', ( tester, ) async { final a = manager.open('probe', 'a'); final b = manager.open('probe', 'b'); await pumpProbe(tester, manager); final stateA = tester.state(find.byKey(const ValueKey('a-field'))); // b covers a where they overlap. expect(find.byKey(const ValueKey('a-dialog')).hitTestable(), findsNothing); expect(manager.active!.id, b.id); // a's title bar shows above b. await tester.tapAt( windowRect(tester, 'Probe a').topLeft + const Offset(16, 12), ); await tester.pumpAndSettle(); expect(manager.active!.id, a.id); expect( find.byKey(const ValueKey('a-dialog')).hitTestable(), findsOneWidget, ); expect(tester.state(find.byKey(const ValueKey('a-field'))), same(stateA)); expect(inWindow('Probe b', find.byType(ProbeContent)), findsOneWidget); }); testWidgets('the keyboard goes to the shown window and back when it hides', ( tester, ) async { await pumpProbe(tester, manager); await tester.tap(find.byKey(const ValueKey('chat-composer'))); await tester.pump(); final composer = FocusManager.instance.primaryFocus; bool focusIn(Finder area) => find .descendant( of: area, matching: find.byWidget( FocusManager.instance.primaryFocus!.context!.widget, ), ) .evaluate() .isNotEmpty; expect(focusIn(find.byKey(const ValueKey('chat-composer'))), isTrue); final a = manager.open('probe', 'a'); await tester.pumpAndSettle(); bool inside(String title) => focusIn(window(title)); expect(inside('Probe a'), isTrue); final b = manager.open('probe', 'b'); await tester.pumpAndSettle(); expect(inside('Probe b'), isTrue); // The top window hides: the next one takes the keyboard. manager.setMinimized(b.id, true); await tester.pumpAndSettle(); expect(inside('Probe a'), isTrue); manager.setGroupShown('probe', false); await tester.pumpAndSettle(); expect(FocusManager.instance.primaryFocus, same(composer)); expect(manager.byId(a.id), isNotNull); }); testWidgets('a window works and animates over another destination', ( tester, ) async { manager.open('probe', 'a'); await pumpProbe(tester, manager, destination: 1); expect(find.text('Probe a').hitTestable(), findsOneWidget); // Not muted by the hidden destination. final content = tester.element(find.byType(ProbeContent)); expect(TickerMode.valuesOf(content).enabled, isTrue); await tester.tap(find.byKey(const ValueKey('a-field'))); await tester.pump(); await tester.enterText(find.byKey(const ValueKey('a-field')), 'over runs'); expect(find.text('over runs'), findsOneWidget); }); testWidgets('a frame larger than a small app is drawn inside it', ( tester, ) async { manager.open('probe', 'a', frame: const Rect.fromLTWH(900, 600, 800, 700)); await pumpProbe(tester, manager); expect( windowRect(tester, 'Probe a'), const Rect.fromLTWH(480, 100, 800, 700), ); tester.view.physicalSize = const Size(600, 400); await tester.pumpAndSettle(); expect(windowRect(tester, 'Probe a'), const Rect.fromLTWH(0, 0, 600, 400)); expect(tester.takeException(), isNull); }); }