feat(app): add native content-sized graph stage node

One content-sized stage node as an ordinary native control. Material uses
stock Card.outlined/Card.filled inside an InkWell; Cupertino uses
CupertinoButton.tinted/filled. onSelected reports the definition key
verbatim, one merged button semantics node carries the exact label and
selected state, recorded status is shown only when the caller supplies it,
and invisible U+200B break opportunities reproduce the web's anywhere
wrapping. Painted-boundary and painted-surface tests cover the UI-review
2x outline and Cupertino container defects.
This commit is contained in:
2026-10-21 18:44:18 +01:00
parent f4d4a9869c
commit e63b01154b
6 changed files with 983 additions and 0 deletions
@@ -0,0 +1,210 @@
import 'dart:typed_data';
import 'dart:ui' as ui;
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/features/graph/domain/graph_definition.dart';
import 'package:pipelines_app/features/graph/widgets/graph_stage_node.dart';
const _stage = GraphNode(key: 'build', terminal: false, stageIndex: 1);
const _long = GraphNode(
key: 'implementer-with-an-unshortened-stage-label',
terminal: false,
stageIndex: 2,
);
const _longLabel = 'implementer-with-an-unshortened-stage-label';
Widget _material(Widget child) => MaterialApp(
home: Scaffold(body: Center(child: child)),
);
Widget _cupertino(Widget child) => CupertinoApp(
home: CupertinoPageScaffold(child: Center(child: child)),
);
/// The node centred on an opaque page, inside its own repaint boundary so the
/// painted result can be read back as pixels.
Widget _framed(Key key, Widget Function(Widget) wrap, Widget child) => wrap(
RepaintBoundary(
key: key,
child: ColoredBox(
color: const Color(0xFFFFFFFF),
child: Center(child: child),
),
),
);
Finder get _materialSurface =>
find.descendant(of: find.byType(Card), matching: find.byType(Material));
Finder _visual(String label) => find.byWidgetPredicate(
(widget) => widget is Text && widget.data?.replaceAll('\u200B', '') == label,
);
final class _Shot {
_Shot(this.bytes, this.size, this.origin);
final Uint8List bytes;
final Size size;
final Offset origin;
Color at(Offset global) {
final x = (global.dx - origin.dx).round();
final y = (global.dy - origin.dy).round();
expect(x, inInclusiveRange(0, size.width.round() - 1));
expect(y, inInclusiveRange(0, size.height.round() - 1));
final i = (y * size.width.round() + x) * 4;
return Color.fromARGB(bytes[i + 3], bytes[i], bytes[i + 1], bytes[i + 2]);
}
}
Future<_Shot> _shot(WidgetTester tester, Key key) async {
final boundary = tester.renderObject<RenderRepaintBoundary>(find.byKey(key));
final image = (await tester.runAsync(() => boundary.toImage()))!;
final data = (await tester.runAsync(
() => image.toByteData(format: ui.ImageByteFormat.rawRgba),
))!;
image.dispose();
return _Shot(
data.buffer.asUint8List(),
tester.getSize(find.byKey(key)),
tester.getTopLeft(find.byKey(key)),
);
}
double _distance(Color a, Color b) => <double>[
(a.r - b.r).abs() * 255,
(a.g - b.g).abs() * 255,
(a.b - b.b).abs() * 255,
].reduce((value, next) => value > next ? value : next);
void main() {
testWidgets('material node paints a rounded surface, not a stadium', (
tester,
) async {
await tester.pumpWidget(
_material(GraphStageNode(node: _stage, onSelected: (_) {})),
);
final material = tester.widget<Material>(_materialSurface);
expect(
material.shape,
isA<RoundedRectangleBorder>(),
reason:
'Material button shapes are stadiums; a stadium border cuts into a '
'wrapped label the way the reviewed 2x control did',
);
});
testWidgets('wrapped glyph boxes stay inside the painted material surface', (
tester,
) async {
for (final scale in [1.0, 2.0]) {
await tester.pumpWidget(
_material(
Builder(
builder: (context) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(scale)),
child: SizedBox(
width: 150,
child: GraphStageNode(node: _long, onSelected: (_) {}),
),
),
),
),
);
expect(tester.takeException(), isNull);
final rect = tester.getRect(_materialSurface);
final shape = tester.widget<Material>(_materialSurface).shape!;
final path = shape.getOuterPath(Offset.zero & rect.size);
final paragraph = tester.renderObject<RenderParagraph>(
_visual(_longLabel),
);
final boxes = paragraph.getBoxesForSelection(
TextSelection(
baseOffset: 0,
extentOffset: paragraph.text.toPlainText().length,
),
);
expect(boxes, isNotEmpty);
for (final box in boxes) {
final corners = <Offset>[
Offset(box.left, box.top),
Offset(box.right, box.top),
Offset(box.left, box.bottom),
Offset(box.right, box.bottom),
];
for (final corner in corners) {
final local = paragraph.localToGlobal(corner) - rect.topLeft;
expect(
path.contains(local),
isTrue,
reason:
'at ${scale}x a glyph box corner at $local escapes the '
'painted control boundary $rect',
);
}
}
}
});
testWidgets('unselected cupertino node paints a visible container', (
tester,
) async {
final key = GlobalKey();
await tester.pumpWidget(
_framed(
key,
_cupertino,
GraphStageNode(node: _stage, onSelected: (_) {}),
),
);
final shot = await _shot(tester, key);
final rect = tester.getRect(find.byType(CupertinoButton));
// Just inside the left edge of the control, clear of the label's inset.
final body = shot.at(rect.centerLeft + const Offset(4, 0));
final page = shot.at(const Offset(4, 4));
expect(
_distance(body, page),
greaterThan(24),
reason: 'an unselected cupertino node needs its own visible surface',
);
});
testWidgets('selected state changes the painted surface in both hosts', (
tester,
) async {
for (final wrap in [_material, _cupertino]) {
final colors = <bool, Color>{};
for (final selected in [false, true]) {
final key = GlobalKey();
await tester.pumpWidget(
_framed(
key,
wrap,
GraphStageNode(
node: _stage,
selected: selected,
onSelected: (_) {},
),
),
);
final shot = await _shot(tester, key);
final rect = tester.getRect(find.byType(GraphStageNode));
colors[selected] = shot.at(rect.centerLeft + const Offset(4, 0));
}
expect(
_distance(colors[false]!, colors[true]!),
greaterThan(16),
reason:
'selection must be visible, not only announced: the surface fill '
'and the outlined border change together',
);
}
});
}
@@ -0,0 +1,235 @@
import 'dart:ui' show Tristate;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/graph/domain/graph_definition.dart';
import 'package:pipelines_app/features/graph/widgets/graph_stage_node.dart';
const _stage = GraphNode(key: 'build', terminal: false, stageIndex: 1);
const _success = GraphNode(key: '#success', terminal: true);
const _failure = GraphNode(key: '#failure', terminal: true);
Widget _material(Widget child) => MaterialApp(
home: Scaffold(body: Center(child: child)),
);
Widget _cupertino(Widget child) => CupertinoApp(
home: CupertinoPageScaffold(child: Center(child: child)),
);
/// Applies a 2x text scale below the app so the fixture renders as a real
/// scale-2 layout rather than a test-only text property.
Widget _scaled(Widget child) => Builder(
builder: (context) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: const TextScaler.linear(2)),
child: child,
),
);
/// The merged accessibility node a screen reader receives for one node.
SemanticsData _semantics(WidgetTester tester) =>
tester.getSemantics(find.byType(MergeSemantics)).getSemanticsData();
void _expectSelectable(WidgetTester tester, {required bool selected}) {
final data = _semantics(tester);
expect(data.flagsCollection.isButton, isTrue);
expect(data.flagsCollection.isSelected == Tristate.isTrue, selected);
expect(data.actions & SemanticsAction.tap.index, isNot(0));
}
/// Finds the visual line for [label], ignoring its invisible break chars.
Finder _visual(String label) => find.byWidgetPredicate(
(widget) => widget is Text && widget.data?.replaceAll('\u200B', '') == label,
);
void main() {
testWidgets('selection callback carries the exact definition key', (
tester,
) async {
final picked = <String>[];
await tester.pumpWidget(
_material(GraphStageNode(node: _success, onSelected: picked.add)),
);
await tester.tap(find.byType(GraphStageNode));
await tester.pump();
expect(picked, ['#success']);
_expectSelectable(tester, selected: false);
expect(_semantics(tester).label, 'Success');
});
testWidgets('selected node is announced selected in both native hosts', (
tester,
) async {
await tester.pumpWidget(
_material(
GraphStageNode(node: _stage, selected: true, onSelected: (_) {}),
),
);
expect(find.byType(Card), findsOneWidget);
expect(find.byType(InkWell), findsOneWidget);
_expectSelectable(tester, selected: true);
await tester.pumpWidget(
_cupertino(
GraphStageNode(node: _stage, selected: true, onSelected: (_) {}),
),
);
expect(find.byType(CupertinoButton), findsOneWidget);
expect(find.byType(Card), findsNothing);
_expectSelectable(tester, selected: true);
});
testWidgets('unselected node is not announced selected', (tester) async {
await tester.pumpWidget(
_material(GraphStageNode(node: _stage, onSelected: (_) {})),
);
expect(find.byType(Card), findsOneWidget);
_expectSelectable(tester, selected: false);
});
testWidgets('keyboard activation selects the node', (tester) async {
for (final wrap in [_material, _cupertino]) {
final picked = <String>[];
await tester.pumpWidget(
wrap(GraphStageNode(node: _stage, onSelected: picked.add)),
);
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
await tester.pumpAndSettle();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(picked, ['build']);
}
});
testWidgets('keyboard selection keeps focus inside the material node', (
tester,
) async {
var selected = false;
await tester.pumpWidget(
_material(
StatefulBuilder(
builder: (context, setState) => GraphStageNode(
node: _stage,
selected: selected,
onSelected: (_) => setState(() => selected = true),
),
),
),
);
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
await tester.pumpAndSettle();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(selected, isTrue);
final focused = FocusManager.instance.primaryFocus;
expect(focused?.context, isNotNull);
expect(
focused?.context?.findAncestorWidgetOfExactType<Card>(),
isNotNull,
reason: 'switching card emphasis must not drop keyboard focus',
);
});
testWidgets('long label wraps unshortened in a narrow 2x layout', (
tester,
) async {
const label = 'implementer-with-an-unshortened-stage-label';
const node = GraphNode(key: label, terminal: false, stageIndex: 0);
for (final wrap in [_material, _cupertino]) {
await tester.pumpWidget(
wrap(
_scaled(
SizedBox(
width: 150,
child: GraphStageNode(node: node, onSelected: (_) {}),
),
),
),
);
expect(tester.takeException(), isNull);
final text = tester.widget<Text>(_visual(label));
expect(text.data!.replaceAll('\u200B', ''), label);
expect(text.maxLines, isNull);
expect(text.overflow, isNull);
final paragraph = tester.renderObject<RenderParagraph>(_visual(label));
expect(paragraph.size.width, lessThanOrEqualTo(150));
expect(
paragraph.size.height,
greaterThan(50),
reason: 'the 2x label must occupy more than one 2x text line',
);
// No glyph may paint outside the paragraph: every laid-out line,
// including a hyphen-broken segment, fits the width the control gave it.
final painter = TextPainter(
text: paragraph.text,
textDirection: paragraph.textDirection,
textScaler: paragraph.textScaler,
textAlign: paragraph.textAlign,
)..layout(maxWidth: paragraph.size.width);
expect(
painter.computeLineMetrics().every(
(line) => line.width <= paragraph.size.width + 0.5,
),
isTrue,
reason: 'an unshortened label must wrap, not paint over the control',
);
}
});
testWidgets('terminal shows its definition label and no invented status', (
tester,
) async {
for (final node in [_success, _failure]) {
await tester.pumpWidget(
_material(GraphStageNode(node: node, onSelected: (_) {})),
);
final texts = tester
.widgetList<Text>(find.byType(Text))
.map((text) => text.data?.replaceAll('\u200B', ''))
.toList();
expect(texts, [node.label]);
expect(texts.join(' ').toLowerCase(), isNot(contains('executed')));
expect(texts.join(' ').toLowerCase(), isNot(contains('visited')));
expect(_semantics(tester).label, node.label);
expect(_semantics(tester).label, isNot(contains('\u200B')));
}
});
testWidgets('a recorded status is shown verbatim', (tester) async {
await tester.pumpWidget(
_cupertino(
GraphStageNode(
node: _stage,
recordedStatus: 'failed',
onSelected: (_) {},
),
),
);
expect(_visual('build'), findsOneWidget);
expect(_visual('failed'), findsOneWidget);
final label = _semantics(tester).label;
expect(label, contains('build'));
expect(label, contains('failed'));
expect(label, isNot(contains('\u200B')));
});
testWidgets('a blank recorded status adds no line', (tester) async {
await tester.pumpWidget(
_material(
GraphStageNode(node: _stage, recordedStatus: ' ', onSelected: (_) {}),
),
);
expect(find.byType(Text), findsOneWidget);
expect(_semantics(tester).label, 'build');
});
}