CmvrControlStation/tests/jointflow_canvas_test.cpp
linbo ad1a42c761 feat: support multi-robot workflows and sync server protocol
Add independent robot connections, per-node device binding and isolated demo state. Improve canvas multi-selection, undo and parameter layouts. Remove remote configuration management and align protos with cmvr-es c8b71b2b. Include Release startup and loopback regression coverage.
2026-11-25 17:25:39 +08:00

68 lines
4.1 KiB
C++

#include <QtWidgets>
#include "jointfloweditor.h"
#include "applicationstyle.h"
#include "workflowcanvas.h"
#include <stdexcept>
static void check(bool value, const char *message) {
if (!value) throw std::runtime_error(message);
}
static void pan(QGraphicsView *view, Qt::MouseButton button) {
const QPoint from(380, 150), to(180, 110);
QMouseEvent press(QEvent::MouseButtonPress, from, button, button, Qt::NoModifier);
QApplication::sendEvent(view->viewport(), &press);
QMouseEvent move(QEvent::MouseMove, to, Qt::NoButton, button, Qt::NoModifier);
QApplication::sendEvent(view->viewport(), &move);
QMouseEvent release(QEvent::MouseButtonRelease, to, button, Qt::NoButton, Qt::NoModifier);
QApplication::sendEvent(view->viewport(), &release);
}
int main(int argc, char **argv) {
QApplication app(argc, argv); ApplicationStyle::install(app);
try {
JointFlowEditor editor;
editor.resize(1050, 430); editor.show(); QApplication::processEvents();
editor.setAvailablePointNames({QStringLiteral("Point")});
auto *canvas=editor.findChild<QGraphicsView *>();
QMimeData component;component.setData("application/x-cmvr-workflow-component","point:Point");
QDragEnterEvent drag(QPoint(100,100),Qt::CopyAction,&component,Qt::LeftButton,Qt::NoModifier);
QApplication::sendEvent(canvas->viewport(),&drag);
QDropEvent drop(QPointF(100,100),Qt::CopyAction,&component,Qt::LeftButton,Qt::NoModifier);
QApplication::sendEvent(canvas->viewport(),&drop);
check(editor.toJson().size()==1,"point palette drop creates node");
editor.setEditingEnabled(false);
static_cast<WorkflowCanvas *>(canvas)->componentDropped("point:Point");
check(editor.toJson().size()==1,"execution guard blocks drop");
editor.setEditingEnabled(true);editor.clear();
for (int i = 0; i < 80; ++i) editor.addSingleNode(QString::number(i), "Point", 1);
QApplication::processEvents();
auto *view = editor.findChild<QGraphicsView *>();
const auto nodes = editor.toJson();
const QRectF last(nodes.last().toObject()["x"].toDouble(), 40, 190, 76);
check(view->mapToScene(view->viewport()->rect()).boundingRect().contains(last), "new node revealed");
editor.resetView();
for (const auto button : {Qt::RightButton, Qt::MiddleButton}) {
const int before = view->horizontalScrollBar()->value(); pan(view, button);
check(view->horizontalScrollBar()->value() > before, "pan moves viewport");
}
const QPointF center = view->mapToScene(view->viewport()->rect().center());
editor.setActiveNode(nodes.last().toObject()["id"].toString());
check(QLineF(center, view->mapToScene(view->viewport()->rect().center())).length() < 3,
"redraw preserves panned viewport");
QWheelEvent wheel(QPointF(300, 150), QPointF(300, 150), QPoint(), QPoint(0, 120),
Qt::NoButton, Qt::ControlModifier, Qt::NoScrollPhase, false);
QApplication::sendEvent(view->viewport(), &wheel);
check(view->transform().m11() > 1, "Ctrl wheel zoom");
editor.fitAll();
check(view->mapToScene(view->viewport()->rect()).boundingRect().contains(view->scene()->itemsBoundingRect()),
"fit shows all 80 cards");
editor.grab().save("build/tests/canvas-many.png");
QJsonArray shortFlow{nodes.first()};
check(editor.fromJson(shortFlow), "load another flow"); QApplication::processEvents();
check(qAbs(view->transform().m11() - 1) < .001, "switch resets zoom");
check(view->viewport()->rect().contains(view->mapFromScene(QPointF(40, 40))), "switch reveals start");
editor.grab().save("build/tests/canvas-switch.png");
check(editor.toJson() == shortFlow, "view changes leave saved flow intact");
puts("PASS 80-node canvas, right/middle pan gestures and left selection, Ctrl-wheel zoom, fit-all, redraw stability and flow switch reset");
return 0;
} catch (const std::exception &error) { fprintf(stderr, "FAIL: %s\n", error.what()); return 1; }
}