UASUnitTest.cc 8.68 KB
Newer Older
1
#include "UASUnitTest.h"
2
#include <stdio.h>
3
#include <QObject>
Lorenz Meier's avatar
Lorenz Meier committed
4

Don Gagne's avatar
Don Gagne committed
5 6
UT_REGISTER_TEST(UASUnitTest)

7 8 9
UASUnitTest::UASUnitTest()
{
}
10 11
//This function is called after every test
void UASUnitTest::init()
12
{
Don Gagne's avatar
Don Gagne committed
13 14 15
    UnitTest::init();
    
    _mavlink = new MAVLinkProtocol();
16
    _uas = new UAS(_mavlink, UASID);
Don Gagne's avatar
Don Gagne committed
17
    _uas->deleteSettings();
18
}
19 20
//this function is called after every test
void UASUnitTest::cleanup()
21
{
Don Gagne's avatar
Don Gagne committed
22 23
    delete _uas;
    _uas = NULL;
24

Don Gagne's avatar
Don Gagne committed
25 26
    delete _mavlink;
    _mavlink = NULL;
Don Gagne's avatar
Don Gagne committed
27 28
    
    UnitTest::cleanup();
29 30 31 32 33
}

void UASUnitTest::getUASID_test()
{
    // Test a default ID of zero is assigned
34
    UAS* uas2 = new UAS(_mavlink);
35 36 37 38
    QCOMPARE(uas2->getUASID(), 0);
    delete uas2;

    // Test that the chosen ID was assigned at construction
Don Gagne's avatar
Don Gagne committed
39
    QCOMPARE(_uas->getUASID(), UASID);
40

41
    // Make sure that no other ID was set
42
    QEXPECT_FAIL("", "When you set an ID it does not use the default ID of 0", Continue);
Don Gagne's avatar
Don Gagne committed
43
    QCOMPARE(_uas->getUASID(), 0);
44 45

    // Make sure that ID >= 0
Don Gagne's avatar
Don Gagne committed
46
    QCOMPARE(_uas->getUASID(), 100);
47

48 49 50 51
}

void UASUnitTest::getUASName_test()
{
52
  // Test that the name is build as MAV + ID
Don Gagne's avatar
Don Gagne committed
53
  QCOMPARE(_uas->getUASName(), "MAV " + QString::number(UASID));
54 55 56 57 58

}

void UASUnitTest::getUpTime_test()
{
59
    UAS* uas2 = new UAS(_mavlink);
60 61 62
    // Test that the uptime starts at zero to a
    // precision of seconds
    QCOMPARE(floor(uas2->getUptime()/1000.0), 0.0);
63

64 65
    // Sleep for three seconds
    QTest::qSleep(3000);
66

67 68 69
    // Test that the up time is computed correctly to a
    // precision of seconds
    QCOMPARE(floor(uas2->getUptime()/1000.0), 3.0);
70

71
    delete uas2;
72 73 74 75
}

void UASUnitTest::getCommunicationStatus_test()
{
76
    // Verify that upon construction the Comm status is disconnected
Don Gagne's avatar
Don Gagne committed
77
    QCOMPARE(_uas->getCommunicationStatus(), static_cast<int>(UASInterface::COMM_DISCONNECTED));
78 79 80 81
}

void UASUnitTest::filterVoltage_test()
{
Don Gagne's avatar
Don Gagne committed
82
    float verificar=_uas->filterVoltage(0.4f);
83 84 85 86 87

    // We allow the voltage returned to be within a small delta
    const float allowedDelta = 0.05f;
    const float desiredVoltage = 7.36f;
    QVERIFY(verificar > (desiredVoltage - allowedDelta) && verificar < (desiredVoltage + allowedDelta));
88
}
89

90 91
void UASUnitTest:: getAutopilotType_test()
{
Don Gagne's avatar
Don Gagne committed
92
    int type = _uas->getAutopilotType();
93 94
    // Verify that upon construction the autopilot is set to -1
    QCOMPARE(type, -1);
95
}
96

97 98
void UASUnitTest::setAutopilotType_test()
{
Don Gagne's avatar
Don Gagne committed
99
    _uas->setAutopilotType(2);
100
    // Verify that the autopilot is set
Don Gagne's avatar
Don Gagne committed
101
    QCOMPARE(_uas->getAutopilotType(), 2);
102 103
}

Don Gagne's avatar
Don Gagne committed
104
//verify that the correct status is returned if a certain statue is given to _uas
105 106
void UASUnitTest::getStatusForCode_test()
{
107 108 109
    QString state, desc;
    state = "";
    desc = "";
110

Don Gagne's avatar
Don Gagne committed
111
    _uas->getStatusForCode(MAV_STATE_UNINIT, state, desc);
112
    QVERIFY(state == "UNINIT");
113

Don Gagne's avatar
Don Gagne committed
114
    _uas->getStatusForCode(MAV_STATE_UNINIT, state, desc);
115
    QVERIFY(state == "UNINIT");
116

Don Gagne's avatar
Don Gagne committed
117
    _uas->getStatusForCode(MAV_STATE_BOOT, state, desc);
118
    QVERIFY(state == "BOOT");
119

Don Gagne's avatar
Don Gagne committed
120
    _uas->getStatusForCode(MAV_STATE_CALIBRATING, state, desc);
121
    QVERIFY(state == "CALIBRATING");
122

Don Gagne's avatar
Don Gagne committed
123
    _uas->getStatusForCode(MAV_STATE_ACTIVE, state, desc);
124
    QVERIFY(state == "ACTIVE");
125

Don Gagne's avatar
Don Gagne committed
126
    _uas->getStatusForCode(MAV_STATE_STANDBY, state, desc);
127
    QVERIFY(state == "STANDBY");
128

Don Gagne's avatar
Don Gagne committed
129
    _uas->getStatusForCode(MAV_STATE_CRITICAL, state, desc);
130
    QVERIFY(state == "CRITICAL");
131

Don Gagne's avatar
Don Gagne committed
132
    _uas->getStatusForCode(MAV_STATE_EMERGENCY, state, desc);
133
    QVERIFY(state == "EMERGENCY");
134

Don Gagne's avatar
Don Gagne committed
135
    _uas->getStatusForCode(MAV_STATE_POWEROFF, state, desc);
136
    QVERIFY(state == "SHUTDOWN");
137

Don Gagne's avatar
Don Gagne committed
138
    _uas->getStatusForCode(5325, state, desc);
139
    QVERIFY(state == "UNKNOWN");
140 141 142 143
}

void UASUnitTest::getLocalX_test()
{
Don Gagne's avatar
Don Gagne committed
144
   QCOMPARE(_uas->getLocalX(), 0.0);
145 146 147
}
void UASUnitTest::getLocalY_test()
{
Don Gagne's avatar
Don Gagne committed
148
    QCOMPARE(_uas->getLocalY(), 0.0);
149 150 151
}
void UASUnitTest::getLocalZ_test()
{
Don Gagne's avatar
Don Gagne committed
152
    QCOMPARE(_uas->getLocalZ(), 0.0);
153 154
}
void UASUnitTest::getLatitude_test()
Jessica's avatar
Jessica committed
155
{
Don Gagne's avatar
Don Gagne committed
156
    QCOMPARE(_uas->getLatitude(), 0.0);
157 158 159
}
void UASUnitTest::getLongitude_test()
{
Don Gagne's avatar
Don Gagne committed
160
    QCOMPARE(_uas->getLongitude(), 0.0);
161
}
Don Gagne's avatar
Don Gagne committed
162
void UASUnitTest::getAltitudeAMSL_test()
163
{
Don Gagne's avatar
Don Gagne committed
164
    QCOMPARE(_uas->getAltitudeAMSL(), 0.0);
Don Gagne's avatar
Don Gagne committed
165 166 167
}
void UASUnitTest::getAltitudeRelative_test()
{
Don Gagne's avatar
Don Gagne committed
168
    QCOMPARE(_uas->getAltitudeRelative(), 0.0);
169 170 171
}
void UASUnitTest::getRoll_test()
{
Don Gagne's avatar
Don Gagne committed
172
    QCOMPARE(_uas->getRoll(), 0.0);
173 174 175
}
void UASUnitTest::getPitch_test()
{
Don Gagne's avatar
Don Gagne committed
176
    QCOMPARE(_uas->getPitch(), 0.0);
177 178 179
}
void UASUnitTest::getYaw_test()
{
Don Gagne's avatar
Don Gagne committed
180
    QCOMPARE(_uas->getYaw(), 0.0);
181
}
182 183

void UASUnitTest::getSelected_test()
184
{
Don Gagne's avatar
Don Gagne committed
185
    QCOMPARE(_uas->getSelected(), false);
186 187
}

188
void UASUnitTest::getSystemType_test()
189
{    //check that system type is set to MAV_TYPE_GENERIC when initialized
Don Gagne's avatar
Don Gagne committed
190 191 192
    QCOMPARE(_uas->getSystemType(), 0);
    _uas->setSystemType(13);
    QCOMPARE(_uas->getSystemType(), 13);
193 194
}

195
void UASUnitTest::getAirframe_test()
196
{
Don Gagne's avatar
Don Gagne committed
197 198
    //when _uas is constructed, airframe is set to QGC_AIRFRAME_GENERIC
    QVERIFY(_uas->getAirframe() == UASInterface::QGC_AIRFRAME_GENERIC);
199
}
200

201 202
void UASUnitTest::setAirframe_test()
{
203
    //check at construction, that airframe=0 (GENERIC)
Don Gagne's avatar
Don Gagne committed
204
    QVERIFY(_uas->getAirframe() == UASInterface::QGC_AIRFRAME_GENERIC);
205 206

    //check that set airframe works
Don Gagne's avatar
Don Gagne committed
207 208
    _uas->setAirframe(UASInterface::QGC_AIRFRAME_HEXCOPTER);
    QVERIFY(_uas->getAirframe() == UASInterface::QGC_AIRFRAME_HEXCOPTER);
209 210 211

    //check that setAirframe will not assign a number to airframe, that is 
    //not defined in the enum 
Don Gagne's avatar
Don Gagne committed
212 213
    _uas->setAirframe(UASInterface::QGC_AIRFRAME_END_OF_ENUM);
    QVERIFY(_uas->getAirframe() == UASInterface::QGC_AIRFRAME_HEXCOPTER);
214
}
215

216
void UASUnitTest::getWaypointList_test()
217
{
Don Gagne's avatar
Don Gagne committed
218
    QList<Waypoint*> kk = _uas->getWaypointManager()->getWaypointEditableList();
219 220
    QCOMPARE(kk.count(), 0);

221
    Waypoint* wp = new Waypoint(0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,false, false, MAV_FRAME_GLOBAL, MAV_CMD_MISSION_START, "blah");
Don Gagne's avatar
Don Gagne committed
222
    _uas->getWaypointManager()->addWaypointEditable(wp, true);
223

Don Gagne's avatar
Don Gagne committed
224
    kk = _uas->getWaypointManager()->getWaypointEditableList();
225 226 227
    QCOMPARE(kk.count(), 1);

    wp = new Waypoint();
Don Gagne's avatar
Don Gagne committed
228
    _uas->getWaypointManager()->addWaypointEditable(wp, false);
229

Don Gagne's avatar
Don Gagne committed
230
    kk = _uas->getWaypointManager()->getWaypointEditableList();
231 232
    QCOMPARE(kk.count(), 2);

Don Gagne's avatar
Don Gagne committed
233 234
    _uas->getWaypointManager()->removeWaypoint(1);
    kk = _uas->getWaypointManager()->getWaypointEditableList();
235 236
    QCOMPARE(kk.count(), 1);

Don Gagne's avatar
Don Gagne committed
237 238
    _uas->getWaypointManager()->removeWaypoint(0);
    kk = _uas->getWaypointManager()->getWaypointEditableList();
239 240 241
    QCOMPARE(kk.count(), 0);

    qDebug()<<"disconnect SIGNAL waypointListChanged";
242

243 244 245 246
}

void UASUnitTest::getWaypoint_test()
{
Jessica's avatar
Jessica committed
247
    Waypoint* wp = new Waypoint(0,5.6,2.0,3.0,0.0,0.0,0.0,0.0,false, false, MAV_FRAME_GLOBAL, MAV_CMD_MISSION_START, "blah");
248

Don Gagne's avatar
Don Gagne committed
249
    _uas->getWaypointManager()->addWaypointEditable(wp, true);
250

Don Gagne's avatar
Don Gagne committed
251
    QList<Waypoint*> wpList = _uas->getWaypointManager()->getWaypointEditableList();
252 253 254 255

    QCOMPARE(wpList.count(), 1);
    QCOMPARE(static_cast<quint16>(0), static_cast<Waypoint*>(wpList.at(0))->getId());

Jessica's avatar
Jessica committed
256
    Waypoint*  wp3 = new Waypoint(1, 5.6, 2.0, 3.0);
Don Gagne's avatar
Don Gagne committed
257 258
    _uas->getWaypointManager()->addWaypointEditable(wp3, true);
    wpList = _uas->getWaypointManager()->getWaypointEditableList();
259 260
    Waypoint* wp2 = static_cast<Waypoint*>(wpList.at(0));

Jessica's avatar
Jessica committed
261
    QCOMPARE(wpList.count(), 2);
Jessica's avatar
Jessica committed
262 263 264
    QCOMPARE(wp3->getX(), wp2->getX());
    QCOMPARE(wp3->getY(), wp2->getY());
    QCOMPARE(wp3->getZ(), wp2->getZ());
Jessica's avatar
Jessica committed
265
    QCOMPARE(wpList.at(1)->getId(), static_cast<quint16>(1));
Jessica's avatar
Jessica committed
266 267
    QCOMPARE(wp3->getFrame(), MAV_FRAME_GLOBAL);
    QCOMPARE(wp3->getFrame(), wp2->getFrame());
Jessica's avatar
Jessica committed
268

Jessica's avatar
Jessica committed
269 270
    delete wp3;
    delete wp;
271 272
}

273
void UASUnitTest::signalWayPoint_test()
274
{
Don Gagne's avatar
Don Gagne committed
275
    QSignalSpy spy(_uas->getWaypointManager(), SIGNAL(waypointEditableListChanged()));
Jessica's avatar
Jessica committed
276

Jessica's avatar
Jessica committed
277
    Waypoint* wp = new Waypoint(0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,false, false, MAV_FRAME_GLOBAL, MAV_CMD_MISSION_START, "blah");
Don Gagne's avatar
Don Gagne committed
278
    _uas->getWaypointManager()->addWaypointEditable(wp, true);
279

280
    QCOMPARE(spy.count(), 1); // 1 listChanged for add wayPoint
Don Gagne's avatar
Don Gagne committed
281
    _uas->getWaypointManager()->removeWaypoint(0);
282
    QCOMPARE(spy.count(), 2); // 2 listChanged for remove wayPoint
Jessica's avatar
Jessica committed
283

Don Gagne's avatar
Don Gagne committed
284
    QSignalSpy spyDestroyed(_uas->getWaypointManager(), SIGNAL(destroyed()));
285 286 287
    QVERIFY(spyDestroyed.isValid());
    QCOMPARE( spyDestroyed.count(), 0 );

Don Gagne's avatar
Don Gagne committed
288 289 290
    delete _uas;// delete(destroyed) _uas for validating
    _uas = NULL;
    QCOMPARE(spyDestroyed.count(), 1);// count destroyed _uas should are 1
291
    _uas = new UAS(_mavlink, UASID);
Don Gagne's avatar
Don Gagne committed
292
    QSignalSpy spy2(_uas->getWaypointManager(), SIGNAL(waypointEditableListChanged()));
293
    QCOMPARE(spy2.count(), 0);
Jessica's avatar
Jessica committed
294
    Waypoint* wp2 = new Waypoint(0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,false, false, MAV_FRAME_GLOBAL, MAV_CMD_MISSION_START, "blah");
295

Don Gagne's avatar
Don Gagne committed
296
    _uas->getWaypointManager()->addWaypointEditable(wp2, true);
297 298
    QCOMPARE(spy2.count(), 1);

Don Gagne's avatar
Don Gagne committed
299 300
    _uas->getWaypointManager()->clearWaypointList();
    QList<Waypoint*> wpList = _uas->getWaypointManager()->getWaypointEditableList();
301
    QCOMPARE(wpList.count(), 1);
Don Gagne's avatar
Don Gagne committed
302 303
    delete _uas;
    _uas = NULL;
Jessica's avatar
Jessica committed
304
    delete wp2;
305
}