FactSystemTestBase.cc 3.23 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
#include "QGCApplication.h"
#include "QGCQuickWidget.h"
22
#include "ParameterManager.h"
Don Gagne's avatar
Don Gagne committed
23 24 25 26

#include <QQuickItem>

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

Don Gagne's avatar
Don Gagne committed
30 31
}

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

36
    _connectMockLink(autopilot);
37

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

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

/// Basic test of parameter values in Fact System
48
void FactSystemTestBase::_parameter_default_component_id_test(void)
Don Gagne's avatar
Don Gagne committed
49
{
50 51
    QVERIFY(_vehicle->parameterManager()->parameterExists(FactSystem::defaultComponentId, "RC_MAP_THROTTLE"));
    Fact* fact = _vehicle->parameterManager()->getParameter(FactSystem::defaultComponentId, "RC_MAP_THROTTLE");
52
    QVERIFY(fact != NULL);
Don Gagne's avatar
Don Gagne committed
53
    QVariant factValue = fact->rawValue();
54
    QCOMPARE(factValue.isValid(), true);
55

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

void FactSystemTestBase::_parameter_specific_component_id_test(void)
{
61 62
    QVERIFY(_vehicle->parameterManager()->parameterExists(MAV_COMP_ID_AUTOPILOT1, "RC_MAP_THROTTLE"));
    Fact* fact = _vehicle->parameterManager()->getParameter(MAV_COMP_ID_AUTOPILOT1, "RC_MAP_THROTTLE");
Don Gagne's avatar
Don Gagne committed
63
    QVERIFY(fact != NULL);
Don Gagne's avatar
Don Gagne committed
64
    QVariant factValue = fact->rawValue();
Don Gagne's avatar
Don Gagne committed
65
    QCOMPARE(factValue.isValid(), true);
66
    QCOMPARE(factValue.toInt(), 3);
Don Gagne's avatar
Don Gagne committed
67 68 69
}

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

74
    widget->setAutoPilot(_plugin);
75

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

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

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

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

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

93
    widget->setAutoPilot(_plugin);
94

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

97
    // Change the value
98

Don Gagne's avatar
Don Gagne committed
99
    QVariant paramValue = 12;
100
    qgcApp()->toolbox()->multiVehicleManager()->activeVehicle()->parameterManager()->getParameter(FactSystem::defaultComponentId, "RC_MAP_THROTTLE")->setRawValue(paramValue);
Don Gagne's avatar
Don Gagne committed
101 102

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

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

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

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