2026-10-09 21:27:40 +08:00
|
|
|
// 依赖走线测试:自动错开、手动偏移、命中测试与 JSON 往返。
|
2026-11-22 10:02:48 +08:00
|
|
|
import 'dart:ui' show Size, Offset, Rect;
|
2026-10-09 21:27:40 +08:00
|
|
|
|
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
2026-11-22 10:02:48 +08:00
|
|
|
import 'package:sequence_diagram/modules/sequence/layout/dep_routes.dart';
|
|
|
|
|
import 'package:sequence_diagram/modules/sequence/layout/layout.dart';
|
2026-10-09 21:27:40 +08:00
|
|
|
import 'package:sequence_diagram/model/models.dart';
|
|
|
|
|
|
|
|
|
|
/// 构造测试工程:A1/A2/A3 同属一条泳道(L0),A3 依赖 A1 与 A2,
|
|
|
|
|
/// 用于验证“同组依赖自动错开 + 手动偏移”。
|
|
|
|
|
Project _project({double offset = 0}) {
|
|
|
|
|
return Project(
|
|
|
|
|
name: 'test',
|
|
|
|
|
cycle: const Cycle(durationMs: 2400),
|
|
|
|
|
stations: const <Station>[
|
|
|
|
|
Station(id: 'ST1', name: '工位 1', order: 0, color: 0xFF4CC9F0),
|
|
|
|
|
],
|
|
|
|
|
actions: <Action>[
|
|
|
|
|
const Action(
|
|
|
|
|
id: 'A1',
|
|
|
|
|
name: 'A1',
|
|
|
|
|
stationId: 'ST1',
|
|
|
|
|
driver: 'x',
|
|
|
|
|
startAngle: 0,
|
|
|
|
|
durationAngle: 30,
|
|
|
|
|
layer: 0,
|
|
|
|
|
color: 0xFF4CC9F0),
|
2026-11-22 10:02:48 +08:00
|
|
|
Action(
|
2026-10-09 21:27:40 +08:00
|
|
|
id: 'A2',
|
|
|
|
|
name: 'A2',
|
|
|
|
|
stationId: 'ST1',
|
|
|
|
|
driver: 'x',
|
|
|
|
|
startAngle: 40,
|
|
|
|
|
durationAngle: 30,
|
|
|
|
|
layer: 0,
|
|
|
|
|
color: 0xFF7BD88F),
|
|
|
|
|
Action(
|
|
|
|
|
id: 'A3',
|
|
|
|
|
name: 'A3',
|
|
|
|
|
stationId: 'ST1',
|
|
|
|
|
driver: 'x',
|
|
|
|
|
startAngle: 80,
|
|
|
|
|
durationAngle: 30,
|
|
|
|
|
layer: 0,
|
|
|
|
|
color: 0xFFFFD166,
|
|
|
|
|
deps: <Dependency>[
|
|
|
|
|
Dependency(fromId: 'A1', toId: 'A3', routeOffset: offset),
|
|
|
|
|
const Dependency(fromId: 'A2', toId: 'A3'),
|
|
|
|
|
]),
|
|
|
|
|
],
|
|
|
|
|
units: const UnitSettings(),
|
|
|
|
|
viewState: const ViewState(mode: ViewMode.bar),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
const Size size = Size(1280, 800);
|
|
|
|
|
|
|
|
|
|
test('条形:同组依赖自动错开,手动偏移叠加', () {
|
|
|
|
|
final Project p = _project(offset: 20);
|
|
|
|
|
final BarLayout layout = BarLayout(p, size);
|
|
|
|
|
final List<BarDepRoute> routes = buildBarDepRoutes(p, layout);
|
|
|
|
|
expect(routes.length, 2);
|
|
|
|
|
final BarDepRoute r1 = routes.firstWhere((BarDepRoute r) => r.key == 'A1->A3');
|
|
|
|
|
final BarDepRoute r2 = routes.firstWhere((BarDepRoute r) => r.key == 'A2->A3');
|
|
|
|
|
// A1→A3 手动偏移 +20;A2→A3 自动错开 −6:两条线纵坐标相差 26。
|
|
|
|
|
final double y1 = r1.points.first.dy;
|
|
|
|
|
final double y2 = r2.points.first.dy;
|
|
|
|
|
expect(y1 - y2, closeTo(26, 0.001));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('条形:命中测试选中最近的连线', () {
|
|
|
|
|
final Project p = _project();
|
|
|
|
|
final List<BarDepRoute> routes = buildBarDepRoutes(p, BarLayout(p, size));
|
|
|
|
|
final BarDepRoute r1 = routes.firstWhere((BarDepRoute r) => r.key == 'A1->A3');
|
|
|
|
|
final Offset mid = Offset(
|
|
|
|
|
(r1.points.first.dx + r1.points.last.dx) / 2,
|
|
|
|
|
r1.points.first.dy,
|
|
|
|
|
);
|
|
|
|
|
expect(hitBarDep(routes, mid, 7)?.key, 'A1->A3');
|
|
|
|
|
expect(hitBarDep(routes, mid + const Offset(0, 40), 7), isNull);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('依赖 routeOffset JSON 往返', () {
|
|
|
|
|
const Dependency d =
|
|
|
|
|
Dependency(fromId: 'A1', toId: 'A2', offsetAngle: 10, routeOffset: 24);
|
|
|
|
|
final Dependency back = Dependency.fromJson(d.toJson());
|
|
|
|
|
expect(back.routeOffset, 24);
|
|
|
|
|
expect(back.offsetAngle, 10);
|
|
|
|
|
// 旧文件没有 routeOffset 字段时回退为 0。
|
|
|
|
|
final Dependency legacy = Dependency.fromJson(<String, dynamic>{
|
|
|
|
|
'fromId': 'A1',
|
|
|
|
|
'toId': 'A2',
|
|
|
|
|
'type': 'FS',
|
|
|
|
|
'offsetAngle': 5,
|
|
|
|
|
});
|
|
|
|
|
expect(legacy.routeOffset, 0);
|
|
|
|
|
});
|
2026-11-22 10:02:48 +08:00
|
|
|
|
|
|
|
|
test('条形:默认从端点中心引出;端部被遮挡时退到上下角', () {
|
|
|
|
|
Project make({required bool blocker}) => Project(
|
|
|
|
|
name: 'anchor',
|
|
|
|
|
cycle: const Cycle(durationMs: 2400),
|
|
|
|
|
stations: const <Station>[
|
|
|
|
|
Station(id: 'ST1', name: 'S', order: 0, color: 0xFF4CC9F0),
|
|
|
|
|
],
|
|
|
|
|
actions: <Action>[
|
|
|
|
|
const Action(
|
|
|
|
|
id: 'A1',
|
|
|
|
|
name: 'A1',
|
|
|
|
|
stationId: 'ST1',
|
|
|
|
|
driver: '',
|
|
|
|
|
startAngle: 0,
|
|
|
|
|
durationAngle: 30,
|
|
|
|
|
layer: 0,
|
|
|
|
|
color: 0xFF4CC9F0),
|
|
|
|
|
if (blocker)
|
|
|
|
|
const Action(
|
|
|
|
|
id: 'A2',
|
|
|
|
|
name: 'A2',
|
|
|
|
|
stationId: 'ST1',
|
|
|
|
|
driver: '',
|
|
|
|
|
startAngle: 33,
|
|
|
|
|
durationAngle: 30,
|
|
|
|
|
layer: 0,
|
|
|
|
|
color: 0xFF7BD88F),
|
|
|
|
|
const Action(
|
|
|
|
|
id: 'A3',
|
|
|
|
|
name: 'A3',
|
|
|
|
|
stationId: 'ST1',
|
|
|
|
|
driver: '',
|
|
|
|
|
startAngle: 80,
|
|
|
|
|
durationAngle: 30,
|
|
|
|
|
layer: 0,
|
|
|
|
|
color: 0xFFFFD166,
|
|
|
|
|
deps: <Dependency>[Dependency(fromId: 'A1', toId: 'A3')]),
|
|
|
|
|
],
|
|
|
|
|
units: const UnitSettings(),
|
|
|
|
|
viewState: const ViewState(mode: ViewMode.bar),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Project p = make(blocker: false);
|
|
|
|
|
BarLayout layout = BarLayout(p, size);
|
|
|
|
|
BarDepRoute r = buildBarDepRoutes(p, layout).single;
|
|
|
|
|
final Rect ra = layout.rectFor(p.actionById('A1')!);
|
|
|
|
|
final Rect rb = layout.rectFor(p.actionById('A3')!);
|
|
|
|
|
expect(r.points.first.dx, closeTo(ra.right + 2, 0.001));
|
|
|
|
|
expect(r.points.first.dy, closeTo(ra.center.dy, 0.001));
|
|
|
|
|
expect(r.points.last.dx, closeTo(rb.left - 2, 0.001));
|
|
|
|
|
expect(r.points.last.dy, closeTo(rb.center.dy, 0.001));
|
|
|
|
|
|
|
|
|
|
// A2 紧贴 A1 右端:源端应退到下角,目标端仍从中心进入。
|
|
|
|
|
p = make(blocker: true);
|
|
|
|
|
layout = BarLayout(p, size);
|
|
|
|
|
r = buildBarDepRoutes(p, layout).single;
|
|
|
|
|
final Rect ra2 = layout.rectFor(p.actionById('A1')!);
|
|
|
|
|
final Rect rb2 = layout.rectFor(p.actionById('A3')!);
|
|
|
|
|
expect(r.points.first.dy, closeTo(ra2.bottom - 2, 0.001));
|
|
|
|
|
expect(r.points.length, greaterThan(2));
|
|
|
|
|
expect(r.points.last.dy, closeTo(rb2.center.dy, 0.001));
|
|
|
|
|
expect(r.points.last.dx, closeTo(rb2.left - 2, 0.001));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('条形:前后贴紧时隐藏连线,标签位于接缝处', () {
|
|
|
|
|
final Project q = Project(
|
|
|
|
|
name: 'tight',
|
|
|
|
|
cycle: const Cycle(durationMs: 2400),
|
|
|
|
|
stations: const <Station>[
|
|
|
|
|
Station(id: 'ST1', name: 'S', order: 0, color: 0xFF4CC9F0),
|
|
|
|
|
],
|
|
|
|
|
actions: const <Action>[
|
|
|
|
|
Action(
|
|
|
|
|
id: 'A1',
|
|
|
|
|
name: 'A1',
|
|
|
|
|
stationId: 'ST1',
|
|
|
|
|
driver: '',
|
|
|
|
|
startAngle: 0,
|
|
|
|
|
durationAngle: 70,
|
|
|
|
|
layer: 0,
|
|
|
|
|
color: 0xFF4CC9F0),
|
|
|
|
|
Action(
|
|
|
|
|
id: 'A2',
|
|
|
|
|
name: 'A2',
|
|
|
|
|
stationId: 'ST1',
|
|
|
|
|
driver: '',
|
|
|
|
|
startAngle: 70,
|
|
|
|
|
durationAngle: 60,
|
|
|
|
|
layer: 0,
|
|
|
|
|
color: 0xFF7BD88F,
|
|
|
|
|
deps: <Dependency>[Dependency(fromId: 'A1', toId: 'A2')]),
|
|
|
|
|
],
|
|
|
|
|
units: const UnitSettings(),
|
|
|
|
|
viewState: const ViewState(mode: ViewMode.bar),
|
|
|
|
|
);
|
|
|
|
|
final BarLayout layout = BarLayout(q, size);
|
|
|
|
|
final BarDepRoute r = buildBarDepRoutes(q, layout).single;
|
|
|
|
|
final Rect ra = layout.rectFor(q.actionById('A1')!);
|
|
|
|
|
final Rect rb = layout.rectFor(q.actionById('A2')!);
|
|
|
|
|
expect(r.junctionOnly, isTrue);
|
|
|
|
|
expect(r.points, isEmpty);
|
|
|
|
|
expect(r.chipCenter.dx, closeTo((ra.right + rb.left) / 2, 0.001));
|
|
|
|
|
expect(r.chipCenter.dy, lessThan(ra.top));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('圆周:同环依赖的绕行与标签位于环外空隙', () {
|
|
|
|
|
final Project p = Project(
|
|
|
|
|
name: 'circ',
|
|
|
|
|
cycle: const Cycle(durationMs: 2400),
|
|
|
|
|
stations: const <Station>[
|
|
|
|
|
Station(id: 'ST1', name: 'S', order: 0, color: 0xFF4CC9F0),
|
|
|
|
|
],
|
|
|
|
|
actions: const <Action>[
|
|
|
|
|
Action(
|
|
|
|
|
id: 'A1',
|
|
|
|
|
name: 'A1',
|
|
|
|
|
stationId: 'ST1',
|
|
|
|
|
driver: '',
|
|
|
|
|
startAngle: 0,
|
|
|
|
|
durationAngle: 70,
|
|
|
|
|
layer: 0,
|
|
|
|
|
color: 0xFF4CC9F0),
|
|
|
|
|
Action(
|
|
|
|
|
id: 'A2',
|
|
|
|
|
name: 'A2',
|
|
|
|
|
stationId: 'ST1',
|
|
|
|
|
driver: '',
|
|
|
|
|
startAngle: 70,
|
|
|
|
|
durationAngle: 60,
|
|
|
|
|
layer: 0,
|
|
|
|
|
color: 0xFF7BD88F,
|
|
|
|
|
deps: <Dependency>[Dependency(fromId: 'A1', toId: 'A2')]),
|
|
|
|
|
],
|
|
|
|
|
units: const UnitSettings(),
|
|
|
|
|
viewState: const ViewState(mode: ViewMode.circular),
|
|
|
|
|
);
|
|
|
|
|
final CircularLayout layout = CircularLayout(p, size);
|
|
|
|
|
final ArcDepRoute r = buildArcDepRoutes(p, layout).single;
|
|
|
|
|
expect(r.junctionOnly, isTrue);
|
|
|
|
|
expect(r.rC, greaterThan(r.rB));
|
|
|
|
|
expect(r.chipR, greaterThan(r.rB));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('条形:FS / FF 取后端,SS / SF 取前端', () {
|
|
|
|
|
final Project p = Project(
|
|
|
|
|
name: 'semantics',
|
|
|
|
|
cycle: const Cycle(durationMs: 2400),
|
|
|
|
|
stations: const <Station>[
|
|
|
|
|
Station(id: 'ST1', name: 'S', order: 0, color: 0xFF4CC9F0),
|
|
|
|
|
],
|
|
|
|
|
actions: <Action>[
|
|
|
|
|
const Action(
|
|
|
|
|
id: 'A1',
|
|
|
|
|
name: 'A1',
|
|
|
|
|
stationId: 'ST1',
|
|
|
|
|
driver: '',
|
|
|
|
|
startAngle: 0,
|
|
|
|
|
durationAngle: 30,
|
|
|
|
|
layer: 0,
|
|
|
|
|
color: 0xFF4CC9F0),
|
|
|
|
|
const Action(
|
|
|
|
|
id: 'A2',
|
|
|
|
|
name: 'A2',
|
|
|
|
|
stationId: 'ST1',
|
|
|
|
|
driver: '',
|
|
|
|
|
startAngle: 60,
|
|
|
|
|
durationAngle: 30,
|
|
|
|
|
layer: 1,
|
|
|
|
|
color: 0xFF7BD88F,
|
|
|
|
|
deps: <Dependency>[
|
|
|
|
|
Dependency(fromId: 'A1', toId: 'A2', type: DepType.ff)
|
|
|
|
|
]),
|
|
|
|
|
const Action(
|
|
|
|
|
id: 'A3',
|
|
|
|
|
name: 'A3',
|
|
|
|
|
stationId: 'ST1',
|
|
|
|
|
driver: '',
|
|
|
|
|
startAngle: 100,
|
|
|
|
|
durationAngle: 30,
|
|
|
|
|
layer: 2,
|
|
|
|
|
color: 0xFFFFD166,
|
|
|
|
|
deps: <Dependency>[
|
|
|
|
|
Dependency(fromId: 'A2', toId: 'A3', type: DepType.ss)
|
|
|
|
|
]),
|
|
|
|
|
],
|
|
|
|
|
units: const UnitSettings(),
|
|
|
|
|
viewState: const ViewState(mode: ViewMode.bar),
|
|
|
|
|
);
|
|
|
|
|
final BarLayout layout = BarLayout(p, size);
|
|
|
|
|
final List<BarDepRoute> routes = buildBarDepRoutes(p, layout);
|
|
|
|
|
final Rect r1 = layout.rectFor(p.actionById('A1')!);
|
|
|
|
|
final Rect r2 = layout.rectFor(p.actionById('A2')!);
|
|
|
|
|
final Rect r3 = layout.rectFor(p.actionById('A3')!);
|
|
|
|
|
|
|
|
|
|
// FF:源后端(右端)→ 目标后端(右端)。
|
|
|
|
|
final BarDepRoute ff =
|
|
|
|
|
routes.firstWhere((BarDepRoute r) => r.key == 'A1->A2');
|
|
|
|
|
expect(ff.points.first.dx, greaterThan(r1.right));
|
|
|
|
|
expect(ff.points.last.dx, greaterThan(r2.right));
|
|
|
|
|
expect(ff.arrowDir.dx, lessThan(0));
|
|
|
|
|
|
|
|
|
|
// SS:源前端(左端)→ 目标前端(左端)。
|
|
|
|
|
final BarDepRoute ss =
|
|
|
|
|
routes.firstWhere((BarDepRoute r) => r.key == 'A2->A3');
|
|
|
|
|
expect(ss.points.first.dx, lessThan(r2.left));
|
|
|
|
|
expect(ss.points.last.dx, lessThan(r3.left));
|
|
|
|
|
expect(ss.arrowDir.dx, greaterThan(0));
|
|
|
|
|
});
|
2026-10-09 21:27:40 +08:00
|
|
|
}
|