FactSystemTestBase.cc 4.05 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1
/*=====================================================================
2

Don Gagne's avatar
Don Gagne committed
3
 QGroundControl Open Source Ground Control Station
4

Don Gagne's avatar
Don Gagne committed
5
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
6

Don Gagne's avatar
Don Gagne committed
7
 This file is part of the QGROUNDCONTROL project
8

Don Gagne's avatar
Don Gagne committed
9 10 11 12
 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.
13

Don Gagne's avatar
Don Gagne committed
14 15 16 17
 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.
18

Don Gagne's avatar
Don Gagne committed
19 20
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
21

Don Gagne's avatar
Don Gagne committed
22 23 24 25 26
 ======================================================================*/

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

27
#include "FactSystemTestBase.h"
Don Gagne's avatar
Don Gagne committed
28
#include "LinkManager.h"
29
#ifdef QT_DEBUG
Don Gagne's avatar
Don Gagne committed
30
#include "MockLink.h"
31
#endif
32
#include "MultiVehicleManager.h"
Don Gagne's avatar
Don Gagne committed
33
#include "QGCApplication.h"
34
#include "QGCMessageBox.h"
Don Gagne's avatar
Don Gagne committed
35 36 37 38 39
#include "QGCQuickWidget.h"

#include <QQuickItem>

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

Don Gagne's avatar
Don Gagne committed
43 44
}

45
void FactSystemTestBase::_init(MAV_AUTOPILOT autopilot)
Don Gagne's avatar
Don Gagne committed
46 47
{
    UnitTest::init();
48

49
    _connectMockLink(autopilot);
50

51
    _plugin = qgcApp()->toolbox()->multiVehicleManager()->activeVehicle()->autopilotPlugin();
Don Gagne's avatar
Don Gagne committed
52 53 54
    Q_ASSERT(_plugin);
}

55
void FactSystemTestBase::_cleanup(void)
Don Gagne's avatar
Don Gagne committed
56 57 58 59 60
{
    UnitTest::cleanup();
}

/// Basic test of parameter values in Fact System
61
void FactSystemTestBase::_parameter_default_component_id_test(void)
Don Gagne's avatar
Don Gagne committed
62
{
63 64 65
    QVERIFY(_plugin->factExists(FactSystem::ParameterProvider, FactSystem::defaultComponentId, "RC_MAP_THROTTLE"));
    Fact* fact = _plugin->getFact(FactSystem::ParameterProvider, FactSystem::defaultComponentId, "RC_MAP_THROTTLE");
    QVERIFY(fact != NULL);
Don Gagne's avatar
Don Gagne committed
66
    QVariant factValue = fact->rawValue();
67
    QCOMPARE(factValue.isValid(), true);
68

69
    QCOMPARE(factValue.toInt(), 3);
70 71 72 73 74 75
}

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
76
    QVERIFY(fact != NULL);
Don Gagne's avatar
Don Gagne committed
77
    QVariant factValue = fact->rawValue();
Don Gagne's avatar
Don Gagne committed
78
    QCOMPARE(factValue.isValid(), true);
79 80


81
    QCOMPARE(factValue.toInt(), 3);
82

83 84 85 86
    // Test another component id
    QVERIFY(_plugin->factExists(FactSystem::ParameterProvider, 51, "COMPONENT_51"));
    fact = _plugin->getFact(FactSystem::ParameterProvider, 51, "COMPONENT_51");
    QVERIFY(fact != NULL);
Don Gagne's avatar
Don Gagne committed
87
    factValue = fact->rawValue();
88
    QCOMPARE(factValue.isValid(), true);
89

90
    QCOMPARE(factValue.toInt(), 51);
Don Gagne's avatar
Don Gagne committed
91 92 93
}

/// Test that QML can reference a Fact
94
void FactSystemTestBase::_qml_test(void)
Don Gagne's avatar
Don Gagne committed
95 96
{
    QGCQuickWidget* widget = new QGCQuickWidget;
97

98
    widget->setAutoPilot(_plugin);
99

Don Gagne's avatar
Don Gagne committed
100
    widget->setSource(QUrl::fromUserInput("qrc:unittest/FactSystemTest.qml"));
101

Don Gagne's avatar
Don Gagne committed
102 103 104 105 106
    QQuickItem* rootObject = widget->rootObject();
    QObject* control = rootObject->findChild<QObject*>("testControl");
    QVERIFY(control != NULL);
    QVariant qmlValue = control->property("text").toInt();

107
    QCOMPARE(qmlValue.toInt(), 3);
108

Don Gagne's avatar
Don Gagne committed
109
    delete widget;
Don Gagne's avatar
Don Gagne committed
110 111 112
}

/// Test QML getting an updated Fact value
113
void FactSystemTestBase::_qmlUpdate_test(void)
Don Gagne's avatar
Don Gagne committed
114 115
{
    QGCQuickWidget* widget = new QGCQuickWidget;
116

117
    widget->setAutoPilot(_plugin);
118

Don Gagne's avatar
Don Gagne committed
119
    widget->setSource(QUrl::fromUserInput("qrc:unittest/FactSystemTest.qml"));
120

121
    // Change the value
122

Don Gagne's avatar
Don Gagne committed
123
    QVariant paramValue = 12;
Don Gagne's avatar
Don Gagne committed
124
    _plugin->getParameterFact(FactSystem::defaultComponentId, "RC_MAP_THROTTLE")->setRawValue(paramValue);
Don Gagne's avatar
Don Gagne committed
125 126

    QTest::qWait(500); // Let the signals flow through
127

Don Gagne's avatar
Don Gagne committed
128
    // Make sure the qml has the right value
129

Don Gagne's avatar
Don Gagne committed
130 131 132 133
    QQuickItem* rootObject = widget->rootObject();
    QObject* control = rootObject->findChild<QObject*>("testControl");
    QVERIFY(control != NULL);
    QCOMPARE(control->property("text").toInt(), 12);
134

Don Gagne's avatar
Don Gagne committed
135
    delete widget;
Don Gagne's avatar
Don Gagne committed
136 137
}