SimpleMissionItemTest.cc 13.2 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

#include "SimpleMissionItemTest.h"
#include "SimpleMissionItem.h"
13
#include "QGCApplication.h"
14
#include "QGroundControlQmlGlobal.h"
15
#include "SettingsManager.h"
16 17 18 19 20 21 22 23 24 25 26 27 28

const SimpleMissionItemTest::ItemInfo_t SimpleMissionItemTest::_rgItemInfo[] = {
    { MAV_CMD_NAV_WAYPOINT,     MAV_FRAME_GLOBAL_RELATIVE_ALT },
    { MAV_CMD_NAV_LOITER_UNLIM, MAV_FRAME_GLOBAL_RELATIVE_ALT },
    { MAV_CMD_NAV_LOITER_TURNS, MAV_FRAME_GLOBAL_RELATIVE_ALT },
    { MAV_CMD_NAV_LOITER_TIME,  MAV_FRAME_GLOBAL_RELATIVE_ALT },
    { MAV_CMD_NAV_LAND,         MAV_FRAME_GLOBAL_RELATIVE_ALT },
    { MAV_CMD_NAV_TAKEOFF,      MAV_FRAME_GLOBAL_RELATIVE_ALT },
    { MAV_CMD_CONDITION_DELAY,  MAV_FRAME_MISSION },
    { MAV_CMD_DO_JUMP,          MAV_FRAME_MISSION },
};

const SimpleMissionItemTest::FactValue_t SimpleMissionItemTest::_rgFactValuesWaypoint[] = {
Don Gagne's avatar
Don Gagne committed
29 30
    { "Altitude",   70.1234567 },
    { "Hold",       10.1234567 },
31 32 33
};

const SimpleMissionItemTest::FactValue_t SimpleMissionItemTest::_rgFactValuesLoiterUnlim[] = {
Don Gagne's avatar
Don Gagne committed
34 35
    { "Altitude",   70.1234567 },
    { "Radius",     30.1234567 },
36 37 38
};

const SimpleMissionItemTest::FactValue_t SimpleMissionItemTest::_rgFactValuesLoiterTurns[] = {
Don Gagne's avatar
Don Gagne committed
39 40 41
    { "Altitude",   70.1234567 },
    { "Radius",     30.1234567 },
    { "Turns",      10.1234567 },
42 43 44
};

const SimpleMissionItemTest::FactValue_t SimpleMissionItemTest::_rgFactValuesLoiterTime[] = {
Don Gagne's avatar
Don Gagne committed
45 46 47
    { "Altitude",   70.1234567 },
    { "Radius",     30.1234567 },
    { "Hold",       10.1234567 },
48 49 50
};

const SimpleMissionItemTest::FactValue_t SimpleMissionItemTest::_rgFactValuesLand[] = {
Don Gagne's avatar
Don Gagne committed
51
    { "Altitude",   70.1234567 },
52 53 54
};

const SimpleMissionItemTest::FactValue_t SimpleMissionItemTest::_rgFactValuesTakeoff[] = {
Don Gagne's avatar
Don Gagne committed
55 56
    { "Pitch",      10.1234567 },
    { "Altitude",   70.1234567 },
57 58 59
};

const SimpleMissionItemTest::FactValue_t SimpleMissionItemTest::_rgFactValuesConditionDelay[] = {
Don Gagne's avatar
Don Gagne committed
60
    { "Hold",       10.1234567 },
61 62 63
};

const SimpleMissionItemTest::FactValue_t SimpleMissionItemTest::_rgFactValuesDoJump[] = {
Don Gagne's avatar
Don Gagne committed
64 65
    { "Item #",     10.1234567 },
    { "Repeat",     20.1234567 },
66 67 68 69 70 71 72 73
};

const SimpleMissionItemTest::ItemExpected_t SimpleMissionItemTest::_rgItemExpected[] = {
    { sizeof(SimpleMissionItemTest::_rgFactValuesWaypoint)/sizeof(SimpleMissionItemTest::_rgFactValuesWaypoint[0]),             SimpleMissionItemTest::_rgFactValuesWaypoint,       true },
    { sizeof(SimpleMissionItemTest::_rgFactValuesLoiterUnlim)/sizeof(SimpleMissionItemTest::_rgFactValuesLoiterUnlim[0]),       SimpleMissionItemTest::_rgFactValuesLoiterUnlim,    true },
    { sizeof(SimpleMissionItemTest::_rgFactValuesLoiterTurns)/sizeof(SimpleMissionItemTest::_rgFactValuesLoiterTurns[0]),       SimpleMissionItemTest::_rgFactValuesLoiterTurns,    true },
    { sizeof(SimpleMissionItemTest::_rgFactValuesLoiterTime)/sizeof(SimpleMissionItemTest::_rgFactValuesLoiterTime[0]),         SimpleMissionItemTest::_rgFactValuesLoiterTime,     true },
    { sizeof(SimpleMissionItemTest::_rgFactValuesLand)/sizeof(SimpleMissionItemTest::_rgFactValuesLand[0]),                     SimpleMissionItemTest::_rgFactValuesLand,           true },
74
    { sizeof(SimpleMissionItemTest::_rgFactValuesTakeoff)/sizeof(SimpleMissionItemTest::_rgFactValuesTakeoff[0]),               SimpleMissionItemTest::_rgFactValuesTakeoff,        true },
75 76 77 78 79
    { sizeof(SimpleMissionItemTest::_rgFactValuesConditionDelay)/sizeof(SimpleMissionItemTest::_rgFactValuesConditionDelay[0]), SimpleMissionItemTest::_rgFactValuesConditionDelay, false },
    { sizeof(SimpleMissionItemTest::_rgFactValuesDoJump)/sizeof(SimpleMissionItemTest::_rgFactValuesDoJump[0]),                 SimpleMissionItemTest::_rgFactValuesDoJump,         false },
};

SimpleMissionItemTest::SimpleMissionItemTest(void)
Don Gagne's avatar
Don Gagne committed
80
    : _offlineVehicle(NULL)
81 82 83 84
{
    
}

Don Gagne's avatar
Don Gagne committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
void SimpleMissionItemTest::init(void)
{
    UnitTest::init();
    _offlineVehicle = new Vehicle(MAV_AUTOPILOT_PX4,
                                  MAV_TYPE_QUADROTOR,
                                  qgcApp()->toolbox()->firmwarePluginManager(),
                                  this);

}

void SimpleMissionItemTest::cleanup(void)
{
    delete _offlineVehicle;
    UnitTest::cleanup();
}

101 102
void SimpleMissionItemTest::_testEditorFacts(void)
{
103
    Vehicle* vehicle = new Vehicle(MAV_AUTOPILOT_PX4, MAV_TYPE_FIXED_WING, qgcApp()->toolbox()->firmwarePluginManager());
104

105 106 107 108
    for (size_t i=0; i<sizeof(_rgItemInfo)/sizeof(_rgItemInfo[0]); i++) {
        const ItemInfo_t* info = &_rgItemInfo[i];
        const ItemExpected_t* expected = &_rgItemExpected[i];
        
Don Gagne's avatar
Don Gagne committed
109
        qDebug() << "Command" << info->command;
110 111 112 113 114 115 116 117 118 119 120 121 122
        
        MissionItem missionItem(1,              // sequence number
                                info->command,
                                info->frame,
                                10.1234567,     // param 1-7
                                20.1234567,
                                30.1234567,
                                40.1234567,
                                50.1234567,
                                60.1234567,
                                70.1234567,
                                true,           // autoContinue
                                false);         // isCurrentItem
123
        SimpleMissionItem simpleMissionItem(vehicle, missionItem);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

        // Validate that the fact values are correctly returned

        size_t factCount = 0;
        for (int i=0; i<simpleMissionItem.textFieldFacts()->count(); i++) {
            Fact* fact = qobject_cast<Fact*>(simpleMissionItem.textFieldFacts()->get(i));
            
            bool found = false;
            for (size_t j=0; j<expected->cFactValues; j++) {
                const FactValue_t* factValue = &expected->rgFactValues[j];
                
                if (factValue->name == fact->name()) {
                    QCOMPARE(fact->rawValue().toDouble(), factValue->value);
                    factCount ++;
                    found = true;
                    break;
                }
            }
            
143
            qDebug() << fact->name();
144 145 146 147 148 149 150
            QVERIFY(found);
        }
        QCOMPARE(factCount, expected->cFactValues);

        int expectedCount = expected->relativeAltCheckbox ? 1 : 0;
        QCOMPARE(simpleMissionItem.checkboxFacts()->count(), expectedCount);
    }
151 152

    delete vehicle;
153 154 155 156
}

void SimpleMissionItemTest::_testDefaultValues(void)
{
Don Gagne's avatar
Don Gagne committed
157
    SimpleMissionItem item(_offlineVehicle);
158 159 160

    item.missionItem().setCommand(MAV_CMD_NAV_WAYPOINT);
    item.missionItem().setFrame(MAV_FRAME_GLOBAL_RELATIVE_ALT);
161
    QCOMPARE(item.missionItem().param7(), qgcApp()->toolbox()->settingsManager()->appSettings()->defaultMissionItemAltitude()->rawValue().toDouble());
162 163 164 165 166 167 168 169 170 171
}

void SimpleMissionItemTest::_testSignals(void)
{
    enum {
        commandChangedIndex = 0,
        frameChangedIndex,
        friendlyEditAllowedChangedIndex,
        headingDegreesChangedIndex,
        rawEditChangedIndex,
Don Gagne's avatar
Don Gagne committed
172 173 174
        coordinateChangedIndex,
        exitCoordinateChangedIndex,
        dirtyChangedIndex,
175 176 177 178 179 180 181 182 183
        maxSignalIndex
    };

    enum {
        commandChangedMask =                1 << commandChangedIndex,
        frameChangedMask =                  1 << frameChangedIndex,
        friendlyEditAllowedChangedMask =    1 << friendlyEditAllowedChangedIndex,
        headingDegreesChangedMask =         1 << headingDegreesChangedIndex,
        rawEditChangedMask =                1 << rawEditChangedIndex,
Don Gagne's avatar
Don Gagne committed
184 185 186
        coordinateChangedMask =             1 << coordinateChangedIndex,
        exitCoordinateChangedMask =         1 << exitCoordinateChangedIndex,
        dirtyChangedMask =                  1 << dirtyChangedIndex,
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    };

    static const size_t cSimpleMissionItemSignals = maxSignalIndex;
    const char*         rgSimpleMissionItemSignals[cSimpleMissionItemSignals];

    rgSimpleMissionItemSignals[commandChangedIndex] =               SIGNAL(commandChanged(int));
    rgSimpleMissionItemSignals[coordinateChangedIndex] =            SIGNAL(coordinateChanged(const QGeoCoordinate&));
    rgSimpleMissionItemSignals[exitCoordinateChangedIndex] =        SIGNAL(exitCoordinateChanged(const QGeoCoordinate&));
    rgSimpleMissionItemSignals[dirtyChangedIndex] =                 SIGNAL(dirtyChanged(bool));
    rgSimpleMissionItemSignals[frameChangedIndex] =                 SIGNAL(frameChanged(int));
    rgSimpleMissionItemSignals[friendlyEditAllowedChangedIndex] =   SIGNAL(friendlyEditAllowedChanged(bool));
    rgSimpleMissionItemSignals[headingDegreesChangedIndex] =        SIGNAL(headingDegreesChanged(double));
    rgSimpleMissionItemSignals[rawEditChangedIndex] =               SIGNAL(rawEditChanged(bool));

    MissionItem missionItem(1,              // sequence number
                            MAV_CMD_NAV_WAYPOINT,
                            MAV_FRAME_GLOBAL_RELATIVE_ALT,
                            10.1234567,     // param 1-7
                            20.1234567,
                            30.1234567,
                            40.1234567,
                            50.1234567,
                            60.1234567,
                            70.1234567,
                            true,           // autoContinue
                            false);         // isCurrentItem
Don Gagne's avatar
Don Gagne committed
213
    SimpleMissionItem simpleMissionItem(_offlineVehicle, missionItem);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

    // It's important top check that the right signals are emitted at the right time since that drives ui change.
    // It's also important to check that things are not being over-signalled when they should not be, since that can lead
    // to incorrect ui or perf impact of uneeded signals propogating ui change.

    MultiSignalSpy* multiSpy = new MultiSignalSpy();
    Q_CHECK_PTR(multiSpy);
    QCOMPARE(multiSpy->init(&simpleMissionItem, rgSimpleMissionItemSignals, cSimpleMissionItemSignals), true);

    // Check commandChanged signalling. Call setCommand should trigger:
    //      commandChanged
    //      dirtyChanged
    //      coordinateChanged - since altitude will be set back to default

    simpleMissionItem.setCommand(MavlinkQmlSingleton::MAV_CMD_NAV_WAYPOINT);
    QVERIFY(multiSpy->checkNoSignals());
    simpleMissionItem.setCommand(MavlinkQmlSingleton::MAV_CMD_NAV_LOITER_TIME);
Don Gagne's avatar
Don Gagne committed
231
    QVERIFY(multiSpy->checkSignalsByMask(commandChangedMask | dirtyChangedMask | coordinateChangedMask));
232 233 234 235 236 237 238
    multiSpy->clearAllSignals();

    // Check coordinateChanged signalling. Calling setCoordinate should trigger:
    //      coordinateChanged
    //      dirtyChanged

    // Check that changing to the same coordinate does not signal
239
    simpleMissionItem.setCoordinate(QGeoCoordinate(50.1234567, 60.1234567, qgcApp()->toolbox()->settingsManager()->appSettings()->defaultMissionItemAltitude()->rawValue().toDouble()));
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    QVERIFY(multiSpy->checkNoSignals());

    // Check that actually changing coordinate signals correctly
    simpleMissionItem.setCoordinate(QGeoCoordinate(50.1234567, 60.1234567, 70.1234567));
    QVERIFY(multiSpy->checkOnlySignalByMask(coordinateChangedMask | dirtyChangedMask));
    multiSpy->clearAllSignals();

    // Check dirtyChanged signalling

    // Reset param 1-5 for testing
    simpleMissionItem.missionItem().setParam1(10.1234567);
    simpleMissionItem.missionItem().setParam2(20.1234567);
    simpleMissionItem.missionItem().setParam3(30.1234567);
    simpleMissionItem.missionItem().setParam4(40.1234567);
    multiSpy->clearAllSignals();

    simpleMissionItem.missionItem().setParam1(10.1234567);
    QVERIFY(multiSpy->checkNoSignals());
    simpleMissionItem.missionItem().setParam1(20.1234567);
    QVERIFY(multiSpy->checkOnlySignalByMask(dirtyChangedMask));
    multiSpy->clearAllSignals();

    simpleMissionItem.missionItem().setParam2(20.1234567);
    QVERIFY(multiSpy->checkNoSignals());
    simpleMissionItem.missionItem().setParam2(30.1234567);
    QVERIFY(multiSpy->checkOnlySignalByMask(dirtyChangedMask));
    multiSpy->clearAllSignals();

    simpleMissionItem.missionItem().setParam3(30.1234567);
    QVERIFY(multiSpy->checkNoSignals());
    simpleMissionItem.missionItem().setParam3(40.1234567);
    QVERIFY(multiSpy->checkOnlySignalByMask(dirtyChangedMask));
    multiSpy->clearAllSignals();

    simpleMissionItem.missionItem().setParam4(40.1234567);
    QVERIFY(multiSpy->checkNoSignals());
    simpleMissionItem.missionItem().setParam4(50.1234567);
    QVERIFY(multiSpy->checkOnlySignalByMask(dirtyChangedMask));
    multiSpy->clearAllSignals();

    // Check frameChanged signalling. Calling setFrame should signal:
    //      frameChanged
    //      dirtyChanged
    //      friendlyEditAllowedChanged - this signal is not very smart on when it gets sent

    simpleMissionItem.setCommand(MavlinkQmlSingleton::MAV_CMD_NAV_WAYPOINT);
    simpleMissionItem.missionItem().setFrame(MAV_FRAME_GLOBAL_RELATIVE_ALT);
    multiSpy->clearAllSignals();
    simpleMissionItem.missionItem().setFrame(MAV_FRAME_GLOBAL_RELATIVE_ALT);
    QVERIFY(multiSpy->checkNoSignals());
    simpleMissionItem.missionItem().setFrame(MAV_FRAME_GLOBAL);
    QVERIFY(multiSpy->checkOnlySignalByMask(frameChangedMask | dirtyChangedMask | friendlyEditAllowedChangedMask));
    multiSpy->clearAllSignals();
}