qt_clean_probe.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // This harness instantiates the actual MainWindow and clicks its real buttons.
  2. // Default mode is read-only. Only --mode start invokes physical host controls.
  3. #include "mainwindow.h"
  4. #include "backendlink.h"
  5. #include "stationdata.h"
  6. #include "localplans.h"
  7. #include <QAbstractButton>
  8. #include <QApplication>
  9. #include <QCommandLineParser>
  10. #include <QDateTime>
  11. #include <QDir>
  12. #include <QFile>
  13. #include <QFontDatabase>
  14. #include <QJsonArray>
  15. #include <QJsonDocument>
  16. #include <QJsonObject>
  17. #include <QLabel>
  18. #include <QLineEdit>
  19. #include <QPointer>
  20. #include <QPlainTextEdit>
  21. #include <QSaveFile>
  22. #include <QTemporaryDir>
  23. #include <QTimer>
  24. #include <QWebSocket>
  25. #include <QWebSocketServer>
  26. #include <functional>
  27. static bool writeJson(const QString &path, const QJsonObject &value)
  28. {
  29. QSaveFile file(path);
  30. const QByteArray bytes = QJsonDocument(value).toJson(QJsonDocument::Indented);
  31. return file.open(QIODevice::WriteOnly) && file.write(bytes) == bytes.size() && file.commit();
  32. }
  33. class Fixture : public QObject
  34. {
  35. public:
  36. QWebSocketServer server{QStringLiteral("Qt clean contract fixture"), QWebSocketServer::NonSecureMode};
  37. QPointer<QWebSocket> socket;
  38. int connections = 0;
  39. int starts = 0;
  40. int actions = 0;
  41. bool duplicate = false;
  42. bool unexpected = false;
  43. bool correctParams = true;
  44. bool disconnectTest = false;
  45. bool silentMode = false;
  46. bool runAmt = false;
  47. bool amtResultsReady = false;
  48. bool earlyAmtExit = false;
  49. int amtLaunches = 0;
  50. bool failAtEnd = false;
  51. bool unexpectedBootMode = false;
  52. bool changedBoot = false;
  53. bool oldFailedSnapshotSent = false;
  54. int uartReplays = 0;
  55. QStringList methods;
  56. QSet<QString> inputIds;
  57. QJsonObject command;
  58. QJsonArray events;
  59. int step = -1;
  60. qint64 started = 0;
  61. quint64 sequence = 0;
  62. explicit Fixture(bool testAmt = false) : runAmt(testAmt)
  63. {
  64. server.listen(QHostAddress::LocalHost, 0);
  65. connect(&server, &QWebSocketServer::newConnection, this, [this] {
  66. socket = server.nextPendingConnection();
  67. ++connections;
  68. QWebSocket *client = socket;
  69. connect(client, &QWebSocket::textMessageReceived, this, [this](const QString &text) { receive(text); });
  70. connect(client, &QWebSocket::disconnected, client, &QObject::deleteLater);
  71. pushState();
  72. if (silentMode) return; // Deliberately silent TCP peer: exercise client watchdog.
  73. // A fresh socket starts from byte zero. BackendLink must suppress
  74. // already displayed bytes and recover anything missed offline.
  75. for (const QJsonValue &row : events) send(row.toObject());
  76. ++uartReplays;
  77. });
  78. }
  79. QString url() const { return QStringLiteral("ws://127.0.0.1:%1/ws?view=qt-clean").arg(server.serverPort()); }
  80. void send(const QJsonObject &frame)
  81. {
  82. if (socket && socket->state() == QAbstractSocket::ConnectedState)
  83. socket->sendTextMessage(QString::fromUtf8(QJsonDocument(frame).toJson(QJsonDocument::Compact)));
  84. }
  85. QJsonObject station() const
  86. {
  87. QString stage, phase = QStringLiteral("uefi"), action;
  88. int cpu = 0;
  89. if (step == 0 || step == 1) { stage = "uip1"; phase = "uip"; action = "continue_uip"; cpu = step; }
  90. else if (step == 2 || step == 3) { stage = "amt"; phase = "amt"; action = runAmt ? "start_amt" : "skip_amt"; cpu = step - 2; }
  91. else if (runAmt && (step == 4 || step == 5)) { stage = "amt"; phase = "amt"; action = "skip_amt"; cpu = step - 4; }
  92. else if (step == (runAmt ? 6 : 4)) { stage = "uip2"; phase = "uefi"; action = "continue_uip"; }
  93. else if (step >= (runAmt ? 7 : 5)) { stage = "uefi"; phase = "uefi"; action = "start_mt"; }
  94. else { stage = "uefi"; phase = "uefi"; action = "start_mt"; }
  95. const QJsonObject input{{"id", QStringLiteral("fixture-input-%1").arg(step)}, {"stage", stage},
  96. {"cpu", cpu}, {"ready", true}, {"test_complete", amtResultsReady}, {"actions", QJsonArray{action}}};
  97. return QJsonObject{{"station_id", "S1"}, {"connection", "online"}, {"power", "on"}, {"can_start", true},
  98. {"phase", phase}, {"run_state", changedBoot || (failAtEnd && step >= 5)
  99. || (step < 0 && command.value("state").toString() == "succeeded")
  100. ? "failed" : (step >= 0 ? "waiting_input" : "idle")},
  101. {"started_unix_ms", QString::number(started)},
  102. {"boot_id", changedBoot ? "fixture-boot-2" : (step >= 0 ? "fixture-boot-1" : "fixture-boot-0")},
  103. {"verdict", failAtEnd && step >= 5 ? "fail" : "not_tested"},
  104. {"input", input}, {"command", command},
  105. {"channels", QJsonArray{QJsonObject{{"name", "CPU0_CHA"}, {"state", "unknown"}, {"temperature_c", 48.0}}}}};
  106. }
  107. QJsonObject state() const
  108. {
  109. return QJsonObject{{"version", "0.8.0-fixture"}, {"mode", "fixture"}, {"server_id", "qt-clean-fixture"},
  110. {"stations", QJsonArray{station()}}};
  111. }
  112. void pushState() { send(QJsonObject{{"v", 1}, {"event", "state"}, {"result", state()}}); }
  113. void log(const QString &message)
  114. {
  115. const QByteArray bytes = (message + QLatin1Char('\n')).toUtf8();
  116. const quint64 first = sequence;
  117. sequence += static_cast<quint64>(bytes.size());
  118. const QJsonObject frame{{"v", 1}, {"event", "bios_uart"}, {"result", QJsonObject{
  119. {"station_id", "S1"}, {"session_id", "fixture-uart-session"},
  120. {"offset", QString::number(first)}, {"next_offset", QString::number(sequence)},
  121. {"data_base64", QString::fromLatin1(bytes.toBase64())}}}};
  122. events.append(frame);
  123. send(frame);
  124. }
  125. void receive(const QString &text)
  126. {
  127. const QJsonObject request = QJsonDocument::fromJson(text.toUtf8()).object();
  128. const QString method = request.value("method").toString();
  129. const QJsonObject params = request.value("params").toObject();
  130. methods << method;
  131. auto respond = [this, request](const QJsonObject &result) {
  132. send(QJsonObject{{"v", 1}, {"id", request.value("id")}, {"ok", true}, {"result", result}});
  133. };
  134. if (method == "GetState") {
  135. if (step < 0 && command.value("state").toString() == "succeeded") oldFailedSnapshotSent = true;
  136. respond(state()); return;
  137. }
  138. if (method == "GetCommand") { respond(command); return; }
  139. if (method == "StartTest") {
  140. ++starts;
  141. command = QJsonObject{{"command_id", params.value("command_id")}, {"state", "queued"}, {"error", ""}};
  142. // Reply succeeds before the asynchronously refreshed state. The old
  143. // UEFI input must never finish the new client's local plan.
  144. QTimer::singleShot(250, this, [this, respond] {
  145. const qint64 confirmedStarted = QDateTime::currentMSecsSinceEpoch();
  146. command.insert("state", "succeeded");
  147. command.insert("result", QJsonObject{{"boot_id", "fixture-boot-1"},
  148. {"started_unix_ms", QString::number(confirmedStarted)}});
  149. respond(command);
  150. QTimer::singleShot(200, this, [this, confirmedStarted] {
  151. started = confirmedStarted;
  152. step = 0;
  153. log(QStringLiteral("Fixture: fresh BIOS boot confirmed"));
  154. pushState();
  155. });
  156. });
  157. return;
  158. }
  159. if (method == "StageAction") {
  160. ++actions;
  161. const QString input = params.value("input_id").toString();
  162. if (inputIds.contains(input)) duplicate = true;
  163. inputIds.insert(input);
  164. const QJsonObject memory = params.value("memory_params").toObject();
  165. if (step < 2) correctParams = correctParams && memory.value("tCL").toInt() == 45;
  166. if (step == (runAmt ? 6 : 4)) correctParams = correctParams && memory.isEmpty();
  167. const QString action = params.value("action").toString();
  168. if (action == "start_amt") ++amtLaunches;
  169. if ((step == 2 || step == 3) && action != (runAmt ? "start_amt" : "skip_amt")) unexpected = true;
  170. if (runAmt && (step == 4 || step == 5) && action == "skip_amt" && !amtResultsReady) earlyAmtExit = true;
  171. command = QJsonObject{{"command_id", params.value("command_id")}, {"state", "running"}, {"error", ""}};
  172. respond(command);
  173. if (unexpectedBootMode && step == 0) {
  174. // A new BIOS boot is observed while a submitted stage command
  175. // remains running. Failure must not be hidden by either guard.
  176. QTimer::singleShot(90, this, [this] {
  177. changedBoot = true;
  178. log(QStringLiteral("Fixture: unexpected BIOS restart during pending stage"));
  179. pushState();
  180. });
  181. return;
  182. }
  183. if (step == 0 && socket) {
  184. disconnectTest = true;
  185. socket->close();
  186. }
  187. QTimer::singleShot(90, this, [this] {
  188. ++step;
  189. command.insert("state", "succeeded");
  190. log(QStringLiteral("Fixture: stage action %1 completed").arg(actions));
  191. pushState();
  192. if (runAmt && step == 4) {
  193. QTimer::singleShot(300, this, [this] {
  194. amtResultsReady = true;
  195. log(QStringLiteral("Fixture: actual AMT algorithm results completed"));
  196. pushState();
  197. });
  198. }
  199. });
  200. return;
  201. }
  202. unexpected = true;
  203. send(QJsonObject{{"v", 1}, {"id", request.value("id")}, {"ok", false},
  204. {"error", QJsonObject{{"code", "UNEXPECTED_METHOD"}, {"message", method}}}});
  205. }
  206. };
  207. int main(int argc, char **argv)
  208. {
  209. QApplication app(argc, argv);
  210. QCommandLineParser parser;
  211. parser.addHelpOption();
  212. parser.addOption({"mode", "read-only, start, fixture, fixture-amt, fixture-fail, fixture-unexpected-boot, fixture-silent. start physically starts/reboots one host.", "mode", "read-only"});
  213. parser.addOption({"url", "Clean backend WebSocket URL", "url"});
  214. parser.addOption({"report", "Write JSON verification evidence", "path", "qt-clean-probe.json"});
  215. parser.addOption({"screenshot", "Write actual Qt window PNG", "path"});
  216. parser.addOption({"station", "Station number 1..4", "number", "1"});
  217. parser.addOption({"expect-phase", "Required final phase for a real Start", "phase", "uefi"});
  218. parser.addOption({"timeout", "Maximum seconds", "seconds", "90"});
  219. parser.process(app);
  220. const QString mode = parser.value("mode");
  221. if (mode != "read-only" && mode != "start" && mode != "fixture" && mode != "fixture-amt"
  222. && mode != "fixture-fail" && mode != "fixture-unexpected-boot" && mode != "fixture-silent") return 2;
  223. const int station = parser.value("station").toInt() - 1;
  224. if (station < 0 || station > 3) return 2;
  225. qputenv("STL_QT_DISABLE_SERIAL", "1");
  226. if (!parser.value("url").isEmpty()) qputenv("STL_QT_BACKEND_URL", parser.value("url").toUtf8());
  227. QFile style(":/style/app.qss");
  228. if (style.open(QIODevice::ReadOnly)) app.setStyleSheet(QString::fromUtf8(style.readAll()));
  229. const QStringList families = QFontDatabase().families();
  230. for (const QString &family : QStringList{"Noto Sans CJK SC", "WenQuanYi Micro Hei", "DejaVu Sans"}) {
  231. if (families.contains(family)) { app.setFont(QFont(family, 9)); break; }
  232. }
  233. QJsonObject checks;
  234. QTemporaryDir selfTestDir;
  235. const QByteArray oldPlans = qgetenv("STL_QT_PLANS_FILE");
  236. qputenv("STL_QT_PLANS_FILE", selfTestDir.filePath("local-plans.json").toUtf8());
  237. LocalPlans plans;
  238. QString planError;
  239. const bool saved = plans.saveMemoryParams(0, QJsonObject{{"tCL", 45}}, &planError);
  240. const LocalPlans reloaded;
  241. checks.insert("local_plan_saved_and_reloaded", saved && reloaded.memoryParams(0).value("tCL").toInt() == 45);
  242. checks.insert("saved_plan_arms_uip_only_for_station", reloaded.plan(0).value("uip").toObject().value("apply_memory_params").toBool()
  243. && reloaded.memoryParams(1).value("tCL").toInt() != 45);
  244. if (oldPlans.isEmpty()) qunsetenv("STL_QT_PLANS_FILE"); else qputenv("STL_QT_PLANS_FILE", oldPlans);
  245. StationStore probeStore(1);
  246. StationData data;
  247. for (const QString &name : channelNames()) data.channels.append(ChannelInfo(name, -1, ChannelInfo::Idle));
  248. probeStore.initStation(0, data);
  249. probeStore.applyChannelTemperatures(0, QByteArray(24, char(48)));
  250. checks.insert("temperature_never_implies_pass", probeStore.countState(0, ChannelInfo::Pass) == 0
  251. && probeStore.station(0).channels.first().temperature == 48.0);
  252. probeStore.setChannelState(0, 0, ChannelInfo::Fail, 48);
  253. probeStore.applyChannelTemperatures(0, QByteArray(24, char(0xff)));
  254. checks.insert("missing_temperature_preserves_fail", probeStore.station(0).channels.first().state == ChannelInfo::Fail
  255. && probeStore.station(0).channels.first().temperature < 0.0);
  256. QTemporaryDir fixtureDir;
  257. Fixture *fixture = nullptr;
  258. if (mode.startsWith("fixture")) {
  259. fixture = new Fixture(mode == "fixture-amt");
  260. fixture->failAtEnd = mode == "fixture-fail";
  261. fixture->unexpectedBootMode = mode == "fixture-unexpected-boot";
  262. fixture->silentMode = mode == "fixture-silent";
  263. if (!fixture->server.isListening()) return 2;
  264. qputenv("STL_QT_BACKEND_URL", fixture->url().toUtf8());
  265. qputenv("STL_QT_CONFIG_DIR", fixtureDir.path().toUtf8());
  266. qputenv("STL_QT_PLANS_FILE", fixtureDir.filePath("qt-plans.json").toUtf8());
  267. writeJson(fixtureDir.filePath("config.json"), QJsonObject{{"memoryParams", QJsonObject{{"tCL", 44}}},
  268. {"qtPlan", QJsonObject{{"uip", QJsonObject{{"action", "continue_uip"}, {"apply_memory_params", false}}},
  269. {"amt", QJsonObject{{"action", mode == "fixture-amt" ? "start_amt" : "skip_amt"}, {"exit_after", true}}},
  270. {"uefi", QJsonObject{{"action", "hold"}}}}}});
  271. }
  272. MainWindow window;
  273. window.resize(1920, 1080);
  274. window.show();
  275. BackendLink *backend = window.findChild<BackendLink *>();
  276. if (!backend) return 2;
  277. const qint64 begin = QDateTime::currentMSecsSinceEpoch();
  278. QJsonArray states, phases;
  279. QJsonObject latest;
  280. int snapshots = 0;
  281. int logs = 0;
  282. bool clicked = false;
  283. bool startedSuccessfully = false;
  284. bool finished = false;
  285. qint64 fixtureCompletedAt = 0;
  286. QString failure;
  287. QObject::connect(backend, &BackendLink::stationState, &app, [&](int index, const QJsonObject &state) {
  288. if (index != station) return;
  289. ++snapshots;
  290. latest = state;
  291. const QString phase = state.value("phase").toString();
  292. if (phases.isEmpty() || phases.last().toString() != phase) phases.append(phase);
  293. if (states.isEmpty() || states.last().toObject() != state) states.append(state);
  294. });
  295. QObject::connect(backend, &BackendLink::logLine, &app, [&](int index, const QString &) { if (index == station) ++logs; });
  296. QObject::connect(backend, &BackendLink::commandFinished, &app,
  297. [&](int index, const QString &method, bool success, const QString &error) {
  298. if (index != station) return;
  299. if (!success) failure = error.isEmpty() ? QStringLiteral("command failed") : error;
  300. if (method == "StartTest" && success) startedSuccessfully = true;
  301. });
  302. if (fixture && mode != "fixture-silent") {
  303. // Calling the real Start slot while offline must neither send nor start timing.
  304. QMetaObject::invokeMethod(&window, "startTest", Qt::DirectConnection, Q_ARG(int, 0));
  305. checks.insert("offline_start_does_not_start_timer", !window.store()->station(0).startTime.isValid() && fixture->starts == 0);
  306. window.findChild<QAbstractButton *>("cardDetail_0")->click();
  307. window.findChild<QLineEdit *>("paramEdit_0_2")->setText("45");
  308. window.findChild<QLineEdit *>("paramEdit_0_0")->setText("1070");
  309. window.findChild<QAbstractButton *>("btnParamSubmit")->click();
  310. checks.insert("actual_form_save_is_local", LocalPlans().memoryParams(0).value("tCL").toInt() == 45 && fixture->methods.isEmpty());
  311. window.findChild<QLineEdit *>("paramEdit_0_0")->setText("invalid");
  312. window.findChild<QAbstractButton *>("btnParamSubmit")->click();
  313. checks.insert("invalid_parameter_not_coerced_to_zero", LocalPlans().memoryParams(0).value("mem vdd").toInt() == 1070);
  314. window.store()->appendLog(0, QStringLiteral("fixture-unique-live-log"));
  315. window.findChild<QAbstractButton *>("btnBackMonitor")->click();
  316. window.findChild<QAbstractButton *>("cardDetail_1")->click();
  317. checks.insert("partial_plan_clears_other_station_form", window.findChild<QLineEdit *>("paramEdit_0_0")->text().isEmpty()
  318. && window.findChild<QLineEdit *>("paramEdit_0_2")->text() == "44");
  319. window.findChild<QAbstractButton *>("btnBackMonitor")->click();
  320. window.findChild<QAbstractButton *>("cardDetail_0")->click();
  321. checks.insert("detail_reopen_does_not_duplicate_live_log", window.findChild<QPlainTextEdit *>("detailLog")
  322. ->toPlainText().count(QStringLiteral("fixture-unique-live-log")) == 1);
  323. window.findChild<QAbstractButton *>("btnBackMonitor")->click();
  324. }
  325. auto complete = [&](const QString &reason) {
  326. if (finished) return;
  327. finished = true;
  328. checks.insert("actual_mainwindow_created", true);
  329. checks.insert("live_websocket_connected", backend->isConnected());
  330. checks.insert("clean_station_snapshot_received", snapshots > 0);
  331. checks.insert("no_command_error", failure.isEmpty());
  332. if (mode != "read-only" && mode != "fixture-silent") {
  333. checks.insert("actual_start_button_clicked", clicked);
  334. checks.insert("fresh_bios_start_confirmed", startedSuccessfully
  335. && latest.value("started_unix_ms").toVariant().toLongLong() >= begin);
  336. checks.insert("fresh_boot_id_matches_command", !backend->bootId(station).isEmpty()
  337. && latest.value("boot_id").toVariant().toString() == backend->bootId(station));
  338. checks.insert("no_firmware_execution_failure", latest.value("run_state").toString() != "failed"
  339. && latest.value("verdict").toString() != "fail");
  340. checks.insert("expected_phase_reached", latest.value("phase").toString().compare(parser.value("expect-phase"), Qt::CaseInsensitive) == 0);
  341. checks.insert("local_plan_finished_or_waiting", !backend->isBusy(station));
  342. }
  343. if (fixture && mode != "fixture-silent") {
  344. checks.insert("old_failed_snapshot_after_start_ignored", fixture->oldFailedSnapshotSent && startedSuccessfully);
  345. checks.insert("exactly_one_host_start", fixture->starts == 1);
  346. checks.insert("all_expected_prompt_actions", fixture->actions == (fixture->runAmt ? 7 : 5));
  347. checks.insert("no_duplicate_prompt_action", !fixture->duplicate);
  348. checks.insert("only_clean_methods", !fixture->unexpected);
  349. checks.insert("parameters_and_uip2_separation", fixture->correctParams);
  350. checks.insert("reconnected_without_reboot_replay", fixture->disconnectTest && fixture->connections >= 2 && fixture->starts == 1);
  351. checks.insert("unknown_channel_remains_idle", window.store()->station(0).channels.first().state == ChannelInfo::Idle);
  352. checks.insert("raw_uart_replayed_after_reconnect", fixture->uartReplays >= 2);
  353. checks.insert("missed_uart_bytes_displayed_once", window.store()->station(0).logs.join("\n")
  354. .count(QStringLiteral("Fixture: stage action 1 completed")) == 1);
  355. checks.insert("no_event_log_rpc", !fixture->methods.contains(QStringLiteral("ReadEventQueue")));
  356. if (fixture->runAmt) {
  357. checks.insert("amt_launched_once_per_cpu", fixture->amtLaunches == 2);
  358. checks.insert("amt_exits_only_after_real_result", fixture->amtResultsReady && !fixture->earlyAmtExit);
  359. }
  360. if (fixture->unexpectedBootMode) {
  361. checks.insert("unexpected_boot_failure_is_prompt", !failure.isEmpty()
  362. && reason != QStringLiteral("verification timeout")
  363. && QDateTime::currentMSecsSinceEpoch() - begin < 10000);
  364. checks.insert("unexpected_boot_stopped_with_pending_stage", fixture->changedBoot
  365. && fixture->command.value("state").toString() == "running" && !backend->isBusy(station));
  366. }
  367. }
  368. if (mode == "fixture-silent") {
  369. checks.insert("silent_websocket_was_reconnected", fixture && fixture->connections >= 2);
  370. checks.insert("state_received_after_reconnect", snapshots >= 2);
  371. checks.insert("watchdog_never_issued_host_start", fixture && fixture->starts == 0);
  372. }
  373. if (!parser.value("screenshot").isEmpty())
  374. checks.insert("screenshot_saved", window.grab().save(parser.value("screenshot")));
  375. bool pass = reason.isEmpty();
  376. for (auto i = checks.begin(); i != checks.end(); ++i) pass = pass && i.value().toBool();
  377. QJsonArray methods;
  378. if (fixture) for (const QString &method : fixture->methods) methods.append(method);
  379. const QJsonObject report{{"pass", pass}, {"mode", mode}, {"reason", reason}, {"command_error", failure},
  380. {"checks", checks}, {"url", backend->url()}, {"phase_history", phases}, {"snapshots", states},
  381. {"snapshot_count", snapshots}, {"log_count", logs}, {"fixture_methods", methods},
  382. {"command_boot_id", backend->bootId(station)}, {"elapsed_ms", QDateTime::currentMSecsSinceEpoch() - begin}};
  383. const bool written = writeJson(parser.value("report"), report);
  384. qInfo().noquote() << QJsonDocument(report).toJson(QJsonDocument::Compact);
  385. app.exit(pass && written ? 0 : 1);
  386. };
  387. QTimer poll;
  388. poll.setInterval(100);
  389. QObject::connect(&poll, &QTimer::timeout, &app, [&] {
  390. if (!failure.isEmpty()) { complete(failure); return; }
  391. if (mode == "read-only" || mode == "fixture-silent") {
  392. if (mode == "fixture-silent") {
  393. if (fixture->connections >= 2 && snapshots >= 2) complete({});
  394. return;
  395. }
  396. if (snapshots > 0 && QDateTime::currentMSecsSinceEpoch() - begin >= 3000) complete({});
  397. return;
  398. }
  399. if (!clicked && snapshots > 0) {
  400. QAbstractButton *button = window.findChild<QAbstractButton *>(QStringLiteral("startTest_%1").arg(station));
  401. if (!button || !button->isEnabled()) return;
  402. clicked = true;
  403. button->click();
  404. if (fixture) button->click(); // Duplicate immediate click must not issue a second StartTest.
  405. }
  406. if (clicked && startedSuccessfully && !backend->isBusy(station)
  407. && latest.value("phase").toString().compare(parser.value("expect-phase"), Qt::CaseInsensitive) == 0) {
  408. if (fixture) {
  409. if (!fixtureCompletedAt) fixtureCompletedAt = QDateTime::currentMSecsSinceEpoch();
  410. if (QDateTime::currentMSecsSinceEpoch() - fixtureCompletedAt < 2000) return;
  411. }
  412. complete({});
  413. }
  414. });
  415. poll.start();
  416. QTimer::singleShot(qMax(5, parser.value("timeout").toInt()) * 1000, &app,
  417. [&] { complete(QStringLiteral("verification timeout")); });
  418. const int code = app.exec();
  419. // These app-context lambdas capture locals declared after window; remove
  420. // them before stack unwinding can destroy those captured variables.
  421. QObject::disconnect(backend, nullptr, &app, nullptr);
  422. delete fixture;
  423. return code;
  424. }