MissionControllerTest.cc 8.56 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

10 11 12 13

#include "MissionControllerTest.h"
#include "LinkManager.h"
#include "MultiVehicleManager.h"
14
#include "SimpleMissionItem.h"
Don Gagne's avatar
Don Gagne committed
15
#include "MissionSettingsItem.h"
16 17 18
#include "QGCApplication.h"
#include "SettingsManager.h"
#include "AppSettings.h"
19 20

MissionControllerTest::MissionControllerTest(void)
21 22 23
    : _multiSpyMissionController(nullptr)
    , _multiSpyMissionItem(nullptr)
    , _missionController(nullptr)
24 25 26 27 28 29
{
    
}

void MissionControllerTest::cleanup(void)
{
30
    delete _masterController;
31
    _masterController = nullptr;
32

Don Gagne's avatar
Don Gagne committed
33
    delete _multiSpyMissionController;
34
    _multiSpyMissionController = nullptr;
Don Gagne's avatar
Don Gagne committed
35 36

    delete _multiSpyMissionItem;
37
    _multiSpyMissionItem = nullptr;
Don Gagne's avatar
Don Gagne committed
38

39 40 41 42 43 44 45
    MissionControllerManagerTest::cleanup();
}

void MissionControllerTest::_initForFirmwareType(MAV_AUTOPILOT firmwareType)
{
    MissionControllerManagerTest::_initForFirmwareType(firmwareType);

Don Gagne's avatar
Don Gagne committed
46 47
    // VisualMissionItem signals
    _rgVisualItemSignals[coordinateChangedSignalIndex] = SIGNAL(coordinateChanged(const QGeoCoordinate&));
Don Gagne's avatar
Don Gagne committed
48 49

    // MissionController signals
50
    _rgMissionControllerSignals[visualItemsChangedSignalIndex] =    SIGNAL(visualItemsChanged());
51

52 53 54 55
    // Master controller pulls offline vehicle info from settings
    qgcApp()->toolbox()->settingsManager()->appSettings()->offlineEditingFirmwareType()->setRawValue(firmwareType);
    _masterController = new PlanMasterController(this);
    _missionController = _masterController->missionController();
56

Don Gagne's avatar
Don Gagne committed
57 58 59
    _multiSpyMissionController = new MultiSignalSpy();
    Q_CHECK_PTR(_multiSpyMissionController);
    QCOMPARE(_multiSpyMissionController->init(_missionController, _rgMissionControllerSignals, _cMissionControllerSignals), true);
60

61
    _masterController->start(false /* flyView */);
62 63

    // All signals should some through on start
Don Gagne's avatar
Don Gagne committed
64
    QCOMPARE(_multiSpyMissionController->checkOnlySignalsByMask(visualItemsChangedSignalMask), true);
Don Gagne's avatar
Don Gagne committed
65
    _multiSpyMissionController->clearAllSignals();
66

67 68
    QmlObjectListModel* visualItems = _missionController->visualItems();
    QVERIFY(visualItems);
69 70

    // Empty vehicle only has home position
71
    QCOMPARE(visualItems->count(), 1);
72

Don Gagne's avatar
Don Gagne committed
73 74 75 76 77 78
    // Mission Settings should be in first slot
    MissionSettingsItem* settingsItem = visualItems->value<MissionSettingsItem*>(0);
    QVERIFY(settingsItem);

    // Offline vehicle, so no home position
    QCOMPARE(settingsItem->coordinate().isValid(), false);
79

Don Gagne's avatar
Don Gagne committed
80 81
    // Empty mission, so no child items possible
    QCOMPARE(settingsItem->childItems()->count(), 0);
82 83 84 85 86 87 88 89 90 91 92 93 94 95

    // No waypoint lines
    QmlObjectListModel* waypointLines = _missionController->waypointLines();
    QVERIFY(waypointLines);
    QCOMPARE(waypointLines->count(), 0);
}

void MissionControllerTest::_testEmptyVehicleWorker(MAV_AUTOPILOT firmwareType)
{
    _initForFirmwareType(firmwareType);

    // FYI: A significant amount of empty vehicle testing is in _initForFirmwareType since that
    // sets up an empty vehicle

96 97
    QmlObjectListModel* visualItems = _missionController->visualItems();
    QVERIFY(visualItems);
Don Gagne's avatar
Don Gagne committed
98 99
    VisualMissionItem* visualItem = visualItems->value<VisualMissionItem*>(0);
    QVERIFY(visualItem);
100

Don Gagne's avatar
Don Gagne committed
101
    _setupVisualItemSignals(visualItem);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
}

void MissionControllerTest::_testEmptyVehiclePX4(void)
{
    _testEmptyVehicleWorker(MAV_AUTOPILOT_PX4);
}

void MissionControllerTest::_testEmptyVehicleAPM(void)
{
    _testEmptyVehicleWorker(MAV_AUTOPILOT_ARDUPILOTMEGA);
}

void MissionControllerTest::_testAddWaypointWorker(MAV_AUTOPILOT firmwareType)
{
    _initForFirmwareType(firmwareType);

    QGeoCoordinate coordinate(37.803784, -122.462276);

120
    _missionController->insertSimpleMissionItem(coordinate, _missionController->visualItems()->count());
121

122 123
    QmlObjectListModel* visualItems = _missionController->visualItems();
    QVERIFY(visualItems);
124

125
    QCOMPARE(visualItems->count(), 2);
126

Don Gagne's avatar
Don Gagne committed
127 128 129 130
    MissionSettingsItem* settingsItem = visualItems->value<MissionSettingsItem*>(0);
    SimpleMissionItem* simpleItem = visualItems->value<SimpleMissionItem*>(1);
    QVERIFY(settingsItem);
    QVERIFY(simpleItem);
131

132
    QCOMPARE((MAV_CMD)simpleItem->command(), MAV_CMD_NAV_TAKEOFF);
Don Gagne's avatar
Don Gagne committed
133
    QCOMPARE(simpleItem->childItems()->count(), 0);
134

135 136
    // Planned home position should always be set after first item
    QVERIFY(settingsItem->coordinate().isValid());
137

Don Gagne's avatar
Don Gagne committed
138 139 140 141 142
    // ArduPilot takeoff command has no coordinate, so should be child item
    QCOMPARE(settingsItem->childItems()->count(), firmwareType == MAV_AUTOPILOT_ARDUPILOTMEGA ? 1 : 0);

    // Check waypoint line from home to takeoff
    int expectedLineCount = firmwareType == MAV_AUTOPILOT_ARDUPILOTMEGA ? 0 : 1;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    QmlObjectListModel* waypointLines = _missionController->waypointLines();
    QVERIFY(waypointLines);
    QCOMPARE(waypointLines->count(), expectedLineCount);
}

void MissionControllerTest::_testAddWayppointAPM(void)
{
    _testAddWaypointWorker(MAV_AUTOPILOT_ARDUPILOTMEGA);
}


void MissionControllerTest::_testAddWayppointPX4(void)
{
    _testAddWaypointWorker(MAV_AUTOPILOT_PX4);
}
Don Gagne's avatar
Don Gagne committed
158

159
#if 0
Don Gagne's avatar
Don Gagne committed
160 161 162 163 164
void MissionControllerTest::_testOfflineToOnlineWorker(MAV_AUTOPILOT firmwareType)
{
    // Start offline and add item
    _missionController = new MissionController();
    Q_CHECK_PTR(_missionController);
165
    _missionController->start(false /* flyView */);
166
    _missionController->insertSimpleMissionItem(QGeoCoordinate(37.803784, -122.462276), _missionController->visualItems()->count());
Don Gagne's avatar
Don Gagne committed
167 168 169 170

    // Go online to empty vehicle
    MissionControllerManagerTest::_initForFirmwareType(firmwareType);

Don Gagne's avatar
Don Gagne committed
171 172 173 174 175
#if 1
    // Due to current limitations, offline items will go away
    QCOMPARE(_missionController->visualItems()->count(), 1);
#else
    //Make sure our offline mission items are still there
176
    QCOMPARE(_missionController->visualItems()->count(), 2);
Don Gagne's avatar
Don Gagne committed
177
#endif
Don Gagne's avatar
Don Gagne committed
178 179 180 181 182 183 184 185 186 187 188
}

void MissionControllerTest::_testOfflineToOnlineAPM(void)
{
    _testOfflineToOnlineWorker(MAV_AUTOPILOT_ARDUPILOTMEGA);
}

void MissionControllerTest::_testOfflineToOnlinePX4(void)
{
    _testOfflineToOnlineWorker(MAV_AUTOPILOT_PX4);
}
189
#endif
Don Gagne's avatar
Don Gagne committed
190

Don Gagne's avatar
Don Gagne committed
191
void MissionControllerTest::_setupVisualItemSignals(VisualMissionItem* visualItem)
Don Gagne's avatar
Don Gagne committed
192 193 194 195 196
{
    delete _multiSpyMissionItem;

    _multiSpyMissionItem = new MultiSignalSpy();
    Q_CHECK_PTR(_multiSpyMissionItem);
Don Gagne's avatar
Don Gagne committed
197
    QCOMPARE(_multiSpyMissionItem->init(visualItem, _rgVisualItemSignals, _cVisualItemSignals), true);
Don Gagne's avatar
Don Gagne committed
198
}
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

void MissionControllerTest::_testGimbalRecalc(void)
{
    _initForFirmwareType(MAV_AUTOPILOT_PX4);
    _missionController->insertSimpleMissionItem(QGeoCoordinate(0, 0), 1);
    _missionController->insertSimpleMissionItem(QGeoCoordinate(0, 0), 2);
    _missionController->insertSimpleMissionItem(QGeoCoordinate(0, 0), 3);
    _missionController->insertSimpleMissionItem(QGeoCoordinate(0, 0), 4);

    // No specific gimbal yaw set yet
    for (int i=1; i<_missionController->visualItems()->count(); i++) {
        VisualMissionItem* visualItem = _missionController->visualItems()->value<VisualMissionItem*>(i);
        QVERIFY(qIsNaN(visualItem->missionGimbalYaw()));
    }

    // Specify gimbal yaw on settings item should generate yaw on all items
    MissionSettingsItem* settingsItem = _missionController->visualItems()->value<MissionSettingsItem*>(0);
    settingsItem->cameraSection()->setSpecifyGimbal(true);
    settingsItem->cameraSection()->gimbalYaw()->setRawValue(0.0);
    for (int i=1; i<_missionController->visualItems()->count(); i++) {
        VisualMissionItem* visualItem = _missionController->visualItems()->value<VisualMissionItem*>(i);
        QCOMPARE(visualItem->missionGimbalYaw(), 0.0);
    }
}
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

void MissionControllerTest::_testLoadJsonSectionAvailable(void)
{
    _initForFirmwareType(MAV_AUTOPILOT_PX4);
    _masterController->loadFromFile(":/unittest/SectionTest.plan");

    QmlObjectListModel* visualItems = _missionController->visualItems();
    QVERIFY(visualItems);
    QCOMPARE(visualItems->count(), 5);

    // Check that only waypoint items have camera and speed sections
    for (int i=1; i<visualItems->count(); i++) {
        SimpleMissionItem* item = visualItems->value<SimpleMissionItem*>(i);
        QVERIFY(item);
        if ((int)item->command() == MAV_CMD_NAV_WAYPOINT) {
            QCOMPARE(item->cameraSection()->available(), true);
            QCOMPARE(item->speedSection()->available(), true);
        } else {
            QCOMPARE(item->cameraSection()->available(), false);
            QCOMPARE(item->speedSection()->available(), false);
        }

    }
}