From a3b4f2fee900aa9ee709f8c23e9d8bc8cfa753a7 Mon Sep 17 00:00:00 2001 From: Don Gagne Date: Tue, 6 Dec 2016 19:08:39 -0800 Subject: [PATCH] Vehicle::sendMavCommand unit test --- qgroundcontrol.pro | 2 + src/Vehicle/SendMavCommandTest.cc | 151 ++++++++++++++++++++++++++++++ src/Vehicle/SendMavCommandTest.h | 31 ++++++ src/comm/MockLink.cc | 34 +++++++ src/qgcunittest/UnitTestList.cc | 2 + 5 files changed, 220 insertions(+) create mode 100644 src/Vehicle/SendMavCommandTest.cc create mode 100644 src/Vehicle/SendMavCommandTest.h diff --git a/qgroundcontrol.pro b/qgroundcontrol.pro index 85adc4fab..c0cd800d1 100644 --- a/qgroundcontrol.pro +++ b/qgroundcontrol.pro @@ -381,6 +381,7 @@ DebugBuild { PX4FirmwarePlugin { PX4FirmwarePluginFactory { APMFirmwarePlugin { src/qgcunittest/TCPLinkTest.h \ src/qgcunittest/TCPLoopBackServer.h \ src/qgcunittest/UnitTest.h \ + src/Vehicle/SendMavCommandTest.h \ SOURCES += \ src/AnalyzeView/LogDownloadTest.cc \ @@ -409,6 +410,7 @@ DebugBuild { PX4FirmwarePlugin { PX4FirmwarePluginFactory { APMFirmwarePlugin { src/qgcunittest/TCPLoopBackServer.cc \ src/qgcunittest/UnitTest.cc \ src/qgcunittest/UnitTestList.cc \ + src/Vehicle/SendMavCommandTest.cc \ } } } } } } # Main QGC Headers and Source files diff --git a/src/Vehicle/SendMavCommandTest.cc b/src/Vehicle/SendMavCommandTest.cc new file mode 100644 index 000000000..850bcc980 --- /dev/null +++ b/src/Vehicle/SendMavCommandTest.cc @@ -0,0 +1,151 @@ +/**************************************************************************** + * + * (c) 2009-2016 QGROUNDCONTROL PROJECT + * + * QGroundControl is licensed according to the terms in the file + * COPYING.md in the root of the source code directory. + * + ****************************************************************************/ + + +#include "SendMavCommandTest.h" +#include "MultiVehicleManager.h" +#include "QGCApplication.h" + +void SendMavCommandTest::_noFailure(void) +{ + _connectMockLink(); + + MultiVehicleManager* vehicleMgr = qgcApp()->toolbox()->multiVehicleManager(); + Vehicle* vehicle = vehicleMgr->activeVehicle(); + QVERIFY(vehicle); + + vehicle->sendMavCommand(MAV_COMP_ID_ALL, MAV_CMD_USER_1, true /* showError */); + + QSignalSpy spyResult(vehicle, SIGNAL(mavCommandResult(int, int, int, int, bool))); + QCOMPARE(spyResult.wait(10000), true); + QList arguments = spyResult.takeFirst(); + QCOMPARE(arguments.count(), 5); + QCOMPARE(arguments.at(0).toInt(), vehicle->id()); + QCOMPARE(arguments.at(2).toInt(), (int)MAV_CMD_USER_1); + QCOMPARE(arguments.at(3).toInt(), (int)MAV_RESULT_ACCEPTED); + QCOMPARE(arguments.at(4).toBool(), false); +} + +void SendMavCommandTest::_failureShowError(void) +{ + // Will pop error about request failure + setExpectedMessageBox(QMessageBox::Ok); + + _connectMockLink(); + + MultiVehicleManager* vehicleMgr = qgcApp()->toolbox()->multiVehicleManager(); + Vehicle* vehicle = vehicleMgr->activeVehicle(); + QVERIFY(vehicle); + + vehicle->sendMavCommand(MAV_COMP_ID_ALL, MAV_CMD_USER_2, true /* showError */); + + QSignalSpy spyResult(vehicle, SIGNAL(mavCommandResult(int, int, int, int, bool))); + QCOMPARE(spyResult.wait(10000), true); + QList arguments = spyResult.takeFirst(); + QCOMPARE(arguments.count(), 5); + QCOMPARE(arguments.at(0).toInt(), vehicle->id()); + QCOMPARE(arguments.at(2).toInt(), (int)MAV_CMD_USER_2); + QCOMPARE(arguments.at(3).toInt(), (int)MAV_RESULT_FAILED); + QCOMPARE(arguments.at(4).toBool(), false); + + // User should have been notified + checkExpectedMessageBox(); +} + +void SendMavCommandTest::_failureNoShowError(void) +{ + _connectMockLink(); + + MultiVehicleManager* vehicleMgr = qgcApp()->toolbox()->multiVehicleManager(); + Vehicle* vehicle = vehicleMgr->activeVehicle(); + QVERIFY(vehicle); + + vehicle->sendMavCommand(MAV_COMP_ID_ALL, MAV_CMD_USER_2, false /* showError */); + + QSignalSpy spyResult(vehicle, SIGNAL(mavCommandResult(int, int, int, int, bool))); + QCOMPARE(spyResult.wait(10000), true); + QList arguments = spyResult.takeFirst(); + QCOMPARE(arguments.count(), 5); + QCOMPARE(arguments.at(0).toInt(), vehicle->id()); + QCOMPARE(arguments.at(2).toInt(), (int)MAV_CMD_USER_2); + QCOMPARE(arguments.at(3).toInt(), (int)MAV_RESULT_FAILED); + QCOMPARE(arguments.at(4).toBool(), false); +} + +void SendMavCommandTest::_noFailureAfterRetry(void) +{ + _connectMockLink(); + + MultiVehicleManager* vehicleMgr = qgcApp()->toolbox()->multiVehicleManager(); + Vehicle* vehicle = vehicleMgr->activeVehicle(); + QVERIFY(vehicle); + + vehicle->sendMavCommand(MAV_COMP_ID_ALL, MAV_CMD_USER_3, true /* showError */); + + QSignalSpy spyResult(vehicle, SIGNAL(mavCommandResult(int, int, int, int, bool))); + QCOMPARE(spyResult.wait(10000), true); + QList arguments = spyResult.takeFirst(); + QCOMPARE(arguments.count(), 5); + QCOMPARE(arguments.at(0).toInt(), vehicle->id()); + QCOMPARE(arguments.at(2).toInt(), (int)MAV_CMD_USER_3); + QCOMPARE(arguments.at(3).toInt(), (int)MAV_RESULT_ACCEPTED); + QCOMPARE(arguments.at(4).toBool(), false); +} + +void SendMavCommandTest::_failureAfterRetry(void) +{ + // Will pop error about request failure + setExpectedMessageBox(QMessageBox::Ok); + + _connectMockLink(); + + MultiVehicleManager* vehicleMgr = qgcApp()->toolbox()->multiVehicleManager(); + Vehicle* vehicle = vehicleMgr->activeVehicle(); + QVERIFY(vehicle); + + vehicle->sendMavCommand(MAV_COMP_ID_ALL, MAV_CMD_USER_4, true /* showError */); + + QSignalSpy spyResult(vehicle, SIGNAL(mavCommandResult(int, int, int, int, bool))); + QCOMPARE(spyResult.wait(10000), true); + QList arguments = spyResult.takeFirst(); + QCOMPARE(arguments.count(), 5); + QCOMPARE(arguments.at(0).toInt(), vehicle->id()); + QCOMPARE(arguments.at(2).toInt(), (int)MAV_CMD_USER_4); + QCOMPARE(arguments.at(3).toInt(), (int)MAV_RESULT_FAILED); + QCOMPARE(arguments.at(4).toBool(), false); + + // User should have been notified + checkExpectedMessageBox(); +} + +void SendMavCommandTest::_failureAfterNoReponse(void) +{ + // Will pop error about request failure + setExpectedMessageBox(QMessageBox::Ok); + + _connectMockLink(); + + MultiVehicleManager* vehicleMgr = qgcApp()->toolbox()->multiVehicleManager(); + Vehicle* vehicle = vehicleMgr->activeVehicle(); + QVERIFY(vehicle); + + vehicle->sendMavCommand(MAV_COMP_ID_ALL, MAV_CMD_USER_5, true /* showError */); + + QSignalSpy spyResult(vehicle, SIGNAL(mavCommandResult(int, int, int, int, bool))); + QCOMPARE(spyResult.wait(10000), true); + QList arguments = spyResult.takeFirst(); + QCOMPARE(arguments.count(), 5); + QCOMPARE(arguments.at(0).toInt(), vehicle->id()); + QCOMPARE(arguments.at(2).toInt(), (int)MAV_CMD_USER_5); + QCOMPARE(arguments.at(3).toInt(), (int)MAV_RESULT_FAILED); + QCOMPARE(arguments.at(4).toBool(), true); + + // User should have been notified + checkExpectedMessageBox(); +} diff --git a/src/Vehicle/SendMavCommandTest.h b/src/Vehicle/SendMavCommandTest.h new file mode 100644 index 000000000..28624573b --- /dev/null +++ b/src/Vehicle/SendMavCommandTest.h @@ -0,0 +1,31 @@ +/**************************************************************************** + * + * (c) 2009-2016 QGROUNDCONTROL PROJECT + * + * QGroundControl is licensed according to the terms in the file + * COPYING.md in the root of the source code directory. + * + ****************************************************************************/ + + +#ifndef SendMavCommandTest_H +#define SendMavCommandTest_H + +#include "UnitTest.h" + +class SendMavCommandTest : public UnitTest +{ + Q_OBJECT + +private slots: + void _noFailure(void); + void _failureShowError(void); + void _failureNoShowError(void); + void _noFailureAfterRetry(void); + void _failureAfterRetry(void); + void _failureAfterNoReponse(void); + +private: +}; + +#endif diff --git a/src/comm/MockLink.cc b/src/comm/MockLink.cc index 4509928d7..411287c4d 100644 --- a/src/comm/MockLink.cc +++ b/src/comm/MockLink.cc @@ -807,6 +807,40 @@ void MockLink::_handleCommandLong(const mavlink_message_t& msg) commandResult = MAV_RESULT_ACCEPTED; _respondWithAutopilotVersion(); break; + case MAV_CMD_USER_1: + // Test command which always returns MAV_RESULT_ACCEPTED + commandResult = MAV_RESULT_ACCEPTED; + break; + case MAV_CMD_USER_2: + // Test command which always returns MAV_RESULT_FAILED + commandResult = MAV_RESULT_FAILED; + break; + case MAV_CMD_USER_3: + // Test command which returns MAV_RESULT_ACCEPTED on second attempt + static bool firstCmdUser3 = true; + if (firstCmdUser3) { + firstCmdUser3 = false; + return; + } else { + firstCmdUser3 = true; + commandResult = MAV_RESULT_ACCEPTED; + } + break; + case MAV_CMD_USER_4: + // Test command which returns MAV_RESULT_FAILED on second attempt + static bool firstCmdUser4 = true; + if (firstCmdUser4) { + firstCmdUser4 = false; + return; + } else { + firstCmdUser4 = true; + commandResult = MAV_RESULT_FAILED; + } + break; + case MAV_CMD_USER_5: + // No response + return; + break; } mavlink_message_t commandAck; diff --git a/src/qgcunittest/UnitTestList.cc b/src/qgcunittest/UnitTestList.cc index d0c70f8a7..924ae4024 100644 --- a/src/qgcunittest/UnitTestList.cc +++ b/src/qgcunittest/UnitTestList.cc @@ -31,6 +31,7 @@ #include "ParameterManagerTest.h" #include "MissionCommandTreeTest.h" #include "LogDownloadTest.h" +#include "SendMavCommandTest.h" UT_REGISTER_TEST(FactSystemTestGeneric) UT_REGISTER_TEST(FactSystemTestPX4) @@ -50,6 +51,7 @@ UT_REGISTER_TEST(TCPLinkTest) UT_REGISTER_TEST(ParameterManagerTest) UT_REGISTER_TEST(MissionCommandTreeTest) UT_REGISTER_TEST(LogDownloadTest) +UT_REGISTER_TEST(SendMavCommandTest) // List of unit test which are currently disabled. // If disabling a new test, include reason in comment. -- 2.22.0