FactSystemTestBase.cc 5.15 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
#include "LinkManager.h"
#include "MockLink.h"
#include "AutoPilotPluginManager.h"
#include "UASManager.h"
#include "QGCApplication.h"
33
#include "QGCMessageBox.h"
Don Gagne's avatar
Don Gagne committed
34 35 36 37 38
#include "QGCQuickWidget.h"

#include <QQuickItem>

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

44
void FactSystemTestBase::_init(MAV_AUTOPILOT autopilot)
Don Gagne's avatar
Don Gagne committed
45 46 47 48 49 50
{
    UnitTest::init();
    
    LinkManager* _linkMgr = LinkManager::instance();
    
    MockLink* link = new MockLink();
51
    link->setAutopilotType(autopilot);
52
    _linkMgr->_addLink(link);
53 54 55 56 57
    
    if (autopilot == MAV_AUTOPILOT_ARDUPILOTMEGA) {
        // Connect will pop a warning dialog
        setExpectedMessageBox(QGCMessageBox::Ok);
    }
Don Gagne's avatar
Don Gagne committed
58 59 60 61 62 63 64
    _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);
    
65 66 67 68
    if (autopilot == MAV_AUTOPILOT_ARDUPILOTMEGA) {
        checkExpectedMessageBox();
    }
    
Don Gagne's avatar
Don Gagne committed
69 70 71 72 73 74 75 76
    _uas = UASManager::instance()->getActiveUAS();
    Q_ASSERT(_uas);
    
    // Get the plugin for the uas
    
    AutoPilotPluginManager* pluginMgr = AutoPilotPluginManager::instance();
    Q_ASSERT(pluginMgr);
    
77
    _plugin = pluginMgr->getInstanceForAutoPilotPlugin(_uas).data();
Don Gagne's avatar
Don Gagne committed
78 79 80 81
    Q_ASSERT(_plugin);

    // Wait for the plugin to be ready
    
82 83
    QSignalSpy spyPlugin(_plugin, SIGNAL(pluginReadyChanged(bool)));
    if (!_plugin->pluginReady()) {
84
        QCOMPARE(spyPlugin.wait(60000), true);
Don Gagne's avatar
Don Gagne committed
85
    }
86
    Q_ASSERT(_plugin->pluginReady());
Don Gagne's avatar
Don Gagne committed
87 88
}

89
void FactSystemTestBase::_cleanup(void)
Don Gagne's avatar
Don Gagne committed
90 91 92 93 94
{
    UnitTest::cleanup();
}

/// Basic test of parameter values in Fact System
95
void FactSystemTestBase::_parameter_default_component_id_test(void)
Don Gagne's avatar
Don Gagne committed
96
{
97 98 99 100 101
    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);
Don Gagne's avatar
Don Gagne committed
102
    
103
    QCOMPARE(factValue.toInt(), 3);
104 105 106 107 108 109
}

void FactSystemTestBase::_parameter_specific_component_id_test(void)
{
    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
110 111 112 113
    QVERIFY(fact != NULL);
    QVariant factValue = fact->value();
    QCOMPARE(factValue.isValid(), true);
    
114
    
115
    QCOMPARE(factValue.toInt(), 3);
116 117 118 119 120 121 122 123
    
    // 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);
    
124
    QCOMPARE(factValue.toInt(), 51);
Don Gagne's avatar
Don Gagne committed
125 126 127
}

/// Test that QML can reference a Fact
128
void FactSystemTestBase::_qml_test(void)
Don Gagne's avatar
Don Gagne committed
129 130 131
{
    QGCQuickWidget* widget = new QGCQuickWidget;
    
132 133
    widget->setAutoPilot(_plugin);
    
Don Gagne's avatar
Don Gagne committed
134 135 136 137 138 139 140
    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();

141
    QCOMPARE(qmlValue.toInt(), 3);
Don Gagne's avatar
Don Gagne committed
142 143
    
    delete widget;
Don Gagne's avatar
Don Gagne committed
144 145 146
}

/// Test QML getting an updated Fact value
147
void FactSystemTestBase::_qmlUpdate_test(void)
Don Gagne's avatar
Don Gagne committed
148 149 150
{
    QGCQuickWidget* widget = new QGCQuickWidget;
    
151 152
    widget->setAutoPilot(_plugin);
    
Don Gagne's avatar
Don Gagne committed
153 154
    widget->setSource(QUrl::fromUserInput("qrc:unittest/FactSystemTest.qml"));
    
155
    // Change the value
Don Gagne's avatar
Don Gagne committed
156 157
    
    QVariant paramValue = 12;
Don Gagne's avatar
Don Gagne committed
158
    _plugin->getParameterFact(FactSystem::defaultComponentId, "RC_MAP_THROTTLE")->setValue(paramValue);
Don Gagne's avatar
Don Gagne committed
159 160 161 162 163 164 165 166 167

    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);
Don Gagne's avatar
Don Gagne committed
168 169
    
    delete widget;
Don Gagne's avatar
Don Gagne committed
170 171
}