import 'dart:ui' show Tristate; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/shell/adaptive_shell.dart'; import 'package:pipelines_app/shell/destination.dart'; import 'package:pipelines_app/shell/list_detail.dart'; import 'package:pipelines_app/shell/navigation_rail.dart'; import 'package:pipelines_app/shell/shell_bar.dart'; import 'package:pipelines_app/shell/windows/window_host.dart'; import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart'; import 'windows/window_test_harness.dart'; /// On a wide window the destinations sit in a Material navigation rail at the /// left edge (Settings pinned to its foot); narrower windows keep the floating /// bar. The rail keeps the badges and the keys of the bar it replaces. void main() { Future pumpShell( WidgetTester tester, { required double width, double height = 800, int runningRunCount = 0, bool withPanes = false, }) async { tester.view.physicalSize = Size(width, height); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final shell = AdaptiveShell( destination: withPanes ? AppDestination.runs : AppDestination.pipelines, runningRunCount: runningRunCount, destinationBodies: { AppDestination.pipelines: (_) => const Text('pipelines body'), AppDestination.runs: (_) => withPanes ? ListDetailPanes( list: const ShellBar(child: Text('runs list')), detail: const Center(child: Text('runs detail')), ) : const Text('runs body'), AppDestination.chat: (_) => const Text('chat body'), AppDestination.search: (_) => const Text('search body'), AppDestination.settings: (_) => const Text('settings body'), }, ); await tester.pumpWidget(MaterialApp(home: shell)); await tester.pumpAndSettle(); } Finder rail() => find.byType(NavigationRail); Finder inRail(Finder finder) => find.descendant(of: rail(), matching: finder); testWidgets('a wide window shows a labelled rail at the left edge, full ' 'height, with Settings pinned to its foot', (tester) async { await pumpShell(tester, width: 1000); expect(rail(), findsOneWidget); expect(find.byType(FloatingDestinationBar), findsNothing); for (final label in ['Pipelines', 'Runs', 'Chat', 'Search', 'Settings']) { expect(inRail(find.text(label)), findsOneWidget, reason: label); } final railRect = tester.getRect(rail()); expect(railRect.left, 0); expect(railRect.top, 0); expect(railRect.height, 800); expect(railRect.width, kNavigationRailWidth); // Settings sits below every tab, at the bottom of the rail. final settings = tester.getRect( find.byKey(const ValueKey('shell.destination.settings')), ); final runs = tester.getRect( find.byKey(const ValueKey('shell.destination.runs')), ); expect(settings.top, greaterThan(runs.bottom)); expect(settings.bottom, closeTo(railRect.bottom, 1)); }); testWidgets('the rail marks the selected tab and one tap selects another', ( tester, ) async { await pumpShell(tester, width: 1000); expect(tester.widget(rail()).selectedIndex, 0); expect(find.text('pipelines body'), findsOneWidget); await tester.tap(find.byKey(const ValueKey('shell.destination.runs'))); await tester.pumpAndSettle(); expect(tester.widget(rail()).selectedIndex, 1); expect(find.text('runs body'), findsOneWidget); }); testWidgets('Settings opens from the rail foot as a destination with no ' 'Back and no tab is selected meanwhile', (tester) async { final semantics = tester.ensureSemantics(); await pumpShell(tester, width: 1000); await tester.tap(find.byKey(const ValueKey('shell.destination.settings'))); await tester.pumpAndSettle(); expect(find.text('settings body'), findsOneWidget); expect(tester.widget(rail()).selectedIndex, isNull); expect( tester .getSemantics(inRail(find.bySemanticsLabel('Settings'))) .flagsCollection .isSelected, Tristate.isTrue, ); // Opened from its rail item, Settings is not a page: no Back. expect(find.byKey(const ValueKey('shell.back')), findsNothing); // The Pipelines item leaves it for the Pipelines tab. await tester.tap(find.byKey(const ValueKey('shell.destination.pipelines'))); await tester.pumpAndSettle(); expect(tester.widget(rail()).selectedIndex, 0); semantics.dispose(); }); testWidgets('the running count stays on the Runs item of the rail', ( tester, ) async { await pumpShell(tester, width: 1000, runningRunCount: 3); final badge = find.byKey(const ValueKey('shell.badge.runs.running')); expect( find.descendant(of: badge, matching: find.text('3')), findsOneWidget, ); expect( find.descendant( of: find.byKey(const ValueKey('shell.destination.runs')), matching: badge, ), findsOneWidget, ); }); testWidgets('a list and its open item lay out to the right of the rail', ( tester, ) async { await pumpShell(tester, width: 1000, withPanes: true); final list = tester.getRect(find.byKey(const ValueKey('list-detail-list'))); final body = tester.getRect(find.byKey(const ValueKey('list-detail-body'))); expect(list.left, closeTo(kNavigationRailSpace, 0.5)); expect(body.right, closeTo(1000, 0.5)); expect(body.left, greaterThan(list.right)); }); testWidgets('the rail appears from the threshold width, and below it the ' 'floating bar stays', (tester) async { expect( kNavigationRailMinWindowWidth, kListDetailMinWidth + kNavigationRailSpace, ); await pumpShell(tester, width: kNavigationRailMinWindowWidth - 1); expect(rail(), findsNothing); expect(find.byType(FloatingDestinationBar), findsOneWidget); await pumpShell(tester, width: kNavigationRailMinWindowWidth); expect(rail(), findsOneWidget); expect(find.byType(FloatingDestinationBar), findsNothing); }); testWidgets('at the threshold the body beside the rail meets the list-detail ' 'minimum, so the list sits beside its detail', (tester) async { await pumpShell( tester, width: kNavigationRailMinWindowWidth, withPanes: true, ); final list = tester.getRect(find.byKey(const ValueKey('list-detail-list'))); final body = tester.getRect(find.byKey(const ValueKey('list-detail-body'))); // The list and detail fill the window beside the rail, at the split // threshold: the list starts after the rail and the detail ends at the // window's right edge, with the detail to the right of the list. expect(list.left, closeTo(kNavigationRailSpace, 0.5)); expect(body.right, closeTo(kNavigationRailMinWindowWidth, 0.5)); expect(body.left, greaterThan(list.right)); }); testWidgets('a phone shows the navigation bar with Settings and no rail', ( tester, ) async { await pumpShell(tester, width: 390); expect(rail(), findsNothing); expect(find.byType(NavigationBar), findsOneWidget); expect( find.byKey(const ValueKey('shell.destination.settings')), findsOneWidget, ); expect(find.byType(FloatingDestinationBar), findsNothing); }); testWidgets('a narrow window below the rail keeps the floating bar, no rail ' 'and no navigation bar', (tester) async { // 640 is wide enough for the list beside its open item, which keeps the // floating bar under the list column. await pumpShell(tester, width: 640); expect(rail(), findsNothing); expect(find.byType(NavigationBar), findsNothing); expect(find.byType(FloatingDestinationBar), findsOneWidget); }); testWidgets('floating windows keep to the right of the rail on a wide ' 'window, and use the whole window without one', (tester) async { final manager = probeManager(); manager.open('probe', 'a'); Future pumpWindows(double width) async { tester.view.physicalSize = Size(width, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); await tester.pumpWidget( MaterialApp( home: Scaffold( body: Stack( fit: StackFit.expand, children: [ const SizedBox.expand(), Positioned.fill( child: WindowHost(manager: manager, reservesRail: true), ), ], ), ), ), ); await tester.pumpAndSettle(); } await pumpWindows(1280); // The probe window opens at x=100; beside the rail it starts after it. expect( windowRect(tester, 'Probe a').left, closeTo(100 + kNavigationRailSpace, 0.5), ); await pumpWindows(500); expect(windowRect(tester, 'Probe a').left, closeTo(100, 0.5)); }); testWidgets('crossing the rail threshold keeps the destination mounted and ' 'its state', (tester) async { tester.view.physicalSize = Size(kNavigationRailMinWindowWidth - 1, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( MaterialApp( home: AdaptiveShell( destination: AppDestination.runs, destinationBodies: {AppDestination.runs: (_) => const _Counter()}, ), ), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('counter.increment'))); await tester.pump(); final state = tester.state(find.byType(_Counter)); for (final width in [kNavigationRailMinWindowWidth, 390.0]) { tester.view.physicalSize = Size(width, 800); await tester.pumpAndSettle(); expect( tester.state(find.byType(_Counter)), same(state), reason: '$width', ); expect(find.text('count 1'), findsOneWidget, reason: '$width'); } }); } class _Counter extends StatefulWidget { const _Counter(); @override State<_Counter> createState() => _CounterState(); } class _CounterState extends State<_Counter> { int _count = 0; @override Widget build(BuildContext context) => Center( child: TextButton( key: const ValueKey('counter.increment'), onPressed: () => setState(() => _count += 1), child: Text('count $_count'), ), ); }