#include "../src/tcpcalibrationwidget.h" #include "../src/tcptoolstore.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Qt 5.12.4 QMessageBox::showEvent dereferences the Windows native interface, // which the offscreen plugin does not provide. Skip only that show hook in // isolated tests; exec(), the standard buttons and their return values remain real. class OffscreenDialogFilter : public QObject { public: using QObject::QObject; bool eventFilter(QObject *object, QEvent *event) override { return QGuiApplication::platformName() == QStringLiteral("offscreen") && event->type() == QEvent::Show && qobject_cast(object); } }; class TcpWorkflowTest : public QObject { Q_OBJECT QTemporaryDir settingsDir; const QString server = QStringLiteral("test-server:50052"); const QString arm = QStringLiteral("aubo_test"); static TcpCalibration::Pose flange(int i) { const double angles[][3] = {{0,0,0},{0.8,0.1,-0.3},{-0.4,0.9,0.5},{0.2,-0.7,1.1}}; TcpCalibration::Pose pose{{0,0,0,angles[i][0],angles[i][1],angles[i][2]}}; TcpCalibration::Matrix matrix{}; TcpCalibration::poseMatrix(pose, matrix); const double tcp[] = {.03,-.02,.15}, fixed[] = {.4,-.2,.6}; for (int row=0; row<3; ++row) { pose[size_t(row)] = fixed[row]; for (int col=0; col<3; ++col) pose[size_t(row)] -= matrix[size_t(4*row+col)] * tcp[col]; } return pose; } static QVariantMap poseMap(const TcpCalibration::Pose &pose) { const char *names[] = {"x","y","z","rx","ry","rz"}; QVariantMap result; for (int i=0;i<6;++i) result.insert(QString::fromLatin1(names[i]), pose[size_t(i)]); return result; } static void dismissDialog() { QTimer::singleShot(100, [] { for (QWidget *widget : QApplication::topLevelWidgets()) if (auto *box = qobject_cast(widget)) { auto *button = box->button(QMessageBox::Yes); if (!button) button = box->button(QMessageBox::Ok); if (button) button->click(); } }); } static QPushButton *calculate(TcpCalibrationWidget &widget) { for (auto *button : widget.findChildren()) if (button->property("tcpAction").toString() == QStringLiteral("calculate")) return button; return nullptr; } void ready(TcpCalibrationWidget &widget) { widget.resize(1250,1100); widget.show(); QTest::qWait(20); widget.setContext(server, arm, true, false); widget.findChild(QStringLiteral("tcpSourceConfirmed"))->setChecked(true); } void capture(TcpCalibrationWidget &widget, int row, const TcpCalibration::Pose &pose) { QSignalSpy requests(&widget, &TcpCalibrationWidget::captureRequested); auto *button = widget.findChild(QStringLiteral("tcpCapture%1").arg(row)); QVERIFY(button && button->isEnabled()); button->click(); QCOMPARE(requests.size(), 1); widget.acceptPose(requests.at(0).at(1).toULongLong(), arm, poseMap(pose), {{"connected",true},{"moving",false},{"fault",false}}, true, QString()); } private slots: void controllerOffsetReadIsScopedAndValidated() { TcpCalibrationWidget widget; widget.setContext(server, arm, true, false); auto *read = widget.findChild("tcpReadControllerOffset"); QVERIFY(read); QSignalSpy requests(&widget, SIGNAL(controllerOffsetRequested(QString,quint64))); read->click(); QCOMPARE(requests.count(), 1); const quint64 request = requests.at(0).at(1).toULongLong(); const QVariantMap offset = poseMap({{.012, -.023, .145, .1, -.2, .3}}); QVERIFY(QMetaObject::invokeMethod(&widget, "acceptControllerOffset", Q_ARG(quint64, request), Q_ARG(QString, arm), Q_ARG(QVariantMap, offset), Q_ARG(bool, true), Q_ARG(QString, QString()))); QVERIFY(widget.referenceConfirmed()); QVERIFY(std::abs(widget.activeOffset()[0] - .012) < 1e-8); QVERIFY(std::abs(widget.activeOffset()[5] - .3) < 1e-6); read->click(); const quint64 stale = requests.last().at(1).toULongLong(); widget.setContext(server, "other_arm", true, false); QVERIFY(QMetaObject::invokeMethod(&widget, "acceptControllerOffset", Q_ARG(quint64, stale), Q_ARG(QString, arm), Q_ARG(QVariantMap, offset), Q_ARG(bool, true), Q_ARG(QString, QString()))); QVERIFY(!widget.referenceConfirmed()); } void initTestCase() { qApp->installEventFilter(new OffscreenDialogFilter(qApp)); QVERIFY(settingsDir.isValid()); QCoreApplication::setOrganizationName(QStringLiteral("CMVR-TCP-Isolated-Test")); QCoreApplication::setApplicationName(QStringLiteral("Workflow")); QSettings::setDefaultFormat(QSettings::IniFormat); QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, settingsDir.path()); QFile style(QFINDTESTDATA("../styles.qss")); QVERIFY(style.open(QIODevice::ReadOnly)); qApp->setStyleSheet(QString::fromUtf8(style.readAll())); } void init() { QSettings settings; settings.clear(); settings.sync(); } void persistenceIsolationAndRestart() { TcpToolStore::Tool tool{QStringLiteral("工具一"), {{.03,-.02,.15,.1,-.2,.3}}, .12, 6}; QString error; { QSettings settings; QVERIFY2(TcpToolStore::save(settings,server,arm,tool,false,error), qPrintable(error)); } QSettings reloaded; TcpToolStore::Snapshot snapshot; QVERIFY(TcpToolStore::load(reloaded,server,arm,snapshot,error)); QCOMPARE(snapshot.tools.size(),1); QCOMPARE(snapshot.current,tool.name); QCOMPARE(snapshot.tools.first().offset[5],.3); QVERIFY(TcpToolStore::load(reloaded,server,QStringLiteral("other-arm"),snapshot,error)); QVERIFY(snapshot.tools.isEmpty()); QVERIFY(TcpToolStore::load(reloaded,QStringLiteral("other-server"),arm,snapshot,error)); QVERIFY(snapshot.tools.isEmpty()); QVERIFY(!TcpToolStore::save(reloaded,server,arm,tool,false,error)); tool.offset[0] = .04; QVERIFY(TcpToolStore::save(reloaded,server,arm,tool,true,error)); QVERIFY(TcpToolStore::load(reloaded,server,arm,snapshot,error)); QCOMPARE(snapshot.tools.first().offset[0],.04); QVERIFY(TcpToolStore::remove(reloaded,server,arm,tool.name,error)); QVERIFY(TcpToolStore::load(reloaded,server,arm,snapshot,error)); QVERIFY(snapshot.tools.isEmpty()); } void invalidStorageAndWriteFailure() { QSettings settings; QString error; TcpToolStore::Tool tool{QStringLiteral("tool"), {{0,0,.1,0,0,0}}, .2, 4}; tool.rmsMm = 2.001; QVERIFY(!TcpToolStore::save(settings,server,arm,tool,false,error)); tool.rmsMm = .2; tool.name.clear(); QVERIFY(!TcpToolStore::save(settings,server,arm,tool,false,error)); tool.name = QStringLiteral("tool"); tool.offset[0] = std::numeric_limits::quiet_NaN(); QVERIFY(!TcpToolStore::save(settings,server,arm,tool,false,error)); tool.offset[0] = 0; QVERIFY(TcpToolStore::save(settings,server,arm,tool,false,error)); const QString key = settings.allKeys().first(); QVariantMap bad = settings.value(key).toMap(); bad[QStringLiteral("tools")] = QStringLiteral("not a list"); settings.setValue(key,bad); settings.sync(); TcpToolStore::Snapshot snapshot; QVERIFY(!TcpToolStore::load(settings,server,arm,snapshot,error)); QVERIFY(!TcpToolStore::save(settings,server,arm,tool,true,error)); QSettings unwritable(settingsDir.path(),QSettings::IniFormat); // directory, not writable as file QVERIFY(!TcpToolStore::save(unwritable,server,arm,tool,false,error)); } void captureSolveSaveReloadAndClear() { TcpCalibrationWidget widget; ready(widget); QSignalSpy changed(&widget,&TcpCalibrationWidget::toolsChanged); widget.findChild(QStringLiteral("tcpToolName"))->setText(QStringLiteral("探针")); for (int i=0;i<4;++i) capture(widget,i,flange(i)); QVERIFY(calculate(widget)->isEnabled()); calculate(widget)->click(); QCOMPARE(widget.findChild(QStringLiteral("tcpResult0"))->text(),QStringLiteral("30.000")); auto *save = widget.findChild(QStringLiteral("tcpSave")); QVERIFY(save->isEnabled()); dismissDialog(); save->click(); QCOMPARE(changed.size(),1); QCOMPARE(changed.first().at(0).toString(),server); QCOMPARE(changed.first().at(1).toString(),arm); QCOMPARE(changed.first().at(2).toString(),QStringLiteral("探针")); QVERIFY(widget.referenceConfirmed()); // saving does not change the robot's active offset TcpCalibrationWidget reopened; reopened.setContext(server,arm,false,false); QCOMPARE(reopened.findChild(QStringLiteral("tcpLocalTools"))->currentText(),QStringLiteral("探针")); QCOMPARE(reopened.findChild(QStringLiteral("tcpResult2"))->text(),QStringLiteral("150.000")); reopened.setContext(server,QString(),false,false); QCOMPARE(reopened.findChild(QStringLiteral("tcpLocalTools"))->count(),1); QVERIFY(reopened.findChild(QStringLiteral("tcpToolName"))->text().isEmpty()); reopened.setContext(server,arm,false,false); QCOMPARE(reopened.findChild(QStringLiteral("tcpResult2"))->text(),QStringLiteral("150.000")); reopened.findChild(QStringLiteral("tcpClear"))->click(); QVERIFY(!reopened.findChild(QStringLiteral("tcpSave"))->isEnabled()); QSettings settings; TcpToolStore::Snapshot snapshot; QString error; QVERIFY(TcpToolStore::load(settings,server,arm,snapshot,error)); QCOMPARE(snapshot.tools.size(),1); QSignalSpy deleted(&reopened,&TcpCalibrationWidget::toolsChanged); dismissDialog(); reopened.findChild(QStringLiteral("tcpDelete"))->click(); QCOMPARE(deleted.size(),1); QCOMPARE(deleted.first().at(1).toString(),arm); QVERIFY(deleted.first().at(2).toString().isEmpty()); QVERIFY(TcpToolStore::load(settings,server,arm,snapshot,error)); QVERIFY(snapshot.tools.isEmpty()); } void activeTcpRotationConversion() { TcpCalibrationWidget widget; ready(widget); const TcpCalibration::Pose serverOffset{{.01,-.02,.12,.2,-.3,.4}}; for (int i=0;i<6;++i) widget.findChild(QStringLiteral("tcpSource%1").arg(i)) ->setValue(i<3 ? serverOffset[size_t(i)]*1000 : serverOffset[size_t(i)]*180/3.141592653589793); widget.findChild(QStringLiteral("tcpSourceConfirmed"))->setChecked(true); for (int i=0;i<4;++i) { TcpCalibration::Pose feedback{}; QVERIFY(TcpCalibration::toolPose(flange(i),serverOffset,feedback)); capture(widget,i,feedback); } calculate(widget)->click(); QCOMPARE(widget.findChild(QStringLiteral("tcpResult0"))->text(),QStringLiteral("30.000")); QCOMPARE(widget.findChild(QStringLiteral("tcpResult2"))->text(),QStringLiteral("150.000")); } void lateResponsesAndDeviceChanges() { TcpCalibrationWidget widget; ready(widget); QSignalSpy requests(&widget,&TcpCalibrationWidget::captureRequested); widget.findChild(QStringLiteral("tcpCapture0"))->click(); const auto token = requests.at(0).at(1).toULongLong(); widget.findChild(QStringLiteral("tcpClear"))->click(); widget.acceptPose(token,arm,poseMap(flange(0)),{{"connected",true},{"moving",false},{"fault",false}},true,{}); auto *table = widget.findChild(QStringLiteral("tcpSamples")); QCOMPARE(table->item(0,1)->text(),QStringLiteral("未采集")); capture(widget,0,flange(0)); widget.setContext(server,QStringLiteral("other-arm"),true,false); QCOMPARE(table->item(0,1)->text(),QStringLiteral("未采集")); QVERIFY(!widget.findChild(QStringLiteral("tcpSourceConfirmed"))->isChecked()); } void recaptureDuplicateFailureAndSourceReset() { TcpCalibrationWidget widget; ready(widget); capture(widget,0,flange(0)); dismissDialog(); capture(widget,1,flange(0)); auto *table = widget.findChild(QStringLiteral("tcpSamples")); QCOMPARE(table->item(1,1)->text(),QStringLiteral("未采集")); capture(widget,0,flange(1)); QCOMPARE(table->item(0,1)->text(),QStringLiteral("已采集")); QSignalSpy requests(&widget,&TcpCalibrationWidget::captureRequested); widget.findChild(QStringLiteral("tcpCapture1"))->click(); dismissDialog(); widget.acceptPose(requests.at(0).at(1).toULongLong(),arm,{}, {},false,QStringLiteral("test unavailable")); QCOMPARE(table->item(1,1)->text(),QStringLiteral("未采集")); widget.findChild(QStringLiteral("tcpSource0"))->setValue(1); QCOMPARE(table->item(0,1)->text(),QStringLiteral("未采集")); QVERIFY(!widget.findChild(QStringLiteral("tcpCapture0"))->isEnabled()); } void highErrorCannotSave() { TcpCalibrationWidget widget; ready(widget); widget.findChild(QStringLiteral("tcpToolName"))->setText(QStringLiteral("bad")); for (int i=0;i<4;++i) { auto pose=flange(i); if (i==3) pose[0]+=.03; capture(widget,i,pose); } calculate(widget)->click(); QVERIFY(widget.findChild(QStringLiteral("tcpRms"))->text().contains(QStringLiteral("超限"))); QVERIFY(!widget.findChild(QStringLiteral("tcpSave"))->isEnabled()); } void invalidAndFailedRecaptureCannotReuseOldPoint() { TcpCalibrationWidget widget; ready(widget); capture(widget,0,flange(0)); QSignalSpy requests(&widget,&TcpCalibrationWidget::captureRequested); widget.findChild(QStringLiteral("tcpCapture0"))->click(); auto invalid = poseMap(flange(0)); invalid.remove(QStringLiteral("rx")); dismissDialog(); widget.acceptPose(requests.last().at(1).toULongLong(),arm,invalid, {{"connected",true},{"moving",false},{"fault",false}},true,{}); auto *table=widget.findChild(QStringLiteral("tcpSamples")); QCOMPARE(table->item(0,1)->text(),QStringLiteral("未采集")); QVERIFY(!calculate(widget)->isEnabled()); widget.findChild(QStringLiteral("tcpCapture0"))->click(); dismissDialog(); widget.acceptPose(requests.last().at(1).toULongLong(),arm,poseMap(flange(0)), {{"connected",true},{"moving",true},{"fault",false}},true,{}); QCOMPARE(table->item(0,1)->text(),QStringLiteral("未采集")); } void reconnectAndMethodInvalidation() { TcpCalibrationWidget widget; ready(widget); for(int i=0;i<4;++i) capture(widget,i,flange(i)); calculate(widget)->click(); auto *method=widget.findChild(QStringLiteral("tcpCalibrationMethod")); QVERIFY(method); QCOMPARE(method->count(),3); method->setCurrentIndex(method->findData(QStringLiteral("six_point_xz"))); QVERIFY(!calculate(widget)->isEnabled()); QCOMPARE(widget.findChild(QStringLiteral("tcpResult0"))->text(),QStringLiteral("--")); auto *table=widget.findChild(QStringLiteral("tcpSamples")); QCOMPARE(table->rowCount(),6); for(int row=0;row<6;++row) QCOMPARE(table->item(row,1)->text(),QStringLiteral("未采集")); QSignalSpy requests(&widget,&TcpCalibrationWidget::captureRequested); widget.findChild(QStringLiteral("tcpCapture0"))->click(); const auto staleToken=requests.last().at(1).toULongLong(); QVERIFY(!method->isEnabled()); // A programmatic context change must invalidate an outstanding reply, // too, even though interactive method changes are disabled while busy. method->setCurrentIndex(method->findData(QStringLiteral("four_point"))); widget.acceptPose(staleToken,arm,poseMap(flange(0)), {{"connected",true},{"moving",false},{"fault",false}},true,{}); QCOMPARE(table->rowCount(),4); QCOMPARE(table->item(0,1)->text(),QStringLiteral("未采集")); widget.findChild(QStringLiteral("tcpCapture0"))->click(); widget.setContext(server,arm,false,false); ready(widget); widget.acceptPose(requests.last().at(1).toULongLong(),arm,poseMap(flange(0)), {{"connected",true},{"moving",false},{"fault",false}},true,{}); QCOMPARE(widget.findChild(QStringLiteral("tcpSamples"))->item(0,1)->text(),QStringLiteral("未采集")); } void sixPointCaptureSaveReload_data() { QTest::addColumn("xz"); QTest::newRow("XZ") << true; QTest::newRow("XY") << false; } void sixPointCaptureSaveReload() { QFETCH(bool,xz); TcpCalibrationWidget widget; ready(widget); const QString methodId=xz?QStringLiteral("six_point_xz"):QStringLiteral("six_point_xy"); auto *method=widget.findChild(QStringLiteral("tcpCalibrationMethod")); QVERIFY(method); for(int i=3;i<6;++i) QVERIFY(!widget.findChild(QStringLiteral("tcpOrientation%1").arg(i))->isReadOnly()); method->setCurrentIndex(method->findData(methodId)); auto *table=widget.findChild(QStringLiteral("tcpSamples")); QCOMPARE(table->rowCount(),6); for(int i=3;i<6;++i) QVERIFY(widget.findChild(QStringLiteral("tcpOrientation%1").arg(i))->isReadOnly()); widget.findChild(QStringLiteral("tcpToolName"))->setText(QStringLiteral("六点探针")); // Base-frame tool axes: a known proper orthonormal basis, not aligned // with either the base or the nonidentity direction-origin flange. const double axes[3][3]={{2./3,-2./3,1./3},{1./3,2./3,2./3},{-2./3,-1./3,2./3}}; std::vector poses{flange(0),flange(1),flange(2),flange(1),flange(1),flange(1)}; for(int row=0;row<3;++row) { poses[4][row]+=.3*axes[0][row]; poses[5][row]+=.35*axes[xz?2:1][row]; } for(int i=0;i<6;++i) capture(widget,i,poses[size_t(i)]); QVERIFY(calculate(widget)->isEnabled()); calculate(widget)->click(); QCOMPARE(widget.findChild(QStringLiteral("tcpResult0"))->text(),QStringLiteral("30.000")); QCOMPARE(widget.findChild(QStringLiteral("tcpResult2"))->text(),QStringLiteral("150.000")); for(int row=0;row<3;++row) QCOMPARE(table->item(row,8)->text(),QStringLiteral("0.000")); for(int row=3;row<6;++row) QCOMPARE(table->item(row,8)->text(),QStringLiteral("--")); auto *save=widget.findChild(QStringLiteral("tcpSave")); QVERIFY(save->isEnabled()); dismissDialog(); save->click(); QSettings settings; TcpToolStore::Snapshot snapshot; QString error; QVERIFY2(TcpToolStore::load(settings,server,arm,snapshot,error),qPrintable(error)); QCOMPARE(snapshot.tools.size(),1); const auto tool=snapshot.tools.first(); QCOMPARE(tool.method,methodId); QCOMPARE(tool.sampleCount,6); QVERIFY(tool.rmsMm<1e-8); TcpCalibration::Pose baseTool{}; TcpCalibration::Matrix rotation{}; QVERIFY(TcpCalibration::toolPose(poses[3],tool.offset,baseTool)); QVERIFY(TcpCalibration::poseMatrix(baseTool,rotation)); for(int axis=0;axis<3;++axis) for(int row=0;row<3;++row) QVERIFY(std::abs(rotation[row*4+axis]-axes[axis][row])<1e-10); TcpCalibrationWidget reopened; reopened.setContext(server,arm,false,false); QCOMPARE(reopened.findChild(QStringLiteral("tcpCalibrationMethod"))->currentData().toString(),methodId); for(int i=3;i<6;++i) { auto *angle=reopened.findChild(QStringLiteral("tcpOrientation%1").arg(i)); QVERIFY(angle->isReadOnly()); QVERIFY(std::abs(angle->value()-tool.offset[i]*180/3.141592653589793)<.000051); } widget.resize(1250,1100); widget.show(); QTest::qWait(20); QVERIFY(widget.grab().save(QStringLiteral("tcp-six-%1.png").arg(xz?"xz":"xy"))); } void storageLegacyMethodAndInvalidMethods() { QSettings settings; QString error; TcpToolStore::Snapshot snapshot; TcpToolStore::Tool tool{QStringLiteral("legacy"),{{.01,.02,.1,.1,.2,.3}},.1,5}; QVERIFY(TcpToolStore::save(settings,server,arm,tool,false,error)); const QString key=settings.allKeys().first(); auto value=settings.value(key).toMap(); auto records=value.value(QStringLiteral("tools")).toList(); auto record=records.first().toMap(); record.remove(QStringLiteral("method")); records[0]=record; value[QStringLiteral("tools")]=records; settings.setValue(key,value); settings.sync(); QVERIFY(TcpToolStore::load(settings,server,arm,snapshot,error)); QCOMPARE(snapshot.tools.first().method,QStringLiteral("fixed_point")); QCOMPARE(snapshot.tools.first().sampleCount,5); QCOMPARE(snapshot.tools.first().offset[5],.3); TcpCalibrationWidget widget; widget.setContext(server,arm,false,false); QVERIFY(!widget.findChild(QStringLiteral("tcpOrientation5"))->isReadOnly()); tool.method=QStringLiteral("unknown"); QVERIFY(!TcpToolStore::save(settings,server,arm,tool,true,error)); tool.method=QStringLiteral("six_point_xz"); QVERIFY(!TcpToolStore::save(settings,server,arm,tool,true,error)); tool.method=QStringLiteral("four_point"); QVERIFY(!TcpToolStore::save(settings,server,arm,tool,true,error)); record[QStringLiteral("method")]=QStringLiteral("unknown"); records[0]=record; value[QStringLiteral("tools")]=records; settings.setValue(key,value); settings.sync(); QVERIFY(!TcpToolStore::load(settings,server,arm,snapshot,error)); QVERIFY(!error.isEmpty()); } void demoCannotCaptureAndRender() { TcpCalibrationWidget widget; widget.setContext(server,arm,true,true); QVERIFY(!widget.findChild(QStringLiteral("tcpCapture0"))->isEnabled()); ready(widget); widget.findChild(QStringLiteral("tcpToolName"))->setText(QStringLiteral("探针 TCP")); for(int i=0;i<4;++i) capture(widget,i,flange(i)); calculate(widget)->click(); widget.resize(1250,1100); widget.show(); QTest::qWait(50); QVERIFY(widget.grab().save(QStringLiteral("tcp-page.png"))); } void neutralUiAndPerRobotTools() { TcpCalibrationWidget widget; ready(widget); for(auto *label:widget.findChildren()) { QVERIFY(!label->text().contains(QStringLiteral("AUBO"))); QVERIFY(!label->text().contains(server)); QVERIFY(!label->text().contains(arm)); } QSettings settings; QString error; TcpToolStore::Tool a{QStringLiteral("同名工具"),{{.01,0,.1,0,0,0}},.1,4}; auto b=a; b.offset[0]=.02; QVERIFY(TcpToolStore::save(settings,server,arm,a,false,error)); QVERIFY(TcpToolStore::save(settings,server,"arm-b",b,false,error)); widget.setContext(server,"arm-b",true,false); QCOMPARE(widget.findChild("tcpResult0")->text(),QStringLiteral("20.000")); widget.setContext(server,arm,true,false); QCOMPARE(widget.findChild("tcpResult0")->text(),QStringLiteral("10.000")); widget.setContext("another-server",arm,true,false); QCOMPARE(widget.findChild("tcpLocalTools")->count(),1); QVERIFY(TcpToolStore::select(settings,server,arm,QString(),error)); TcpToolStore::Snapshot snapshot; QVERIFY(TcpToolStore::load(settings,server,arm,snapshot,error)); QVERIFY(snapshot.current.isEmpty()); QCOMPARE(snapshot.tools.size(),1); } }; QTEST_MAIN(TcpWorkflowTest) #include "tcpworkflow_test.moc"