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 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 >[ const [], [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? 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); }); }