Merge branch 'main' into flutter/p2-node-widget
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pipelines_app/features/graph/routing/route_models.dart';
|
||||
|
||||
/// Shared hand-built routes for the edge-layer painting tests.
|
||||
|
||||
EdgeRoute edgeRoute({
|
||||
required List<RoutePoint> points,
|
||||
RouteKind kind = RouteKind.forward,
|
||||
RoutePortSide targetSide = RoutePortSide.left,
|
||||
double portX = 0,
|
||||
double portY = 0,
|
||||
}) {
|
||||
final start = points.isEmpty ? RoutePoint(portX, portY) : points.first;
|
||||
final end = points.isEmpty ? RoutePoint(portX, portY) : points.last;
|
||||
return EdgeRoute(
|
||||
id: 1,
|
||||
source: 'a',
|
||||
outcome: 'success',
|
||||
target: 'b',
|
||||
label: 'success',
|
||||
fallback: false,
|
||||
kind: kind,
|
||||
straight: false,
|
||||
sourcePort: RoutePort(side: RoutePortSide.right, point: start),
|
||||
targetPort: RoutePort(side: targetSide, point: end),
|
||||
points: points,
|
||||
);
|
||||
}
|
||||
|
||||
Offset routeOffset(RoutePoint point) => Offset(point.x, point.y);
|
||||
|
||||
/// The direction of the route's last non-degenerate segment.
|
||||
Offset arrival(EdgeRoute route) {
|
||||
final points = route.points;
|
||||
final end = points.last;
|
||||
for (var index = points.length - 2; index >= 0; index -= 1) {
|
||||
final delta = Offset(end.x - points[index].x, end.y - points[index].y);
|
||||
if (delta != Offset.zero) return delta / delta.distance;
|
||||
}
|
||||
return Offset.zero;
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/graph/painting/graph_edge_layer.dart';
|
||||
import 'package:pipelines_app/features/graph/routing/route_models.dart';
|
||||
|
||||
import '../routing/routing_fixtures.dart';
|
||||
import 'edge_layer_fixtures.dart';
|
||||
|
||||
void main() {
|
||||
const color = Colors.black87;
|
||||
final straight = edgeRoute(
|
||||
points: const [RoutePoint(10, 20), RoutePoint(90, 20)],
|
||||
);
|
||||
final wrapping = edgeRoute(
|
||||
points: const [
|
||||
RoutePoint(10, 20),
|
||||
RoutePoint(40, 20),
|
||||
RoutePoint(40, 60),
|
||||
RoutePoint(80, 60),
|
||||
RoutePoint(80, 50),
|
||||
],
|
||||
);
|
||||
|
||||
group('path', () {
|
||||
test('keeps every orthogonal vertex of the supplied route', () {
|
||||
final painter = GraphEdgePainter(
|
||||
routes: [wrapping],
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
);
|
||||
final path = painter.linePath(wrapping);
|
||||
final metric = path.computeMetrics().single;
|
||||
expect(metric.length, closeTo(30 + 40 + 40 + 10, 0.001));
|
||||
expect(
|
||||
metric.getTangentForOffset(0)!.position,
|
||||
routeOffset(wrapping.points.first),
|
||||
);
|
||||
expect(
|
||||
metric.getTangentForOffset(metric.length)!.position,
|
||||
routeOffset(wrapping.points.last),
|
||||
);
|
||||
expect(path.getBounds(), const Rect.fromLTRB(10, 20, 80, 60));
|
||||
});
|
||||
|
||||
test('a straight route is the exact port-to-port segment', () {
|
||||
final painter = GraphEdgePainter(
|
||||
routes: [straight],
|
||||
color: color,
|
||||
strokeWidth: 1,
|
||||
);
|
||||
final metric = painter.linePath(straight).computeMetrics().single;
|
||||
expect(metric.length, 80);
|
||||
expect(metric.getTangentForOffset(40)!.vector, const Offset(1, 0));
|
||||
});
|
||||
|
||||
test('zero- and one-point routes produce an empty path', () {
|
||||
final painter = GraphEdgePainter(
|
||||
routes: const [],
|
||||
color: color,
|
||||
strokeWidth: 1,
|
||||
);
|
||||
final empty = edgeRoute(points: const []);
|
||||
final single = edgeRoute(points: const [RoutePoint(5, 5)]);
|
||||
for (final route in [empty, single]) {
|
||||
final path = painter.linePath(route);
|
||||
expect(path.getBounds().isEmpty, isTrue);
|
||||
expect(path.computeMetrics().isEmpty, isTrue);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group('arrow', () {
|
||||
test('points into a forward target port', () {
|
||||
final painter = GraphEdgePainter(
|
||||
routes: [straight],
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
);
|
||||
final arrow = painter.arrowPath(straight);
|
||||
final tip = routeOffset(straight.points.last);
|
||||
expect(
|
||||
arrow.computeMetrics().first.getTangentForOffset(0)!.position,
|
||||
tip,
|
||||
);
|
||||
// 12 long and 8 wide for a 2px stroke, pointing right.
|
||||
expect(arrow.getBounds(), const Rect.fromLTRB(78, 16, 90, 24));
|
||||
expect(arrow.contains(tip.translate(-5, 0)), isTrue);
|
||||
expect(arrow.contains(tip.translate(2, 0)), isFalse);
|
||||
expect(arrow.contains(tip.translate(-14, 0)), isFalse);
|
||||
});
|
||||
|
||||
test('follows self-loop arrival instead of assuming a rightward edge', () {
|
||||
final routing = routeFor(selfLoopGraphFixture());
|
||||
final loop = routing.routes.singleWhere(
|
||||
(route) => route.kind == RouteKind.selfLoop,
|
||||
);
|
||||
final painter = GraphEdgePainter(
|
||||
routes: routing.routes,
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
);
|
||||
final direction = arrival(loop);
|
||||
final tip = routeOffset(loop.points.last);
|
||||
final arrow = painter.arrowPath(loop);
|
||||
|
||||
expect(loop.targetPort.side, RoutePortSide.right);
|
||||
expect(direction.dx, -1);
|
||||
expect(arrow.getBounds().left, tip.dx);
|
||||
expect(arrow.contains(tip.translate(5, 0)), isTrue);
|
||||
expect(arrow.contains(tip.translate(-2, 0)), isFalse);
|
||||
});
|
||||
|
||||
test('a backward route keeps its leftward arrival into a right port', () {
|
||||
final back = edgeRoute(
|
||||
kind: RouteKind.back,
|
||||
targetSide: RoutePortSide.right,
|
||||
points: const [
|
||||
RoutePoint(120, 20),
|
||||
RoutePoint(140, 20),
|
||||
RoutePoint(140, 60),
|
||||
RoutePoint(80, 60),
|
||||
],
|
||||
);
|
||||
final painter = GraphEdgePainter(
|
||||
routes: [back],
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
);
|
||||
final arrow = painter.arrowPath(back);
|
||||
final tip = routeOffset(back.points.last);
|
||||
|
||||
expect(back.kind, RouteKind.back);
|
||||
expect(arrow.getBounds().left, tip.dx);
|
||||
expect(arrow.contains(tip.translate(5, 0)), isTrue);
|
||||
expect(arrow.contains(tip.translate(-2, 0)), isFalse);
|
||||
});
|
||||
|
||||
test('keeps the routed arrival direction for every cycle edge', () {
|
||||
final routing = routeFor(cycleGraphFixture());
|
||||
final painter = GraphEdgePainter(
|
||||
routes: routing.routes,
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
);
|
||||
expect(routing.routes, isNotEmpty);
|
||||
for (final route in routing.routes) {
|
||||
final direction = arrival(route);
|
||||
final tip = routeOffset(route.points.last);
|
||||
final arrow = painter.arrowPath(route);
|
||||
expect(
|
||||
arrow.computeMetrics().first.getTangentForOffset(0)!.position,
|
||||
tip,
|
||||
);
|
||||
expect(arrow.contains(tip - direction * 5), isTrue);
|
||||
expect(arrow.contains(tip + direction * 2), isFalse);
|
||||
}
|
||||
});
|
||||
|
||||
test('a routed forward edge ends exactly at its target port', () {
|
||||
final routing = routeFor(chainGraphFixture());
|
||||
final painter = GraphEdgePainter(
|
||||
routes: routing.routes,
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
);
|
||||
expect(routing.routes, isNotEmpty);
|
||||
for (final route in routing.routes) {
|
||||
expect(route.points.last, route.targetPort.point);
|
||||
final arrow = painter.arrowPath(route);
|
||||
expect(
|
||||
arrow.computeMetrics().first.getTangentForOffset(0)!.position,
|
||||
routeOffset(route.targetPort.point),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('anchors the tip at the target port, not the last route point', () {
|
||||
final route = EdgeRoute(
|
||||
id: 7,
|
||||
source: 'a',
|
||||
outcome: 'success',
|
||||
target: 'b',
|
||||
label: 'success',
|
||||
fallback: false,
|
||||
kind: RouteKind.forward,
|
||||
straight: true,
|
||||
sourcePort: const RoutePort(
|
||||
side: RoutePortSide.right,
|
||||
point: RoutePoint(10, 20),
|
||||
),
|
||||
targetPort: const RoutePort(
|
||||
side: RoutePortSide.left,
|
||||
point: RoutePoint(85, 20),
|
||||
),
|
||||
points: const [RoutePoint(10, 20), RoutePoint(80, 20)],
|
||||
);
|
||||
final arrow = GraphEdgePainter(
|
||||
routes: [route],
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
).arrowPath(route);
|
||||
expect(
|
||||
arrow.computeMetrics().first.getTangentForOffset(0)!.position,
|
||||
const Offset(85, 20),
|
||||
);
|
||||
});
|
||||
|
||||
test('scales with the caller stroke width', () {
|
||||
final small = GraphEdgePainter(
|
||||
routes: [straight],
|
||||
color: color,
|
||||
strokeWidth: 1,
|
||||
).arrowPath(straight);
|
||||
final large = GraphEdgePainter(
|
||||
routes: [straight],
|
||||
color: color,
|
||||
strokeWidth: 4,
|
||||
).arrowPath(straight);
|
||||
expect(small.getBounds().width, 6);
|
||||
expect(small.getBounds().height, 4);
|
||||
expect(large.getBounds().width, 24);
|
||||
expect(large.getBounds().height, 16);
|
||||
});
|
||||
|
||||
test('falls back to the port inward normal for a degenerate tail', () {
|
||||
final route = edgeRoute(
|
||||
points: const [RoutePoint(50, 50), RoutePoint(50, 50)],
|
||||
targetSide: RoutePortSide.top,
|
||||
);
|
||||
final arrow = GraphEdgePainter(
|
||||
routes: [route],
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
).arrowPath(route);
|
||||
final tip = routeOffset(route.points.last);
|
||||
expect(arrow.getBounds(), const Rect.fromLTRB(46, 38, 54, 50));
|
||||
expect(arrow.contains(tip.translate(0, -5)), isTrue);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/graph/painting/graph_edge_layer.dart';
|
||||
import 'package:pipelines_app/features/graph/routing/route_models.dart';
|
||||
|
||||
import 'edge_layer_fixtures.dart';
|
||||
|
||||
void main() {
|
||||
const color = Colors.black87;
|
||||
final straight = edgeRoute(
|
||||
points: const [RoutePoint(10, 20), RoutePoint(90, 20)],
|
||||
);
|
||||
final wrapping = edgeRoute(
|
||||
points: const [
|
||||
RoutePoint(10, 20),
|
||||
RoutePoint(40, 20),
|
||||
RoutePoint(40, 60),
|
||||
RoutePoint(80, 60),
|
||||
RoutePoint(80, 50),
|
||||
],
|
||||
);
|
||||
|
||||
test('draws one stroke and one filled arrow per route, clipped', () {
|
||||
final canvas = TestRecordingCanvas();
|
||||
GraphEdgePainter(
|
||||
routes: [straight, wrapping],
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
).paint(canvas, const Size(120, 80));
|
||||
|
||||
final calls = canvas.invocations
|
||||
.where((call) => call.invocation.memberName == #drawPath)
|
||||
.toList();
|
||||
expect(calls.length, 4);
|
||||
final paints = [
|
||||
for (final call in calls)
|
||||
call.invocation.positionalArguments.last as Paint,
|
||||
];
|
||||
expect(paints[0].style, PaintingStyle.stroke);
|
||||
expect(paints[1].style, PaintingStyle.stroke);
|
||||
expect(paints[2].style, PaintingStyle.fill);
|
||||
expect(paints[3].style, PaintingStyle.fill);
|
||||
expect(
|
||||
paints.every((paint) => paint.color.toARGB32() == color.toARGB32()),
|
||||
isTrue,
|
||||
);
|
||||
|
||||
final clip = canvas.invocations.singleWhere(
|
||||
(call) => call.invocation.memberName == #clipRect,
|
||||
);
|
||||
expect(
|
||||
clip.invocation.positionalArguments.first,
|
||||
const Rect.fromLTWH(0, 0, 120, 80),
|
||||
);
|
||||
});
|
||||
|
||||
test('paints nothing for an empty route list', () {
|
||||
final canvas = TestRecordingCanvas();
|
||||
GraphEdgePainter(
|
||||
routes: const [],
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
).paint(canvas, const Size(120, 80));
|
||||
expect(canvas.invocations, isEmpty);
|
||||
});
|
||||
|
||||
test('reports every canvas point as a miss', () {
|
||||
final painter = GraphEdgePainter(
|
||||
routes: [straight],
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
);
|
||||
expect(painter.hitTest(const Offset(50, 20)), isFalse);
|
||||
expect(painter.hitTest(const Offset(500, 500)), isFalse);
|
||||
});
|
||||
|
||||
testWidgets('clips painted geometry to the canvas bounds', (tester) async {
|
||||
final recorder = ui.PictureRecorder();
|
||||
GraphEdgePainter(
|
||||
routes: [
|
||||
edgeRoute(points: const [RoutePoint(-30, 10), RoutePoint(60, 10)]),
|
||||
],
|
||||
color: Colors.black,
|
||||
strokeWidth: 6,
|
||||
).paint(Canvas(recorder), const Size(20, 20));
|
||||
final data = await tester.runAsync(() async {
|
||||
final image = await recorder.endRecording().toImage(80, 40);
|
||||
final bytes = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
|
||||
image.dispose();
|
||||
return bytes;
|
||||
});
|
||||
expect(data, isNotNull);
|
||||
final bytes = data!.buffer.asUint8List();
|
||||
int alphaAt(int x, int y) => bytes[(y * 80 + x) * 4 + 3];
|
||||
expect(alphaAt(10, 10), 255, reason: 'inside the clip is painted');
|
||||
expect(alphaAt(40, 10), 0, reason: 'the route continues past the clip');
|
||||
expect(alphaAt(10, 35), 0, reason: 'below the canvas stays empty');
|
||||
});
|
||||
|
||||
testWidgets('lets taps through to a sibling below the canvas', (
|
||||
tester,
|
||||
) async {
|
||||
var taps = 0;
|
||||
Widget harness(List<EdgeRoute> routes) => MaterialApp(
|
||||
home: Center(
|
||||
child: SizedBox(
|
||||
width: 200,
|
||||
height: 100,
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onTap: () => taps += 1,
|
||||
),
|
||||
),
|
||||
GraphEdgeLayer(
|
||||
routes: routes,
|
||||
canvasSize: const Size(200, 100),
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
for (final routes in <List<EdgeRoute>>[
|
||||
const <EdgeRoute>[],
|
||||
[straight],
|
||||
]) {
|
||||
await tester.pumpWidget(harness(routes));
|
||||
final origin = tester.getTopLeft(find.byType(GraphEdgeLayer));
|
||||
final before = taps;
|
||||
await tester.tapAt(origin + const Offset(50, 20));
|
||||
await tester.tapAt(origin + const Offset(150, 80));
|
||||
expect(taps, before + 2, reason: 'routes=$routes');
|
||||
}
|
||||
});
|
||||
|
||||
test('repaints on route, color or stroke changes only', () {
|
||||
final painter = GraphEdgePainter(
|
||||
routes: [straight],
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
);
|
||||
GraphEdgePainter copy({
|
||||
List<EdgeRoute>? routes,
|
||||
Color? color,
|
||||
double? strokeWidth,
|
||||
}) => GraphEdgePainter(
|
||||
routes: routes ?? [straight],
|
||||
color: color ?? Colors.black87,
|
||||
strokeWidth: strokeWidth ?? 2,
|
||||
);
|
||||
|
||||
expect(painter.shouldRepaint(copy()), isFalse);
|
||||
expect(painter.shouldRepaint(copy(color: Colors.red)), isTrue);
|
||||
expect(painter.shouldRepaint(copy(strokeWidth: 3)), isTrue);
|
||||
expect(
|
||||
painter.shouldRepaint(
|
||||
copy(
|
||||
routes: [
|
||||
edgeRoute(points: const [RoutePoint(10, 20), RoutePoint(90, 30)]),
|
||||
],
|
||||
),
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('lays out at the supplied canvas size', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Center(
|
||||
child: GraphEdgeLayer(
|
||||
routes: [wrapping],
|
||||
canvasSize: const Size(120, 80),
|
||||
color: color,
|
||||
strokeWidth: 2,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tester.getSize(find.byType(GraphEdgeLayer)), const Size(120, 80));
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user