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/detail_page.dart'; import 'package:pipelines_app/shell/list_detail.dart'; import 'package:pipelines_app/shell/shell_bar.dart'; import 'package:pipelines_app/ui/adaptive/floating_bar_layout.dart'; import 'package:pipelines_app/ui/adaptive/glass_pill.dart'; import 'package:pipelines_app/ui/adaptive/segmented_bar.dart'; import 'package:pipelines_app/ui/adaptive/window_margins.dart'; /// On a wide window every top bar keeps the floating tab bar's 16 px from the /// top and the sides, in the list pane and the detail pane alike; a phone /// keeps the bar's 8 px top and 12 px sides. void main() { Future setSize(WidgetTester tester, Size size) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); } Widget list() => ShellBar( actions: const [Icon(Icons.sort, semanticLabel: 'Sort')], child: Builder( builder: (context) => ListView( key: const Key('list-list'), padding: withTopBarClearance(context, EdgeInsets.zero), children: [ for (var index = 0; index < 30; index++) SizedBox(height: 40, child: Text('list row $index')), ], ), ), ); Widget item() => ShellBar( title: 'Open item', actions: const [Icon(Icons.more_vert, semanticLabel: 'More')], child: Builder( builder: (context) => ListView( key: const Key('item-list'), padding: withTopBarClearance(context, EdgeInsets.zero), children: [ for (var index = 0; index < 30; index++) SizedBox(height: 40, child: Text('item row $index')), ], ), ), ); Widget app({required bool split}) { final shell = AdaptiveShell( destinationBodies: { AppDestination.runs: (_) => split ? ListDetailPanes( list: list(), detail: DetailPane( destination: AppDestination.runs, implyBack: false, builder: (_) => item(), ), ) : list(), }, destination: AppDestination.runs, ); Widget margins(BuildContext context, Widget? child) => WindowMargins(child: child!); return MaterialApp(builder: margins, home: shell); } Finder pills(Finder bar) => find.descendant(of: bar, matching: find.byType(GlassPill)); final shellBar = find.byWidgetPredicate( (widget) => widget is SegmentedBar && widget.title == 'Runs', ); final paneBar = find.descendant( of: find.byType(DetailPane), matching: find.byType(SegmentedBar), ); /// The bar's pieces span: the leading edge of the first, the trailing /// edge of the last, and their shared top and bottom. Rect span(WidgetTester tester, Finder bar) { final rects = pills(bar).evaluate().map((element) { final box = element.renderObject! as RenderBox; return box.localToGlobal(Offset.zero) & box.size; }).toList(); return rects.reduce((a, b) => a.expandToInclude(b)); } testWidgets('1280: list and detail bars keep 16 px from the top ' 'and every side', (tester) async { await setSize(tester, const Size(1280, 800)); await tester.pumpWidget(app(split: true)); await tester.pump(); // The pieces pop in; measure them at rest. await tester.pump(const Duration(seconds: 2)); final listPane = tester.getRect( find.byKey(const ValueKey('list-detail-list')), ); final body = tester.getRect(find.byKey(const ValueKey('list-detail-body'))); final listBar = span(tester, shellBar); final itemBar = span(tester, paneBar); expect(listBar.top, closeTo(16, 0.5)); expect(listBar.left, closeTo(listPane.left + 16, 0.5)); expect(listBar.right, closeTo(listPane.right - 16, 0.5)); expect(itemBar.top, closeTo(16, 0.5)); expect(itemBar.left, closeTo(body.left + 16, 0.5)); expect(itemBar.right, closeTo(body.right - 16, 0.5)); // Each pane's content still starts below its bar (and its 8 px gap) // at rest, so nothing hides behind the taller margin. expect( tester.getTopLeft(find.text('list row 0')).dy, greaterThanOrEqualTo(listBar.bottom + 8 - 0.5), ); expect( tester.getTopLeft(find.text('item row 0')).dy, greaterThanOrEqualTo(itemBar.bottom + 8 - 0.5), ); expect(tester.takeException(), isNull); }); testWidgets('390: the phone keeps the bar at 8 px from the top and ' '12 px from the sides', (tester) async { await setSize(tester, const Size(390, 844)); await tester.pumpWidget(app(split: false)); await tester.pump(); // The pieces pop in; measure them at rest. await tester.pump(const Duration(seconds: 2)); final bar = span(tester, shellBar); expect(bar.top, closeTo(8, 0.5)); expect(bar.left, closeTo(12, 0.5)); expect(bar.right, closeTo(390 - 12, 0.5)); expect( tester.getTopLeft(find.text('list row 0')).dy, greaterThanOrEqualTo(bar.bottom + 8 - 0.5), ); expect(tester.takeException(), isNull); }); testWidgets('1280: a pushed page\'s bar keeps the wide margins', ( tester, ) async { await setSize(tester, const Size(1280, 800)); final bar = SegmentedBar( title: 'Settings', actions: const [Icon(Icons.more_vert, semanticLabel: 'More')], ); final body = Builder( builder: (context) => ListView( padding: withTopBarClearance(context, EdgeInsets.zero), children: const [SizedBox(height: 40, child: Text('first row'))], ), ); Widget margins(BuildContext context, Widget? child) => WindowMargins(child: child!); await tester.pumpWidget( MaterialApp( builder: margins, home: Scaffold(extendBodyBehindAppBar: true, appBar: bar, body: body), ), ); await tester.pump(const Duration(seconds: 2)); final pieces = span(tester, find.byType(SegmentedBar)); expect(pieces.top, closeTo(16, 0.5)); expect(pieces.left, closeTo(16, 0.5)); expect(pieces.right, closeTo(1280 - 16, 0.5)); expect( tester.getTopLeft(find.text('first row')).dy, closeTo(pieces.bottom + 8, 0.5), ); expect(tester.takeException(), isNull); }); }