import 'dart:math' as math; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/conversation/composer_running_outline.dart'; /// Records the strokes a [RunningRRectOutline] draws. class _Recorder implements Canvas { final lines = <(Offset, Offset)>[]; final arcs = <(Rect, double, double)>[]; @override void drawLine(Offset p1, Offset p2, Paint paint) => lines.add((p1, p2)); @override void drawArc(Rect rect, double start, double sweep, bool c, Paint paint) => arcs.add((rect, start, sweep)); @override dynamic noSuchMethod(Invocation invocation) => throw UnsupportedError('${invocation.memberName}'); } void main() { const shape = RoundedRectangleBorder( borderRadius: BorderRadius.all(Radius.circular(24)), ); for (final size in const [Size(320, 48), Size(300, 100), Size(40, 30)]) { test('follows the path outline of a $size pill by arc length', () { final rect = Offset.zero & size; final outline = RunningRRectOutline.of(shape, rect)!; final metric = shape.getOuterPath(rect).computeMetrics().single; // Path metrics approximate curves to about a pixel over the lap. expect(outline.length, closeTo(metric.length, 1.5)); for (var k = 0; k < 64; k++) { final d = metric.length * k / 64; final expected = metric.getTangentForOffset(d)!.position; expect( (outline.pointAt(d) - expected).distance, lessThan(1.5), reason: 'at $d of ${metric.length}', ); } }); } test('draws a range as lines along edges and arcs round corners', () { final outline = RunningRRectOutline.of( shape, const Rect.fromLTWH(0, 0, 320, 48), )!; // A 320x48 pill with radius 24: no left straight, then the top-left // corner, then the top edge. final canvas = _Recorder(); outline.draw(canvas, 10, 60, Paint()); expect(canvas.arcs, hasLength(1)); expect( canvas.arcs.single.$1, Rect.fromCircle(center: const Offset(24, 24), radius: 24), ); expect(canvas.arcs.single.$2, closeTo(math.pi + 10 / 24, 1e-9)); expect(canvas.lines, hasLength(1)); final (from, to) = canvas.lines.single; expect(from, const Offset(24, 0)); expect(to.dx, closeTo(24 + 60 - 12 * math.pi, 1e-9)); }); test('other shapes fall back to the measured path', () { expect( RunningRRectOutline.of( const StadiumBorder(), const Rect.fromLTWH(0, 0, 9, 9), ), isNull, ); expect( RunningRRectOutline.of( const RoundedRectangleBorder( borderRadius: BorderRadius.only(topLeft: Radius.circular(4)), ), const Rect.fromLTWH(0, 0, 9, 9), ), isNull, ); }); }