Files
cutdex/packages/cutdex_timeline_ui/test/timeline_timecode_ruler_test.dart
T
lingjhf 06f1c26681
Gitea CI / Assemble and validate CI artifacts (push) Successful in 1m47s
Gitea CI / Check code and tests (push) Successful in 7m15s
Gitea CI / Build Android APK (push) Successful in 4m6s
Gitea CI / Build Windows x64 app (push) Successful in 3m34s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m29s
Gitea CI / Gitea CI passed (push) Successful in 1s
Gitea Release / Prepare draft Release (push) Failing after 8s
Gitea Release / Upload Android to draft Release (push) Skipped
Gitea Release / Upload Windows to draft Release (push) Skipped
Gitea Release / Verify and publish Release (push) Skipped
Move editor UI into the app and release verified Core dependencies (#14)
2026-10-11 19:59:40 +00:00

534 lines
18 KiB
Dart

import 'dart:ui' as ui;
import 'package:cutdex_core/timeline/engine.dart';
import 'package:cutdex_core/timeline/layout.dart';
import 'package:cutdex_core/timeline/model.dart';
import 'package:cutdex_timeline_ui/cutdex_timeline_ui.dart';
import 'package:cutdex_timeline_ui/src/timeline_ruler_scale.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test(
'desktop density moves from seconds to frames at the chosen spacing',
() {
for (final (pixelsPerSecond, major, minor) in [
(12.0, 300, 30),
(30.0, 150, 15),
(120.0, 30, 5),
(360.0, 10, 1),
]) {
final scale = TimelineRulerScale.resolve(
pixelsPerFrame: pixelsPerSecond / 30,
nominalFramesPerSecond: 30,
labelSpacing: 120,
);
expect(scale.majorFrames, major);
expect(scale.minorFrames, minor);
}
},
);
test('labels and subdivisions stay legible and on the frame grid', () {
for (final fps in [24, 25, 30, 48, 50, 60, 120]) {
for (final pixelsPerFrame in [.001, .01, .1, .4, 1, 4, 7.99, 8, 16, 32]) {
for (final width in [120.0, 180.0, 320.0]) {
final scale = TimelineRulerScale.resolve(
pixelsPerFrame: pixelsPerFrame.toDouble(),
nominalFramesPerSecond: fps,
labelSpacing: width,
);
expect(
scale.majorFrames * pixelsPerFrame,
greaterThanOrEqualTo(width),
);
expect(scale.minorFrames * pixelsPerFrame, greaterThanOrEqualTo(8));
expect(scale.majorFrames % scale.minorFrames, 0);
if (scale.middleFrames case final middle?) {
expect(scale.majorFrames % middle, 0);
expect(middle % scale.minorFrames, 0);
expect(middle, greaterThan(scale.minorFrames));
expect(middle, lessThan(scale.majorFrames));
}
if (pixelsPerFrame >= 8) expect(scale.minorFrames, 1);
}
}
}
});
test('complete timecode counts actual frames rather than milliseconds', () {
for (final format in [
TimelineFormat.fps24,
TimelineFormat.fps25,
TimelineFormat.fps30,
TimelineFormat(frameDuration: RationalTime(1001, 24000)),
TimelineFormat(frameDuration: RationalTime(1001, 30000)),
TimelineFormat(frameDuration: RationalTime(1, 60)),
]) {
final timecode = TimelineRulerTimecode(format);
final fps = timecode.framesPerSecond;
expect(timecode.formatFrame(0), '00:00:00:00');
expect(timecode.formatFrame(fps - 1), '00:00:00:${fps - 1}');
expect(timecode.formatFrame(fps), '00:00:01:00');
expect(timecode.formatFrame(fps * 60), '00:01:00:00');
expect(timecode.formatFrame(fps * 3600), '01:00:00:00');
expect(timecode.formatFrame(fps * 3600 * 100), '100:00:00:00');
expect(timecode.formatTick(timecode.frameTicks * fps), '00:00:01:00');
}
final ntsc = TimelineRulerTimecode(
TimelineFormat(frameDuration: RationalTime(1001, 30000)),
);
expect(ntsc.formatTick(TimelineClock.ticksPerSecond), '00:00:00:29');
});
test('extreme overview scales remain bounded by frame address capacity', () {
final scale = TimelineRulerScale.resolve(
pixelsPerFrame: 1e-30,
nominalFramesPerSecond: 30,
labelSpacing: 120,
);
expect(scale.majorFrames, greaterThan(0));
expect(scale.majorFrames, lessThanOrEqualTo(0x7fffffffffffffff));
expect(scale.minorFrames, scale.majorFrames);
});
test('drop-frame timecode skips the correct labels at minute boundaries', () {
for (final multiplier in [1, 2]) {
final timecode = TimelineRulerTimecode(
TimelineFormat(
frameDuration: RationalTime(1001, 30000 * multiplier),
timecodeFormat: TimelineTimecodeFormat.dropFrame,
),
);
expect(timecode.formatFrame(0), '00:00:00;00');
expect(
timecode.formatFrame(1800 * multiplier - 1),
'00:00:59;${30 * multiplier - 1}',
);
expect(
timecode.formatFrame(1800 * multiplier),
'00:01:00;${(2 * multiplier).toString().padLeft(2, '0')}',
);
expect(timecode.formatFrame(17982 * multiplier), '00:10:00;00');
expect(timecode.formatFrame(107892 * multiplier), '01:00:00;00');
}
});
test(
'project start time shifts addresses without changing local geometry',
() {
final timecode = TimelineRulerTimecode(
TimelineFormat(
frameDuration: RationalTime(1, 25),
startTime: RationalTime.seconds(3600),
),
);
expect(timecode.formatFrame(0), '01:00:00:00');
expect(timecode.formatFrame(26), '01:00:01:01');
final negative = TimelineRulerTimecode(
TimelineFormat(
frameDuration: RationalTime(1, 25),
startTime: RationalTime.seconds(-1),
),
);
expect(negative.formatFrame(0), '-00:00:01:00');
expect(negative.formatFrame(25), '00:00:00:00');
},
);
for (final layout in TimelineRulerTimecodeLayout.values) {
for (final pixelsPerSecond in [30.0, 360.0]) {
testWidgets(
'${layout.name} edge timecodes pan until all text is clipped at $pixelsPerSecond px/s',
(tester) async {
final c = _controller(pixelsPerSecond: pixelsPerSecond);
addTearDown(c.dispose);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: TimelineRuler(
controller: c,
style: TimelineRulerStyle.timecode,
timecodeLayout: layout,
showPlayhead: false,
theme: const TimelineThemeData.dark().copyWith(
rulerHeight: 28,
rulerTextStyle: const TextStyle(fontSize: 12),
),
),
),
);
List<Rect> paint() {
final canvas = _RulerCanvas();
tester
.widget<CustomPaint>(find.byType(CustomPaint))
.painter!
.paint(canvas, const Size(640, 28));
expect(canvas.clips, contains(const Rect.fromLTWH(0, 0, 640, 28)));
return canvas.labels.toSet().toList();
}
final original = c.camera;
final initialLabels = paint();
final label = initialLabels.firstWhere(
(r) => r.left > 100 && r.right < 640,
);
Matcher sameBounds(Rect expected) => predicate<Rect>(
(r) =>
(r.topLeft - expected.topLeft).distance < 1e-6 &&
(r.bottomRight - expected.bottomRight).distance < 1e-6,
'bounds matching $expected',
);
Future<List<Rect>> pan(double pixels) async {
c.setCamera(original.panBy(horizontalPixels: pixels));
await tester.pump();
return paint();
}
// Leave only the last few pixels visible, after the major mark and
// several subdivisions have already moved outside the viewport.
final leftPan = label.right - 8.5;
final partialLeft = label.shift(Offset(-leftPan, 0));
expect(await pan(leftPan), contains(sameBounds(partialLeft)));
expect(
await pan(leftPan + 4),
contains(sameBounds(partialLeft.shift(const Offset(-4, 0)))),
);
expect(
await pan(leftPan + 9),
isNot(contains(sameBounds(partialLeft.shift(const Offset(-9, 0))))),
);
// Right-edge labels enter progressively rather than waiting until the
// complete timecode fits. Centered labels can enter before their mark.
final spacing = initialLabels[1].left - initialLabels[0].left;
final rightLabel = label.shift(
Offset(spacing * (640 / spacing).ceil(), 0),
);
final rightPan = rightLabel.left - 631.5;
final partialRight = rightLabel.shift(Offset(-rightPan, 0));
expect(await pan(rightPan), contains(sameBounds(partialRight)));
expect(
await pan(rightPan + 4),
contains(sameBounds(partialRight.shift(const Offset(-4, 0)))),
);
expect(
await pan(rightPan - 9),
isNot(contains(sameBounds(partialRight.shift(const Offset(9, 0))))),
);
expect(tester.takeException(), isNull);
},
);
}
testWidgets('${layout.name} timecode stays on the seek and pan grid', (
tester,
) async {
final c = _controller();
addTearDown(c.dispose);
await tester.pumpWidget(
MaterialApp(
home: Align(
alignment: Alignment.topLeft,
child: SizedBox(
width: 640,
child: TimelineRuler(
controller: c,
style: TimelineRulerStyle.timecode,
timecodeLayout: layout,
showPlayhead: false,
theme: const TimelineThemeData.dark().copyWith(
rulerHeight: 28,
rulerTextStyle: const TextStyle(fontSize: 12),
),
),
),
),
),
);
_RulerCanvas paint() {
final canvas = _RulerCanvas();
tester
.widget<CustomPaint>(
find.descendant(
of: find.byType(TimelineRuler),
matching: find.byType(CustomPaint),
),
)
.painter!
.paint(canvas, const Size(640, 28));
return canvas;
}
final before = paint();
final labels = before.labels.toSet().toList();
expect(labels.length, greaterThan(1));
expect(before.rects, [
const Rect.fromLTWH(0, 0, 640, 28),
], reason: 'Labels must not erase rectangular sections of the ruler.');
expect(before.marksAfterLabel, isFalse);
for (final mark in before.marks) {
expect(mark.$1.dx / 15, closeTo((mark.$1.dx / 15).round(), 1e-6));
expect(mark.$2.dy, greaterThan(mark.$1.dy));
expect(mark.$1.dy, greaterThanOrEqualTo(0));
expect(mark.$2.dy, lessThanOrEqualTo(28));
}
for (final rect in labels) {
expect(rect.center.dy, 14);
expect(rect.left, lessThan(640));
expect(rect.right, greaterThan(0));
}
for (var i = 1; i < labels.length; i++) {
expect(labels[i].left - labels[i - 1].right, greaterThanOrEqualTo(24));
}
if (layout == TimelineRulerTimecodeLayout.overlay) {
expect(
before.labels.length,
labels.length * 2,
reason: 'Overlay uses a glyph halo followed by the text fill.',
);
expect(
before.marks.any(
(m) => labels.any((r) => r.contains((m.$1 + m.$2) / 2)),
),
isTrue,
);
} else {
final subdivisions = before.marks.where((m) => m.$2.dy - m.$1.dy <= 10);
for (final mark in subdivisions) {
if (layout == TimelineRulerTimecodeLayout.centered &&
mark.$1.dy >= 20) {
continue;
}
expect(labels.any((r) => r.inflate(4).contains(mark.$1)), isFalse);
}
}
if (layout == TimelineRulerTimecodeLayout.centered) {
// Labels remain anchored even when their text is clipped at an edge.
for (final rect in labels.where((r) => r.center.dx < 640)) {
expect(
before.marks.any(
(m) =>
(m.$1.dx - rect.center.dx).abs() < 1e-6 &&
m.$1.dy > rect.center.dy,
),
isTrue,
);
}
}
final original = c.camera;
c.setCamera(original.panBy(horizontalPixels: 37));
await tester.pump();
final after = paint();
for (final mark in after.marks) {
final localX = mark.$1.dx + 37;
expect(localX / 15, closeTo((localX / 15).round(), 1e-6));
}
expect(after.marks.length, lessThanOrEqualTo(45));
final oldTick = c.playheadTick;
await tester.tapAt(
tester.getTopLeft(find.byType(TimelineRuler)) + const Offset(300, 10),
);
await tester.pump();
expect(c.playheadTick, greaterThan(oldTick));
expect(tester.takeException(), isNull);
});
}
testWidgets('layout switches repaint without changing the viewport state', (
tester,
) async {
final c = _controller();
addTearDown(c.dispose);
Widget view(TimelineRulerTimecodeLayout layout) => Directionality(
textDirection: TextDirection.ltr,
child: TimelineViewport(
controller: c,
rulerStyle: TimelineRulerStyle.timecode,
rulerTimecodeLayout: layout,
child: const SizedBox.expand(),
),
);
CustomPainter painter() => tester
.widget<CustomPaint>(
find.descendant(
of: find.byType(TimelineRuler),
matching: find.byType(CustomPaint),
),
)
.painter!;
await tester.pumpWidget(view(TimelineRulerTimecodeLayout.inline));
await tester.pump();
c.seekTo(2 * TimelineClock.ticksPerSecond);
final camera = c.camera;
final tick = c.playheadTick;
var previous = painter();
for (final layout in TimelineRulerTimecodeLayout.values.skip(1)) {
await tester.pumpWidget(view(layout));
await tester.pump();
expect(
tester.widget<TimelineRuler>(find.byType(TimelineRuler)).timecodeLayout,
layout,
);
final next = painter();
expect(next.shouldRepaint(previous), isTrue);
expect(c.camera.baseTick, camera.baseTick);
expect(c.camera.pixelsPerTick, camera.pixelsPerTick);
expect(c.playheadTick, tick);
previous = next;
}
expect(tester.takeException(), isNull);
});
testWidgets('all layouts clip edge labels at narrow widths and large text', (
tester,
) async {
final c = _controller();
addTearDown(c.dispose);
for (final layout in TimelineRulerTimecodeLayout.values) {
for (final scale in [1.0, 2.0]) {
await tester.pumpWidget(
MediaQuery(
data: MediaQueryData(textScaler: TextScaler.linear(scale)),
child: Directionality(
textDirection: TextDirection.ltr,
child: Align(
alignment: Alignment.topLeft,
child: SizedBox(
width: 320,
child: TimelineRuler(
controller: c,
style: TimelineRulerStyle.timecode,
timecodeLayout: layout,
showPlayhead: false,
theme: const TimelineThemeData.dark().copyWith(
rulerHeight: 28,
rulerTextStyle: const TextStyle(fontSize: 12),
),
),
),
),
),
),
);
final canvas = _RulerCanvas();
final size = tester.getSize(find.byType(TimelineRuler));
tester
.widget<CustomPaint>(find.byType(CustomPaint))
.painter!
.paint(canvas, size);
expect(canvas.labels, isNotEmpty);
for (final rect in canvas.labels) {
expect(rect.left, lessThan(size.width));
expect(rect.right, greaterThan(0));
expect(rect.top, greaterThanOrEqualTo(0));
expect(rect.bottom, lessThanOrEqualTo(size.height));
}
expect(tester.takeException(), isNull);
}
}
});
testWidgets('large text increases label spacing without reducing font size', (
tester,
) async {
final c = _controller(pixelsPerSecond: 360);
addTearDown(c.dispose);
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(textScaler: TextScaler.linear(2)),
child: Directionality(
textDirection: TextDirection.ltr,
child: TimelineRuler(
controller: c,
style: TimelineRulerStyle.timecode,
showPlayhead: false,
theme: const TimelineThemeData.dark().copyWith(
rulerHeight: 40,
rulerTextStyle: const TextStyle(fontSize: 12),
),
),
),
),
);
final canvas = _RulerCanvas();
tester
.widget<CustomPaint>(find.byType(CustomPaint))
.painter!
.paint(canvas, const Size(1000, 40));
expect(canvas.labels.length, greaterThan(1));
for (var i = 1; i < canvas.labels.length; i++) {
expect(canvas.labels[i].height, greaterThanOrEqualTo(24));
expect(
canvas.labels[i].left - canvas.labels[i - 1].right,
greaterThanOrEqualTo(24),
);
}
});
}
class _RulerCanvas extends TestRecordingCanvas {
final marks = <(Offset, Offset)>[];
final labels = <Rect>[];
final rects = <Rect>[];
final clips = <Rect>[];
bool marksAfterLabel = false;
@override
void clipRect(
Rect rect, {
ui.ClipOp clipOp = ui.ClipOp.intersect,
bool doAntiAlias = true,
}) => clips.add(rect);
@override
void drawRect(Rect rect, Paint paint) => rects.add(rect);
@override
void drawLine(Offset first, Offset second, Paint paint) {
if (first.dx == second.dx) {
marks.add((first, second));
if (labels.isNotEmpty) marksAfterLabel = true;
}
}
@override
void drawParagraph(ui.Paragraph paragraph, Offset offset) {
labels.add(offset & Size(paragraph.longestLine, paragraph.height));
}
}
TimelineViewController _controller({double pixelsPerSecond = 30}) {
final kernel = TimelineKernel.importDto(
TimelineDto(
id: SequenceId('ruler-test'),
format: TimelineFormat.fps30,
primaryStorylineId: StorylineId('primary'),
storylines: [
StorylineDto(id: StorylineId('primary'), elementIds: const []),
],
elements: const [],
),
);
final projection = const TimelineLayoutSceneBuilder().project(
version: kernel.current,
stacks: const [],
appearance: (_, _, _) => throw StateError('no connections'),
);
final layout = TimelineLayoutEngine(kernel: kernel).initializeProjection(
kernel: kernel,
projection: projection,
policy: LayoutPolicy(version: 1, stacks: const []),
);
return TimelineViewController(
version: layout.version,
storyline: kernel.current.query().primaryStoryline,
geometry: TimelineViewportGeometry(primaryTop: 0, primaryHeight: 48),
format: TimelineFormat.fps30,
camera: TimelineCamera(
baseTick: 0,
pixelsPerTick: pixelsPerSecond / TimelineClock.ticksPerSecond,
),
);
}