CmvrControlStation/tests/agvmapeditor_smoke.cpp

204 lines
8.4 KiB
C++
Raw Permalink Normal View History

#include "agvmapdocument.h"
#include "agvmapeditorwidget.h"
#include "agvmapviewwidget.h"
#include "robotclient.h"
#include <QApplication>
#include <QCheckBox>
#include <QComboBox>
#include <QColor>
#include <QDialog>
#include <QDialogButtonBox>
#include <QDoubleSpinBox>
#include <QElapsedTimer>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QPointer>
#include <QResource>
#include <QThread>
#include <QTimer>
#include <QVBoxLayout>
#include <functional>
namespace {
void require(bool condition, const char *message)
{
if (!condition)
qFatal("AGV editor smoke failed: %s", message);
}
void waitFor(const std::function<bool()> &condition, const char *message)
{
QElapsedTimer timer;
timer.start();
while (!condition() && timer.elapsed() < 7000) {
QApplication::processEvents();
QThread::msleep(10);
}
require(condition(), message);
}
void fillNextDialog(const QString &kind, const std::function<void(QDialog *)> &fill)
{
auto *timer = new QTimer(qApp);
timer->setInterval(10);
QObject::connect(timer, &QTimer::timeout, timer, [timer, kind, fill]() {
auto *dialog = qobject_cast<QDialog *>(QApplication::activeModalWidget());
if (!dialog || dialog->objectName() != kind)
return;
timer->stop();
fill(dialog);
auto *box = dialog->findChild<QDialogButtonBox *>();
require(box != nullptr, "dialog has action buttons");
box->button(QDialogButtonBox::Ok)->click();
timer->deleteLater();
});
timer->start();
QTimer::singleShot(7000, qApp, [guard = QPointer<QTimer>(timer)]() {
if (guard && guard->isActive())
qFatal("AGV editor smoke failed: expected dialog did not open");
});
}
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Q_INIT_RESOURCE(resources);
RobotClient client;
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, 1);
window.resize(1050, 700);
QImage image(1280, 720, QImage::Format_RGB32);
image.fill(QColor("#eef2f6"));
view->setMapFrame(image, {{"mapId", "demo_map"}, {"dimension", 2},
{"resolution", 0.01}, {"renderScale", 1.0},
{"originX", 0.0}, {"originY", 0.0}, {"originTheta", 0.0}});
bool connected = false;
editor->setContextProvider([&]() {
AgvMapEditorWidget::Context context;
context.connected = connected;
context.demo = true;
context.endpoint = "demo:50052";
context.deviceId = "agv_smoke";
context.mapName = "demo_map";
context.pose = {{"map", "demo_map"}, {"localized", true},
{"x", 1.0}, {"y", 1.0}, {"theta", 0.0}};
return context;
});
QObject::connect(&client, &RobotClient::connectionChanged,
[&connected, editor](bool value, int, const QString &) {
connected = value;
editor->refreshContext();
});
window.show();
client.connectToServer("127.0.0.1", 50052, true);
waitFor([&]() { return connected; }, "Demo connected");
int originalRouteCount = -1;
QObject::connect(&client, &RobotClient::agvMapContentReceived,
[&](quint64 token, const QString &, const QString &, const QString &content,
bool ok, const QString &) {
if (token != 10000)
return;
AgvMapDocument original;
QString error;
require(ok && original.load(content, &error), "original Demo map is valid");
originalRouteCount = original.routes().size();
});
client.requestAgvMapContent(10000, "agv_smoke", "demo_map");
waitFor([&]() { return originalRouteCount >= 0; }, "original route count loaded");
auto *coordinates = editor->findChild<QPushButton *>("agvCoordinateStationButton");
auto *route = editor->findChild<QPushButton *>("agvAddRouteButton");
auto *save = editor->findChild<QPushButton *>("agvSaveMapObjectsButton");
auto *status = editor->findChild<QLabel *>("agvMapEditorStatus");
require(coordinates && route && save && status, "editor controls exist");
const auto addStation = [&](const QString &id, double x, double y) {
fillNextDialog("agvStationDialog", [=](QDialog *dialog) {
dialog->findChild<QLineEdit *>("agvNewStationId")->setText(id);
dialog->findChild<QDoubleSpinBox *>("agvNewStationX")->setValue(x);
dialog->findChild<QDoubleSpinBox *>("agvNewStationY")->setValue(y);
});
coordinates->click();
waitFor([&]() { return status->text().contains(id) && save->isEnabled(); },
"station draft created");
};
addStation("P1", 2.0, 3.0);
fillNextDialog("agvRouteDialog", [](QDialog *dialog) {
dialog->findChild<QLineEdit *>("agvNewRouteId")->setText("HOME_P1");
dialog->findChild<QCheckBox *>("agvNewRouteBidirectional")->setChecked(true);
});
route->click();
view->stationSelected("HOME");
view->stationSelected("P1");
require(save->isEnabled(), "route draft remains unsaved");
int saves = 0;
QObject::connect(editor, &AgvMapEditorWidget::mapSaved, [&]() { ++saves; });
save->click();
waitFor([&]() { return saves == 1; }, "station and two directions saved");
require(!save->isEnabled(), "successful save clears dirty state");
QString downloaded;
bool received = false;
bool uploaded = false;
bool inventoryUpdated = false;
QObject::connect(&client, &RobotClient::agvMapContentReceived,
[&](quint64 token, const QString &, const QString &, const QString &content,
bool ok, const QString &) {
if (token == 10001 || token == 10003) {
require(ok, "verification read succeeded");
downloaded = content;
received = true;
}
});
QObject::connect(&client, &RobotClient::agvMapSaveFinished,
[&](quint64 token, const QString &, const QString &, bool ok, const QString &) {
if (token == 10002) {
require(ok, "simulated independent map edit saved");
uploaded = true;
}
});
QObject::connect(&client, &RobotClient::agvInventoryReceived,
[&](const QStringList &, const QVariantList &stations) {
for (const QVariant &value : stations)
inventoryUpdated |= value.toMap().value("id").toString() == "P1";
});
client.requestAgvMapContent(10001, "agv_smoke", "demo_map");
waitFor([&]() { return received; }, "read saved map back from Demo service");
AgvMapDocument result;
QString error;
require(result.load(downloaded, &error), "saved map is valid SMAP");
require(result.hasStation("P1") && result.routes().size() == originalRouteCount + 2,
"saved station and forward/reverse routes persisted");
client.requestAgvInventory("agv_smoke");
waitFor([&]() { return inventoryUpdated; }, "saved station appears in navigation inventory");
addStation("P2", 4.0, 3.0);
require(result.addStation("REMOTE", 5.0, 6.0, 0.0, &error), "independent station addition");
QString changedMap = result.content();
changedMap.insert(changedMap.indexOf(QLatin1Char('{')) + 1,
QStringLiteral("\"external64\":9007199254740993,"));
client.saveAgvMapContent(10002, "agv_smoke", "demo_map", changedMap);
waitFor([&]() { return uploaded; }, "independent update stored before editor save");
save->click();
waitFor([&]() { return saves == 2; }, "editor rebases additions onto latest map");
received = false;
client.requestAgvMapContent(10003, "agv_smoke", "demo_map");
waitFor([&]() { return received; }, "read merged map");
require(result.load(downloaded, &error), "merged map remains valid");
require(result.hasStation("P1") && result.hasStation("P2") && result.hasStation("REMOTE"),
"both local and independent additions preserved");
require(result.routes().size() == originalRouteCount + 2 && downloaded.contains("9007199254740993"),
"previous routes and unknown integer are preserved");
if (argc > 1)
require(window.grab().save(QString::fromLocal8Bit(argv[1])), "editor screenshot saved");
client.disconnectFromServer();
waitFor([&]() { return !connected; }, "Demo disconnected cleanly");
qInfo("PASS: AGV editor add station, bidirectional route, save, reload and rebase");
return 0;
}