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/segmented_bar.dart'; void _size(WidgetTester tester, double width) { tester.view.physicalSize = Size(width, 700); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); } const _names = ['Needs you', 'Usage', 'Filter', 'Sort', 'Select']; Key _key(String name) => ValueKey('action.$name'); /// An icon action like the list bars' own, at the host's touch target. Widget _icon(bool cupertino, String name, VoidCallback onPressed) => cupertino ? Semantics( button: true, label: name, excludeSemantics: true, child: CupertinoButton( key: _key(name), padding: EdgeInsets.zero, minimumSize: const Size.square(kMinInteractiveDimensionCupertino), onPressed: onPressed, child: const Icon(CupertinoIcons.circle), ), ) : IconButton( key: _key(name), tooltip: name, onPressed: onPressed, icon: const Icon(Icons.circle_outlined), ); /// The Runs bar's shape: a bell that always stays, then four that may move. List _actions(bool cupertino, List pressed) => [ for (final name in _names) if (name == 'Needs you') _icon(cupertino, name, () => pressed.add(name)) else SegmentedOverflowAction( overflowEntry: SegmentedOverflowEntry( label: name, icon: Icons.circle_outlined, onPressed: () => pressed.add(name), ), child: _icon(cupertino, name, () => pressed.add(name)), ), ]; Widget _app(bool cupertino, SegmentedBar bar, {Brightness? brightness}) => cupertino ? CupertinoApp( theme: CupertinoThemeData(brightness: brightness), home: CupertinoPageScaffold( navigationBar: bar, child: const SizedBox.expand(), ), ) : MaterialApp( theme: ThemeData(brightness: brightness ?? Brightness.light), home: Scaffold( extendBodyBehindAppBar: true, appBar: bar, body: const SizedBox.expand(), ), ); /// Pumps the bar and the frame its More menu settles in. Future _pump(WidgetTester tester, Widget app) async { await tester.pumpWidget(app); await tester.pump(); await tester.pump(const Duration(seconds: 1)); } bool _whole(WidgetTester tester, String text) { final paragraph = tester.renderObject(find.text(text)); final natural = (TextPainter( text: TextSpan(text: text, style: paragraph.text.style), textDirection: TextDirection.ltr, textScaler: paragraph.textScaler, )..layout()).width; return !paragraph.didExceedMaxLines && paragraph.size.width >= natural - 0.5; } void main() { for (final cupertino in [false, true]) { final host = cupertino ? 'Cupertino' : 'Material'; for (final width in [240.0, 270.0, 320.0]) { for (final brightness in Brightness.values) { testWidgets('$host ${brightness.name} ${width.toInt()}px: the title ' 'shows whole, actions keep their targets, the rest are in More', ( tester, ) async { _size(tester, width); final pressed = []; await _pump( tester, _app( cupertino, brightness: brightness, SegmentedBar( cupertino: cupertino, title: 'Runs', actions: _actions(cupertino, pressed), ), ), ); expect(tester.takeException(), isNull); expect(_whole(tester, 'Runs'), isTrue); final title = tester.getRect(find.text('Runs')); final shown = [ for (final name in _names) if (find.byKey(_key(name)).hitTestable().evaluate().isNotEmpty) name, ]; // The bell cannot move; at these widths the others do not all fit. expect(shown, contains('Needs you')); expect(shown.length, lessThan(_names.length)); final more = find.byKey(segmentedMoreKey).hitTestable(); expect(more, findsOneWidget); final rects = [ for (final name in shown) tester.getRect(find.byKey(_key(name))), tester.getRect(more), ]; final target = cupertino ? 44.0 : 48.0; for (final (index, rect) in rects.indexed) { expect(rect.width, greaterThanOrEqualTo(target)); expect(rect.height, greaterThanOrEqualTo(target)); // Clear of the title and inside the bar's edge padding. expect(rect.left, greaterThan(title.right)); expect(rect.right, lessThanOrEqualTo(width - 12)); if (index > 0) { expect(rect.left, greaterThanOrEqualTo(rects[index - 1].right)); } } // Each moved action is in the More menu and runs from there. await tester.tap(more); await tester.pumpAndSettle(); final moved = [ for (final name in _names) if (!shown.contains(name)) name, ]; for (final name in moved) { expect(find.text(name), findsOneWidget, reason: name); } await tester.tap(find.text(moved.last)); await tester.pumpAndSettle(); expect(pressed, [moved.last]); }); } } testWidgets('$host: a wide bar shows every action and no More', ( tester, ) async { _size(tester, 800); await _pump( tester, _app( cupertino, SegmentedBar( cupertino: cupertino, title: 'Runs', actions: _actions(cupertino, []), ), ), ); for (final name in _names) { expect(find.byKey(_key(name)).hitTestable(), findsOneWidget); } expect(find.byKey(segmentedMoreKey).hitTestable(), findsNothing); // A hidden More is out of the semantics tree too. expect(find.bySemanticsLabel('More actions'), findsNothing); }); testWidgets('$host: a longer title keeps a readable share at 240px', ( tester, ) async { _size(tester, 240); await _pump( tester, _app( cupertino, SegmentedBar( cupertino: cupertino, title: 'Pipelines', actions: _actions(cupertino, []), ), ), ); expect(tester.takeException(), isNull); // The test font's glyphs are squares, wider than a real font's, so // the name may ellipsise here, but never to nothing. final paragraph = tester.renderObject( find.text('Pipelines'), ); expect(paragraph.size.width, greaterThan(3 * 14)); expect(find.byKey(segmentedMoreKey).hitTestable(), findsOneWidget); }); testWidgets('$host: a choice entry offers its choices from More', ( tester, ) async { _size(tester, 240); String? picked; await _pump( tester, _app( cupertino, SegmentedBar( cupertino: cupertino, title: 'Runs', actions: [ for (final name in ['A', 'B', 'C']) SegmentedOverflowAction( overflowEntry: SegmentedOverflowEntry( label: 'Sort $name', icon: Icons.sort, choices: [ for (final order in ['Newest', 'Oldest']) SegmentedMenuChoice( label: order, selected: order == 'Newest', onSelected: () => picked = '$name $order', ), ], ), child: _icon(cupertino, name, () {}), ), ], ), ), ); await tester.tap(find.byKey(segmentedMoreKey)); await tester.pumpAndSettle(); await tester.tap(find.text('Sort C')); await tester.pumpAndSettle(); await tester.tap(find.text('Oldest')); await tester.pumpAndSettle(); expect(picked, 'C Oldest'); }); } }