UASUnitTest.cc 8.47 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::filterVoltage_test()
{
Don Gagne's avatar
Don Gagne committed
76
    float verificar=_uas->filterVoltage(0.4f);
77 78 79 80 81

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

84 85
void UASUnitTest:: getAutopilotType_test()
{
Don Gagne's avatar
Don Gagne committed
86
    int type = _uas->getAutopilotType();
87 88
    // Verify that upon construction the autopilot is set to -1
    QCOMPARE(type, -1);
89
}
90

91 92
void UASUnitTest::setAutopilotType_test()
{
Don Gagne's avatar
Don Gagne committed
93
    _uas->setAutopilotType(2);
94
    // Verify that the autopilot is set
Don Gagne's avatar
Don Gagne committed
95
    QCOMPARE(_uas->getAutopilotType(), 2);
96 97
}

Don Gagne's avatar
Don Gagne committed
98
//verify that the correct status is returned if a certain statue is given to _uas
99 100
void UASUnitTest::getStatusForCode_test()
{
101 102 103
    QString state, desc;
    state = "";
    desc = "";
104

Don Gagne's avatar
Don Gagne committed
105
    _uas->getStatusForCode(MAV_STATE_UNINIT, state, desc);
106
    QVERIFY(state == "UNINIT");
107

Don Gagne's avatar
Don Gagne committed
108
    _uas->getStatusForCode(MAV_STATE_UNINIT, state, desc);
109
    QVERIFY(state == "UNINIT");
110

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

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

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

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

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

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

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

Don Gagne's avatar
Don Gagne committed
132
    _uas->getStatusForCode(5325, state, desc);
133
    QVERIFY(state == "UNKNOWN");
134 135 136 137
}

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

void UASUnitTest::getSelected_test()
178
{
Don Gagne's avatar
Don Gagne committed
179
    QCOMPARE(_uas->getSelected(), false);
180 181
}

182
void UASUnitTest::getSystemType_test()
183
{    //check that system type is set to MAV_TYPE_GENERIC when initialized
Don Gagne's avatar
Don Gagne committed
184 185 186
    QCOMPARE(_uas->getSystemType(), 0);
    _uas->setSystemType(13);
    QCOMPARE(_uas->getSystemType(), 13);
187 188
}

189
void UASUnitTest::getAirframe_test()
190
{
Don Gagne's avatar
Don Gagne committed
191 192
    //when _uas is constructed, airframe is set to QGC_AIRFRAME_GENERIC
    QVERIFY(_uas->getAirframe() == UASInterface::QGC_AIRFRAME_GENERIC);
193
}
194

195 196
void UASUnitTest::setAirframe_test()
{
197
    //check at construction, that airframe=0 (GENERIC)
Don Gagne's avatar
Don Gagne committed
198
    QVERIFY(_uas->getAirframe() == UASInterface::QGC_AIRFRAME_GENERIC);
199 200

    //check that set airframe works
Don Gagne's avatar
Don Gagne committed
201 202
    _uas->setAirframe(UASInterface::QGC_AIRFRAME_HEXCOPTER);
    QVERIFY(_uas->getAirframe() == UASInterface::QGC_AIRFRAME_HEXCOPTER);
203 204 205

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

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

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");
Don Gagne's avatar
Don Gagne committed
216
    _uas->getWaypointManager()->addWaypointEditable(wp, true);
217

Don Gagne's avatar
Don Gagne committed
218
    kk = _uas->getWaypointManager()->getWaypointEditableList();
219 220 221
    QCOMPARE(kk.count(), 1);

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

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

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

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

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

237 238 239 240
}

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

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

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

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

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

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

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

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

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

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

Don Gagne's avatar
Don Gagne committed
278
    QSignalSpy spyDestroyed(_uas->getWaypointManager(), SIGNAL(destroyed()));
279 280 281
    QVERIFY(spyDestroyed.isValid());
    QCOMPARE( spyDestroyed.count(), 0 );

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

Don Gagne's avatar
Don Gagne committed
290
    _uas->getWaypointManager()->addWaypointEditable(wp2, true);
291 292
    QCOMPARE(spy2.count(), 1);

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