Files
EasyDesignCollection/test/arch_screenshots_test.dart
T

336 lines
12 KiB
Dart
Raw Normal View History

// 架构图模块演示截图生成:整机外壳(1280×800 平板基准)+ 识别对照表。
// 运行方式:flutter test --update-goldens test\arch_screenshots_test.dart
// 输出:demo/arch-*.png
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:sequence_diagram/arch/arch_models.dart';
import 'package:sequence_diagram/arch/arch_painter.dart';
import 'package:sequence_diagram/arch/arch_recognizer.dart';
import 'package:sequence_diagram/arch/arch_samples.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';
import 'helpers.dart';
void main() {
testWidgets('generate architecture demo screenshots', (WidgetTester tester) async {
await tester.runAsync(() => loadZhFonts());
await tester.runAsync(() => loadMaterialIcons());
tester.view.physicalSize = const Size(1280, 800);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
final AppController c = AppController(project: demoTiming());
addTearDown(c.dispose);
Widget shell(Tokens t) => RepaintBoundary(
key: const ValueKey<String>('shot'),
child: MaterialApp(
theme: buildTheme(t),
debugShowCheckedModeBanner: false,
home: AppShell(controller: c),
),
);
Future<void> shot(String path) async {
await tester.pump();
await expectLater(
find.byKey(const ValueKey<String>('shot')), matchesGoldenFile(path));
}
await tester.pumpWidget(shell(Tokens.light));
await tester.pump();
// 0) 首页(空白启动):新建文档网格 + 未开放模块灰显。
c.goHome();
await tester.pump();
// 等最近打开列表真实读盘(flutter_test 需要 runAsync 才推进真实 IO)。
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 300)));
await tester.pump();
await shot('../demo/home-light.png');
// 切换到架构图模块(左上角「设计合集」菜单的动作)。
c.setAppMode(AppMode.architecture);
await tester.pump();
// 展开左右侧栏(浮层)并钉住,展示停靠布局(与 tablet 截图一致的操作)。
await tester.tapAt(const Offset(10, 300));
await tester.pump();
if (tester.any(find.byIcon(Icons.push_pin_outlined))) {
await tester.tap(find.byIcon(Icons.push_pin_outlined).first);
await tester.pump();
}
await tester.tapAt(const Offset(1270, 300));
await tester.pump();
if (tester.any(find.byIcon(Icons.push_pin_outlined))) {
await tester.tap(find.byIcon(Icons.push_pin_outlined).first);
await tester.pump();
}
// 1) 合集菜单(左上角 logo 展开):首页 / 时序图 / 架构图 / 未开放模块。
await tester.tap(find.text('架构图设计').first);
await tester.pumpAndSettle();
await shot('../demo/arch-menu-light.png');
await tester.tapAt(const Offset(5, 5));
await tester.pumpAndSettle();
// 2) 手绘稿(墨迹,待识别)。
c.archLoadDemo(ArchDemoKind.ink);
await tester.pump();
await shot('../demo/arch-ink-light.png');
// 3) 完成图(识别 + 连线 + 标签)。
c.archLoadDemo(ArchDemoKind.done);
await tester.pump();
await shot('../demo/arch-done-light.png');
// 3.5) 图元库演示:插入「接地 / 指示灯」两个图元(可扩展图元库)。
c.addArchShape(const ArchShape(
id: 'SY1',
type: ArchShapeType.symbol,
symbolId: 'pe-ground',
rect: Rect.fromLTWH(1000, 790, 80, 145),
fill: 0x264CC9F0,
stroke: 0xFF4CC9F0,
sw: 1.8,
));
c.addArchShape(const ArchShape(
id: 'SY2',
type: ArchShapeType.symbol,
symbolId: 'hl-lamp',
rect: Rect.fromLTWH(1125, 790, 80, 175),
fill: 0x26FFD166,
stroke: 0xFFFFD166,
sw: 1.8,
));
await tester.pump();
await shot('../demo/arch-symbols-light.png');
// 4) 深色主题完成图(主题切换有 200ms 过渡,等动画结束再截)。
c.toggleTheme();
await tester.pumpWidget(shell(Tokens.dark));
await tester.pump(const Duration(milliseconds: 400));
await shot('../demo/arch-done-dark.png');
// 5) 参数化草图:几何 + 约束图标 + 驱动尺寸 + 参数列表。
c.toggleTheme();
await tester.pumpWidget(shell(Tokens.light));
await tester.pump(const Duration(milliseconds: 400));
c.setSketchMode(true);
c.setSketchTool(SketchTool.line);
c.changeSketch((_) => demoSampleSketch());
c.selectSketchIds(<String>['E1']);
await tester.pump();
await shot('../demo/arch-sketch-light.png');
});
testWidgets('generate architecture recognition sheet', (WidgetTester tester) async {
await tester.runAsync(() => loadZhFonts());
await tester.runAsync(() => loadMaterialIcons());
tester.view.physicalSize = const Size(1280, 800);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
final List<(String, List<Offset>)> items = archRecognitionSheet();
// 对照表同时充当「识别回归样本」:每个样本都必须识别成功。
for (final (String, List<Offset>) it in items) {
expect(recognizeArchStroke(it.$2), isNotNull, reason: '样本「${it.$1}」应被识别');
}
await tester.pumpWidget(
RepaintBoundary(
key: const ValueKey<String>('sheet'),
child: MaterialApp(
theme: buildTheme(Tokens.light),
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Tokens.light.bg,
body: Center(
child: CustomPaint(
size: const Size(1216, 720),
painter: _SheetPainter(items),
),
),
),
),
),
);
await tester.pump();
await expectLater(find.byKey(const ValueKey<String>('sheet')),
matchesGoldenFile('../demo/arch-recognition-light.png'));
});
}
/// 参数化草图演示:矩形(H/V 约束)+ 圆(半径尺寸)+ 斜线(平行约束、长度尺寸)
/// + 四分之一圆弧;求解一次使尺寸与几何一致。
SketchDoc demoSampleSketch() {
const SketchDoc d = SketchDoc(
points: <SketchPoint>[
SketchPoint(id: 'P1', x: 120, y: 140),
SketchPoint(id: 'P2', x: 390, y: 140),
SketchPoint(id: 'P3', x: 390, y: 300),
SketchPoint(id: 'P4', x: 120, y: 300),
SketchPoint(id: 'P5', x: 520, y: 220),
SketchPoint(id: 'P6', x: 150, y: 430),
SketchPoint(id: 'P7', x: 430, y: 430),
SketchPoint(id: 'P8', x: 560, y: 430),
SketchPoint(id: 'P9', x: 660, y: 430),
SketchPoint(id: 'P10', x: 560, y: 530),
],
entities: <SketchEntity>[
SketchEntity(id: 'E1', kind: SketchKind.line, p1: 'P1', p2: 'P2'),
SketchEntity(id: 'E2', kind: SketchKind.line, p1: 'P2', p2: 'P3'),
SketchEntity(id: 'E3', kind: SketchKind.line, p1: 'P3', p2: 'P4'),
SketchEntity(id: 'E4', kind: SketchKind.line, p1: 'P4', p2: 'P1'),
SketchEntity(id: 'E5', kind: SketchKind.circle, p1: 'P5', r: 70),
SketchEntity(id: 'E6', kind: SketchKind.line, p1: 'P6', p2: 'P7'),
SketchEntity(
id: 'E7',
kind: SketchKind.arc,
p1: 'P8',
p2: 'P9',
p3: 'P10',
r: 100),
],
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'),
SketchConstraint(id: 'K5', type: SketchConsType.parallel, a: 'E6', b: 'E1'),
],
dims: <SketchDim>[
SketchDim(id: 'D1', type: SketchDimType.length, value: 270, a: 'E1'),
SketchDim(id: 'D2', type: SketchDimType.radius, value: 70, a: 'E5'),
SketchDim(id: 'D3', type: SketchDimType.length, value: 280, a: 'E6'),
],
);
return solveSketch(d).doc;
}
/// 识别对照表:3 列网格,每格「手绘墨迹 → 识别结果」两个小画格。
class _SheetPainter extends CustomPainter {
_SheetPainter(this.items);
final List<(String, List<Offset>)> items;
@override
void paint(Canvas canvas, Size size) {
final Tokens t = Tokens.light;
const int cols = 3;
final int rows = (items.length + cols - 1) ~/ cols;
final double cw = size.width / cols;
final double ch = size.height / rows;
for (int i = 0; i < items.length; i++) {
final Rect cell = Rect.fromLTWH(
(i % cols) * cw,
(i ~/ cols) * ch,
cw,
ch,
);
_cell(canvas, t, cell, items[i].$1, items[i].$2);
}
}
void _cell(Canvas canvas, Tokens t, Rect cell, String label, List<Offset> ink) {
final Rect box = cell.deflate(14);
canvas.drawRRect(
RRect.fromRectAndRadius(box, const Radius.circular(12)),
Paint()..color = t.panel,
);
canvas.drawRRect(
RRect.fromRectAndRadius(box, const Radius.circular(12)),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1
..color = t.line2,
);
final TextPainter tp = TextPainter(
text: TextSpan(
text: '$label · 手绘',
style: TextStyle(
color: t.sub,
fontSize: 12.5,
fontWeight: FontWeight.w700,
fontFamily: 'Microsoft YaHei UI',
fontFamilyFallback: const <String>['Microsoft YaHei', 'Segoe UI'],
),
),
textDirection: TextDirection.ltr,
)..layout();
tp.paint(canvas, box.topLeft + const Offset(12, 8));
final TextPainter tp2 = TextPainter(
text: TextSpan(
text: '→ 识别结果',
style: TextStyle(
color: t.ok,
fontSize: 11,
fontWeight: FontWeight.w600,
fontFamily: 'Microsoft YaHei UI',
fontFamilyFallback: const <String>['Microsoft YaHei', 'Segoe UI'],
),
),
textDirection: TextDirection.ltr,
)..layout();
tp2.paint(canvas, box.topLeft + Offset(12 + tp.width + 12, 10));
final Rect leftPane = Rect.fromLTWH(
box.left + 12, box.top + 34, box.width / 2 - 18, box.height - 46);
final Rect rightPane = Rect.fromLTWH(
box.center.dx + 6, box.top + 34, box.width / 2 - 18, box.height - 46);
const int inkColor = 0xFF64748B;
final List<ArchStroke> strokes = <ArchStroke>[
ArchStroke(id: 'k', pts: ink, color: inkColor),
];
_pane(canvas, t, leftPane, ArchDoc(strokes: strokes));
final ArchRecognition? r = recognizeArchStroke(ink);
_pane(
canvas,
t,
rightPane,
r == null
? ArchDoc(strokes: strokes)
: ArchDoc(shapes: <ArchShape>[
archShapeFromRecognition(r, id: 'r', color: kArchPalette[0]),
]),
);
}
void _pane(Canvas canvas, Tokens t, Rect pane, ArchDoc doc) {
canvas.save();
canvas.clipRRect(RRect.fromRectAndRadius(pane, const Radius.circular(8)));
canvas.drawRRect(
RRect.fromRectAndRadius(pane, const Radius.circular(8)),
Paint()..color = t.canvas2,
);
canvas.save();
canvas.translate(pane.left, pane.top);
final Rect b = doc.contentBounds().inflate(18);
final double z =
math.min(pane.width / b.width, pane.height / b.height).clamp(0.05, 2.0);
ArchPainter(
doc: doc.copyWith(
zoom: z,
panX: pane.width / 2 - b.center.dx * z,
panY: pane.height / 2 - b.center.dy * z,
),
t: t,
).paint(canvas, pane.size);
canvas.restore();
canvas.restore();
}
@override
bool shouldRepaint(covariant _SheetPainter oldDelegate) => false;
}