FactSystemTestBase.cc 3.45 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (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.
 *
 ****************************************************************************/
9

Don Gagne's avatar
Don Gagne committed
10 11 12 13

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

14
#include "FactSystemTestBase.h"
Don Gagne's avatar
Don Gagne committed
15
#include "LinkManager.h"
16
#ifdef QT_DEBUG
Don Gagne's avatar
Don Gagne committed
17
#include "MockLink.h"
18
#endif
19
#include "MultiVehicleManager.h"
Don Gagne's avatar
Don Gagne committed
20 21 22 23 24 25
#include "QGCApplication.h"
#include "QGCQuickWidget.h"

#include <QQuickItem>

/// FactSystem Unit Test
26
FactSystemTestBase::FactSystemTestBase(void)
Don Gagne's avatar
Don Gagne committed
27
{
28

Don Gagne's avatar
Don Gagne committed
29 30
}

31
void FactSystemTestBase::_init(MAV_AUTOPILOT autopilot)
Don Gagne's avatar
Don Gagne committed
32 33
{
    UnitTest::init();
34

35
    _connectMockLink(autopilot);
36

37
    _plugin = qgcApp()->toolbox()->multiVehicleManager()->activeVehicle()->autopilotPlugin();
Don Gagne's avatar
Don Gagne committed
38 39 40
    Q_ASSERT(_plugin);
}

41
void FactSystemTestBase::_cleanup(void)
Don Gagne's avatar
Don Gagne committed
42 43 44 45 46
{
    UnitTest::cleanup();
}

/// Basic test of parameter values in Fact System
47
void FactSystemTestBase::_parameter_default_component_id_test(void)
Don Gagne's avatar
Don Gagne committed
48
{
49 50 51
    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
52
    QVariant factValue = fact->rawValue();
53
    QCOMPARE(factValue.isValid(), true);
54

55
    QCOMPARE(factValue.toInt(), 3);
56 57 58 59 60 61
}

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
62
    QVERIFY(fact != NULL);
Don Gagne's avatar
Don Gagne committed
63
    QVariant factValue = fact->rawValue();
Don Gagne's avatar
Don Gagne committed
64
    QCOMPARE(factValue.isValid(), true);
65 66


67
    QCOMPARE(factValue.toInt(), 3);
68

69 70 71 72
    // 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
73
    factValue = fact->rawValue();
74
    QCOMPARE(factValue.isValid(), true);
75

76
    QCOMPARE(factValue.toInt(), 51);
Don Gagne's avatar
Don Gagne committed
77 78 79
}

/// Test that QML can reference a Fact
80
void FactSystemTestBase::_qml_test(void)
Don Gagne's avatar
Don Gagne committed
81 82
{
    QGCQuickWidget* widget = new QGCQuickWidget;
83

84
    widget->setAutoPilot(_plugin);
85

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

Don Gagne's avatar
Don Gagne committed
88 89 90 91 92
    QQuickItem* rootObject = widget->rootObject();
    QObject* control = rootObject->findChild<QObject*>("testControl");
    QVERIFY(control != NULL);
    QVariant qmlValue = control->property("text").toInt();

93
    QCOMPARE(qmlValue.toInt(), 3);
94

Don Gagne's avatar
Don Gagne committed
95
    delete widget;
Don Gagne's avatar
Don Gagne committed
96 97 98
}

/// Test QML getting an updated Fact value
99
void FactSystemTestBase::_qmlUpdate_test(void)
Don Gagne's avatar
Don Gagne committed
100 101
{
    QGCQuickWidget* widget = new QGCQuickWidget;
102

103
    widget->setAutoPilot(_plugin);
104

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

107
    // Change the value
108

Don Gagne's avatar
Don Gagne committed
109
    QVariant paramValue = 12;
Don Gagne's avatar
Don Gagne committed
110
    _plugin->getParameterFact(FactSystem::defaultComponentId, "RC_MAP_THROTTLE")->setRawValue(paramValue);
Don Gagne's avatar
Don Gagne committed
111 112

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

Don Gagne's avatar
Don Gagne committed
114
    // Make sure the qml has the right value
115

Don Gagne's avatar
Don Gagne committed
116 117 118 119
    QQuickItem* rootObject = widget->rootObject();
    QObject* control = rootObject->findChild<QObject*>("testControl");
    QVERIFY(control != NULL);
    QCOMPARE(control->property("text").toInt(), 12);
120

Don Gagne's avatar
Don Gagne committed
121
    delete widget;
Don Gagne's avatar
Don Gagne committed
122 123
}