FactSystemTestBase.cc 6.65 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
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
/*=====================================================================
 
 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/>.
 
 ======================================================================*/

/// @file
///     @author Don Gagne <don@thegagnes.com>

27
#include "FactSystemTestBase.h"
Don Gagne's avatar
Don Gagne committed
28 29 30 31 32 33 34 35 36 37
#include "LinkManager.h"
#include "MockLink.h"
#include "AutoPilotPluginManager.h"
#include "UASManager.h"
#include "QGCApplication.h"
#include "QGCQuickWidget.h"

#include <QQuickItem>

/// FactSystem Unit Test
38
FactSystemTestBase::FactSystemTestBase(void)
Don Gagne's avatar
Don Gagne committed
39 40 41 42
{
    
}

43
void FactSystemTestBase::_init(MAV_AUTOPILOT autopilot)
Don Gagne's avatar
Don Gagne committed
44 45 46 47 48 49
{
    UnitTest::init();
    
    LinkManager* _linkMgr = LinkManager::instance();
    
    MockLink* link = new MockLink();
50
    link->setAutopilotType(autopilot);
51
    _linkMgr->_addLink(link);
Don Gagne's avatar
Don Gagne committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    _linkMgr->connectLink(link);
    
    // Wait for the uas to work it's way through the various threads

    QSignalSpy spyUas(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)));
    QCOMPARE(spyUas.wait(5000), true);
    
    _uas = UASManager::instance()->getActiveUAS();
    Q_ASSERT(_uas);
    
    _paramMgr = _uas->getParamManager();
    Q_ASSERT(_paramMgr);
    
    // Get the plugin for the uas
    
    AutoPilotPluginManager* pluginMgr = AutoPilotPluginManager::instance();
    Q_ASSERT(pluginMgr);
    
    _plugin = pluginMgr->getInstanceForAutoPilotPlugin(_uas);
    Q_ASSERT(_plugin);

    // Wait for the plugin to be ready
    
75 76
    QSignalSpy spyPlugin(_plugin, SIGNAL(pluginReadyChanged(bool)));
    if (!_plugin->pluginReady()) {
Don Gagne's avatar
Don Gagne committed
77 78
        QCOMPARE(spyPlugin.wait(5000), true);
    }
79
    Q_ASSERT(_plugin->pluginReady());
Don Gagne's avatar
Don Gagne committed
80 81
}

82
void FactSystemTestBase::_cleanup(void)
Don Gagne's avatar
Don Gagne committed
83 84 85 86 87
{
    UnitTest::cleanup();
}

/// Basic test of parameter values in Fact System
88
void FactSystemTestBase::_parameter_default_component_id_test(void)
Don Gagne's avatar
Don Gagne committed
89
{
90
    // Compare the value in the Parameter Manager with the value from the FactSystem.
Don Gagne's avatar
Don Gagne committed
91
    
92 93 94 95 96 97 98
    QVERIFY(_plugin->factExists(FactSystem::ParameterProvider, FactSystem::defaultComponentId, "RC_MAP_THROTTLE"));
    Fact* fact = _plugin->getFact(FactSystem::ParameterProvider, FactSystem::defaultComponentId, "RC_MAP_THROTTLE");
    QVERIFY(fact != NULL);
    QVariant factValue = fact->value();
    QCOMPARE(factValue.isValid(), true);
    QVariant paramValue;
    Q_ASSERT(_paramMgr->getParameterValue(_paramMgr->getDefaultComponentId(), "RC_MAP_THROTTLE", paramValue));
Don Gagne's avatar
Don Gagne committed
99
    
100 101 102 103 104 105
    QCOMPARE(factValue.toInt(), paramValue.toInt());
}

void FactSystemTestBase::_parameter_specific_component_id_test(void)
{
    // Compare the value in the Parameter Manager with the value from the FactSystem.
Don Gagne's avatar
Don Gagne committed
106
    
107 108
    QVERIFY(_plugin->factExists(FactSystem::ParameterProvider, 50, "RC_MAP_THROTTLE"));
    Fact* fact = _plugin->getFact(FactSystem::ParameterProvider, 50, "RC_MAP_THROTTLE");
Don Gagne's avatar
Don Gagne committed
109 110 111 112
    QVERIFY(fact != NULL);
    QVariant factValue = fact->value();
    QCOMPARE(factValue.isValid(), true);
    
113
    
Don Gagne's avatar
Don Gagne committed
114
    QVariant paramValue;
115 116 117 118 119 120 121 122 123 124 125 126
    Q_ASSERT(_paramMgr->getParameterValue(50, "RC_MAP_THROTTLE", paramValue));
    
    QCOMPARE(factValue.toInt(), paramValue.toInt());
    
    // Test another component id
    QVERIFY(_plugin->factExists(FactSystem::ParameterProvider, 51, "COMPONENT_51"));
    fact = _plugin->getFact(FactSystem::ParameterProvider, 51, "COMPONENT_51");
    QVERIFY(fact != NULL);
    factValue = fact->value();
    QCOMPARE(factValue.isValid(), true);
    
    Q_ASSERT(_paramMgr->getParameterValue(51, "COMPONENT_51", paramValue));
Don Gagne's avatar
Don Gagne committed
127 128 129 130 131
    
    QCOMPARE(factValue.toInt(), paramValue.toInt());
}

/// Test that QML can reference a Fact
132
void FactSystemTestBase::_qml_test(void)
Don Gagne's avatar
Don Gagne committed
133 134 135
{
    QGCQuickWidget* widget = new QGCQuickWidget;
    
136 137
    widget->setAutoPilot(_plugin);
    
Don Gagne's avatar
Don Gagne committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151
    widget->setSource(QUrl::fromUserInput("qrc:unittest/FactSystemTest.qml"));
    
    QQuickItem* rootObject = widget->rootObject();
    QObject* control = rootObject->findChild<QObject*>("testControl");
    QVERIFY(control != NULL);
    QVariant qmlValue = control->property("text").toInt();

    QVariant paramMgrValue;
    Q_ASSERT(_paramMgr->getParameterValue(_paramMgr->getDefaultComponentId(), "RC_MAP_THROTTLE", paramMgrValue));
    
    QCOMPARE(qmlValue.toInt(), paramMgrValue.toInt());
}

// Test correct behavior when the Param Manager gets a parameter update
152
void FactSystemTestBase::_paramMgrSignal_test(void)
Don Gagne's avatar
Don Gagne committed
153 154 155
{
    // Get the parameter Fact from the AutoPilot
    
156
    Fact* fact = _plugin->getFact(FactSystem::ParameterProvider, -1, "RC_MAP_THROTTLE");
Don Gagne's avatar
Don Gagne committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    QVERIFY(fact != NULL);
    
    // Setting a new value into the parameter should trigger a valueChanged signal on the Fact

    QSignalSpy spyFact(fact, SIGNAL(valueChanged(QVariant)));
    
    QVariant paramValue = 12;
    _paramMgr->setParameter(_paramMgr->getDefaultComponentId(), "RC_MAP_THROTTLE", paramValue);
    _paramMgr->sendPendingParameters(true, false);

    // Wait for the Fact::valueChanged signal to come through
    QCOMPARE(spyFact.wait(5000), true);

    // Make sure the signal has the right value
    QList<QVariant> arguments = spyFact.takeFirst();
    qDebug() << arguments.at(0).type();
    QCOMPARE(arguments.at(0).toInt(), 12);
    
    // Make sure the Fact has the new value
    QCOMPARE(fact->value().toInt(), 12);
}

/// Test QML getting an updated Fact value
180
void FactSystemTestBase::_qmlUpdate_test(void)
Don Gagne's avatar
Don Gagne committed
181 182 183
{
    QGCQuickWidget* widget = new QGCQuickWidget;
    
184 185
    widget->setAutoPilot(_plugin);
    
Don Gagne's avatar
Don Gagne committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    widget->setSource(QUrl::fromUserInput("qrc:unittest/FactSystemTest.qml"));
    
    // Change the value using param manager
    
    QVariant paramValue = 12;
    _paramMgr->setParameter(_paramMgr->getDefaultComponentId(), "RC_MAP_THROTTLE", paramValue);
    _paramMgr->sendPendingParameters(true, false);

    QTest::qWait(500); // Let the signals flow through
    
    // Make sure the qml has the right value
    
    QQuickItem* rootObject = widget->rootObject();
    QObject* control = rootObject->findChild<QObject*>("testControl");
    QVERIFY(control != NULL);
    QCOMPARE(control->property("text").toInt(), 12);
}