Files
pipelines/app/test/features/chat/conversation/composer_running_arc_test.dart
T
alan d9ec43e607 Animate composer border at constant arc speed
Lay the running highlight out by rounded-border arc length so it advances at a steady linear speed without a loop jump, while preserving the existing colours and reduced-motion border. Add focused geometry tests and implementation notes.
2026-11-12 10:54:42 +00:00

160 lines
5.8 KiB
Dart

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.
List<PathMetric> _outline(Size size, {double radius = 24}) {
final path = RoundedRectangleBorder(
borderRadius: BorderRadius.circular(radius),
).getOuterPath(Offset.zero & size);
return path.computeMetrics().toList();
}
double _length(List<PathMetric> metrics) =>
metrics.fold<double>(0, (sum, m) => sum + m.length);
/// The arc the visible band covers: from its first piece to its last. The
/// pieces overlap by a seam each, so their own lengths would overcount.
double _visibleSpan(List<RunningArcSegment> segments) =>
segments.last.end - segments.first.start;
/// Colours that differ only by float rounding at a band edge 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);
}
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('colours wrap: a fraction and its +1 are the same colour', () {
for (var f = 0.0; f < 1; f += .02) {
_expectSameColor(
runningHighlightColorAt(f + 1, _primary, _tertiary),
runningHighlightColorAt(f, _primary, _tertiary),
reason: 'f=$f',
);
}
expect(runningHighlightColorAt(0, _primary, _tertiary).a, 0);
});
test('the highlight travels by arc: a shift of one step shifts colours', () {
// The colour at a given arc point, one step later, equals the colour one
// step earlier at a point shifted back by the arc that step covers.
const dt = .1;
final arc = dt * length;
for (var x = 0.0; x < length; x += length / 40) {
final before = runningHighlightColorAt(
(x - runningHighlightOrigin(0, length)) / length,
_primary,
_tertiary,
);
final after = runningHighlightColorAt(
(x + arc - runningHighlightOrigin(dt, length)) / length,
_primary,
_tertiary,
);
_expectSameColor(after, before, reason: 'x=$x');
}
});
test('the visible band covers the same arc on a long and a square shape', () {
// The band is 38% of a lap. Measured along the outline it has the same
// length whatever the aspect, which an angle-based sweep would not give.
final long = _outline(const Size(320, 48));
final square = _outline(const Size(120, 120), radius: 12);
for (final shape in [long, square]) {
final len = _length(shape);
final segments = runningHighlightSegments(
progress: .3,
length: len,
primary: _primary,
tertiary: _tertiary,
);
expect(_visibleSpan(segments), closeTo(.38 * len, 2 * 3 + 1));
}
});
test('the visible segments tile the outline 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('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));
});
}