UASUnitTest.cc 12.5 KB
Newer Older
1
#include "UASUnitTest.h"
2
#include <stdio.h>
3
#include <QObject>
4 5 6
UASUnitTest::UASUnitTest()
{
}
7 8
//This function is called after every test
void UASUnitTest::init()
9
{
10
    mav = new MAVLinkProtocol();
11 12
    uas = new UAS(mav, UASID);
    uas->deleteSettings();
13
}
14 15
//this function is called after every test
void UASUnitTest::cleanup()
16
{
17 18 19 20 21
    delete uas;
    uas = NULL;

    delete mav;
    mav = NULL;
22 23 24 25 26 27 28 29 30 31 32 33
}

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

    // Test that the chosen ID was assigned at construction
    QCOMPARE(uas->getUASID(), UASID);

34
    // Make sure that no other ID was set
35 36
    QEXPECT_FAIL("", "When you set an ID it does not use the default ID of 0", Continue);
    QCOMPARE(uas->getUASID(), 0);
37 38 39

    // Make sure that ID >= 0
    QCOMPARE(uas->getUASID(), 100);
40

41 42 43 44
}

void UASUnitTest::getUASName_test()
{
45 46
  // Test that the name is build as MAV + ID
  QCOMPARE(uas->getUASName(), "MAV " + QString::number(UASID));
47 48 49 50 51

}

void UASUnitTest::getUpTime_test()
{
52 53 54 55
    UAS* uas2 = new UAS(mav);
    // Test that the uptime starts at zero to a
    // precision of seconds
    QCOMPARE(floor(uas2->getUptime()/1000.0), 0.0);
56

57 58
    // Sleep for three seconds
    QTest::qSleep(3000);
59

60 61 62
    // Test that the up time is computed correctly to a
    // precision of seconds
    QCOMPARE(floor(uas2->getUptime()/1000.0), 3.0);
63

64
    delete uas2;
65 66 67 68
}

void UASUnitTest::getCommunicationStatus_test()
{
69 70
    // Verify that upon construction the Comm status is disconnected
    QCOMPARE(uas->getCommunicationStatus(), static_cast<int>(UASInterface::COMM_DISCONNECTED));
71 72 73 74 75
}

void UASUnitTest::filterVoltage_test()
{
    float verificar=uas->filterVoltage(0.4f);
76 77 78 79 80

    // 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));
81
}
82

83 84
void UASUnitTest:: getAutopilotType_test()
{
85 86 87
    int type = uas->getAutopilotType();
    // Verify that upon construction the autopilot is set to -1
    QCOMPARE(type, -1);
88
}
89

90 91
void UASUnitTest::setAutopilotType_test()
{
92 93 94
    uas->setAutopilotType(2);
    // Verify that the autopilot is set
    QCOMPARE(uas->getAutopilotType(), 2);
95 96
}

97
//verify that the correct status is returned if a certain statue is given to uas
98 99
void UASUnitTest::getStatusForCode_test()
{
100 101 102
    QString state, desc;
    state = "";
    desc = "";
103

104 105
    uas->getStatusForCode(MAV_STATE_UNINIT, state, desc);
    QVERIFY(state == "UNINIT");
106

107 108
    uas->getStatusForCode(MAV_STATE_UNINIT, state, desc);
    QVERIFY(state == "UNINIT");
109

110 111
    uas->getStatusForCode(MAV_STATE_BOOT, state, desc);
    QVERIFY(state == "BOOT");
112

113 114
    uas->getStatusForCode(MAV_STATE_CALIBRATING, state, desc);
    QVERIFY(state == "CALIBRATING");
115

116 117
    uas->getStatusForCode(MAV_STATE_ACTIVE, state, desc);
    QVERIFY(state == "ACTIVE");
118

119 120
    uas->getStatusForCode(MAV_STATE_STANDBY, state, desc);
    QVERIFY(state == "STANDBY");
121

122 123
    uas->getStatusForCode(MAV_STATE_CRITICAL, state, desc);
    QVERIFY(state == "CRITICAL");
124

125 126
    uas->getStatusForCode(MAV_STATE_EMERGENCY, state, desc);
    QVERIFY(state == "EMERGENCY");
127

128 129
    uas->getStatusForCode(MAV_STATE_POWEROFF, state, desc);
    QVERIFY(state == "SHUTDOWN");
130

131 132
    uas->getStatusForCode(5325, state, desc);
    QVERIFY(state == "UNKNOWN");
133 134 135 136
}

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

void UASUnitTest::getSelected_test()
177
{
178
    QCOMPARE(uas->getSelected(), false);
179 180
}

181
void UASUnitTest::getSystemType_test()
182 183 184
{    //check that system type is set to MAV_TYPE_GENERIC when initialized
    QCOMPARE(uas->getSystemType(), 0);
    uas->setSystemType(13);
185
    QCOMPARE(uas->getSystemType(), 13);
186 187
}

188
void UASUnitTest::getAirframe_test()
189
{
190 191
    //when uas is constructed, airframe is set to QGC_AIRFRAME_GENERIC
    QVERIFY(uas->getAirframe() == UASInterface::QGC_AIRFRAME_GENERIC);
192
}
193

194 195
void UASUnitTest::setAirframe_test()
{
196
    //check at construction, that airframe=0 (GENERIC)
197
    QVERIFY(uas->getAirframe() == UASInterface::QGC_AIRFRAME_GENERIC);
198 199

    //check that set airframe works
200 201
    uas->setAirframe(UASInterface::QGC_AIRFRAME_HEXCOPTER);
    QVERIFY(uas->getAirframe() == UASInterface::QGC_AIRFRAME_HEXCOPTER);
202 203 204

    //check that setAirframe will not assign a number to airframe, that is 
    //not defined in the enum 
205 206
    uas->setAirframe(UASInterface::QGC_AIRFRAME_END_OF_ENUM);
    QVERIFY(uas->getAirframe() == UASInterface::QGC_AIRFRAME_HEXCOPTER);
207
}
208

209
void UASUnitTest::getWaypointList_test()
210
{
Don Gagne's avatar
Don Gagne committed
211
    QList<Waypoint*> kk = uas->getWaypointManager()->getWaypointEditableList();
212 213
    QCOMPARE(kk.count(), 0);

214 215
    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");
    uas->getWaypointManager()->addWaypointEditable(wp, true);
216

217
    kk = uas->getWaypointManager()->getWaypointEditableList();
218 219 220
    QCOMPARE(kk.count(), 1);

    wp = new Waypoint();
221
    uas->getWaypointManager()->addWaypointEditable(wp, false);
222

223
    kk = uas->getWaypointManager()->getWaypointEditableList();
224 225 226
    QCOMPARE(kk.count(), 2);

    uas->getWaypointManager()->removeWaypoint(1);
227
    kk = uas->getWaypointManager()->getWaypointEditableList();
228 229 230
    QCOMPARE(kk.count(), 1);

    uas->getWaypointManager()->removeWaypoint(0);
231
    kk = uas->getWaypointManager()->getWaypointEditableList();
232 233 234
    QCOMPARE(kk.count(), 0);

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

236 237 238 239
}

void UASUnitTest::getWaypoint_test()
{
Jessica's avatar
Jessica committed
240
    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");
241

242
    uas->getWaypointManager()->addWaypointEditable(wp, true);
243

Don Gagne's avatar
Don Gagne committed
244
    QList<Waypoint*> wpList = uas->getWaypointManager()->getWaypointEditableList();
245 246 247 248

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

Jessica's avatar
Jessica committed
249
    Waypoint*  wp3 = new Waypoint(1, 5.6, 2.0, 3.0);
Jessica's avatar
Jessica committed
250
    uas->getWaypointManager()->addWaypointEditable(wp3, true);
Jessica's avatar
Jessica committed
251
    wpList = uas->getWaypointManager()->getWaypointEditableList();
252 253
    Waypoint* wp2 = static_cast<Waypoint*>(wpList.at(0));

Jessica's avatar
Jessica committed
254
    QCOMPARE(wpList.count(), 2);
Jessica's avatar
Jessica committed
255 256 257
    QCOMPARE(wp3->getX(), wp2->getX());
    QCOMPARE(wp3->getY(), wp2->getY());
    QCOMPARE(wp3->getZ(), wp2->getZ());
Jessica's avatar
Jessica committed
258
    QCOMPARE(wpList.at(1)->getId(), static_cast<quint16>(1));
Jessica's avatar
Jessica committed
259 260
    QCOMPARE(wp3->getFrame(), MAV_FRAME_GLOBAL);
    QCOMPARE(wp3->getFrame(), wp2->getFrame());
Jessica's avatar
Jessica committed
261

Jessica's avatar
Jessica committed
262 263
    delete wp3;
    delete wp;
264 265
}

266
void UASUnitTest::signalWayPoint_test()
267
{
Jessica's avatar
Jessica committed
268 269
    QSignalSpy spy(uas->getWaypointManager(), SIGNAL(waypointEditableListChanged()));

Jessica's avatar
Jessica committed
270
    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");
271
    uas->getWaypointManager()->addWaypointEditable(wp, true);
272

273
    QCOMPARE(spy.count(), 1); // 1 listChanged for add wayPoint
274 275
    uas->getWaypointManager()->removeWaypoint(0);
    QCOMPARE(spy.count(), 2); // 2 listChanged for remove wayPoint
Jessica's avatar
Jessica committed
276

277 278 279 280 281
    QSignalSpy spyDestroyed(uas->getWaypointManager(), SIGNAL(destroyed()));
    QVERIFY(spyDestroyed.isValid());
    QCOMPARE( spyDestroyed.count(), 0 );

    delete uas;// delete(destroyed) uas for validating
Jessica's avatar
Jessica committed
282
    uas = NULL;
283 284
    QCOMPARE(spyDestroyed.count(), 1);// count destroyed uas should are 1
    uas = new UAS(mav,UASID);
Jessica's avatar
Jessica committed
285
    QSignalSpy spy2(uas->getWaypointManager(), SIGNAL(waypointEditableListChanged()));
286
    QCOMPARE(spy2.count(), 0);
Jessica's avatar
Jessica committed
287
    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");
288

Jessica's avatar
Jessica committed
289
    uas->getWaypointManager()->addWaypointEditable(wp2, true);
290 291 292
    QCOMPARE(spy2.count(), 1);

    uas->getWaypointManager()->clearWaypointList();
Don Gagne's avatar
Don Gagne committed
293
    QList<Waypoint*> wpList = uas->getWaypointManager()->getWaypointEditableList();
294
    QCOMPARE(wpList.count(), 1);
295 296
    delete uas;
    uas = NULL;
Jessica's avatar
Jessica committed
297
    delete wp2;
298 299
}

300
void UASUnitTest::signalUASLink_test()
301
{
302

303
    QSignalSpy spy(uas, SIGNAL(modeChanged(int,QString,QString)));
Don Gagne's avatar
Don Gagne committed
304
    uas->setMode(2, 0);
305 306 307 308
    QCOMPARE(spy.count(), 0);// not solve for UAS not receiving message from UAS

    QSignalSpy spyS(LinkManager::instance(), SIGNAL(newLink(LinkInterface*)));
    SerialLink* link = new SerialLink();
309

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    LinkManager::instance()->add(link);
    LinkManager::instance()->addProtocol(link, mav);
    QCOMPARE(spyS.count(), 1);

    LinkManager::instance()->add(link);
    LinkManager::instance()->addProtocol(link, mav);
    QCOMPARE(spyS.count(), 1);// not add SerialLink, exist in list

    SerialLink* link2 = new SerialLink();
    LinkManager::instance()->add(link2);
    LinkManager::instance()->addProtocol(link2, mav);
    QCOMPARE(spyS.count(), 2);// add SerialLink, not exist in list

    QList<LinkInterface*> links = LinkManager::instance()->getLinks();
    foreach(LinkInterface* link, links)
    {
        qDebug()<< link->getName();
        qDebug()<< QString::number(link->getId());
        QVERIFY(link != NULL);
        uas->addLink(link);
    }

    SerialLink* ff = static_cast<SerialLink*>(uas->getLinks()->at(0));

    QCOMPARE(ff->isConnected(), false);

    QCOMPARE(ff->isRunning(), false);

    QCOMPARE(ff->isFinished(), false);

    QCOMPARE(links.count(), uas->getLinks()->count());
    QCOMPARE(uas->getLinks()->count(), 2);

    LinkInterface* ff99 = static_cast<LinkInterface*>(links.at(1));
    LinkManager::instance()->removeLink(ff99);
345
    delete link2;
346 347 348 349 350 351 352

    QCOMPARE(LinkManager::instance()->getLinks().count(), 1);
    QCOMPARE(uas->getLinks()->count(), 2);

    QCOMPARE(static_cast<LinkInterface*>(LinkManager::instance()->getLinks().at(0))->getId(),
             static_cast<LinkInterface*>(uas->getLinks()->at(0))->getId());

353 354 355
    SerialLink* link3 = new SerialLink();
    LinkManager::instance()->add(link3);
    LinkManager::instance()->addProtocol(link3, mav);
356
    QCOMPARE(spyS.count(), 3);
357 358
    QCOMPARE(LinkManager::instance()->getLinks().count(), 2);

359 360
    //all the links in LinkManager must be deleted because LinkManager::instance
    //is static. 
361 362 363 364 365
    LinkManager::instance()->removeLink(link3);
    delete link3;
    QCOMPARE(LinkManager::instance()->getLinks().count(), 1);
    LinkManager::instance()->removeLink(link);
    delete link;
366

367
    QCOMPARE(LinkManager::instance()->getLinks().count(), 0);
368 369
}

370
void UASUnitTest::signalIdUASLink_test()
371
{
372 373

    QCOMPARE(LinkManager::instance()->getLinks().count(), 0);
374 375 376 377 378
    SerialLink* myLink = new SerialLink();
    myLink->setPortName("COM 17");
    LinkManager::instance()->add(myLink);
    LinkManager::instance()->addProtocol(myLink, mav);

379 380 381 382 383 384 385 386 387 388 389 390 391 392
    SerialLink* myLink2 = new SerialLink();
    myLink2->setPortName("COM 18");
    LinkManager::instance()->add(myLink2);
    LinkManager::instance()->addProtocol(myLink2, mav);

    SerialLink* myLink3 = new SerialLink();
    myLink3->setPortName("COM 19");
    LinkManager::instance()->add(myLink3);
    LinkManager::instance()->addProtocol(myLink3, mav);

    SerialLink* myLink4 = new SerialLink();
    myLink4->setPortName("COM 20");
    LinkManager::instance()->add(myLink4);
    LinkManager::instance()->addProtocol(myLink4, mav);
393 394 395 396 397

    QCOMPARE(LinkManager::instance()->getLinks().count(), 4);

    QList<LinkInterface*> links = LinkManager::instance()->getLinks();

398 399 400 401
    LinkInterface* a = static_cast<LinkInterface*>(links.at(0));
    LinkInterface* b = static_cast<LinkInterface*>(links.at(1));
    LinkInterface* c = static_cast<LinkInterface*>(links.at(2));
    LinkInterface* d = static_cast<LinkInterface*>(links.at(3));
402 403 404 405
    QCOMPARE(a->getName(), QString("COM 17"));
    QCOMPARE(b->getName(), QString("COM 18"));
    QCOMPARE(c->getName(), QString("COM 19"));
    QCOMPARE(d->getName(), QString("COM 20"));
406 407 408 409 410 411 412 413 414 415 416

    LinkManager::instance()->removeLink(myLink4);
    delete myLink4;
    LinkManager::instance()->removeLink(myLink3);
    delete myLink3;
    LinkManager::instance()->removeLink(myLink2);
    delete myLink2;
    LinkManager::instance()->removeLink(myLink);
    delete myLink;

    QCOMPARE(LinkManager::instance()->getLinks().count(), 0);
417
}