MissionItemTest.cc 8.1 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 41
    { "Altitude:",      70.1234567 },
    { "Hold:",          10.1234567 },
    { "Accept radius:", 20.1234567 },
Don Gagne's avatar
Don Gagne committed
42 43 44
};

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

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

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

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

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

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

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

const MissionItemTest::ItemExpected_t MissionItemTest::_rgItemExpected[] = {
83 84 85 86 87 88 89 90
    { 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
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
};

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

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

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

        // 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
135 136 137 138 139 140 141 142 143 144 145
        
        // 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()) {
146
                    QCOMPARE(fact->rawValue().toDouble(), factValue->value);
Don Gagne's avatar
Don Gagne committed
147 148 149 150 151 152
                    factCount ++;
                    found = true;
                    break;
                }
            }
            
Don Gagne's avatar
Don Gagne committed
153 154 155
            if (!found) {
                qDebug() << fact->name();
            }
Don Gagne's avatar
Don Gagne committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
            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;
    }
}
Don Gagne's avatar
Don Gagne committed
180 181 182 183 184 185 186 187 188 189

void MissionItemTest::_testDefaultValues(void)
{
    MissionItem item;

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