MissionCommandTreeTest.cc 8.79 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "MissionCommandTreeTest.h"
#include "QGCApplication.h"
#include "MissionCommandUIInfo.h"
#include "MissionCommandList.h"
#include "FactMetaData.h"

MissionCommandTreeTest::MissionCommandTreeTest(void)
{
    
}

void MissionCommandTreeTest::init(void)
{
DonLakeFlyer's avatar
DonLakeFlyer committed
23
    _commandTree = new MissionCommandTree(qgcApp(), qgcApp()->toolbox(), true /* unitTest */);
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    _commandTree->setToolbox(qgcApp()->toolbox());
}

void MissionCommandTreeTest::cleanup(void)
{
    delete _commandTree;
}

QString MissionCommandTreeTest::_rawName(int id)
{
    return QString("UNITTEST_%1").arg(id);
}

QString MissionCommandTreeTest::_friendlyName(int id)
{
    return QString("Unit Test %1").arg(id);
}

QString MissionCommandTreeTest::_paramLabel(int index)
{
    return QString("param%1").arg(index);
}

/// Verifies that all values have been set
void MissionCommandTreeTest::_checkFullInfoMap(const MissionCommandUIInfo* uiInfo)
{
    QVERIFY(uiInfo->_infoMap.contains(MissionCommandUIInfo::_rawNameJsonKey));
    QVERIFY(uiInfo->_infoMap.contains(MissionCommandUIInfo::_categoryJsonKey));
    QVERIFY(uiInfo->_infoMap.contains(MissionCommandUIInfo::_descriptionJsonKey));
    QVERIFY(uiInfo->_infoMap.contains(MissionCommandUIInfo::_friendlyEditJsonKey));
    QVERIFY(uiInfo->_infoMap.contains(MissionCommandUIInfo::_friendlyNameJsonKey));
    QVERIFY(uiInfo->_infoMap.contains(MissionCommandUIInfo::_standaloneCoordinateJsonKey));
    QVERIFY(uiInfo->_infoMap.contains(MissionCommandUIInfo::_specifiesCoordinateJsonKey));
}

// Verifies that values match settings for base tree
void MissionCommandTreeTest::_checkBaseValues(const MissionCommandUIInfo* uiInfo, int command)
{
62
    QVERIFY(uiInfo != nullptr);
63 64 65 66 67 68 69 70 71 72
    _checkFullInfoMap(uiInfo);
    QCOMPARE(uiInfo->command(), (MAV_CMD)command);
    QCOMPARE(uiInfo->rawName(), _rawName(command));
    QCOMPARE(uiInfo->category(), QStringLiteral("category"));
    QCOMPARE(uiInfo->description(), QStringLiteral("description"));
    QCOMPARE(uiInfo->friendlyEdit(), true);
    QCOMPARE(uiInfo->friendlyName(), _friendlyName(command));
    QCOMPARE(uiInfo->isStandaloneCoordinate(), true);
    QCOMPARE(uiInfo->specifiesCoordinate(), true);
    for (int i=1; i<=7; i++) {
73 74 75
        bool showUI;
        const MissionCmdParamInfo* paramInfo = uiInfo->getParamInfo(i, showUI);
        QVERIFY(showUI);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        QVERIFY(paramInfo);
        QCOMPARE(paramInfo->decimalPlaces(), 1);
        QCOMPARE(paramInfo->defaultValue(), 1.0);
        QCOMPARE(paramInfo->enumStrings().count(), 2);
        QCOMPARE(paramInfo->enumStrings()[0], QStringLiteral("1"));
        QCOMPARE(paramInfo->enumStrings()[1], QStringLiteral("2"));
        QCOMPARE(paramInfo->enumValues().count(), 2);
        QCOMPARE(paramInfo->enumValues()[0].toDouble(), 1.0);
        QCOMPARE(paramInfo->enumValues()[1].toDouble(), 2.0);
        QCOMPARE(paramInfo->label(), _paramLabel(i));
        QCOMPARE(paramInfo->param(), i);
        QCOMPARE(paramInfo->units(), QStringLiteral("units"));
    }
}

// Verifies that values match settings for an override
void MissionCommandTreeTest::_checkOverrideParamValues(const MissionCommandUIInfo* uiInfo, int command, int paramIndex)
{
    QString overrideString = QString("override fw %1 %2").arg(command).arg(paramIndex);

96 97 98
    bool showUI;
    const MissionCmdParamInfo* paramInfo = uiInfo->getParamInfo(paramIndex, showUI);
    QVERIFY(showUI);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    QVERIFY(paramInfo);
    QCOMPARE(paramInfo->decimalPlaces(), 1);
    QCOMPARE(paramInfo->defaultValue(), 1.0);
    QCOMPARE(paramInfo->enumStrings().count(), 2);
    QCOMPARE(paramInfo->enumStrings()[0], QStringLiteral("1"));
    QCOMPARE(paramInfo->enumStrings()[1], QStringLiteral("2"));
    QCOMPARE(paramInfo->enumValues().count(), 2);
    QCOMPARE(paramInfo->enumValues()[0].toDouble(), 1.0);
    QCOMPARE(paramInfo->enumValues()[1].toDouble(), 2.0);
    QCOMPARE(paramInfo->label(), overrideString);
    QCOMPARE(paramInfo->param(), paramIndex);
    QCOMPARE(paramInfo->units(), overrideString);
}

// Verifies that values match settings for an override
void MissionCommandTreeTest::_checkOverrideValues(const MissionCommandUIInfo* uiInfo, int command)
{
116
    bool showUI;
117 118
    QString overrideString = QString("override fw %1").arg(command);

119
    QVERIFY(uiInfo != nullptr);
120 121 122 123 124 125 126 127 128
    _checkFullInfoMap(uiInfo);
    QCOMPARE(uiInfo->command(), (MAV_CMD)command);
    QCOMPARE(uiInfo->rawName(), _rawName(command));
    QCOMPARE(uiInfo->category(), overrideString);
    QCOMPARE(uiInfo->description(), overrideString);
    QCOMPARE(uiInfo->friendlyEdit(), true);
    QCOMPARE(uiInfo->friendlyName(), _friendlyName(command));
    QCOMPARE(uiInfo->isStandaloneCoordinate(), false);
    QCOMPARE(uiInfo->specifiesCoordinate(), false);
129 130 131 132 133 134
    QVERIFY(uiInfo->getParamInfo(2, showUI));
    QCOMPARE(showUI, false);
    QVERIFY(uiInfo->getParamInfo(4, showUI));
    QCOMPARE(showUI, false);
    QVERIFY(uiInfo->getParamInfo(6, showUI));
    QCOMPARE(showUI, false);
135 136 137 138 139 140 141
    _checkOverrideParamValues(uiInfo, command, 1);
    _checkOverrideParamValues(uiInfo, command, 3);
    _checkOverrideParamValues(uiInfo, command, 5);
}

void MissionCommandTreeTest::testJsonLoad(void)
{
142 143
    bool showUI;

144 145
    // Test loading from the bad command list
    MissionCommandList* commandList = _commandTree->_staticCommandTree[MAV_AUTOPILOT_GENERIC][MAV_TYPE_GENERIC];
146
    QVERIFY(commandList != nullptr);
147 148 149

    // Command 1 should have all values defaulted, no params
    MissionCommandUIInfo* uiInfo = commandList->getUIInfo((MAV_CMD)1);
150
    QVERIFY(uiInfo != nullptr);
151 152 153 154 155 156 157 158 159 160
    _checkFullInfoMap(uiInfo);
    QCOMPARE(uiInfo->command(), (MAV_CMD)1);
    QCOMPARE(uiInfo->rawName(), _rawName(1));
    QVERIFY(uiInfo->category() == MissionCommandUIInfo::_advancedCategory);
    QVERIFY(uiInfo->description().isEmpty());
    QCOMPARE(uiInfo->friendlyEdit(), false);
    QCOMPARE(uiInfo->friendlyName(), uiInfo->rawName());
    QCOMPARE(uiInfo->isStandaloneCoordinate(), false);
    QCOMPARE(uiInfo->specifiesCoordinate(), false);
    for (int i=1; i<=7; i++) {
161
        QVERIFY(uiInfo->getParamInfo(i, showUI) == nullptr);
162
        QCOMPARE(showUI, false);
163 164 165 166
    }

    // Command 2 should all values defaulted for param 1
    uiInfo = commandList->getUIInfo((MAV_CMD)2);
167
    QVERIFY(uiInfo != nullptr);
168
    const MissionCmdParamInfo* paramInfo = uiInfo->getParamInfo(1, showUI);
169
    QVERIFY(paramInfo);
170
    QCOMPARE(showUI, true);
171 172 173 174 175 176 177 178
    QCOMPARE(paramInfo->decimalPlaces(), -1);
    QCOMPARE(paramInfo->defaultValue(), 0.0);
    QCOMPARE(paramInfo->enumStrings().count(), 0);
    QCOMPARE(paramInfo->enumValues().count(), 0);
    QCOMPARE(paramInfo->label(), _paramLabel(1));
    QCOMPARE(paramInfo->param(), 1);
    QVERIFY(paramInfo->units().isEmpty());
    for (int i=2; i<=7; i++) {
179
        QVERIFY(uiInfo->getParamInfo(i, showUI) == nullptr);
180
        QCOMPARE(showUI, false);
181 182 183 184 185 186 187 188 189
    }

    // Command 3 should have all values set
    _checkBaseValues(commandList->getUIInfo((MAV_CMD)3), 3);
}

void MissionCommandTreeTest::testOverride(void)
{
    // Generic/Generic should not have any overrides
190
    Vehicle* vehicle = new Vehicle(MAV_AUTOPILOT_GENERIC, MAV_TYPE_GENERIC, qgcApp()->toolbox()->firmwarePluginManager());
191 192 193 194
    _checkBaseValues(_commandTree->getUIInfo(vehicle, (MAV_CMD)4), 4);
    delete vehicle;

    // Generic/FixedWing should have overrides
195
    vehicle = new Vehicle(MAV_AUTOPILOT_GENERIC, MAV_TYPE_FIXED_WING, qgcApp()->toolbox()->firmwarePluginManager());
196 197 198 199 200 201 202 203 204 205 206 207 208
    _checkOverrideValues(_commandTree->getUIInfo(vehicle, (MAV_CMD)4), 4);
    delete vehicle;
}

void MissionCommandTreeTest::testAllTrees(void)
{
    QList<MAV_AUTOPILOT>    firmwareList;
    QList<MAV_TYPE>         vehicleList;

    firmwareList << MAV_AUTOPILOT_GENERIC << MAV_AUTOPILOT_PX4 << MAV_AUTOPILOT_ARDUPILOTMEGA;
    vehicleList << MAV_TYPE_GENERIC << MAV_TYPE_QUADROTOR << MAV_TYPE_FIXED_WING << MAV_TYPE_GROUND_ROVER << MAV_TYPE_SUBMARINE << MAV_TYPE_VTOL_QUADROTOR;

    // This will cause all of the variants of collapsed trees to be built
209 210
    for(MAV_AUTOPILOT firmwareType: firmwareList) {
        for (MAV_TYPE vehicleType: vehicleList) {
DonLakeFlyer's avatar
DonLakeFlyer committed
211 212 213 214
            if (firmwareType == MAV_AUTOPILOT_ARDUPILOTMEGA && vehicleType == MAV_TYPE_VTOL_QUADROTOR) {
                // VTOL in ArduPilot shows up as plane so we can test this pair
                continue;
            }
215
            qDebug() << firmwareType << vehicleType;
216
            Vehicle* vehicle = new Vehicle(firmwareType, vehicleType, qgcApp()->toolbox()->firmwarePluginManager());
217
            QVERIFY(qgcApp()->toolbox()->missionCommandTree()->getUIInfo(vehicle, MAV_CMD_NAV_WAYPOINT) != nullptr);
218 219 220 221 222 223
            delete vehicle;
        }
    }

}