MissionItemTest.cc 8.18 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

#include "MissionItemTest.h"
25
#include "MissionItem.h"
Don Gagne's avatar
Don Gagne committed
26 27

const MissionItemTest::ItemInfo_t MissionItemTest::_rgItemInfo[] = {
28 29 30 31 32 33 34 35
    { 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 },
Don Gagne's avatar
Don Gagne committed
36 37 38
};

const MissionItemTest::FactValue_t MissionItemTest::_rgFactValuesWaypoint[] = {
Don Gagne's avatar
Don Gagne committed
39 40
    { "Altitude:",      70.1234567 },
    { "Hold:",          10.1234567 },
Don Gagne's avatar
Don Gagne committed
41 42 43
};

const MissionItemTest::FactValue_t MissionItemTest::_rgFactValuesLoiterUnlim[] = {
44 45
    { "Altitude:",  70.1234567 },
    { "Radius:",    30.1234567 },
Don Gagne's avatar
Don Gagne committed
46 47 48
};

const MissionItemTest::FactValue_t MissionItemTest::_rgFactValuesLoiterTurns[] = {
49 50 51
    { "Altitude:",  70.1234567 },
    { "Radius:",    30.1234567 },
    { "Turns:",     10.1234567 },
Don Gagne's avatar
Don Gagne committed
52 53 54
};

const MissionItemTest::FactValue_t MissionItemTest::_rgFactValuesLoiterTime[] = {
55 56 57
    { "Altitude:",  70.1234567 },
    { "Radius:",    30.1234567 },
    { "Hold:",      10.1234567 },
Don Gagne's avatar
Don Gagne committed
58 59 60
};

const MissionItemTest::FactValue_t MissionItemTest::_rgFactValuesLand[] = {
61 62 63
    { "Altitude:",  70.1234567 },
    { "Abort Alt:", 10.1234567 },
    { "Heading:",   40.1234567 },
Don Gagne's avatar
Don Gagne committed
64 65 66
};

const MissionItemTest::FactValue_t MissionItemTest::_rgFactValuesTakeoff[] = {
67 68 69
    { "Altitude:",  70.1234567 },
    { "Heading:",   40.1234567 },
    { "Pitch:",     10.1234567 },
Don Gagne's avatar
Don Gagne committed
70 71 72
};

const MissionItemTest::FactValue_t MissionItemTest::_rgFactValuesConditionDelay[] = {
73
    { "Hold:", 10.1234567 },
Don Gagne's avatar
Don Gagne committed
74 75 76
};

const MissionItemTest::FactValue_t MissionItemTest::_rgFactValuesDoJump[] = {
77 78
    { "Item #:",    10.1234567 },
    { "Repeat:",    20.1234567 },
Don Gagne's avatar
Don Gagne committed
79 80 81
};

const MissionItemTest::ItemExpected_t MissionItemTest::_rgItemExpected[] = {
82 83 84 85 86 87 88 89
    { sizeof(MissionItemTest::_rgFactValuesWaypoint)/sizeof(MissionItemTest::_rgFactValuesWaypoint[0]),             MissionItemTest::_rgFactValuesWaypoint },
    { sizeof(MissionItemTest::_rgFactValuesLoiterUnlim)/sizeof(MissionItemTest::_rgFactValuesLoiterUnlim[0]),       MissionItemTest::_rgFactValuesLoiterUnlim },
    { sizeof(MissionItemTest::_rgFactValuesLoiterTurns)/sizeof(MissionItemTest::_rgFactValuesLoiterTurns[0]),       MissionItemTest::_rgFactValuesLoiterTurns },
    { sizeof(MissionItemTest::_rgFactValuesLoiterTime)/sizeof(MissionItemTest::_rgFactValuesLoiterTime[0]),         MissionItemTest::_rgFactValuesLoiterTime },
    { sizeof(MissionItemTest::_rgFactValuesLand)/sizeof(MissionItemTest::_rgFactValuesLand[0]),                     MissionItemTest::_rgFactValuesLand },
    { sizeof(MissionItemTest::_rgFactValuesTakeoff)/sizeof(MissionItemTest::_rgFactValuesTakeoff[0]),               MissionItemTest::_rgFactValuesTakeoff },
    { sizeof(MissionItemTest::_rgFactValuesConditionDelay)/sizeof(MissionItemTest::_rgFactValuesConditionDelay[0]), MissionItemTest::_rgFactValuesConditionDelay },
    { sizeof(MissionItemTest::_rgFactValuesDoJump)/sizeof(MissionItemTest::_rgFactValuesDoJump[0]),                 MissionItemTest::_rgFactValuesDoJump },
Don Gagne's avatar
Don Gagne committed
90 91 92 93 94 95 96 97 98
};

MissionItemTest::MissionItemTest(void)
{
    
}

void MissionItemTest::_test(void)
{
99 100 101
#if 0
    // FIXME: Update to json

Don Gagne's avatar
Don Gagne committed
102 103 104 105 106 107
    for (size_t i=0; i<sizeof(_rgItemInfo)/sizeof(_rgItemInfo[0]); i++) {
        const ItemInfo_t* info = &_rgItemInfo[i];
        const ItemExpected_t* expected = &_rgItemExpected[i];
        
        qDebug() << "Command:" << info->command;
        
108 109
        MissionItem* item = new MissionItem(NULL,           // Vehicle
                                            1,
Don Gagne's avatar
Don Gagne committed
110
                                            info->command,
111
                                            info->frame,
112 113 114 115 116 117 118 119 120
                                            10.1234567,
                                            20.1234567,
                                            30.1234567,
                                            40.1234567,
                                            50.1234567,
                                            60.1234567,
                                            70.1234567,
                                            true,
                                            false);
121

Don Gagne's avatar
Don Gagne committed
122
        // Validate the saving is working correctly
123

Don Gagne's avatar
Don Gagne committed
124 125 126
        QString savedItemString;
        QTextStream saveStream(&savedItemString, QIODevice::WriteOnly);
        item->save(saveStream);
127 128 129 130 131 132 133 134 135 136 137

        // Param floats to string with 18 digits or precision
        QString paramStrings = "10.1234567000000002\t"
                                "20.1234566999999984\t"
                                "30.1234566999999984\t"
                                "40.1234566999999984\t"
                                "50.1234566999999984\t"
                                "60.1234566999999984\t"
                                "70.1234567000000055";
        QString expectedItemString = QString("1\t0\t%1\t%2\t%3\t1\r\n").arg(info->frame).arg(info->command).arg(paramStrings);
        QCOMPARE(savedItemString, expectedItemString);
Don Gagne's avatar
Don Gagne committed
138 139 140 141 142 143 144 145 146 147 148
        
        // Validate that the fact values are correctly returned
        size_t factCount = 0;
        for (int i=0; i<item->textFieldFacts()->count(); i++) {
            Fact* fact = qobject_cast<Fact*>(item->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()) {
149
                    QCOMPARE(fact->rawValue().toDouble(), factValue->value);
Don Gagne's avatar
Don Gagne committed
150 151 152 153 154 155
                    factCount ++;
                    found = true;
                    break;
                }
            }
            
Don Gagne's avatar
Don Gagne committed
156 157 158
            if (!found) {
                qDebug() << fact->name();
            }
Don Gagne's avatar
Don Gagne committed
159 160 161 162 163
            QVERIFY(found);
        }
        QCOMPARE(factCount, expected->cFactValues);
        
        // Validate that loading is working correctly
164
        MissionItem* loadedItem = new MissionItem(NULL /* Vehicle */);
Don Gagne's avatar
Don Gagne committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        QTextStream loadStream(&savedItemString, QIODevice::ReadOnly);
        QVERIFY(loadedItem->load(loadStream));
        QCOMPARE(loadedItem->coordinate().latitude(), item->coordinate().latitude());
        QCOMPARE(loadedItem->coordinate().longitude(), item->coordinate().longitude());
        QCOMPARE(loadedItem->coordinate().altitude(), item->coordinate().altitude());
        QCOMPARE(loadedItem->command(), item->command());
        QCOMPARE(loadedItem->param1(), item->param1());
        QCOMPARE(loadedItem->param2(), item->param2());
        QCOMPARE(loadedItem->param3(), item->param3());
        QCOMPARE(loadedItem->param4(), item->param4());
        QCOMPARE(loadedItem->autoContinue(), item->autoContinue());
        QCOMPARE(loadedItem->isCurrentItem(), item->isCurrentItem());
        QCOMPARE(loadedItem->frame(), item->frame());

        delete item;
        delete loadedItem;
    }
182
#endif
Don Gagne's avatar
Don Gagne committed
183
}
Don Gagne's avatar
Don Gagne committed
184 185 186

void MissionItemTest::_testDefaultValues(void)
{
187
    MissionItem item(NULL /* Vehicle */);
Don Gagne's avatar
Don Gagne committed
188 189 190 191 192

    item.setCommand(MAV_CMD_NAV_WAYPOINT);
    item.setFrame(MAV_FRAME_GLOBAL_RELATIVE_ALT);
    QCOMPARE(item.param7(), MissionItem::defaultAltitude);
}