SpeedSectionTest.cc 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "SpeedSectionTest.h"

SpeedSectionTest::SpeedSectionTest(void)
    : _spySpeed(NULL)
    , _spySection(NULL)
    , _speedSection(NULL)
{
    
}

void SpeedSectionTest::init(void)
{
    SectionTest::init();

    rgSpeedSignals[specifyFlightSpeedChangedIndex] = SIGNAL(specifyFlightSpeedChanged(bool));

    _speedSection = _simpleItem->speedSection();
    _createSpy(_speedSection, &_spySpeed);
    QVERIFY(_spySpeed);
    SectionTest::_createSpy(_speedSection, &_spySection);
    QVERIFY(_spySection);
}

void SpeedSectionTest::cleanup(void)
{
    delete _spySpeed;
    delete _spySection;
    SectionTest::cleanup();
}

void SpeedSectionTest::_createSpy(SpeedSection* speedSection, MultiSignalSpy** speedSpy)
{
    *speedSpy = NULL;
    MultiSignalSpy* spy = new MultiSignalSpy();
    QCOMPARE(spy->init(speedSection, rgSpeedSignals, cSpeedSignals), true);
    *speedSpy = spy;
}

void SpeedSectionTest::_testDirty(void)
{
    // Check for dirty not signalled if same value
    _speedSection->setSpecifyFlightSpeed(_speedSection->specifyFlightSpeed());
    QVERIFY(_spySection->checkNoSignals());
    QCOMPARE(_speedSection->dirty(), false);
    _speedSection->flightSpeed()->setRawValue(_speedSection->flightSpeed()->rawValue());
    QVERIFY(_spySection->checkNoSignals());
    QCOMPARE(_speedSection->dirty(), false);

    // Check for no duplicate dirty signalling on change
    _speedSection->setSpecifyFlightSpeed(!_speedSection->specifyFlightSpeed());
    QVERIFY(_spySection->checkSignalByMask(dirtyChangedMask));
    QCOMPARE(_spySection->pullBoolFromSignalIndex(dirtyChangedIndex), true);
    QCOMPARE(_speedSection->dirty(), true);
    _spySection->clearAllSignals();
    _speedSection->setSpecifyFlightSpeed(!_speedSection->specifyFlightSpeed());
    QVERIFY(_spySection->checkNoSignalByMask(dirtyChangedMask));
    QCOMPARE(_speedSection->dirty(), true);
    _spySection->clearAllSignals();

    // Check that the dirty bit can be cleared
    _speedSection->setDirty(false);
    QVERIFY(_spySection->checkSignalByMask(dirtyChangedMask));
    QCOMPARE(_spySection->pullBoolFromSignalIndex(dirtyChangedIndex), false);
    QCOMPARE(_speedSection->dirty(), false);
    _spySection->clearAllSignals();

76 77 78 79 80 81 82 83 84 85 86 87
    // Flight speed change should only signal if specifyFlightSpeed is set

    _speedSection->setSpecifyFlightSpeed(false);
    _speedSection->setDirty(false);
    _spySection->clearAllSignals();
    _speedSection->flightSpeed()->setRawValue(_speedSection->flightSpeed()->rawValue().toDouble() + 1);
    QVERIFY(_spySection->checkNoSignalByMask(dirtyChangedMask));
    QCOMPARE(_speedSection->dirty(), false);

    _speedSection->setSpecifyFlightSpeed(true);
    _speedSection->setDirty(false);
    _spySection->clearAllSignals();
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    _speedSection->flightSpeed()->setRawValue(_speedSection->flightSpeed()->rawValue().toDouble() + 1);
    QVERIFY(_spySection->checkSignalByMask(dirtyChangedMask));
    QCOMPARE(_spySection->pullBoolFromSignalIndex(dirtyChangedIndex), true);
    QCOMPARE(_speedSection->dirty(), true);
}

void SpeedSectionTest::_testSettingsAvailable(void)
{
    // No settings specified to start
    QCOMPARE(_speedSection->specifyFlightSpeed(), false);
    QCOMPARE(_speedSection->settingsSpecified(), false);

    // Check correct reaction to specifyFlightSpeed on/off

    _speedSection->setSpecifyFlightSpeed(true);
    QCOMPARE(_speedSection->specifyFlightSpeed(), true);
    QCOMPARE(_speedSection->settingsSpecified(), true);
    QVERIFY(_spySpeed->checkSignalByMask(specifyFlightSpeedChangedMask));
    QCOMPARE(_spySpeed->pullBoolFromSignalIndex(specifyFlightSpeedChangedIndex), true);
    QVERIFY(_spySection->checkSignalByMask(settingsSpecifiedChangedMask));
    QCOMPARE(_spySection->pullBoolFromSignalIndex(settingsSpecifiedChangedIndex), true);
    _spySection->clearAllSignals();
    _spySpeed->clearAllSignals();

    _speedSection->setSpecifyFlightSpeed(false);
    QCOMPARE(_speedSection->specifyFlightSpeed(), false);
    QCOMPARE(_speedSection->settingsSpecified(), false);
    QVERIFY(_spySpeed->checkSignalByMask(specifyFlightSpeedChangedMask));
    QCOMPARE(_spySpeed->pullBoolFromSignalIndex(specifyFlightSpeedChangedIndex), false);
    QVERIFY(_spySection->checkSignalByMask(settingsSpecifiedChangedMask));
    QCOMPARE(_spySection->pullBoolFromSignalIndex(settingsSpecifiedChangedIndex), false);
    _spySection->clearAllSignals();
    _spySpeed->clearAllSignals();
}

void SpeedSectionTest::_checkAvailable(void)
{
    MissionItem missionItem(1,              // sequence number
                            MAV_CMD_NAV_TAKEOFF,
                            MAV_FRAME_GLOBAL_RELATIVE_ALT,
                            10.1234567,     // param 1-7
                            20.1234567,
                            30.1234567,
                            40.1234567,
                            50.1234567,
                            60.1234567,
                            70.1234567,
                            true,           // autoContinue
                            false);         // isCurrentItem
137
    SimpleMissionItem* item = new SimpleMissionItem(_offlineVehicle, false /* flyView */, missionItem, this);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    QVERIFY(item->speedSection());
    QCOMPARE(item->speedSection()->available(), false);
}

void SpeedSectionTest::_testItemCount(void)
{
    // No settings specified to start
    QCOMPARE(_speedSection->itemCount(), 0);

    _speedSection->setSpecifyFlightSpeed(true);
    QCOMPARE(_speedSection->itemCount(), 1);
    QVERIFY(_spySection->checkSignalByMask(itemCountChangedMask));
    QCOMPARE(_spySection->pullIntFromSignalIndex(itemCountChangedIndex), 1);
    _spySection->clearAllSignals();
    _spySpeed->clearAllSignals();

    _speedSection->setSpecifyFlightSpeed(false);
    QCOMPARE(_speedSection->itemCount(), 0);
    QVERIFY(_spySection->checkSignalByMask(itemCountChangedMask));
    QCOMPARE(_spySection->pullIntFromSignalIndex(itemCountChangedIndex), 0);
    _spySection->clearAllSignals();
    _spySpeed->clearAllSignals();
}

void SpeedSectionTest::_testAppendSectionItems(void)
{
    int seqNum = 0;
    QList<MissionItem*> rgMissionItems;

    // No settings specified to start
    QCOMPARE(_speedSection->itemCount(), 0);

    _speedSection->appendSectionItems(rgMissionItems, this, seqNum);
    QCOMPARE(rgMissionItems.count(), 0);
    QCOMPARE(seqNum, 0);
    rgMissionItems.clear();

    _speedSection->setSpecifyFlightSpeed(true);
    _speedSection->appendSectionItems(rgMissionItems, this, seqNum);
    QCOMPARE(rgMissionItems.count(), 1);
    QCOMPARE(seqNum, 1);
    MissionItem expectedSpeedItem(0, MAV_CMD_DO_CHANGE_SPEED, MAV_FRAME_MISSION, _offlineVehicle->multiRotor() ? 1 : 0, _speedSection->flightSpeed()->rawValue().toDouble(), -1, 0, 0, 0, 0, true, false);
    _missionItemsEqual(*rgMissionItems[0], expectedSpeedItem);
}

void SpeedSectionTest::_testScanForSection(void)
{
    QCOMPARE(_speedSection->available(), true);

    int scanIndex = 0;
    QmlObjectListModel visualItems;

    _commonScanTest(_speedSection);

    // Check for a scan success

    double flightSpeed = 10.123456;
    MissionItem validSpeedItem(0, MAV_CMD_DO_CHANGE_SPEED, MAV_FRAME_MISSION, _offlineVehicle->multiRotor() ? 1 : 0, flightSpeed, -1, 0, 0, 0, 0, true, false);
196
    SimpleMissionItem simpleItem(_offlineVehicle, false /* flyView */, validSpeedItem, NULL);
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    MissionItem& simpleMissionItem = simpleItem.missionItem();
    visualItems.append(&simpleItem);
    scanIndex = 0;
    QCOMPARE(_speedSection->scanForSection(&visualItems, scanIndex), true);
    QCOMPARE(visualItems.count(), 0);
    QCOMPARE(_speedSection->settingsSpecified(), true);
    QCOMPARE(_speedSection->specifyFlightSpeed(), true);
    QCOMPARE(_speedSection->flightSpeed()->rawValue().toDouble(), flightSpeed);
    _speedSection->setSpecifyFlightSpeed(false);
    visualItems.clear();
    scanIndex = 0;

    // Flight speed command but incorrect settings

    simpleMissionItem = validSpeedItem;
    simpleMissionItem.setParam1(_offlineVehicle->multiRotor() ? 0 : 1);
    visualItems.append(&simpleItem);
    QCOMPARE(_speedSection->scanForSection(&visualItems, scanIndex), false);
    QCOMPARE(visualItems.count(), 1);
    QCOMPARE(_speedSection->settingsSpecified(), false);
    visualItems.clear();
    scanIndex = 0;

    simpleMissionItem = validSpeedItem;
    simpleMissionItem.setParam3(50);
    visualItems.append(&simpleItem);
    QCOMPARE(_speedSection->scanForSection(&visualItems, scanIndex), false);
    QCOMPARE(visualItems.count(), 1);
    QCOMPARE(_speedSection->settingsSpecified(), false);
    visualItems.clear();
    scanIndex = 0;

    simpleMissionItem = validSpeedItem;
    simpleMissionItem.setParam4(1);
    visualItems.append(&simpleItem);
    QCOMPARE(_speedSection->scanForSection(&visualItems, scanIndex), false);
    QCOMPARE(visualItems.count(), 1);
    QCOMPARE(_speedSection->settingsSpecified(), false);
    visualItems.clear();
    scanIndex = 0;

    simpleMissionItem = validSpeedItem;
    simpleMissionItem.setParam5(1);
    visualItems.append(&simpleItem);
    QCOMPARE(_speedSection->scanForSection(&visualItems, scanIndex), false);
    QCOMPARE(visualItems.count(), 1);
    QCOMPARE(_speedSection->settingsSpecified(), false);
    visualItems.clear();
    scanIndex = 0;

    simpleMissionItem = validSpeedItem;
    simpleMissionItem.setParam6(1);
    visualItems.append(&simpleItem);
    QCOMPARE(_speedSection->scanForSection(&visualItems, scanIndex), false);
    QCOMPARE(visualItems.count(), 1);
    QCOMPARE(_speedSection->settingsSpecified(), false);
    visualItems.clear();
    scanIndex = 0;

    simpleMissionItem = validSpeedItem;
    simpleMissionItem.setParam7(1);
    visualItems.append(&simpleItem);
    QCOMPARE(_speedSection->scanForSection(&visualItems, scanIndex), false);
    QCOMPARE(visualItems.count(), 1);
    QCOMPARE(_speedSection->settingsSpecified(), false);
    visualItems.clear();
    scanIndex = 0;

    // Valid item in wrong position
    MissionItem waypointMissionItem(0, MAV_CMD_NAV_WAYPOINT, MAV_FRAME_GLOBAL_RELATIVE_ALT, 0, 0, 0, 0, 0, 0, 0, true, false);
267
    SimpleMissionItem simpleWaypointItem(_offlineVehicle, false /* flyView */, waypointMissionItem, NULL);
268 269 270 271 272 273 274 275 276
    simpleMissionItem = validSpeedItem;
    visualItems.append(&simpleWaypointItem);
    visualItems.append(&simpleMissionItem);
    QCOMPARE(_speedSection->scanForSection(&visualItems, scanIndex), false);
    QCOMPARE(visualItems.count(), 2);
    QCOMPARE(_speedSection->settingsSpecified(), false);
    visualItems.clear();
    scanIndex = 0;
}
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

void SpeedSectionTest::_testSpecifiedFlightSpeedChanged(void)
{
    // specifiedFlightSpeedChanged SHOULD NOT signal if flight speed is changed when specifyFlightSpeed IS NOT set
    _speedSection->setSpecifyFlightSpeed(false);
    _spySpeed->clearAllSignals();
    _speedSection->flightSpeed()->setRawValue(_speedSection->flightSpeed()->rawValue().toDouble() + 1);
    QVERIFY(_spySpeed->checkNoSignalByMask(specifiedFlightSpeedChangedMask));

    // specifiedFlightSpeedChanged SHOULD signal if flight speed is changed when specifyFlightSpeed IS set
    _speedSection->setSpecifyFlightSpeed(true);
    _spySpeed->clearAllSignals();
    _speedSection->flightSpeed()->setRawValue(_speedSection->flightSpeed()->rawValue().toDouble() + 1);
    QVERIFY(_spySpeed->checkSignalByMask(specifiedFlightSpeedChangedMask));
}