Files
pipelines/app/test/ui/adaptive/segmented_bar_overflow_test.dart
T
alan b2e67dbfa2 Center lone segmented More action
Wrap the generated More control in the same pill used by separate action islands so a narrow chat header keeps symmetric insets and consistent height. Add regression coverage and document the focused checks.
2026-11-13 13:34:36 +00:00

253 lines
8.4 KiB
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';
import 'package:pipelines_app/ui/adaptive/segmented_bar_pieces.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(String name, VoidCallback onPressed) => 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<Widget> _actions(List<String> pressed) => [
for (final name in _names)
if (name == 'Needs you')
_icon(name, () => pressed.add(name))
else
SegmentedOverflowAction(
overflowEntry: SegmentedOverflowEntry(
label: name,
icon: Icons.circle_outlined,
onPressed: () => pressed.add(name),
),
child: _icon(name, () => pressed.add(name)),
),
];
Widget _app(SegmentedBar bar, {Brightness? brightness}) => 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<void> _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<RenderParagraph>(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() {
{
const host = '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 = <String>[];
await _pump(
tester,
_app(
brightness: brightness,
SegmentedBar(title: 'Runs', actions: _actions(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),
];
const target = 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(SegmentedBar(title: 'Runs', actions: _actions([]))),
);
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 lone More action is centred in its separate island', (
tester,
) async {
_size(tester, 180);
await _pump(
tester,
_app(
SegmentedBar(
title: 'Runs',
separateActionIslands: true,
actions: [
for (final name in ['Filter', 'Sort', 'Select', 'Usage', 'View'])
SegmentedOverflowAction(
overflowEntry: SegmentedOverflowEntry(
label: name,
icon: Icons.filter_list,
),
child: _icon(name, () {}),
),
],
),
),
);
final more = find.byKey(segmentedMoreKey).hitTestable();
expect(more, findsOneWidget);
final pill = tester.getRect(find.byType(SegmentedPill).last);
final button = tester.getRect(more);
expect(pill.height, segmentedPieceHeight());
expect(button.left - pill.left, closeTo(pill.right - button.right, 0.1));
expect(button.top - pill.top, closeTo(pill.bottom - button.bottom, 0.1));
await tester.pumpWidget(
_app(
SegmentedBar(
title: 'Runs',
separateActionIslands: true,
actions: [_icon('Needs you', () {}), _icon('Usage', () {})],
),
),
);
await tester.pump();
expect(
tester.getRect(find.byType(SegmentedPill).last).height,
pill.height,
);
});
testWidgets('$host: a longer title keeps a readable share at 240px', (
tester,
) async {
_size(tester, 240);
await _pump(
tester,
_app(SegmentedBar(title: 'Pipelines', actions: _actions([]))),
);
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<RenderParagraph>(
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(
SegmentedBar(
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(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');
});
}
}