import 'package:flutter/cupertino.dart'; 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/ui/adaptive/floating_destination_bar.dart'; import 'package:pipelines_app/ui/adaptive/floating_tab_button.dart'; import 'package:pipelines_app/ui/adaptive/glass_pill.dart'; /// The floating tab bar's motion: each button pops in on its own, staggered, /// only when the bar appears or its set of tabs changes; the selection tint /// cross-fades to a newly selected button, whose icon bounces; a press sinks /// the button and it springs back. void main() { const icons = [ Icons.account_tree_outlined, Icons.play_circle_outline, Icons.chat_bubble_outline, Icons.search, ]; Key tabKey(int index) => ValueKey('tab-$index'); List items(int count) => [ for (var index = 0; index < count; index += 1) FloatingDestinationItem( key: tabKey(index), icon: Icon(icons[index]), label: 'Tab $index', ), ]; void animate(WidgetTester tester, {required bool on}) { tester.platformDispatcher.accessibilityFeaturesTestValue = FakeAccessibilityFeatures(disableAnimations: !on); addTearDown(tester.platformDispatcher.clearAccessibilityFeaturesTestValue); tester.view.physicalSize = const Size(560, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); } Widget app(bool cupertino, Widget body) => cupertino ? CupertinoApp(home: CupertinoPageScaffold(child: body)) : MaterialApp(home: Scaffold(body: body)); /// A bar of [count] tabs whose tap selects. Future pumpBar( WidgetTester tester, { required bool cupertino, int count = 3, }) async { var selected = 0; await tester.pumpWidget( app( cupertino, StatefulBuilder( builder: (context, setState) => Align( alignment: Alignment.bottomCenter, child: FloatingDestinationBar( cupertino: cupertino, currentIndex: selected, onTap: (index) => setState(() => selected = index), items: items(count), ), ), ), ), ); } Finder glass(Finder tab) => find.descendant(of: tab, matching: find.byType(GlassPill)); /// The opacity of [tab]'s entrance fade (its glass reveal). double opacityOf(WidgetTester tester, Finder tab) => GlassReveal.maybeOf(tester.element(glass(tab)))?.value ?? 1; /// [tab]'s drawn width (its entrance and press scale it). double widthOf(WidgetTester tester, Finder tab) => tester.getRect(glass(tab)).width; double iconWidth(WidgetTester tester, int index) => tester .getRect( find.descendant( of: find.byKey(tabKey(index)), matching: find.byIcon(icons[index]), ), ) .width; /// The alpha of tab [index]'s selection fill. double fillAlpha(WidgetTester tester, int index) { final fill = tester.widget( find.descendant( of: find.byKey(tabKey(index)), matching: find.byKey(FloatingTabButton.fillKey), ), ); final decoration = fill is Ink ? fill.decoration : (fill as DecoratedBox).decoration; return (decoration! as ShapeDecoration).color!.a; } for (final cupertino in [false, true]) { final host = cupertino ? 'Cupertino' : 'Material'; final size = FloatingDestinationBar.buttonSize(cupertino: cupertino); final iconSize = cupertino ? FloatingDestinationBar.cupertinoIconSize : FloatingDestinationBar.materialIconSize; testWidgets('$host: each button pops in on its own, staggered, from ' '0.6 to full size', (tester) async { animate(tester, on: true); await pumpBar(tester, cupertino: cupertino); for (var index = 0; index < 3; index += 1) { final tab = find.byKey(tabKey(index)); expect(opacityOf(tester, tab), 0); final rest = index == 0 ? FloatingDestinationBar.selectedButtonSize(cupertino: cupertino) : size; expect(widthOf(tester, tab), moreOrLessEquals(rest * 0.6)); } await tester.pump(const Duration(milliseconds: 130)); final first = widthOf(tester, find.byKey(tabKey(0))); final last = widthOf(tester, find.byKey(tabKey(2))); expect(first, greaterThan(last + 4), reason: 'staggered'); expect(opacityOf(tester, find.byKey(tabKey(0))), greaterThan(0.9)); expect(opacityOf(tester, find.byKey(tabKey(2))), lessThan(0.2)); await tester.pumpAndSettle(); for (var index = 0; index < 3; index += 1) { final tab = find.byKey(tabKey(index)); expect(opacityOf(tester, tab), 1); // Tab 0 is selected, so it settles at the larger size. expect( widthOf(tester, tab), index == 0 ? FloatingDestinationBar.selectedButtonSize(cupertino: cupertino) : size, ); } }); testWidgets('$host: the entrance does not replay when the selection ' 'changes', (tester) async { animate(tester, on: true); await pumpBar(tester, cupertino: cupertino); await tester.pumpAndSettle(); for (final target in [1, 2, 0]) { await tester.tap(find.byKey(tabKey(target))); for (var frame = 0; frame < 12; frame += 1) { await tester.pump(const Duration(milliseconds: 25)); for (var index = 0; index < 3; index += 1) { final tab = find.byKey(tabKey(index)); expect(opacityOf(tester, tab), 1, reason: 'tab $index faded'); // The selection grows and shrinks buttons between their two // sizes and the press sinks the tapped one a little; a replayed // pop would start at 0.6. expect( widthOf(tester, tab), greaterThan(size * 0.9), reason: 'tab $index popped again', ); } } await tester.pumpAndSettle(); } }); testWidgets('$host: a new set of tabs pops in again', (tester) async { animate(tester, on: true); var count = 3; late StateSetter update; await tester.pumpWidget( app( cupertino, StatefulBuilder( builder: (context, setState) { update = setState; return Align( alignment: Alignment.bottomCenter, child: FloatingDestinationBar( cupertino: cupertino, currentIndex: 0, onTap: (_) {}, items: items(count), ), ); }, ), ), ); await tester.pumpAndSettle(); expect(opacityOf(tester, find.byKey(tabKey(2))), 1); update(() => count = 4); await tester.pump(); expect(opacityOf(tester, find.byKey(tabKey(3))), 0); expect(opacityOf(tester, find.byKey(tabKey(2))), 0); await tester.pumpAndSettle(); expect(opacityOf(tester, find.byKey(tabKey(3))), 1); expect(widthOf(tester, find.byKey(tabKey(3))), size); }); testWidgets('$host: the selection tint cross-fades to the new button, ' 'whose icon takes the selected colour and bounces', (tester) async { animate(tester, on: true); await pumpBar(tester, cupertino: cupertino); await tester.pumpAndSettle(); final tint = fillAlpha(tester, 0); expect(tint, greaterThan(0.1)); expect(fillAlpha(tester, 1), 0); expect(fillAlpha(tester, 2), 0); await tester.tap(find.byKey(tabKey(2))); await tester.pump(); await tester.pump(const Duration(milliseconds: 80)); expect(fillAlpha(tester, 2), inExclusiveRange(0, tint)); expect(fillAlpha(tester, 0), inExclusiveRange(0, tint)); // The icon grows within its button (which the tap's press sinks). expect( iconWidth(tester, 2) * size / widthOf(tester, find.byKey(tabKey(2))), greaterThan(iconSize * 1.04), ); expect(iconWidth(tester, 1), moreOrLessEquals(iconSize)); await tester.pumpAndSettle(); expect(fillAlpha(tester, 2), tint); expect(fillAlpha(tester, 0), 0); // At rest the selected icon is a little larger than the others. expect( iconWidth(tester, 2), moreOrLessEquals( iconSize * FloatingDestinationBar.selectedButtonSize(cupertino: cupertino) / FloatingDestinationBar.buttonSize(cupertino: cupertino), ), ); expect(iconWidth(tester, 0), moreOrLessEquals(iconSize)); expect(iconWidth(tester, 1), moreOrLessEquals(iconSize)); final context = tester.element(find.byKey(tabKey(2))); final selectedColour = cupertino ? CupertinoTheme.of(context).primaryColor : Theme.of(context).colorScheme.onSecondaryContainer; expect( IconTheme.of(tester.element(find.byIcon(icons[2]))).color, isSameColorAs(selectedColour), ); expect( IconTheme.of(tester.element(find.byIcon(icons[0]))).color, isNot(isSameColorAs(selectedColour)), ); }); testWidgets('$host: a press sinks the button and it springs back', ( tester, ) async { animate(tester, on: true); await pumpBar(tester, cupertino: cupertino); await tester.pumpAndSettle(); final tab = find.byKey(tabKey(1)); final gesture = await tester.startGesture(tester.getCenter(tab)); await tester.pump(); await tester.pump(const Duration(milliseconds: 120)); expect(widthOf(tester, tab), lessThan(size * 0.97)); // Tab 0 is selected, so it is the larger button; the press leaves it. expect( widthOf(tester, find.byKey(tabKey(0))), FloatingDestinationBar.selectedButtonSize(cupertino: cupertino), ); await gesture.up(); await tester.pumpAndSettle(); // The tap selected it: it springs back to the selected size. expect( widthOf(tester, tab), FloatingDestinationBar.selectedButtonSize(cupertino: cupertino), ); }); testWidgets('$host: under reduced motion the tint jumps and the buttons ' 'neither pop nor bounce', (tester) async { animate(tester, on: false); await pumpBar(tester, cupertino: cupertino); await tester.pump(); expect(opacityOf(tester, find.byKey(tabKey(1))), 1); expect(widthOf(tester, find.byKey(tabKey(1))), size); final tint = fillAlpha(tester, 0); await tester.tap(find.byKey(tabKey(2))); await tester.pump(); await tester.pump(); expect(fillAlpha(tester, 2), tint); expect(fillAlpha(tester, 0), 0); // No bounce, and the selected icon jumps straight to its larger size. expect( iconWidth(tester, 2), moreOrLessEquals( iconSize * FloatingDestinationBar.selectedButtonSize(cupertino: cupertino) / FloatingDestinationBar.buttonSize(cupertino: cupertino), ), ); expect(iconWidth(tester, 0), moreOrLessEquals(iconSize)); }); testWidgets('$host: switching the shell tabs never replays the main ' "bar's entrance", (tester) async { animate(tester, on: true); var destination = AppDestination.runs; final shell = StatefulBuilder( builder: (context, setState) => AdaptiveShell( cupertino: cupertino, destination: destination, onDestinationSelected: (value) => setState(() => destination = value), ), ); await tester.pumpWidget( cupertino ? CupertinoApp(home: shell) : MaterialApp(home: shell), ); await tester.pumpAndSettle(); for (final next in [ AppDestination.pipelines, AppDestination.chat, AppDestination.runs, ]) { await tester.tap( find.byKey(ValueKey('shell.destination.${next.name}')), ); for (var frame = 0; frame < 12; frame += 1) { await tester.pump(const Duration(milliseconds: 25)); for (final tab in shellTabDestinations) { final finder = find.byKey( ValueKey('shell.destination.${tab.name}'), ); expect( opacityOf(tester, finder), 1, reason: '${tab.name} faded switching to ${next.name}', ); if (tab != next) { // The previous selection shrinks back to its size; nothing // pops again from 0.6. expect( widthOf(tester, finder), greaterThan(size * 0.9), reason: tab.name, ); } } } await tester.pumpAndSettle(); } }); } }