summaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qml_cpp/tst_sensors_qmlcpp.cpp
blob: bd32f340dda3d4187778e97b08450befa62c40c6 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QtTest/QtTest>
#include <QtTest/QSignalSpy>
#include <QtCore/QDebug>

#include <QtTest/private/qpropertytesthelper_p.h>
#include <QtSensorsQuick/private/qmlsensor_p.h>
// #include <QtSensorsQuick/private/qmlsensorgesture_p.h>

#include "qtemplategestureplugin.h"
#include "qtemplaterecognizer.h"
#include <qsensorgesturemanager.h>
#include <qsensorbackend.h>
#include "qsensormanager.h"

#include "../../common/test_backends.h"
#include <QtSensorsQuick/private/qmlaccelerometer_p.h>
#include <QtSensorsQuick/private/qmlpressuresensor_p.h>
#include <QtSensorsQuick/private/qmlgyroscope_p.h>
#include <QtSensorsQuick/private/qmltapsensor_p.h>
#include <QtSensorsQuick/private/qmlcompass_p.h>
#include <QtSensorsQuick/private/qmlproximitysensor_p.h>
#include <QtSensorsQuick/private/qmlorientationsensor_p.h>
#include <QtSensorsQuick/private/qmlambientlightsensor_p.h>
#include <QtSensorsQuick/private/qmlmagnetometer_p.h>
#include <QtSensorsQuick/private/qmllidsensor_p.h>
#include <QtSensorsQuick/private/qmltiltsensor_p.h>
#include <QtSensorsQuick/private/qmlrotationsensor_p.h>
#include <QtSensorsQuick/private/qmlhumiditysensor_p.h>
#include <QtSensorsQuick/private/qmlambienttemperaturesensor_p.h>
#include <QtSensorsQuick/private/qmllightsensor_p.h>
#include <QtSensorsQuick/private/qmlirproximitysensor_p.h>

QT_USE_NAMESPACE

QT_BEGIN_NAMESPACE

class tst_sensors_qmlcpp : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase();
    void testReadingBindings();
    // void testGesture();
    void testSensorRanges();
};

void tst_sensors_qmlcpp::initTestCase()
{
    qputenv("QT_SENSORS_LOAD_PLUGINS", "0"); // Do not load plugins
}

template<typename SensorClass, typename ReadingClass, typename ValueType>
void testSensorReadings(const char* identifier, const QVariantMap& values)
{
    SensorClass sensor;
    sensor.setIdentifier(identifier);
    sensor.componentComplete();
    sensor.start();

    for (const auto& key : values.keys()) {
        ValueType initialValue = values[key].toList()[0].value<ValueType>();
        ValueType changedValue = values[key].toList()[1].value<ValueType>();
        QTestPrivate::testReadOnlyPropertyBasics<ReadingClass, ValueType>(
                    *static_cast<ReadingClass*>(sensor.reading()),
                    initialValue, changedValue, key.toStdString().c_str(),
                    [&](){ set_test_backend_reading(sensor.sensor(), {{key, changedValue}}); });
        if (QTest::currentTestFailed()) {
            qWarning() << identifier << "::" << key << "test failed.";
            return;
        }
    }
}

void tst_sensors_qmlcpp::testReadingBindings()
{
    register_test_backends();

    testSensorReadings<QmlAccelerometer, QmlAccelerometerReading, qreal>(
                "QAccelerometer",
                {{"x", QVariantList{1.0, 2.0}},
                 {"y", QVariantList{1.0, 2.0}},
                 {"z", QVariantList{1.0, 2.0}}});
    testSensorReadings<QmlAccelerometer, QmlAccelerometerReading, quint64>(
                "QAccelerometer",
                {{"timestamp", QVariantList{1.0, 2.0}}});
    testSensorReadings<QmlAmbientLightSensor, QmlAmbientLightSensorReading, QAmbientLightReading::LightLevel>(
                "QAmbientLightSensor",
                {{"lightLevel", QVariantList{QAmbientLightReading::Twilight, QAmbientLightReading::Sunny}}});
    testSensorReadings<QmlPressureSensor, QmlPressureReading, qreal>(
                "QPressureSensor",
                {{"pressure", QVariantList{1.0, 2.0}},
                 {"temperature", QVariantList{1.0, 2.0}}});
    testSensorReadings<QmlGyroscope, QmlGyroscopeReading, qreal>(
                "QGyroscope",
                {{"x", QVariantList{1.0, 2.0}},
                 {"y", QVariantList{1.0, 2.0}},
                 {"z", QVariantList{1.0, 2.0}}});
    testSensorReadings<QmlTapSensor, QmlTapSensorReading, bool>(
                "QTapSensor",
                {{"doubleTap", QVariantList{true, false}}});
    testSensorReadings<QmlTapSensor, QmlTapSensorReading, QTapReading::TapDirection>(
                "QTapSensor",
                {{"tapDirection", QVariantList{QTapReading::Z_Both, QTapReading::X_Both}}});
    testSensorReadings<QmlCompass, QmlCompassReading, qreal>(
                "QCompass",
                {{"azimuth", QVariantList{1.0, 2.0}},
                 {"calibrationLevel", QVariantList{1.0, 2.0}}});
    testSensorReadings<QmlProximitySensor, QmlProximitySensorReading, bool>(
                "QProximitySensor",
                {{"near", QVariantList{true, false}}});
    testSensorReadings<QmlOrientationSensor, QmlOrientationSensorReading, QOrientationReading::Orientation>(
                "QOrientationSensor",
                {{"orientation", QVariantList{QOrientationReading::LeftUp, QOrientationReading::RightUp}}});
    testSensorReadings<QmlMagnetometer, QmlMagnetometerReading, qreal>(
                "QMagnetometer",
                {{"x", QVariantList{1.0, 2.0}},
                 {"y", QVariantList{1.0, 2.0}},
                 {"z", QVariantList{1.0, 2.0}},
                 {"calibrationLevel", QVariantList{1.0, 2.0}}});
    testSensorReadings<QmlLidSensor, QmlLidReading, bool>(
                "QLidSensor",
                {{"backLidClosed", QVariantList{true, false}},
                 {"frontLidClosed", QVariantList{true, false}}});
    testSensorReadings<QmlTiltSensor, QmlTiltSensorReading, qreal>(
                "QTiltSensor",
                {{"yRotation", QVariantList{1.0, 2.0}},
                 {"xRotation", QVariantList{1.0, 2.0}}});
    // rotation sensor properties need to be tested separately because the setter function is
    // not symmetric with getter functions ("setFromEuler()" vs. "x() & y() & z()")
    testSensorReadings<QmlRotationSensor, QmlRotationSensorReading, qreal>(
                "QRotationSensor",
                {{"x", QVariantList{1.0, 2.0}}});
    testSensorReadings<QmlRotationSensor, QmlRotationSensorReading, qreal>(
                "QRotationSensor",
                {{"y", QVariantList{1.0, 2.0}}});
    testSensorReadings<QmlRotationSensor, QmlRotationSensorReading, qreal>(
                "QRotationSensor",
                {{"z", QVariantList{1.0, 2.0}}});
    testSensorReadings<QmlHumiditySensor, QmlHumidityReading, qreal>(
                "QHumiditySensor",
                {{"relativeHumidity", QVariantList{1.0, 2.0}},
                 {"absoluteHumidity", QVariantList{1.0, 2.0}}});
    testSensorReadings<QmlAmbientTemperatureSensor, QmlAmbientTemperatureReading, qreal>(
                "QAmbientTemperatureSensor",
                {{"temperature", QVariantList{30.0, 40.0}}});
    testSensorReadings<QmlLightSensor, QmlLightSensorReading, qreal>(
                "QLightSensor",
                {{"illuminance", QVariantList{1.0, 2.0}}});
    testSensorReadings<QmlIRProximitySensor, QmlIRProximitySensorReading, qreal>(
                "QIRProximitySensor",
                {{"reflectance", QVariantList{0.5, 0.6}}});

    // The following tests QmlSensor (the baseclass) 'readingChanged' which is
    // emitted every time a sensor value changes. For that we instantiate a
    // concrete sensor. The actual 'reading' value (a QObject pointer) of the
    // 'readingChanged' property will not change, but rather the
    // 'readingChanged' is used to indicate that the value it contains has changed.
    QmlAccelerometer accelerometer;
    accelerometer.setIdentifier("QAccelerometer");
    accelerometer.componentComplete();
    accelerometer.start();
    QTestPrivate::testReadOnlyPropertyBasics<QmlSensor, QmlSensorReading*>(
                accelerometer, accelerometer.reading(), accelerometer.reading(), "reading",
                [&](){ set_test_backend_reading(accelerometer.sensor(), {{"x", 2.0}}); });

    unregister_test_backends();
}

/*
void tst_sensors_qmlcpp::testGesture()
{
    QTemplateGesturePlugin* plugin = new QTemplateGesturePlugin();
    QList <QSensorGestureRecognizer *> recognizers = plugin->createRecognizers();
    QSensorGestureManager manager;

    QmlSensorGesture* gs = new QmlSensorGesture(this);
    gs->componentComplete();
    QSignalSpy spy_availableGesturesChanged(gs, SIGNAL(availableGesturesChanged()));
    QSignalSpy spy_detected(gs, SIGNAL(detected(QString)));
    QSignalSpy spy_gesturesChanged(gs, SIGNAL(gesturesChanged()));
    QSignalSpy spy_validGesturesChanged(gs, SIGNAL(validGesturesChanged()));
    QSignalSpy spy_invalidGesturesChanged(gs, SIGNAL(invalidGesturesChanged()));
    QSignalSpy spy_enabledChanged(gs, SIGNAL(enabledChanged()));

    //This flag is needed if you run this unit test with an alread installed template plugin
    bool registered = false;
    for (int i = 0; i < recognizers.count(); i++){
        registered = manager.registerSensorGestureRecognizer(recognizers[i]);
    }
    if (registered) {
        QCOMPARE(spy_availableGesturesChanged.count(), 2);
    }

    //check creation of a not known plugin
    QCOMPARE(spy_invalidGesturesChanged.count(), 0);
    QCOMPARE(spy_gesturesChanged.count(), 0);
    gs->setGestures(QStringList() << "lollipop");
    QCOMPARE(spy_gesturesChanged.count(), 1);
    QCOMPARE(spy_invalidGesturesChanged.count(), 1);

    //check creation of a known plugin
    QCOMPARE(spy_validGesturesChanged.count(), 0);
    QCOMPARE(spy_gesturesChanged.count(), 1);
    spy_invalidGesturesChanged.clear();
    spy_validGesturesChanged.clear();
    gs->setGestures(QStringList() << "QtSensors.template");
    QCOMPARE(spy_gesturesChanged.count(), 2);
    QCOMPARE(spy_invalidGesturesChanged.count(), 1);
    QCOMPARE(spy_validGesturesChanged.count(), 1);

    //enable "QtSensors.template"
    QCOMPARE(spy_enabledChanged.count(), 0);
    QCOMPARE(spy_detected.count(), 0);
    gs->setEnabled(true);
    QCOMPARE(spy_enabledChanged.count(), 1);
    QCOMPARE(spy_detected.count(), 1);

    //set gesture during running sensor should not emit gesture changed
    spy_gesturesChanged.clear();
    gs->setGestures(QStringList() << "QtSensors.template2");
    QCOMPARE(spy_gesturesChanged.count(), 0);

    gs->setEnabled(false);

    QmlSensorGesture* gs1 = new QmlSensorGesture(this);
    QSignalSpy spy1_detected(gs1, SIGNAL(detected(QString)));
    QSignalSpy spy1_gesturesChanged(gs1, SIGNAL(gesturesChanged()));
    QSignalSpy spy1_validGesturesChanged(gs1, SIGNAL(validGesturesChanged()));
    QSignalSpy spy1_invalidGesturesChanged(gs1, SIGNAL(invalidGesturesChanged()));
    QSignalSpy spy1_enabledChanged(gs1, SIGNAL(enabledChanged()));
    gs1->componentComplete();

    //set enable = true without gesture should
    gs1->setEnabled(true);
    QCOMPARE(spy1_enabledChanged.count(), 1);
    gs1->setEnabled(false);
    spy1_enabledChanged.clear();

    //reding gestures check if we get back an empty string list
    QStringList gestures = gs1->gestures();
    QCOMPARE(gestures.count(), 0);
    QStringList validgestures = gs1->validGestures();
    QCOMPARE(validgestures.count(), 0);
    QStringList invalidgestures = gs1->invalidGestures();
    QCOMPARE(invalidgestures.count(), 0);

    //check types "QtSensors.template" "QtSensors.template1" "lollipop"
    //expect valid 2 not available 1
    gestures << "QtSensors.template" << "QtSensors.template1" << "lollipop";
    gs1->setGestures(gestures);
    gestures = gs1->gestures();
    QCOMPARE(gestures.count(), 3);
    QCOMPARE(spy1_validGesturesChanged.count(), 1);
    QCOMPARE(spy1_invalidGesturesChanged.count(), 1);
    QCOMPARE(spy1_gesturesChanged.count(), 1);
    //set same gesture again should not emit gesture changed
    gs1->setGestures(gestures);
    QCOMPARE(spy1_gesturesChanged.count(), 1);

    spy1_gesturesChanged.clear();
    gestures.clear();
    gs1->setGestures(gestures);
    QCOMPARE(spy1_gesturesChanged.count(), 1);

    //enable "QtSensors.template" and "QtSensors.template1"
    gestures << "QtSensors.template" << "QtSensors.template1";
    gs1->setEnabled(false);
    gs1->setGestures(gestures);
    spy1_enabledChanged.clear();
    spy1_detected.clear();
    gs1->setEnabled(true);
    QCOMPARE(spy1_enabledChanged.count(), 1);
    QCOMPARE(spy1_detected.count(), 2);
    gs1->setEnabled(false);

    //check sensor shouldn't run until the componentComplete gets called
    QmlSensorGesture* gs2 = new QmlSensorGesture(this);
    QSignalSpy spy2_detected(gs2, SIGNAL(detected(QString)));
    gs2->setGestures(QStringList() << "QtSensors.template");
    gs2->setEnabled(true);
    QCOMPARE(spy2_detected.count(), 0);
    gs2->componentComplete();
    QCOMPARE(spy2_detected.count(), 1);
}

*/

class QDummySensorBackend : public QSensorBackend
{
    Q_OBJECT
public:
    QDummySensorBackend(QSensor *sensor) : QSensorBackend(sensor)
    {
        addDataRate(2, 3);
        addDataRate(5, 7);
        addOutputRange(100, 200, 1);
        addOutputRange(600, 700, 10);
        addOutputRange(0, 1, 2);
    }

    void start() override {}
    void stop() override {}
};

class QDummySensorReading : public QSensorReading
{
    Q_OBJECT
public:
    QDummySensorReading(QObject *parent) : QSensorReading(parent, nullptr) {}
};

class QmlDummySensorReading : public QmlSensorReading
{
    Q_OBJECT
public:
    QmlDummySensorReading() :
        m_reading(new QDummySensorReading(this))
    {}

    QSensorReading *reading() const override { return m_reading; }
    void readingUpdate() override {}

private:
    QSensorReading *m_reading = nullptr;
};

class QmlDummySensor : public QmlSensor
{
    Q_OBJECT
public:
    QmlDummySensor(QObject *parent = nullptr) :
        QmlSensor(parent),
        m_sensor(new QSensor("dummy", this))
    {
        QDummySensorBackend b(m_sensor);
        Q_UNUSED(b);
    }

    QSensor *sensor() const override { return m_sensor; }
    QmlSensorReading *createReading() const override { return new QmlDummySensorReading(); }

    void componentComplete() override { QmlSensor::componentComplete(); }

private:
    QSensor *m_sensor = nullptr;
};

void tst_sensors_qmlcpp::testSensorRanges()
{
    QScopedPointer<QmlDummySensor> qmlSensor(new QmlDummySensor);
    qmlSensor->componentComplete();

    auto ranges = qmlSensor->availableDataRates();
    QCOMPARE(ranges.count(&ranges), 2);

    const auto range0 = ranges.at(&ranges, 0);
    QCOMPARE(range0->minimum(), 2);
    QCOMPARE(range0->maximum(), 3);
    QSignalSpy range0Spy(range0, SIGNAL(destroyed()));

    const auto range1 = ranges.at(&ranges, 1);
    QCOMPARE(range1->minimum(), 5);
    QCOMPARE(range1->maximum(), 7);
    QSignalSpy range1Spy(range1, SIGNAL(destroyed()));

    auto outputs = qmlSensor->outputRanges();
    QCOMPARE(outputs.count(&outputs), 3);

    const auto output0 = outputs.at(&outputs, 0);
    QCOMPARE(output0->minimum(), 100);
    QCOMPARE(output0->maximum(), 200);
    QCOMPARE(output0->accuracy(), 1);
    QSignalSpy output0Spy(output0, SIGNAL(destroyed()));

    const auto output1 = outputs.at(&outputs, 1);
    QCOMPARE(output1->minimum(), 600);
    QCOMPARE(output1->maximum(), 700);
    QCOMPARE(output1->accuracy(), 10);
    QSignalSpy output1Spy(output1, SIGNAL(destroyed()));

    const auto output2 = outputs.at(&outputs, 2);
    QCOMPARE(output2->minimum(), 0);
    QCOMPARE(output2->maximum(), 1);
    QCOMPARE(output2->accuracy(), 2);
    QSignalSpy output2Spy(output2, SIGNAL(destroyed()));

    qmlSensor.reset();
    QCOMPARE(range0Spy.count(), 1);
    QCOMPARE(range1Spy.count(), 1);
    QCOMPARE(output0Spy.count(), 1);
    QCOMPARE(output1Spy.count(), 1);
    QCOMPARE(output2Spy.count(), 1);
}

QT_END_NAMESPACE

QTEST_MAIN(tst_sensors_qmlcpp)
#include "tst_sensors_qmlcpp.moc"