MissionItemTest.cc 7.8 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[] = {
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 99 100 101 102 103 104
};

MissionItemTest::MissionItemTest(void)
{
    
}

void MissionItemTest::_test(void)
{
    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;
        
105
        MissionItem* item = new MissionItem(1,
Don Gagne's avatar
Don Gagne committed
106
                                            info->command,
107
                                            info->frame,
108 109 110 111 112 113 114 115 116
                                            10.1234567,
                                            20.1234567,
                                            30.1234567,
                                            40.1234567,
                                            50.1234567,
                                            60.1234567,
                                            70.1234567,
                                            true,
                                            false);
117

Don Gagne's avatar
Don Gagne committed
118
        // Validate the saving is working correctly
119

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

        // 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
134 135 136 137 138 139 140 141 142 143 144
        
        // 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()) {
145
                    QCOMPARE(fact->rawValue().toDouble(), factValue->value);
Don Gagne's avatar
Don Gagne committed
146 147 148 149 150 151
                    factCount ++;
                    found = true;
                    break;
                }
            }
            
Don Gagne's avatar
Don Gagne committed
152 153 154
            if (!found) {
                qDebug() << fact->name();
            }
Don Gagne's avatar
Don Gagne committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
            QVERIFY(found);
        }
        QCOMPARE(factCount, expected->cFactValues);
        
        // Validate that loading is working correctly
        MissionItem* loadedItem = new MissionItem();
        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;
    }
}