Files
EasyDesignCollection/test/sketch_test.dart
T

533 lines
21 KiB
Dart
Raw Normal View History

// 参数化草图测试:约束求解(水平 / 竖直 / 平行 / 垂直 / 相等 / 点在线上)、
// 驱动尺寸(长度 / 半径 / 角度 / 距离)、拖拽求解、修剪、JSON 往返、
// 控制器集成与画布交互(两次点击画直线)。
import 'package:flutter/material.dart' hide Action;
import 'package:flutter_test/flutter_test.dart';
import 'package:sequence_diagram/arch/arch_models.dart';
import 'package:sequence_diagram/model/models.dart';
import 'package:sequence_diagram/sketch/sketch_models.dart';
import 'package:sequence_diagram/sketch/sketch_solver.dart';
import 'package:sequence_diagram/state/app_controller.dart';
import 'package:sequence_diagram/theme/tokens.dart';
import 'package:sequence_diagram/ui/app_shell.dart';
/// 简易构造:两点直线 + 可选约束。
SketchDoc twoPointLine() => const SketchDoc(
points: <SketchPoint>[
SketchPoint(id: 'P1', x: 0, y: 0),
SketchPoint(id: 'P2', x: 100, y: 10),
],
entities: <SketchEntity>[
SketchEntity(id: 'E1', kind: SketchKind.line, p1: 'P1', p2: 'P2'),
],
);
void main() {
group('约束求解', () {
test('水平约束:求解后两端 y 相等', () {
final SketchDoc doc = twoPointLine().copyWith(
constraints: const <SketchConstraint>[
SketchConstraint(id: 'K1', type: SketchConsType.horizontal, a: 'E1'),
],
);
final SketchSolveResult r = solveSketch(doc);
final SketchPoint a = r.doc.pointById('P1')!;
final SketchPoint b = r.doc.pointById('P2')!;
expect((a.y - b.y).abs(), lessThan(0.01));
});
test('竖直约束:求解后两端 x 相等', () {
final SketchDoc doc = twoPointLine().copyWith(
constraints: const <SketchConstraint>[
SketchConstraint(id: 'K1', type: SketchConsType.vertical, a: 'E1'),
],
);
final SketchSolveResult r = solveSketch(doc);
final SketchPoint a = r.doc.pointById('P1')!;
final SketchPoint b = r.doc.pointById('P2')!;
expect((a.x - b.x).abs(), lessThan(0.01));
});
test('长度尺寸:驱动直线长度', () {
final SketchDoc doc = twoPointLine().copyWith(
dims: const <SketchDim>[
SketchDim(id: 'D1', type: SketchDimType.length, value: 250, a: 'E1'),
],
);
final SketchSolveResult r = solveSketch(doc);
final Offset a = r.doc.pointById('P1')!.pos;
final Offset b = r.doc.pointById('P2')!.pos;
expect((b - a).distance, closeTo(250, 0.5));
});
test('半径尺寸:驱动圆半径', () {
final SketchDoc doc = const SketchDoc(
points: <SketchPoint>[SketchPoint(id: 'P1', x: 50, y: 50)],
entities: <SketchEntity>[
SketchEntity(id: 'E1', kind: SketchKind.circle, p1: 'P1', r: 20),
],
dims: <SketchDim>[
SketchDim(id: 'D1', type: SketchDimType.radius, value: 77, a: 'E1'),
],
);
final SketchSolveResult r = solveSketch(doc);
expect(r.doc.entityById('E1')!.r, closeTo(77, 0.1));
});
test('角度尺寸:两线夹角被驱动到 45°', () {
const SketchDoc doc = SketchDoc(
points: <SketchPoint>[
SketchPoint(id: 'P1', x: 0, y: 0),
SketchPoint(id: 'P2', x: 100, y: 0),
SketchPoint(id: 'P3', x: 0, y: 0),
SketchPoint(id: 'P4', x: 100, y: 30),
],
entities: <SketchEntity>[
SketchEntity(id: 'E1', kind: SketchKind.line, p1: 'P1', p2: 'P2'),
SketchEntity(id: 'E2', kind: SketchKind.line, p1: 'P3', p2: 'P4'),
],
dims: <SketchDim>[
SketchDim(id: 'D1', type: SketchDimType.angle, value: 45, a: 'E1', b: 'E2'),
],
);
final SketchSolveResult r = solveSketch(doc);
final double? ang =
sketchMeasure(r.doc, SketchDimType.angle, 'E1', 'E2');
expect(ang, isNotNull);
expect(ang!, closeTo(45, 1.0));
});
test('平行 + 相等:求解后两线平行且等长', () {
const SketchDoc doc = SketchDoc(
points: <SketchPoint>[
SketchPoint(id: 'P1', x: 0, y: 0),
SketchPoint(id: 'P2', x: 100, y: 0),
SketchPoint(id: 'P3', x: 0, y: 50),
SketchPoint(id: 'P4', x: 60, y: 90),
],
entities: <SketchEntity>[
SketchEntity(id: 'E1', kind: SketchKind.line, p1: 'P1', p2: 'P2'),
SketchEntity(id: 'E2', kind: SketchKind.line, p1: 'P3', p2: 'P4'),
],
constraints: <SketchConstraint>[
SketchConstraint(id: 'K1', type: SketchConsType.parallel, a: 'E1', b: 'E2'),
SketchConstraint(id: 'K2', type: SketchConsType.equal, a: 'E1', b: 'E2'),
],
);
final SketchSolveResult r = solveSketch(doc);
final SketchEntity e1 = r.doc.entityById('E1')!;
final SketchEntity e2 = r.doc.entityById('E2')!;
final Offset d1 = r.doc.pointById(e1.p2)!.pos - r.doc.pointById(e1.p1)!.pos;
final Offset d2 = r.doc.pointById(e2.p2)!.pos - r.doc.pointById(e2.p1)!.pos;
final double cross = (d1.dx * d2.dy - d1.dy * d2.dx).abs() /
(d1.distance * d2.distance);
expect(cross, lessThan(0.01));
expect((d1.distance - d2.distance).abs(), lessThan(1.0));
});
test('点在线上:点被拉到直线附近', () {
const SketchDoc doc = SketchDoc(
points: <SketchPoint>[
SketchPoint(id: 'P1', x: 0, y: 0),
SketchPoint(id: 'P2', x: 100, y: 0),
SketchPoint(id: 'P3', x: 40, y: 25),
],
entities: <SketchEntity>[
SketchEntity(id: 'E1', kind: SketchKind.line, p1: 'P1', p2: 'P2'),
],
constraints: <SketchConstraint>[
SketchConstraint(
id: 'K1',
type: SketchConsType.pointOnEntity,
a: 'P3',
b: 'E1'),
],
);
final SketchSolveResult r = solveSketch(doc);
final Offset p = r.doc.pointById('P3')!.pos;
final Offset a = r.doc.pointById('P1')!.pos;
final Offset b = r.doc.pointById('P2')!.pos;
final double dist = (p - a).dx * (b - a).dy - (p - a).dy * (b - a).dx;
expect((dist / (b - a).distance).abs(), lessThan(0.1));
});
});
group('拖拽求解(矩形保持直角)', () {
test('拖一角:水平 / 竖直约束保持', () {
const SketchDoc doc = SketchDoc(
points: <SketchPoint>[
SketchPoint(id: 'A', x: 0, y: 0),
SketchPoint(id: 'B', x: 100, y: 0),
SketchPoint(id: 'C', x: 100, y: 60),
SketchPoint(id: 'D', x: 0, y: 60),
],
entities: <SketchEntity>[
SketchEntity(id: 'E1', kind: SketchKind.line, p1: 'A', p2: 'B'),
SketchEntity(id: 'E2', kind: SketchKind.line, p1: 'B', p2: 'C'),
SketchEntity(id: 'E3', kind: SketchKind.line, p1: 'C', p2: 'D'),
SketchEntity(id: 'E4', kind: SketchKind.line, p1: 'D', p2: 'A'),
],
constraints: <SketchConstraint>[
SketchConstraint(id: 'K1', type: SketchConsType.horizontal, a: 'E1'),
SketchConstraint(id: 'K2', type: SketchConsType.vertical, a: 'E2'),
SketchConstraint(id: 'K3', type: SketchConsType.horizontal, a: 'E3'),
SketchConstraint(id: 'K4', type: SketchConsType.vertical, a: 'E4'),
],
);
final SketchSolveResult r = solveSketch(
doc,
dragPoints: <String, Offset>{'B': const Offset(150, -20)},
);
final SketchPoint a = r.doc.pointById('A')!;
final SketchPoint b = r.doc.pointById('B')!;
final SketchPoint c = r.doc.pointById('C')!;
final SketchPoint d = r.doc.pointById('D')!;
expect((a.y - b.y).abs(), lessThan(0.5));
expect((b.x - c.x).abs(), lessThan(0.5));
expect((c.y - d.y).abs(), lessThan(0.5));
expect((d.x - a.x).abs(), lessThan(0.5));
expect((b.pos - const Offset(150, -20)).distance, lessThan(6));
});
test('拖线段中点:图标整体平移(两热点同移)', () {
final SketchDoc doc = twoPointLine();
final SketchSolveResult r = solveSketch(
doc,
dragPoints: <String, Offset>{
'P1': const Offset(20, 5),
'P2': const Offset(120, 15),
},
);
final Offset p1 = r.doc.pointById('P1')!.pos;
final Offset p2 = r.doc.pointById('P2')!.pos;
expect((p1 - const Offset(20, 5)).distance, lessThan(3));
expect((p2 - const Offset(120, 15)).distance, lessThan(3));
});
});
group('修剪', () {
test('一条直线被另一条切分:点击段被删除,剩余保留', () {
const SketchDoc doc = SketchDoc(
points: <SketchPoint>[
SketchPoint(id: 'P1', x: 0, y: 0),
SketchPoint(id: 'P2', x: 100, y: 0),
SketchPoint(id: 'P3', x: 50, y: -50),
SketchPoint(id: 'P4', x: 50, y: 50),
],
entities: <SketchEntity>[
SketchEntity(id: 'E1', kind: SketchKind.line, p1: 'P1', p2: 'P2'),
SketchEntity(id: 'E2', kind: SketchKind.line, p1: 'P3', p2: 'P4'),
],
);
// 点击 E1 右侧段(x=70)。
final SketchDoc? out = sketchTrimLine(doc, 'E1', const Offset(70, 0));
expect(out, isNotNull);
// 原 E1 消失,剩余一段(0..50)。
expect(out!.entityById('E1'), isNull);
final List<SketchEntity> lines = out.entities
.where((SketchEntity e) => e.kind == SketchKind.line)
.toList();
expect(lines.length, 2); // 剩余段 + 切线 E2
final SketchEntity piece =
lines.firstWhere((SketchEntity e) => e.id != 'E2');
final Offset a = out.pointById(piece.p1)!.pos;
final Offset b = out.pointById(piece.p2)!.pos;
final double near100 = ((a - const Offset(100, 0)).distance +
(b - const Offset(100, 0)).distance);
final double near0 = ((a - Offset.zero).distance + (b - Offset.zero).distance);
// 保留的是近原点的一侧。
expect(near0, lessThan(near100));
});
test('两条切线之间:点击中段被彻底切除(一分为二)', () {
const SketchDoc doc = SketchDoc(
points: <SketchPoint>[
SketchPoint(id: 'P1', x: 0, y: 0),
SketchPoint(id: 'P2', x: 100, y: 0),
SketchPoint(id: 'P3', x: 30, y: -20),
SketchPoint(id: 'P4', x: 30, y: 20),
SketchPoint(id: 'P5', x: 70, y: -20),
SketchPoint(id: 'P6', x: 70, y: 20),
],
entities: <SketchEntity>[
SketchEntity(id: 'E1', kind: SketchKind.line, p1: 'P1', p2: 'P2'),
SketchEntity(id: 'E2', kind: SketchKind.line, p1: 'P3', p2: 'P4'),
SketchEntity(id: 'E3', kind: SketchKind.line, p1: 'P5', p2: 'P6'),
],
);
final SketchDoc? out = sketchTrimLine(doc, 'E1', const Offset(50, 0));
expect(out, isNotNull);
final List<SketchEntity> lines = out!.entities
.where((SketchEntity e) => e.kind == SketchKind.line)
.toList();
expect(lines.length, 4); // 两段残余 + 两条切线
expect(out.entityById('E1'), isNull);
});
test('无交点:整条删除', () {
final SketchDoc doc = twoPointLine();
final SketchDoc? out = sketchTrimLine(doc, 'E1', const Offset(50, 5));
expect(out, isNotNull);
expect(out!.entities, isEmpty);
});
});
group('捕捉与自动约束', () {
test('捕捉已有点:返回该点 id', () {
final SketchDoc doc = twoPointLine();
final SketchSnap snap = sketchSnap(
doc,
const Offset(103, 12),
gridSnap: false,
grid: 20,
);
expect(snap.pointId, 'P2');
});
test('近轴自动约束:水平 / 竖直识别', () {
expect(sketchAutoAxis(Offset.zero, const Offset(100, 1)),
SketchConsType.horizontal);
expect(sketchAutoAxis(Offset.zero, const Offset(1, 100)),
SketchConsType.vertical);
expect(sketchAutoAxis(Offset.zero, const Offset(100, 40)), isNull);
});
test('约束 / 尺寸可用性判断', () {
final SketchDoc doc = twoPointLine();
expect(sketchCanConstrain(doc, SketchConsType.horizontal, <String>['E1']),
isTrue);
expect(
sketchCanConstrain(doc, SketchConsType.parallel, <String>['E1']),
isFalse);
expect(sketchCanDim(doc, SketchDimType.length, <String>['E1']), isTrue);
expect(sketchCanDim(doc, SketchDimType.radius, <String>['E1']), isFalse);
});
});
group('序列化', () {
test('SketchDoc JSON 往返(含约束与尺寸)', () {
final SketchDoc doc = twoPointLine().copyWith(
constraints: const <SketchConstraint>[
SketchConstraint(id: 'K1', type: SketchConsType.horizontal, a: 'E1'),
],
dims: const <SketchDim>[
SketchDim(id: 'D1', type: SketchDimType.length, value: 120, a: 'E1'),
],
);
final SketchDoc back = SketchDoc.decode(doc.encode());
expect(back.points.length, 2);
expect(back.entities.length, 1);
expect(back.constraints.single.type, SketchConsType.horizontal);
expect(back.dims.single.value, 120);
});
test('ArchDoc 往返包含草图', () {
final ArchDoc doc = ArchDoc.empty.copyWith(sketch: twoPointLine());
final ArchDoc back = ArchDoc.decode(doc.encode());
expect(back.sketch.entities.length, 1);
expect(back.sketch.points.length, 2);
});
});
group('自由度', () {
test('宽松草图:剩余自由度为正;全约束:归零附近', () {
final SketchDoc loose = twoPointLine();
expect(sketchDofCount(loose), 4); // 4 变量
final SketchDoc fixed = twoPointLine().copyWith(
points: const <SketchPoint>[
SketchPoint(id: 'P1', x: 0, y: 0, fixed: true),
SketchPoint(id: 'P2', x: 100, y: 10, fixed: true),
],
);
expect(sketchDofCount(fixed), 0);
});
});
group('控制器集成', () {
test('添加尺寸驱动几何;拖动不破坏尺寸;撤销可用', () {
final AppController c = AppController(project: Project.demo());
addTearDown(c.dispose);
c.setSketchMode(true);
c.changeSketch((_) => twoPointLine());
c.selectSketchIds(<String>['E1']);
c.addSketchDim(SketchDimType.length, 300);
final SketchEntity e = c.sketch.entityById('E1')!;
final double len = (c.sketch.pointById(e.p2)!.pos -
c.sketch.pointById(e.p1)!.pos)
.distance;
expect(len, closeTo(300, 1));
// 拖动 P2:尺寸约束优先,长度保持。
c.beginSketchUndo();
c.dragSketch(
points: <String, Offset>{'P2': const Offset(600, 500)});
final double len2 = (c.sketch.pointById(e.p2)!.pos -
c.sketch.pointById(e.p1)!.pos)
.distance;
expect(len2, closeTo(300, 2));
// 撤销回到拖拽前(尺寸仍在)。
c.undo();
expect(c.sketch.dimById('D1'), isNotNull);
// 架构文档 JSON 带草图。
final ArchDoc back = ArchDoc.decode(c.arch.encode());
expect(back.sketch.dims.length, 1);
});
test('约束入口:选择不满足时给出提示且不改动', () {
final AppController c = AppController(project: Project.demo());
addTearDown(c.dispose);
c.changeSketch((_) => twoPointLine());
c.selectSketchIds(<String>['E1']);
final int before = c.sketch.constraints.length;
c.addSketchConstraint(SketchConsType.perpendicular);
expect(c.sketch.constraints.length, before);
expect(c.status, contains('垂直'));
});
});
group('约束 / 尺寸命令流(Creo 风格)', () {
AppController makeCmdController() {
final AppController c = AppController(project: Project.demo());
addTearDown(c.dispose);
c.setSketchMode(true);
c.changeSketch(
(_) => const SketchDoc(
points: <SketchPoint>[
SketchPoint(id: 'P1', x: 0, y: 0),
SketchPoint(id: 'P2', x: 100, y: 10),
SketchPoint(id: 'P3', x: 0, y: 100),
SketchPoint(id: 'P4', x: 100, y: 90),
],
entities: <SketchEntity>[
SketchEntity(id: 'E1', kind: SketchKind.line, p1: 'P1', p2: 'P2'),
SketchEntity(id: 'E2', kind: SketchKind.line, p1: 'P3', p2: 'P4'),
],
),
);
return c;
}
test('水平命令:点命令 → 点直线立即执行;保持激活可连续执行', () {
final AppController c = makeCmdController();
c.beginSketchConstraint(SketchConsType.horizontal);
expect(c.sketchPendingCons, SketchConsType.horizontal);
expect(c.status, contains('点击一条直线'));
c.sketchCommandPick(const SketchHit(entityId: 'E1'));
expect(
c.sketch.constraints.any((SketchConstraint k) =>
k.type == SketchConsType.horizontal && k.a == 'E1'),
isTrue,
);
// 命令保持激活(Creo 风格连续使用)。
expect(c.sketchPendingCons, SketchConsType.horizontal);
c.sketchCommandPick(const SketchHit(entityId: 'E2'));
expect(
c.sketch.constraints
.where((SketchConstraint k) => k.type == SketchConsType.horizontal)
.length,
2,
);
// 求解后 E1 两端 y 相等。
final SketchPoint a = c.sketch.pointById('P1')!;
final SketchPoint b = c.sketch.pointById('P2')!;
expect((a.y - b.y).abs(), lessThan(0.01));
});
test('平行命令:两次点选执行;点错对象给出提示且不执行', () {
final AppController c = makeCmdController();
c.beginSketchConstraint(SketchConsType.parallel);
c.sketchCommandPick(const SketchHit(entityId: 'E1'));
expect(c.sketchPendingIds, <String>['E1']);
c.sketchCommandPick(const SketchHit(entityId: 'E2'));
expect(
c.sketch.constraints
.any((SketchConstraint k) => k.type == SketchConsType.parallel),
isTrue,
);
expect(c.sketchPendingIds, isEmpty);
// 点一个点对象 → 提示且不产生约束。
c.sketchCommandPick(const SketchHit(pointId: 'P1'));
expect(c.status, contains('直线'));
});
test('长度尺寸命令:自动标注当前值并标记待编辑;改值精确驱动', () {
final AppController c = makeCmdController();
c.beginSketchDim(SketchDimType.length);
expect(c.status, contains('点击一条直线'));
c.sketchCommandPick(const SketchHit(entityId: 'E1'));
final SketchDim d = c.sketch.dims.single;
expect(d.type, SketchDimType.length);
expect(d.value, closeTo(100.5, 0.2)); // 当前测量值
expect(c.sketchDimToEdit, d.id); // 待弹数值编辑框
c.clearSketchDimToEdit();
expect(c.sketchDimToEdit, isNull);
// 改值精确驱动。
c.setSketchDimValue(d.id, 250);
final SketchEntity e = c.sketch.entityById('E1')!;
final double len = (c.sketch.pointById(e.p2)!.pos -
c.sketch.pointById(e.p1)!.pos)
.distance;
expect(len, closeTo(250, 0.5));
});
test('距离尺寸命令:两次点选 → 生成距离标注', () {
final AppController c = makeCmdController();
c.beginSketchDim(SketchDimType.distance);
c.sketchCommandPick(const SketchHit(pointId: 'P1'));
c.sketchCommandPick(const SketchHit(pointId: 'P2'));
final SketchDim d = c.sketch.dims.single;
expect(d.type, SketchDimType.distance);
expect(d.value, closeTo(100.5, 0.2));
});
test('取消 / 切换命令:状态正确清理', () {
final AppController c = makeCmdController();
c.beginSketchConstraint(SketchConsType.perpendicular);
c.sketchCommandPick(const SketchHit(entityId: 'E1'));
expect(c.sketchPendingIds, <String>['E1']);
// 切换到尺寸命令 → 约束命令与拾取被清理。
c.beginSketchDim(SketchDimType.length);
expect(c.sketchPendingCons, isNull);
expect(c.sketchPendingIds, isEmpty);
expect(c.sketchPendingDim, SketchDimType.length);
// 取消命令。
c.cancelSketchCommand();
expect(c.sketchPendingDim, isNull);
expect(c.sketchPendingCons, isNull);
// 再点同一命令 = 取消。
c.beginSketchConstraint(SketchConsType.horizontal);
c.beginSketchConstraint(SketchConsType.horizontal);
expect(c.sketchPendingCons, isNull);
});
});
testWidgets('草图画布:直线工具两次点击 → 生成实体 + 自动水平约束',
(WidgetTester tester) async {
tester.view.physicalSize = const Size(1280, 800);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
final AppController c = AppController(project: Project.demo());
addTearDown(c.dispose);
await tester.pumpWidget(MaterialApp(
theme: buildTheme(Tokens.light),
home: AppShell(controller: c),
));
await tester.pump();
c.setAppMode(AppMode.architecture);
await tester.pump();
c.setSketchMode(true);
c.setSketchTool(SketchTool.line);
await tester.pump();
await tester.tapAt(const Offset(500, 300));
await tester.pump();
await tester.tapAt(const Offset(820, 302));
await tester.pump();
expect(c.sketch.entities.length, 1);
expect(c.sketch.points.length, 2);
expect(
c.sketch.constraints.any(
(SketchConstraint k) => k.type == SketchConsType.horizontal),
isTrue,
);
});
}