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

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

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

Don Gagne's avatar
Don Gagne committed
28 29
    delete _mavlink;
    _mavlink = NULL;
30 31 32 33 34
}

void UASUnitTest::getUASID_test()
{
    // Test a default ID of zero is assigned
Don Gagne's avatar
Don Gagne committed
35
    UAS* uas2 = new UAS(_mavlink, QThread::currentThread());
36 37 38 39
    QCOMPARE(uas2->getUASID(), 0);
    delete uas2;

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

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

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

49 50 51 52
}

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

}

void UASUnitTest::getUpTime_test()
{
Don Gagne's avatar
Don Gagne committed
60
    UAS* uas2 = new UAS(_mavlink, QThread::currentThread());
61 62 63
    // Test that the uptime starts at zero to a
    // precision of seconds
    QCOMPARE(floor(uas2->getUptime()/1000.0), 0.0);
64

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

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

72
    delete uas2;
73 74 75 76
}

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

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

    // 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));
89
}
90

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

222
    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
223
    _uas->getWaypointManager()->addWaypointEditable(wp, true);
224

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

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

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

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

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

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

244 245 246 247
}

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

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

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

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

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

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

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

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

Jessica's avatar
Jessica committed
278
    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
279
    _uas->getWaypointManager()->addWaypointEditable(wp, true);
280

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

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

Don Gagne's avatar
Don Gagne committed
289 290 291 292 293
    delete _uas;// delete(destroyed) _uas for validating
    _uas = NULL;
    QCOMPARE(spyDestroyed.count(), 1);// count destroyed _uas should are 1
    _uas = new UAS(_mavlink, QThread::currentThread(), UASID);
    QSignalSpy spy2(_uas->getWaypointManager(), SIGNAL(waypointEditableListChanged()));
294
    QCOMPARE(spy2.count(), 0);
Jessica's avatar
Jessica committed
295
    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");
296

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

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