import 'dart:math' as math; import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/conversation/composer_running_arc.dart'; const _primary = Color(0xFF3366FF); const _tertiary = Color(0xFFFF6633); /// The pill outline as the painter builds it, measured by arc length. Path _outlinePath(Size size, {double radius = 24}) => RoundedRectangleBorder( borderRadius: BorderRadius.circular(radius), ).getOuterPath(Offset.zero & size); List _outline(Size size, {double radius = 24}) => _outlinePath(size, radius: radius).computeMetrics().toList(); double _length(List metrics) => metrics.fold(0, (sum, m) => sum + m.length); /// The arc the visible highlight covers: from its first piece to its last. /// The pieces overlap by a seam each, so their own lengths would overcount. double _visibleSpan(List segments) => segments.last.end - segments.first.start; /// Colours that differ only by float rounding at a stop still match. void _expectSameColor(Color actual, Color expected, {required String reason}) { expect(actual.a, closeTo(expected.a, 1e-6), reason: reason); expect(actual.r, closeTo(expected.r, 1e-6), reason: reason); expect(actual.g, closeTo(expected.g, 1e-6), reason: reason); expect(actual.b, closeTo(expected.b, 1e-6), reason: reason); } /// The pieces cover [span] of the outline. Each overlaps its neighbour by a /// seam (a half pixel on each side), so their summed lengths may exceed it by /// about a pixel per piece; path metrics add about a pixel of approximation. void _expectCovered(double covered, int pieces, double span) { expect(covered, greaterThanOrEqualTo(span - 1)); expect(covered, lessThanOrEqualTo(span + pieces + 1)); } void main() { const size = Size(320, 48); final metrics = _outline(size); final length = _length(metrics); test('the outline is measured by arc length, not by angle', () { // A 320x48 pill with radius 24: two straights of 272 and four quarter // corners that make one full circle of radius 24. Flutter's curve metrics // are approximate, so allow about a pixel. expect(length, closeTo(2 * 272 + 2 * math.pi * 24, 1)); expect(metrics, hasLength(1)); }); test('the origin advances by equal arc for equal lap steps', () { // Curves.linear: the lap value is proportional to elapsed time, so equal // time steps must give equal arc steps all the way round the outline. const steps = 16; final first = runningHighlightOrigin(0, length); final stepArc = runningHighlightOrigin(1 / steps, length) - first; expect(stepArc, closeTo(length / steps, 1e-9)); for (var k = 0; k < steps; k++) { final origin = runningHighlightOrigin(k / steps, length); expect(origin, closeTo(k * stepArc, 1e-9), reason: 'step $k'); } }); test('the highlight wraps seamlessly at the end of a lap', () { expect(runningHighlightOrigin(1, length), 0); expect( runningHighlightOrigin(0.9999, length), closeTo(length, length * 1e-3), ); expect(runningHighlightOrigin(1.25, length), closeTo(length * .25, 1e-9)); final start = runningHighlightSegments( progress: 0, length: length, primary: _primary, tertiary: _tertiary, ); final end = runningHighlightSegments( progress: 1, length: length, primary: _primary, tertiary: _tertiary, ); expect(end.length, start.length); for (var i = 0; i < start.length; i++) { expect(end[i].start, start[i].start); expect(end[i].end, start[i].end); _expectSameColor(end[i].color, start[i].color, reason: 'piece $i'); } }); test( 'the colour fades in, turns tertiary and fades out along its length', () { expect(runningHighlightColorAt(0, _primary, _tertiary).a, 0); expect(runningHighlightColorAt(1, _primary, _tertiary).a, 0); _expectSameColor( runningHighlightColorAt(.3, _primary, _tertiary), _primary, reason: 'primary stop', ); _expectSameColor( runningHighlightColorAt(.6, _primary, _tertiary), _tertiary, reason: 'tertiary stop', ); }, ); test('colour is transparent outside the highlight', () { expect(runningHighlightColorAt(-.1, _primary, _tertiary).a, 0); expect(runningHighlightColorAt(1.1, _primary, _tertiary).a, 0); }); test('the highlight is a short segment, never a trail round the edge', () { for (final shape in [ _outline(const Size(320, 48)), _outline(const Size(120, 120), radius: 12), ]) { final len = _length(shape); final segments = runningHighlightSegments( progress: .3, length: len, primary: _primary, tertiary: _tertiary, ); expect(_visibleSpan(segments), closeTo(runningHighlightSpan * len, 7)); expect(runningHighlightSpan, lessThanOrEqualTo(.15)); } }); test('the visible segments tile the highlight without gaps', () { final segments = runningHighlightSegments( progress: .6, length: length, primary: _primary, tertiary: _tertiary, ); expect(segments, isNotEmpty); for (var i = 1; i < segments.length; i++) { expect(segments[i].start, lessThanOrEqualTo(segments[i - 1].end)); expect(segments[i].start, greaterThanOrEqualTo(segments[i - 1].start)); } expect(segments.first.start, greaterThanOrEqualTo(0)); expect(segments.last.end, lessThanOrEqualTo(length)); }); test('a highlight across the outline start is split into two pieces', () { // At progress .99 the segment begins near the end and wraps to the start. final segments = runningHighlightSegments( progress: .99, length: length, primary: _primary, tertiary: _tertiary, ); expect(segments.any((s) => s.start == 0), isTrue); expect(segments.any((s) => s.end == length), isTrue); _expectCovered( segments.fold(0, (s, p) => s + p.end - p.start), segments.length, runningHighlightSpan * length, ); }); test('only the travelling segment is painted, not the whole outline', () { for (var k = 0; k < 8; k++) { final strokes = runningHighlightStrokes( outline: _outlinePath(size), progress: k / 8, primary: _primary, tertiary: _tertiary, ); final painted = strokes.fold( 0, (sum, s) => sum + _length(s.path.computeMetrics().toList()), ); expect(strokes, isNotEmpty, reason: 'lap $k'); _expectCovered(painted, strokes.length, runningHighlightSpan * length); expect(painted, lessThan(length * .2), reason: 'lap $k'); } }); test('strokes are plain colour strokes inside the outline bounds', () { final strokes = runningHighlightStrokes( outline: _outlinePath(size), progress: .4, primary: _primary, tertiary: _tertiary, ); for (final stroke in strokes) { expect(stroke.color.a, inInclusiveRange(0, 1)); final bounds = stroke.path.getBounds(); expect(bounds.left, greaterThanOrEqualTo(-1)); expect(bounds.top, greaterThanOrEqualTo(-1)); expect(bounds.right, lessThanOrEqualTo(size.width + 1)); expect(bounds.bottom, lessThanOrEqualTo(size.height + 1)); } }); test('runningArcPath draws the requested arc of the outline', () { final piece = runningArcPath(metrics, 10, 30); final pieceMetrics = piece.computeMetrics().toList(); // Curve metrics are approximations; a fraction of a pixel is exact enough. expect(_length(pieceMetrics), closeTo(20, .5)); }); }