import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.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/segmented_bar_motion.dart'; import 'package:pipelines_app/ui/adaptive/segmented_bar_pieces.dart'; import 'package:pipelines_app/ui/motion/motion.dart'; /// The top bar's glass fades in with its blur, and its controls sink when /// pressed (its entrance is in segmented_bar_entrance_test.dart). void main() { Widget page( bool cupertino, { String title = 'Runs', List actions = const [], VoidCallback? onTitle, bool reduced = false, VoidCallback? onBack, }) { final bar = SegmentedBar( cupertino: cupertino, title: title, onTitlePressed: onTitle, leading: SegmentedBackButton( key: const Key('back'), cupertino: cupertino, onPressed: onBack ?? () {}, ), actions: actions, ); final Widget home = cupertino ? CupertinoPageScaffold( navigationBar: bar, child: const SizedBox.expand(), ) : Scaffold( extendBodyBehindAppBar: true, appBar: bar, body: const SizedBox.expand(), ); final app = cupertino ? CupertinoApp(home: home) : MaterialApp(home: home); return MediaQuery( data: MediaQueryData(disableAnimations: reduced), child: app, ); } /// The press scale of the control holding [of], or with [pill] of the /// pill holding it. double pressScale(WidgetTester tester, Finder of, {bool pill = false}) { final presses = find.ancestor( of: of, matching: find.byType(SegmentedPress), ); final press = pill ? presses.last : presses.first; final transform = tester.widget( find.descendant(of: press, matching: find.byType(Transform)).first, ); return transform.transform.storage[0]; } Color pressTint(WidgetTester tester, Finder of) { final press = find .ancestor(of: of, matching: find.byType(SegmentedPress)) .first; final box = tester.widget( find.descendant(of: press, matching: find.byType(DecoratedBox)).first, ); return (box.decoration as ShapeDecoration).color!; } final back = find.byKey(const Key('back')); final title = find.text('Runs'); for (final cupertino in [false, true]) { final host = cupertino ? 'Cupertino' : 'Material'; final icon = cupertino ? CupertinoIcons.refresh : Icons.refresh; Widget action(String key) => IconButton(key: Key(key), icon: Icon(icon), onPressed: () {}); testWidgets('$host: the glass fades in with its blur, not under a layer', ( tester, ) async { await tester.pumpWidget(page(cupertino)); // Back and the title are both still popping in. await tester.pump(const Duration(milliseconds: 100)); final backdrops = tester.layers.whereType().toList(); expect(backdrops, hasLength(2), reason: 'Back and the title'); expect(backdrops.first.backdropKey, isNotNull, reason: 'still grouped'); expect(backdrops.last.backdropKey, same(backdrops.first.backdropKey)); for (final layer in backdrops) { // No opacity layer above a blur: it would read nothing behind it. for (ContainerLayer? up = layer.parent; up != null; up = up.parent) { if (up is OpacityLayer) expect(up.alpha, anyOf(isNull, 255)); } } final mid = tester.widget( find.ancestor(of: back, matching: find.byType(BackdropFilter)).first, ); await tester.pumpAndSettle(); final rest = tester.widget( find.ancestor(of: back, matching: find.byType(BackdropFilter)).first, ); expect(mid.filter, isNot(rest.filter), reason: 'the blur fades in'); }); testWidgets( '$host: Back sinks and tints while pressed, then springs back', (tester) async { var backs = 0; await tester.pumpWidget(page(cupertino, onBack: () => backs++)); await tester.pumpAndSettle(); expect(pressScale(tester, back), 1); expect(pressTint(tester, back).a, 0); final gesture = await tester.startGesture(tester.getCenter(back)); await tester.pump(); await tester.pump(Motion.barPressDuration); expect(pressScale(tester, back), closeTo(Motion.barPressScale, 1e-6)); expect(pressTint(tester, back).a, greaterThan(0.1)); await gesture.up(); await tester.pumpAndSettle(); expect(pressScale(tester, back), closeTo(1, 1e-3)); expect(pressTint(tester, back).a, 0); expect(backs, 1, reason: 'the tap still reaches Back'); }, ); testWidgets('$host: a quick tap still shows the whole press', ( tester, ) async { await tester.pumpWidget(page(cupertino, actions: [action('a')])); await tester.pumpAndSettle(); final a = find.byKey(const Key('a')); await tester.tap(a); await tester.pump(); await tester.pump(const Duration(milliseconds: 80)); // A lone action is the whole pill: the pill sinks, not the icon. expect(pressScale(tester, a, pill: true), lessThan(0.95)); expect(pressScale(tester, a), 1); await tester.pumpAndSettle(); expect(pressScale(tester, a, pill: true), 1); }); testWidgets('$host: beside others each action sinks on its own', ( tester, ) async { var taps = 0; await tester.pumpWidget( page( cupertino, onTitle: () => taps++, actions: [action('a'), action('b')], ), ); await tester.pumpAndSettle(); final a = find.byKey(const Key('a')); final b = find.byKey(const Key('b')); final gesture = await tester.startGesture(tester.getCenter(b)); await tester.pump(); await tester.pump(Motion.barPressDuration); expect(pressScale(tester, b), closeTo(Motion.barPressScale, 1e-6)); expect(pressScale(tester, a), 1); expect(pressScale(tester, back), 1); await gesture.up(); await tester.pumpAndSettle(); // A pressable title sinks as a whole pill and still takes the tap. await tester.tap(title); await tester.pumpAndSettle(); expect(taps, 1); }); testWidgets('$host: hit testing is unchanged at rest', (tester) async { await tester.pumpWidget(page(cupertino, actions: [action('a')])); await tester.pumpAndSettle(); final pill = tester.getRect( find.ancestor(of: back, matching: find.byType(GlassPill)).first, ); expect(pill.left, 12); expect(pill.height, segmentedPieceHeight(cupertino)); // A tap between the pieces reaches no piece; on Back it reaches Back. final backBox = tester.renderObject(back); bool hitsBack(Offset at) => tester.hitTestOnBinding(at).path.any((e) => e.target == backBox); expect(hitsBack(Offset(pill.right + 4, pill.center.dy)), isFalse); expect(hitsBack(pill.center), isTrue); }); } }