CmvrControlStation/tests/joint_program_flows_test.cpp

121 lines
6.4 KiB
C++
Raw Normal View History

#include <QtWidgets>
#include <stdexcept>
#define private public
#include "mainwindow.h"
#undef private
#include "jointfloweditor.h"
#include "applicationstyle.h"
static void check(bool value, const char *message) {
if (!value) throw std::runtime_error(message);
}
int main(int argc, char **argv) {
QApplication app(argc, argv);
ApplicationStyle::install(app);
QTemporaryDir settings;
QSettings::setDefaultFormat(QSettings::IniFormat);
QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, settings.path());
app.setOrganizationName("CMVR-Flows-Test");
app.setApplicationName("Isolated-Flows");
const QString path = QCoreApplication::applicationDirPath() + "/joint_programs.json";
QFile original(path);
const bool existed = original.exists();
original.open(QIODevice::ReadOnly);
const QByteArray backup = original.readAll();
original.close();
int result = 0;
try {
JointFlowEditor editor;
editor.addSingleNode("A", "A", 1);
const QJsonArray oldFlow = editor.toJson();
RecordedJointPoint seedPoint;seedPoint.name="A";seedPoint.deviceId="test_arm";seedPoint.jointNames=QStringList{"joint1"};seedPoint.positions={.1};
auto seedB=seedPoint;seedB.name="B";seedB.positions={.2};const QJsonArray validPoints{seedPoint.toJson(),seedB.toJson()};
QJsonObject oldGroup{{"name", "Legacy"}, {"points", validPoints},
{"flow", oldFlow}, {"moveJVelocity", .25}, {"moveJAcceleration", .5}};
QFile seed(path); check(seed.open(QIODevice::WriteOnly), "write fixture");
seed.write(QJsonDocument(QJsonObject{{"version", 2}, {"activeGroup", "Legacy"},
{"groups", QJsonArray{oldGroup, QJsonObject{{"name", "Other"}, {"points",validPoints}, {"flow", oldFlow}}}}}).toJson());
seed.close();
MainWindow window;
QSettings legacy(settings.filePath("legacy.ini"), QSettings::IniFormat);
window.loadJointPrograms(legacy);
check(window.m_jointProgramGroups[0].flows.size() == 1, "legacy default flow");
check(window.m_jointFlowEditor->toJson().first().toObject()["pointName"] == "A", "legacy nodes preserved");
window.storeCurrentJointProgramGroup();
auto &group = window.m_jointProgramGroups[0];
group.flows.append({"Second", {}, .6, .8});
window.activateJointProgramFlow(1);
check(window.m_jointFlowEditor->toJson().isEmpty(), "new flow independent");
check(window.m_jointFlowVelocitySpin->value() == .6, "per-flow velocity");
window.m_jointFlowEditor->addSingleNode("B", "B", 2);
const QJsonArray secondFlow = window.m_jointFlowEditor->toJson();
window.activateJointProgramFlow(0);
check(window.m_jointFlowEditor->toJson().first().toObject()["pointName"] == "A", "switch preserves original");
check(window.m_jointFlowVelocitySpin->value() == .25, "restore original velocity");
check(window.m_jointFlowAccelerationSpin->value() == .5, "legacy acceleration preserved");
window.activateJointProgramFlow(1);
window.saveJointPrograms();
window.loadJointPrograms(legacy);
check(window.m_jointProgramGroups[0].activeFlow == 1, "active flow persisted");
check(window.m_jointFlowEditor->toJson().first().toObject()["id"] == secondFlow.first().toObject()["id"], "second flow persisted");
check(window.m_jointProgramGroups[1].flows.size() == 1, "inactive old group migrated");
auto copy = window.m_jointProgramGroups[0];
copy.flows[0].nodes = {};
check(!window.m_jointProgramGroups[0].flows[0].nodes.isEmpty(), "group copy independent");
RecordedJointPoint a;
a.name = "A"; a.deviceId = "test_arm";
a.jointNames = QStringList{QStringLiteral("joint1")}; a.positions = {0.1};
RecordedJointPoint b = a; b.name = "B"; b.positions = {0.2};
window.m_recordedJointPoints = {a, b};
window.refreshRecordedJointPoints();
window.m_jointFlowEditor->addSingleNode("custom label", "A", 3);
QString error;
check(!window.renameRecordedJointPoint(0, "B", &error), "reject duplicate name");
check(!window.renameRecordedJointPoint(0, " ", &error), "reject blank name");
check(window.renameRecordedJointPoint(0, "Renamed", &error), "rename point");
for (const auto &flow : window.m_jointProgramGroups[0].flows) {
bool found = false;
for (const auto &value : flow.nodes) {
const auto node = value.toObject();
check(node.value("pointName") != "A", "no stale references");
found = found || node.value("pointName") == "Renamed";
}
check(found, "rename updates every flow");
}
auto *header = window.m_jointPointTable->verticalHeader();
header->moveSection(0, 1);
check(window.m_recordedJointPoints[0].name == "B"
&& window.m_recordedJointPoints[1].name == "Renamed", "header drag reorders data");
check(header->logicalIndex(0) == 0, "header and model order agree");
window.loadJointPrograms(legacy);
check(window.m_recordedJointPoints[0].name == "B"
&& window.m_recordedJointPoints[1].positions == a.positions,
"point order and angles survive reload");
window.m_jointPointMovePending = true;
check(!window.moveRecordedJointPoint(0, 1)
&& !window.renameRecordedJointPoint(0, "Blocked", &error), "block edits during motion");
window.m_jointPointMovePending = false;
if (argc > 1) {
window.m_jointFlowDialog->show();
window.m_jointFlowDialog->resize(1120, 740);
QApplication::processEvents();
window.m_jointFlowDialog->grab().save("build/tests/joint-flow-small.png");
window.m_jointFlowDialog->resize(1480, 920);
QApplication::processEvents();
window.m_jointFlowDialog->grab().save("build/tests/joint-flow-large.png");
}
puts("PASS old JSON migration, independent flows and parameters, switching, persistence and group copying");
puts("PASS point renaming across flows, duplicate/blank rejection, row drag persistence and motion guard");
} catch (const std::exception &error) {
fprintf(stderr, "FAIL: %s\n", error.what());
result = 1;
}
if (existed) {
QFile restore(path); restore.open(QIODevice::WriteOnly); restore.write(backup);
} else {
QFile::remove(path);
}
return result;
}