#include "agvmapdocument.h" #include "agvmapeditorwidget.h" #include "agvmapviewwidget.h" #include "robotclient.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { void require(bool condition, const char *message) { if (!condition) qFatal("AGV editor deletion test failed: %s", message); } struct Request { quint64 id; QString device; QString map; QString content; }; QString mapContent(const QString &name) { return QStringLiteral(R"JSON({ "header":{"mapType":"2D-Map","mapName":"%1","resolution":0.05}, "unknownInteger":9007199254740993, "normalPosList":[{"x":1,"y":2}], "advancedPointList":[ {"className":"LandMark","instanceName":"LM1","desc":"一号","pos":{"x":0,"y":0}}, {"className":"LandMark","instanceName":"LM2","pos":{"x":2,"y":0}}, {"className":"LandMark","instanceName":"LM3","pos":{"x":4,"y":0}} ], "advancedCurveList":[ {"className":"BezierPath","instanceName":"R12","startPos":{"instanceName":"LM1","pos":{"x":0,"y":0}},"endPos":{"instanceName":"LM2","pos":{"x":2,"y":0}}}, {"className":"BezierPath","instanceName":"R21","startPos":{"instanceName":"LM2","pos":{"x":2,"y":0}},"endPos":{"instanceName":"LM1","pos":{"x":0,"y":0}}}, {"className":"BezierPath","instanceName":"R23","startPos":{"instanceName":"LM2","pos":{"x":2,"y":0}},"endPos":{"instanceName":"LM3","pos":{"x":4,"y":0}}} ] })JSON").arg(name); } void nextModal(const std::function &action) { auto *timer = new QTimer(qApp); timer->setInterval(10); QObject::connect(timer, &QTimer::timeout, timer, [timer, action]() { auto *dialog = qobject_cast(QApplication::activeModalWidget()); if (!dialog || !action(dialog)) return; timer->stop(); timer->deleteLater(); }); timer->start(); QTimer::singleShot(7000, qApp, [guard = QPointer(timer)]() { if (guard && guard->isActive()) qFatal("AGV editor deletion test failed: modal did not open"); }); } int rowFor(QTableWidget *table, const QString &id) { for (int row = 0; row < table->rowCount(); ++row) { if (table->item(row, 0) && table->item(row, 0)->text() == id) return row; } return -1; } void confirmNext(bool station, bool accept, const std::function &extra = {}) { nextModal([=](QDialog *dialog) { const QString expected = station ? QStringLiteral("agvDeleteStationConfirmDialog") : QStringLiteral("agvDeleteRouteConfirmDialog"); if (dialog->objectName() != expected) return false; auto *box = qobject_cast(dialog); require(box, "delete confirmation is a message box"); if (extra) { extra(box); } else { box->button(accept ? QMessageBox::Yes : QMessageBox::Cancel)->click(); } return true; }); } void deleteInManager(QDialog *dialog, bool station, const QString &id, bool accept, const std::function &extra = {}) { auto *tabs = dialog->findChild("agvMapObjectsTabs"); auto *table = dialog->findChild(station ? "agvMapStationsTable" : "agvMapRoutesTable"); auto *remove = dialog->findChild(station ? "agvDeleteStationButton" : "agvDeleteRouteButton"); require(tabs && table && remove, "manager delete controls exist"); tabs->setCurrentIndex(station ? 0 : 1); const int row = rowFor(table, id); require(row >= 0, "object to delete exists"); table->setCurrentCell(row, 0); require(remove->isEnabled(), "selecting a row enables deletion"); confirmNext(station, accept, extra); remove->click(); } } int main(int argc, char *argv[]) { QApplication app(argc, argv); const QString screenshotDir = qEnvironmentVariable("AGV_DELETE_SCREENSHOT_DIR"); if (!screenshotDir.isEmpty()) require(QDir().mkpath(screenshotDir), "screenshot directory is available"); QTimer::singleShot(30000, &app, []() { qFatal("AGV deletion test timed out"); }); RobotClient client; QObject::disconnect(&client, nullptr, nullptr, nullptr); QVector reads, writes; QObject::connect(&client, &RobotClient::agvMapContentRequested, [&](quint64 id, const QString &device, const QString &map) { reads.append({id, device, map, {}}); }); QObject::connect(&client, &RobotClient::agvMapSaveRequested, [&](quint64 id, const QString &device, const QString &map, const QString &content) { writes.append({id, device, map, content}); }); QWidget window; auto *layout = new QVBoxLayout(&window); auto *view = new AgvMapViewWidget(&window); auto *editor = new AgvMapEditorWidget(&client, view, &window); layout->addWidget(editor); layout->addWidget(view); window.resize(1050, 700); bool connected = true; QString selectedMap = QStringLiteral("delete_map"); editor->setContextProvider([&]() { AgvMapEditorWidget::Context context; context.connected = connected; context.demo = true; context.endpoint = QStringLiteral("offline:50052"); context.deviceId = QStringLiteral("agv_delete_test"); context.mapName = selectedMap; return context; }); const auto setViewMap = [&]() { QImage image(1280, 720, QImage::Format_RGB32); image.fill(Qt::white); view->setMapFrame(image, {{"mapId", selectedMap}, {"dimension", 2}, {"resolution", 0.05}, {"renderScale", 1.0}, {"originX", 0.0}, {"originY", 0.0}, {"originTheta", 0.0}}); }; setViewMap(); window.show(); app.processEvents(); editor->refreshContext(); auto *manage = editor->findChild("agvManageMapObjectsButton"); auto *save = editor->findChild("agvSaveMapObjectsButton"); auto *undo = editor->findChild("agvUndoMapObjectsButton"); require(manage && save && undo, "editor buttons exist"); const auto replyRead = [&](Request request, const QString &content) { emit client.agvMapContentReceived(request.id, request.device, request.map, content, true, {}); }; const auto manager = [&](const std::function &action) { nextModal([=](QDialog *dialog) { if (dialog->objectName() != QStringLiteral("agvMapObjectsDialog")) return false; QTimer::singleShot(0, dialog, [=]() { action(dialog); dialog->accept(); }); return true; }); const int oldReads = reads.size(); manage->click(); if (reads.size() > oldReads) replyRead(reads.last(), mapContent(selectedMap)); }; manager([&](QDialog *dialog) { auto *removeStation = dialog->findChild("agvDeleteStationButton"); auto *removeRoute = dialog->findChild("agvDeleteRouteButton"); require(!removeStation->isEnabled() && !removeRoute->isEnabled(), "deletion is disabled without a selection"); if (!screenshotDir.isEmpty()) { auto *stations = dialog->findChild("agvMapStationsTable"); auto *routes = dialog->findChild("agvMapRoutesTable"); auto *tabs = dialog->findChild("agvMapObjectsTabs"); stations->setCurrentCell(rowFor(stations, "LM1"), 0); require(dialog->grab().save(screenshotDir + "/stations.png"), "station screenshot saved"); tabs->setCurrentIndex(1); routes->setCurrentCell(rowFor(routes, "R12"), 0); require(dialog->grab().save(screenshotDir + "/routes.png"), "route screenshot saved"); } deleteInManager(dialog, false, QStringLiteral("R12"), false); require(rowFor(dialog->findChild("agvMapRoutesTable"), "R12") >= 0, "cancel keeps selected route"); require(!save->isEnabled() && !undo->isEnabled(), "cancel creates no draft or history"); deleteInManager(dialog, false, QStringLiteral("R12"), true); auto *routes = dialog->findChild("agvMapRoutesTable"); require(rowFor(routes, "R12") < 0 && rowFor(routes, "R21") >= 0 && rowFor(routes, "R23") >= 0, "deleting one direction keeps reverse and unrelated route"); auto *localUndo = dialog->findChild("agvManagerUndoButton"); require(localUndo && localUndo->isEnabled(), "route removal is undoable"); localUndo->click(); require(rowFor(routes, "R12") >= 0 && !localUndo->isEnabled(), "one undo restores route without extra history"); deleteInManager(dialog, true, QStringLiteral("LM1"), false, [&](QMessageBox *box) { require(box->text().contains("R12") && box->text().contains("R21") && box->text().contains(QStringLiteral("2 条")), "station confirmation lists connected route IDs and count"); if (!screenshotDir.isEmpty()) require(box->grab().save(screenshotDir + "/station-confirm.png"), "confirmation screenshot saved"); box->button(QMessageBox::Cancel)->click(); }); require(rowFor(dialog->findChild("agvMapStationsTable"), "LM1") >= 0, "station cancellation preserves station"); deleteInManager(dialog, true, QStringLiteral("LM1"), true); require(rowFor(dialog->findChild("agvMapStationsTable"), "LM1") < 0 && rowFor(routes, "R12") < 0 && rowFor(routes, "R21") < 0 && rowFor(routes, "R23") >= 0, "station removal removes only connected routes"); require(writes.isEmpty() && save->isEnabled() && undo->isEnabled(), "deletion remains local until explicit save"); localUndo->click(); require(rowFor(routes, "R12") >= 0 && rowFor(routes, "R21") >= 0 && !save->isEnabled() && !undo->isEnabled(), "single undo restores station and both connected routes atomically"); }); qInfo("PASS: route direction, station cascade, cancellation and atomic undo"); manager([&](QDialog *dialog) { deleteInManager(dialog, false, QStringLiteral("R12"), true, [&](QMessageBox *box) { connected = false; editor->refreshContext(); // A queued acceptance can arrive before the context polling timer. box->done(QMessageBox::Yes); }); auto *remove = dialog->findChild("agvDeleteRouteButton"); require(!remove->isEnabled(), "connection change disables manager actions immediately"); }); require(writes.isEmpty() && !save->isEnabled(), "disconnect during confirmation cannot delete or upload"); connected = true; editor->refreshContext(); manager([&](QDialog *dialog) { require(rowFor(dialog->findChild("agvMapRoutesTable"), "R12") >= 0, "disconnected acceptance did not delete route"); deleteInManager(dialog, false, QStringLiteral("R12"), true, [&](QMessageBox *box) { selectedMap = QStringLiteral("other_map"); setViewMap(); editor->refreshContext(); box->done(QMessageBox::Yes); }); require(!dialog->findChild("agvDeleteRouteButton")->isEnabled(), "map change disables stale manager actions"); }); selectedMap = QStringLiteral("delete_map"); setViewMap(); editor->refreshContext(); manager([&](QDialog *dialog) { require(rowFor(dialog->findChild("agvMapRoutesTable"), "R12") >= 0, "map-change acceptance did not delete original route"); deleteInManager(dialog, true, QStringLiteral("LM1"), true); }); qInfo("PASS: stale nested confirmation guards connection and map identity"); int saved = 0; QObject::connect(editor, &AgvMapEditorWidget::mapSaved, [&]() { ++saved; }); save->click(); replyRead(reads.last(), mapContent(selectedMap)); require(writes.size() == 1, "one explicit save issues one upload"); AgvMapDocument uploaded; QString error; require(uploaded.load(writes.last().content, &error), "uploaded full map parses"); require(!uploaded.hasStation("LM1") && uploaded.hasStation("LM2") && uploaded.hasStation("LM3") && uploaded.routes().size() == 1, "upload contains confirmed station and cascade deletions"); require(writes.last().content.contains(QStringLiteral("9007199254740993")), "deletion preserves unrelated large integer source text"); const Request upload = writes.last(); emit client.agvMapSaveFinished(upload.id, upload.device, upload.map, true, {}); require(saved == 0 && !save->isEnabled() && !undo->isEnabled(), "upload ACK alone cannot commit deletion draft"); replyRead(reads.last(), upload.content); require(saved == 1 && !save->isEnabled() && !undo->isEnabled(), "readback absence confirms deletion and clears undo history"); qInfo("PASS: deletion save upload and readback confirmation"); if (argc > 1) { QFile report(QString::fromLocal8Bit(argv[1])); require(report.open(QIODevice::WriteOnly | QIODevice::Truncate), "test report path is writable"); report.write("PASS: cancel preserves content and creates no history\n" "PASS: selected directed route deletion preserves reverse route\n" "PASS: station confirmation lists related routes and cascades atomically\n" "PASS: one undo restores station and related routes\n" "PASS: nested confirmation guards disconnect and map change\n" "PASS: edits stay local until one explicit upload\n" "PASS: ACK alone cannot commit; readback absence confirms deletion\n" "PASS: unrelated large integer source text preserved\n"); } return 0; }