MissionControllerTest.cc 6.46 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"
15 16

MissionControllerTest::MissionControllerTest(void)
Don Gagne's avatar
Don Gagne committed
17 18 19
    : _multiSpyMissionController(NULL)
    , _multiSpyMissionItem(NULL)
    , _missionController(NULL)
20 21 22 23 24 25 26
{
    
}

void MissionControllerTest::cleanup(void)
{
    delete _missionController;
Don Gagne's avatar
Don Gagne committed
27
    _missionController = NULL;
28

Don Gagne's avatar
Don Gagne committed
29 30 31 32 33 34
    delete _multiSpyMissionController;
    _multiSpyMissionController = NULL;

    delete _multiSpyMissionItem;
    _multiSpyMissionItem = NULL;

35 36 37 38 39
    MissionControllerManagerTest::cleanup();
}

void MissionControllerTest::_initForFirmwareType(MAV_AUTOPILOT firmwareType)
{
Don Gagne's avatar
Don Gagne committed
40 41
    bool startController = false;

42 43
    MissionControllerManagerTest::_initForFirmwareType(firmwareType);

Don Gagne's avatar
Don Gagne committed
44 45 46 47 48 49
    void coordinateChanged(const QGeoCoordinate& coordinate);
    void headingDegreesChanged(double heading);
    void dirtyChanged(bool dirty);
    void homePositionValidChanged(bool homePostionValid);

    // MissionItem signals
50
    _rgMissionItemSignals[coordinateChangedSignalIndex] = SIGNAL(coordinateChanged(const QGeoCoordinate&));
Don Gagne's avatar
Don Gagne committed
51 52

    // MissionController signals
53
    _rgMissionControllerSignals[visualItemsChangedSignalIndex] =    SIGNAL(visualItemsChanged());
54
    _rgMissionControllerSignals[waypointLinesChangedSignalIndex] =  SIGNAL(waypointLinesChanged());
55

Don Gagne's avatar
Don Gagne committed
56 57 58 59 60
    if (!_missionController) {
        startController = true;
        _missionController = new MissionController();
        Q_CHECK_PTR(_missionController);
    }
61

Don Gagne's avatar
Don Gagne committed
62 63 64
    _multiSpyMissionController = new MultiSignalSpy();
    Q_CHECK_PTR(_multiSpyMissionController);
    QCOMPARE(_multiSpyMissionController->init(_missionController, _rgMissionControllerSignals, _cMissionControllerSignals), true);
65

Don Gagne's avatar
Don Gagne committed
66 67 68
    if (startController) {
        _missionController->start(false /* editMode */);
    }
69 70

    // All signals should some through on start
71
    QCOMPARE(_multiSpyMissionController->checkOnlySignalsByMask(visualItemsChangedSignalMask | waypointLinesChangedSignalMask), true);
Don Gagne's avatar
Don Gagne committed
72
    _multiSpyMissionController->clearAllSignals();
73

74 75
    QmlObjectListModel* visualItems = _missionController->visualItems();
    QVERIFY(visualItems);
76 77

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

Don Gagne's avatar
Don Gagne committed
80
    // Home position should be in first slot, but not yet valid
81
    SimpleMissionItem* homeItem = qobject_cast<SimpleMissionItem*>(visualItems->get(0));
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    QVERIFY(homeItem);
    QCOMPARE(homeItem->homePosition(), true);

    // Home should have no children
    QCOMPARE(homeItem->childItems()->count(), 0);

    // 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

101 102 103
    QmlObjectListModel* visualItems = _missionController->visualItems();
    QVERIFY(visualItems);
    SimpleMissionItem* homeItem = qobject_cast<SimpleMissionItem*>(visualItems->get(0));
104 105
    QVERIFY(homeItem);

Don Gagne's avatar
Don Gagne committed
106
    _setupMissionItemSignals(homeItem);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
}

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);

125
    _missionController->insertSimpleMissionItem(coordinate, _missionController->visualItems()->count());
126

Don Gagne's avatar
Don Gagne committed
127
    QCOMPARE(_multiSpyMissionController->checkOnlySignalsByMask(waypointLinesChangedSignalMask), true);
128

129 130
    QmlObjectListModel* visualItems = _missionController->visualItems();
    QVERIFY(visualItems);
131

132
    QCOMPARE(visualItems->count(), 2);
133

134 135
    SimpleMissionItem* homeItem = qobject_cast<SimpleMissionItem*>(visualItems->get(0));
    SimpleMissionItem* item = qobject_cast<SimpleMissionItem*>(visualItems->get(1));
136 137 138 139
    QVERIFY(homeItem);
    QVERIFY(item);

    QCOMPARE(item->command(), MavlinkQmlSingleton::MAV_CMD_NAV_TAKEOFF);
140
    QCOMPARE(homeItem->childItems()->count(), firmwareType == MAV_AUTOPILOT_ARDUPILOTMEGA ? 1 : 0);
141 142
    QCOMPARE(item->childItems()->count(), 0);

143 144 145
#if 0
    // This needs re-work
    int expectedLineCount = firmwareType == MAV_AUTOPILOT_ARDUPILOTMEGA ? 0 : 1;
146 147 148 149

    QmlObjectListModel* waypointLines = _missionController->waypointLines();
    QVERIFY(waypointLines);
    QCOMPARE(waypointLines->count(), expectedLineCount);
150
#endif
151 152 153 154 155 156 157 158 159 160 161 162
}

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


void MissionControllerTest::_testAddWayppointPX4(void)
{
    _testAddWaypointWorker(MAV_AUTOPILOT_PX4);
}
Don Gagne's avatar
Don Gagne committed
163 164 165 166 167 168 169

void MissionControllerTest::_testOfflineToOnlineWorker(MAV_AUTOPILOT firmwareType)
{
    // Start offline and add item
    _missionController = new MissionController();
    Q_CHECK_PTR(_missionController);
    _missionController->start(true /* editMode */);
170
    _missionController->insertSimpleMissionItem(QGeoCoordinate(37.803784, -122.462276), _missionController->visualItems()->count());
Don Gagne's avatar
Don Gagne committed
171 172 173 174

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

Don Gagne's avatar
Don Gagne committed
175 176 177 178 179
#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
180
    QCOMPARE(_missionController->visualItems()->count(), 2);
Don Gagne's avatar
Don Gagne committed
181
#endif
Don Gagne's avatar
Don Gagne committed
182 183 184 185 186 187 188 189 190 191 192
}

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

void MissionControllerTest::_testOfflineToOnlinePX4(void)
{
    _testOfflineToOnlineWorker(MAV_AUTOPILOT_PX4);
}
Don Gagne's avatar
Don Gagne committed
193

194
void MissionControllerTest::_setupMissionItemSignals(SimpleMissionItem* item)
Don Gagne's avatar
Don Gagne committed
195 196 197 198 199 200 201
{
    delete _multiSpyMissionItem;

    _multiSpyMissionItem = new MultiSignalSpy();
    Q_CHECK_PTR(_multiSpyMissionItem);
    QCOMPARE(_multiSpyMissionItem->init(item, _rgMissionItemSignals, _cMissionItemSignals), true);
}