import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart'; import 'package:pipelines_app/ui/adaptive/glass_pill.dart'; /// The floating tab bar is a row of separate round glass buttons at the /// bottom right on every width: each tab its own circle of the named size, /// evenly spaced, the row keeping the margins from the edges, on both hosts. void main() { const icons = [ Icons.account_tree_outlined, Icons.play_circle_outline, Icons.chat_bubble_outline, Icons.search, Icons.settings_outlined, ]; Key tabKey(int index) => ValueKey('tab-$index'); Widget bar({ required int count, required int selected, ValueChanged? onTap, }) => FloatingDestinationBar( currentIndex: selected, onTap: onTap ?? (_) {}, items: [ for (var index = 0; index < count; index += 1) FloatingDestinationItem( key: tabKey(index), icon: Icon(icons[index]), label: 'Tab $index', ), ], ); Future pump( WidgetTester tester, { required Size size, required Widget child, double textScale = 1, }) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final body = MediaQuery( data: MediaQueryData( size: size, textScaler: TextScaler.linear(textScale), ), child: child, ); await tester.pumpWidget(MaterialApp(home: Scaffold(body: body))); await tester.pumpAndSettle(); } /// Tab [index]'s glass circle. Rect button(WidgetTester tester, int index) => tester.getRect( find.descendant( of: find.byKey(tabKey(index)), matching: find.byType(GlassPill), ), ); Rect row(WidgetTester tester) => tester.getRect(find.byKey(FloatingDestinationBar.rowKey)); final placements = [ (name: 'wide 1200, 4 tabs', size: const Size(1200, 800), count: 4), (name: 'phone 390, 3 tabs', size: const Size(390, 844), count: 3), (name: 'phone 390, 4 tabs', size: const Size(390, 844), count: 4), (name: 'phone 560, 5 tabs', size: const Size(560, 844), count: 5), ]; { const host = 'Material'; final diameter = FloatingDestinationBar.buttonSize(); final gap = FloatingDestinationBar.buttonGap(); final iconSize = FloatingDestinationBar.materialIconSize; test('$host: buttons are larger than the old 44/42 circle, hit targets ' 'meet the platform minimum', () { expect(diameter, 52); expect(diameter, greaterThanOrEqualTo(48)); expect(gap, inInclusiveRange(10, 12)); }); testWidgets('$host: each tab is its own glass circle with the icon ' 'centred in it', (tester) async { await pump( tester, size: const Size(390, 844), child: Align( alignment: Alignment.bottomCenter, child: bar(count: 3, selected: 0), ), ); expect( find.descendant( of: find.byType(FloatingDestinationBar), matching: find.byType(GlassPill), ), findsNWidgets(3), ); for (var index = 0; index < 3; index += 1) { final circle = button(tester, index); // Tab 0 is selected: its whole button is larger than the others. final across = index == 0 ? FloatingDestinationBar.selectedButtonSize() : diameter; expect(circle.width, moreOrLessEquals(across)); expect(circle.height, moreOrLessEquals(across)); expect( tester .widget( find.descendant( of: find.byKey(tabKey(index)), matching: find.byType(GlassPill), ), ) .shape, isA(), ); final icon = tester.getRect(find.byIcon(icons[index])); final side = index == 0 ? iconSize * FloatingDestinationBar.selectedButtonSize() / FloatingDestinationBar.buttonSize() : iconSize; expect(icon.width, moreOrLessEquals(side)); expect(icon.height, moreOrLessEquals(side)); expect(icon.center, circle.center); } }); for (final placement in placements) { testWidgets('$host ${placement.name}: a row at the bottom right, ' 'evenly spaced', (tester) async { final count = placement.count; final size = placement.size; await pump( tester, size: size, child: Align( alignment: Alignment.bottomCenter, child: bar(count: count, selected: 0), ), ); final box = row(tester); expect(box.right, size.width - 16); expect(box.bottom, size.height - 16); // As tall as the selected (larger) button. expect( box.height, moreOrLessEquals(FloatingDestinationBar.selectedButtonSize()), ); expect( box.width, moreOrLessEquals(FloatingDestinationBar.rowWidth(tabs: count)), ); expect(button(tester, 0).left, box.left); expect(button(tester, count - 1).right, box.right); for (var index = 1; index < count; index += 1) { expect( button(tester, index).left - button(tester, index - 1).right, moreOrLessEquals(gap), ); // Every button is centred on the row, whatever its size. expect( button(tester, index).center.dy, moreOrLessEquals(box.center.dy), ); } }); } for (final listWidth in [511.0, 300.0, 240.0]) { testWidgets('$host: centred in a $listWidth list pane, the row keeps ' 'the margins and the selected size', (tester) async { // The shell's wide placement: under the list pane, centred in it. await pump( tester, size: const Size(1280, 800), child: Align( alignment: AlignmentDirectional.bottomStart, child: SizedBox( width: listWidth, child: FloatingDestinationBar( currentIndex: 1, onTap: (_) {}, alignment: Alignment.bottomCenter, items: [ for (var index = 0; index < 4; index += 1) FloatingDestinationItem( key: tabKey(index), icon: Icon(icons[index]), label: 'Tab $index', ), ], ), ), ), ); final box = row(tester); expect(box.center.dx, moreOrLessEquals(listWidth / 2)); // A squeezed row scales down about its centre, which stays put. expect( box.center.dy, moreOrLessEquals( 800 - 16 - FloatingDestinationBar.selectedButtonSize() / 2, ), ); expect(box.left, greaterThanOrEqualTo(16 - 0.01)); expect(box.right, lessThanOrEqualTo(listWidth - 16 + 0.01)); // The selected button keeps its larger size relative to the others. expect( button(tester, 1).width / button(tester, 0).width, moreOrLessEquals( FloatingDestinationBar.selectedButtonSize() / diameter, ), ); expect(tester.takeException(), isNull); }); } testWidgets('$host: taps between and beside the buttons reach the ' 'content, taps on them the tabs', (tester) async { final tapped = []; var content = 0; await pump( tester, size: const Size(390, 844), child: Stack( children: [ Positioned.fill( child: GestureDetector( behavior: HitTestBehavior.opaque, onTap: () => content++, ), ), Align( alignment: Alignment.bottomCenter, child: bar(count: 3, selected: 1, onTap: tapped.add), ), ], ), ); final box = row(tester); expect(box.width, lessThan(390 / 2)); await tester.tapAt(Offset(box.left - 40, box.center.dy)); final between = (button(tester, 0).right + button(tester, 1).left) / 2; await tester.tapAt(Offset(between, box.center.dy)); await tester.tapAt(button(tester, 0).center); await tester.tapAt(button(tester, 2).center); await tester.pumpAndSettle(); expect(content, 2); expect(tapped, [0, 2]); }); testWidgets('$host: 5 tabs at 320 with 2x text narrow their gaps and ' 'stay within the margins', (tester) async { await pump( tester, size: const Size(320, 640), textScale: 2, child: Align( alignment: Alignment.bottomCenter, child: bar(count: 5, selected: 4), ), ); expect(tester.takeException(), isNull); final box = row(tester); expect(box.right, 320 - 16); expect(box.left, greaterThanOrEqualTo(16)); // Tab 4 is selected and larger; the gaps narrow around it. final selected = FloatingDestinationBar.selectedButtonSize(); final narrowed = (288 - 4 * diameter - selected) / 4; for (var index = 1; index < 5; index += 1) { expect( button(tester, index).width, moreOrLessEquals(index == 4 ? selected : diameter), ); expect( button(tester, index).left - button(tester, index - 1).right, moreOrLessEquals(narrowed < gap ? narrowed : gap), ); } }); testWidgets('$host: right to left, the row sits at the bottom left and ' 'reads from the right', (tester) async { await pump( tester, size: const Size(390, 844), child: Directionality( textDirection: TextDirection.rtl, child: Align( alignment: Alignment.bottomCenter, child: bar(count: 3, selected: 0), ), ), ); expect(row(tester).left, 16); expect(button(tester, 0).right, row(tester).right); }); } }