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'; /// The floating tab bar's motion: the tabs pop in only when the bar appears /// or its set of tabs changes, and one selection highlight glides (and /// stretches on the way) to a newly selected tab, whose icon bounces. 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.accessibilityFeaturesTestValue = const FakeAccessibilityFeatures(disableAnimations: true); }); tester.view.physicalSize = const Size(560, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); } /// A bar of [count] tabs whose tap selects. Future pumpBar( WidgetTester tester, { required bool cupertino, int count = 3, }) async { var selected = 0; final body = StatefulBuilder( builder: (context, setState) { return Align( alignment: Alignment.bottomCenter, child: FloatingDestinationBar( cupertino: cupertino, currentIndex: selected, onTap: (index) => setState(() => selected = index), items: items(count), ), ); }, ); await tester.pumpWidget( cupertino ? CupertinoApp(home: CupertinoPageScaffold(child: body)) : MaterialApp(home: Scaffold(body: body)), ); } /// The opacity of [tab]'s entrance fade. double opacityOf(WidgetTester tester, Finder tab) => tester .widget( find .descendant( of: tab, matching: find.byWidgetPredicate( (widget) => widget is FadeTransition && widget.child is AnimatedBuilder, ), ) .last, ) .opacity .value; double iconWidth(WidgetTester tester, Finder tab, IconData icon) => tester .getRect(find.descendant(of: tab, matching: find.byIcon(icon))) .width; Rect highlight(WidgetTester tester) => tester.getRect( find.descendant( of: find.byKey(FloatingDestinationBar.highlightKey), matching: find.byType(DecoratedBox), ), ); Offset iconCentre(WidgetTester tester, int index) => tester.getCenter( find.descendant( of: find.byKey(tabKey(index)), matching: find.byIcon(icons[index]), ), ); for (final cupertino in [false, true]) { final host = cupertino ? 'Cupertino' : 'Material'; final iconSize = cupertino ? FloatingDestinationBar.cupertinoIconSize : FloatingDestinationBar.materialIconSize; final size = cupertino ? FloatingDestinationBar.cupertinoHighlightSize : FloatingDestinationBar.materialHighlightSize; testWidgets('$host: the entrance does not replay when the selection ' 'changes, and plays again for a new set of tabs', (tester) async { animate(tester, on: true); await pumpBar(tester, cupertino: cupertino); expect(opacityOf(tester, find.byKey(tabKey(0))), 0); 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'); // Only the bounce may grow an icon; a replayed pop shrinks it. expect( iconWidth(tester, tab, icons[index]), greaterThanOrEqualTo(iconSize - 0.01), reason: 'tab $index popped again', ); } } } }); testWidgets('$host: a new set of tabs pops in again', (tester) async { animate(tester, on: true); var count = 3; late StateSetter update; Widget build() => StatefulBuilder( builder: (context, setState) { update = setState; return Align( alignment: Alignment.bottomCenter, child: FloatingDestinationBar( cupertino: cupertino, currentIndex: 0, onTap: (_) {}, items: items(count), ), ); }, ); await tester.pumpWidget( cupertino ? CupertinoApp(home: CupertinoPageScaffold(child: build())) : MaterialApp(home: Scaffold(body: build())), ); 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); }); testWidgets('$host: the highlight rests on the selected tab as the old ' 'circle did, glides and stretches between tabs, and the new icon ' 'bounces', (tester) async { animate(tester, on: true); await pumpBar(tester, cupertino: cupertino); await tester.pumpAndSettle(); expect( highlight(tester), rectMoreOrLessEquals( Rect.fromCenter( center: iconCentre(tester, 0), width: size, height: size, ), epsilon: 0.01, ), ); expect(find.byKey(FloatingDestinationBar.highlightKey), findsOneWidget); await tester.tap(find.byKey(tabKey(2))); await tester.pump(); await tester.pump(const Duration(milliseconds: 60)); final from = iconCentre(tester, 0).dx; final to = iconCentre(tester, 2).dx; final moving = highlight(tester); expect(moving.center.dx, greaterThan(from + 1)); expect(moving.center.dx, lessThan(to - 1)); expect(moving.height, moreOrLessEquals(size)); expect(moving.width, greaterThan(size * 1.1)); expect(moving.width, lessThanOrEqualTo(size * 1.25 + 0.01)); expect( iconWidth(tester, find.byKey(tabKey(2)), icons[2]), greaterThan(iconSize * 1.02), ); await tester.pumpAndSettle(); expect( highlight(tester), rectMoreOrLessEquals( Rect.fromCenter( center: iconCentre(tester, 2), width: size, height: size, ), epsilon: 0.01, ), ); expect( iconWidth(tester, find.byKey(tabKey(2)), icons[2]), moreOrLessEquals(iconSize), ); }); testWidgets('$host: under reduced motion the highlight jumps and the ' 'icons 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); await tester.tap(find.byKey(tabKey(2))); await tester.pump(); expect( highlight(tester), rectMoreOrLessEquals( Rect.fromCenter( center: iconCentre(tester, 2), width: size, height: size, ), epsilon: 0.01, ), ); expect( iconWidth(tester, find.byKey(tabKey(2)), icons[2]), 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) { expect( opacityOf( tester, find.byKey(ValueKey('shell.destination.${tab.name}')), ), 1, reason: '${tab.name} faded switching to ${next.name}', ); } } await tester.pumpAndSettle(); } }); } }