FactSystemTestBase.cc 3.11 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
    QCOMPARE(factValue.toInt(), 3);
Don Gagne's avatar
Don Gagne committed
66 67 68
}

/// Test that QML can reference a Fact
69
void FactSystemTestBase::_qml_test(void)
Don Gagne's avatar
Don Gagne committed
70 71
{
    QGCQuickWidget* widget = new QGCQuickWidget;
72

73
    widget->setAutoPilot(_plugin);
74

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

Don Gagne's avatar
Don Gagne committed
77 78 79 80 81
    QQuickItem* rootObject = widget->rootObject();
    QObject* control = rootObject->findChild<QObject*>("testControl");
    QVERIFY(control != NULL);
    QVariant qmlValue = control->property("text").toInt();

82
    QCOMPARE(qmlValue.toInt(), 3);
83

Don Gagne's avatar
Don Gagne committed
84
    delete widget;
Don Gagne's avatar
Don Gagne committed
85 86 87
}

/// Test QML getting an updated Fact value
88
void FactSystemTestBase::_qmlUpdate_test(void)
Don Gagne's avatar
Don Gagne committed
89 90
{
    QGCQuickWidget* widget = new QGCQuickWidget;
91

92
    widget->setAutoPilot(_plugin);
93

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

96
    // Change the value
97

Don Gagne's avatar
Don Gagne committed
98
    QVariant paramValue = 12;
Don Gagne's avatar
Don Gagne committed
99
    _plugin->getParameterFact(FactSystem::defaultComponentId, "RC_MAP_THROTTLE")->setRawValue(paramValue);
Don Gagne's avatar
Don Gagne committed
100 101

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

Don Gagne's avatar
Don Gagne committed
103
    // Make sure the qml has the right value
104

Don Gagne's avatar
Don Gagne committed
105 106 107 108
    QQuickItem* rootObject = widget->rootObject();
    QObject* control = rootObject->findChild<QObject*>("testControl");
    QVERIFY(control != NULL);
    QCOMPARE(control->property("text").toInt(), 12);
109

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