FlightModeConfigTest.cc 10.6 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 76 77 78 79 80 81 82 83 84 85 86 87 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 137 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 196 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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

#include "FlightModeConfigTest.h"
#include "FlightModeConfig.h"
#include "UASManager.h"
#include "MockQGCUASParamManager.h"

/// @file
///     @brief FlightModeConfig unit test
///
///     @author Don Gagne <don@thegagnes.com>

FlightModeConfigUnitTest::FlightModeConfigUnitTest(void) :
    _mockUASManager(NULL)
{
    
}

void FlightModeConfigUnitTest::init(void)
{
    _mockUASManager = new MockUASManager();
    Q_ASSERT(_mockUASManager);
    
    UASManager::setMockUASManager(_mockUASManager);
    
    _mockUAS = new MockUAS();
    Q_ASSERT(_mockUAS);
}

void FlightModeConfigUnitTest::cleanup(void)
{
    Q_ASSERT(_mockUAS);
    delete _mockUAS;
    
    UASManager::setMockUASManager(NULL);

    Q_ASSERT(_mockUASManager);
    delete _mockUASManager;
}

void FlightModeConfigUnitTest::_nullUAS_test(void)
{
    // When there is no UAS the widget should be disabled
    FlightModeConfig* fmc = new FlightModeConfig();
    QCOMPARE(fmc->isEnabled(), false);
}

void FlightModeConfigUnitTest::_validUAS_test(void)
{
    // With an active UAS widget should be enabled
    _mockUASManager->setMockActiveUAS(_mockUAS);
    FlightModeConfig* fmc = new FlightModeConfig();
    QCOMPARE(fmc->isEnabled(), true);
}

void FlightModeConfigUnitTest::_nullToValidUAS_test(void)
{
    // start with no UAS
    FlightModeConfig* fmc = new FlightModeConfig();
    
    // clear it out and validate widget gets disabled
    _mockUASManager->setMockActiveUAS(NULL);
    QCOMPARE(fmc->isEnabled(), false);
}

void FlightModeConfigUnitTest::_simpleModeFixedWing_test(void)
{
    // Fixed wing does not have simple mode, all checkboxes should be disabled
    _mockUAS->setMockSystemType(MAV_TYPE_FIXED_WING);
    _mockUASManager->setMockActiveUAS(_mockUAS);
    FlightModeConfig* fmc = new FlightModeConfig();
    _findControls(fmc);

    for (size_t i=0; i<_cCombo; i++) {
        QCOMPARE(_rgSimpleModeCheckBox[i]->isEnabled(), false);
    }
}

void FlightModeConfigUnitTest::_simpleModeRover_test(void)
{
    // Rover does not have simple mode, all checkboxes should be disabled
    _mockUAS->setMockSystemType(MAV_TYPE_GROUND_ROVER);
    _mockUASManager->setMockActiveUAS(_mockUAS);
    FlightModeConfig* fmc = new FlightModeConfig();
    _findControls(fmc);
    
    for (size_t i=0; i<_cCombo; i++) {
        QCOMPARE(_rgSimpleModeCheckBox[i]->isEnabled(), false);
    }
}

void FlightModeConfigUnitTest::_simpleModeRotor_test(void)
{
    // Rotor has simple mode, all checkboxes should be enabled
    _mockUAS->setMockSystemType(MAV_TYPE_QUADROTOR);
    _mockUASManager->setMockActiveUAS(_mockUAS);
    FlightModeConfig* fmc = new FlightModeConfig();
    _findControls(fmc);
    
    for (size_t i=0; i<_cCombo; i++) {
        QCOMPARE(_rgSimpleModeCheckBox[i]->isEnabled(), true);
    }
}

void FlightModeConfigUnitTest::_modeSwitchParam_test(void)
{
    _mockUAS->setMockSystemType(MAV_TYPE_QUADROTOR);
    _mockUASManager->setMockActiveUAS(_mockUAS);
    FlightModeConfig* fmc = new FlightModeConfig();
    _findControls(fmc);
    
    const char* rgModeSwitchParam[_cCombo] = { "FLTMODE1", "FLTMODE2", "FLTMODE3", "FLTMODE4", "FLTMODE5", "FLTMODE6" };
    const int rgModeSwitchValue[_cCombo] = { 0, 2, 4, 6, 8, 10 };
    
    MockQGCUASParamManager::ParamMap_t mapParams;
    for (size_t i=0; i<_cCombo; i++) {
        mapParams[rgModeSwitchParam[i]] = QVariant(QChar(rgModeSwitchValue[i]));
    }
    
    int simpleModeBitMask = 21;
    mapParams["SIMPLE"] = QVariant(QChar(simpleModeBitMask));
    _mockUAS->setMockParametersAndSignal(mapParams);
    
    // Check that the UI is showing the correct information
    for (size_t i=0; i<_cCombo; i++) {
        QComboBox* combo = _rgCombo[i];
        
        // Check for the correct selection in the combo boxes. Combo boxes store the mode
        // in the item data, so use that to compare
        QCOMPARE(combo->itemData(combo->currentIndex()), mapParams[rgModeSwitchParam[i]]);
        
        // Make sure the text for the current selection doesn't contain the text Unknown
        // which means that it received an unsupported mode
        QCOMPARE(combo->currentText().contains("unknown", Qt::CaseInsensitive), QBool(false));

        // Check that the right simple mode check boxes are checked
        QCOMPARE(_rgSimpleModeCheckBox[i]->isChecked(), !!((1 << i) & simpleModeBitMask));
    }
    
    
    // Click Save button and make sure we get the same values back through setParameter calls
    QTest::mouseClick(_saveButton, Qt::LeftButton);
    MockQGCUASParamManager* paramMgr = _mockUAS->getMockQGCUASParamManager();
    MockQGCUASParamManager::ParamMap_t mapParamsSet = paramMgr->getMockSetParameters();
    QMapIterator<QString, QVariant> i(mapParams);
    while (i.hasNext()) {
        i.next();
        QCOMPARE(mapParamsSet.contains(i.key()), true);
        int receivedValue = mapParamsSet[i.key()].toInt();
        int expectedValue = i.value().toInt();
        QCOMPARE(receivedValue, expectedValue);
    }
}

void FlightModeConfigUnitTest::_pwmTestWorker(int systemType, int modeSwitchChannel, const char* modeSwitchParam)
{
    _mockUAS->setMockSystemType(systemType);
    _mockUASManager->setMockActiveUAS(_mockUAS);
    FlightModeConfig* fmc = new FlightModeConfig();
    _findControls(fmc);
    
    MockQGCUASParamManager::ParamMap_t mapParams;
    if (modeSwitchParam != NULL) {
        // Param is 1-based, code in here is all 0-base
        mapParams[modeSwitchParam] = QVariant(QChar(modeSwitchChannel+1));
        _mockUAS->setMockParametersAndSignal(mapParams);
    }
    
    const int pwmBoundary[] = { 1230, 1360, 1490, 1620, 1749, 1900 };
    
    int lowerPWM = 0;
    for (size_t i=0; i<_cCombo; i++) {
        // emit a PWM value at the mid point of the switch position
        int pwmMidPoint = ((pwmBoundary[i] - lowerPWM) / 2) + lowerPWM;
        _mockUAS->emitRemoteControlChannelRawChanged(modeSwitchChannel, pwmMidPoint);
        
        // Make sure that only the correct pwm label has a style set on it for highlight
        for (size_t j=0; j<_cCombo; j++) {
            if (j == i) {
                QVERIFY(_rgPWMLabel[j]->styleSheet().length() > 0);
            } else {
                QCOMPARE(_rgPWMLabel[j]->styleSheet().length(), 0);
            }
        }
        
        lowerPWM = pwmBoundary[i];
    }
}

void FlightModeConfigUnitTest::_pwmFixedWing_test(void)
{
    // FixedWing has mode switch channel on FLTMODE_CH param
    _pwmTestWorker(MAV_TYPE_FIXED_WING, 7, "FLTMODE_CH");
}

void FlightModeConfigUnitTest::_pwmRotor_test(void)
{
    // Rotor is hardwired to 0-based rc channel 4 for mode wsitch
    _pwmTestWorker(MAV_TYPE_QUADROTOR, 4, NULL);
}

void FlightModeConfigUnitTest::_pwmRover_test(void)
{
    // Rover has mode switch channel on MODE_CH param
    _pwmTestWorker(MAV_TYPE_GROUND_ROVER, 7, "MODE_CH");
}

void FlightModeConfigUnitTest::_pwmInvalidChannel_test(void)
{
    // Rover has mode switch channel on MODE_CH param
    _mockUAS->setMockSystemType(MAV_TYPE_GROUND_ROVER);
    _mockUASManager->setMockActiveUAS(_mockUAS);
    FlightModeConfig* fmc = new FlightModeConfig();
    _findControls(fmc);
    
    int modeSwitchChannel = 7;
    MockQGCUASParamManager::ParamMap_t mapParams;
    mapParams["MODE_CH"] = QVariant(QChar(modeSwitchChannel+1));    // 1-based
    _mockUAS->setMockParametersAndSignal(mapParams);
    
    const int pwmBoundary[] = { 1230, 1360, 1490, 1620, 1749, 1900 };
    
    int lowerPWM = 0;
    for (size_t i=0; i<_cCombo; i++) {
        // emit a PWM value at the mid point of the switch position
        int pwmMidPoint = ((pwmBoundary[i] - lowerPWM) / 2) + lowerPWM;
        // emit rc values on wrong channel
        _mockUAS->emitRemoteControlChannelRawChanged(modeSwitchChannel-1, pwmMidPoint);
        
        // Make sure no label have a style set on it for highlight
        for (size_t j=0; j<_cCombo; j++) {
            QCOMPARE(_rgPWMLabel[j]->styleSheet().length(), 0);
        }
        
        lowerPWM = pwmBoundary[i];
    }
}

void FlightModeConfigUnitTest::_unknownSystemType_test(void)
{
    // Set the system type to something we can't handle, make sure we are disabled
    _mockUAS->setMockSystemType(MAV_TYPE_ENUM_END);
    _mockUASManager->setMockActiveUAS(_mockUAS);
    FlightModeConfig* fmc = new FlightModeConfig();
    QCOMPARE(fmc->isEnabled(), false);
}

void FlightModeConfigUnitTest::_unknownMode_test(void)
{
    _mockUAS->setMockSystemType(MAV_TYPE_QUADROTOR);
    _mockUASManager->setMockActiveUAS(_mockUAS);
    FlightModeConfig* fmc = new FlightModeConfig();
    _findControls(fmc);
    
    // Set an unknown mode
    MockQGCUASParamManager::ParamMap_t mapParams;
    mapParams["FLTMODE1"] = QVariant(QChar(100));
    _mockUAS->setMockParametersAndSignal(mapParams);
    
    // Check for the correct selection in the combo boxes. Combo boxes store the mode
    // in the item data, so use that to compare
    QCOMPARE(_rgCombo[0]->itemData(_rgCombo[0]->currentIndex()), mapParams["FLTMODE1"]);
    
    // Make sure the text for the current selection contains the text Unknown
    QCOMPARE(_rgCombo[0]->currentText().contains("unknown", Qt::CaseInsensitive), QBool(true));
}

void FlightModeConfigUnitTest::_findControls(QObject* fmc)
{
   // Find all the controls
   for (size_t i=0; i<_cCombo; i++) {
       _rgLabel[i] = fmc->findChild<QLabel*>(QString("mode%1Label").arg(i));
       _rgCombo[i] = fmc->findChild<QComboBox*>(QString("mode%1ComboBox").arg(i));
       _rgSimpleModeCheckBox[i] = fmc->findChild<QCheckBox*>(QString("mode%1SimpleCheckBox").arg(i));
       _rgPWMLabel[i] = fmc->findChild<QLabel*>(QString("mode%1PWMLabel").arg(i));
       Q_ASSERT(_rgLabel[i]);
       Q_ASSERT(_rgCombo[i]);
       Q_ASSERT(_rgSimpleModeCheckBox[i]);
       Q_ASSERT(_rgPWMLabel[i]);
   }
    
    _saveButton = fmc->findChild<QPushButton*>("savePushButton");
    Q_ASSERT(_saveButton);
}