1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtSensors module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include <QtCore/QString> |
30 | #include <QtTest/QtTest> |
31 | |
32 | #include <QVariant> |
33 | #include <QSignalSpy> |
34 | |
35 | #include <qsensorgesture.h> |
36 | #include <qsensorgesturemanager.h> |
37 | |
38 | #include <qsensorgesturerecognizer.h> |
39 | #include <qsensorgestureplugininterface.h> |
40 | |
41 | #include "test_backends.h" |
42 | |
43 | class Tst_qsensorgesturePluginsTest : public QObject |
44 | { |
45 | Q_OBJECT |
46 | |
47 | public: |
48 | Tst_qsensorgesturePluginsTest(); |
49 | |
50 | private Q_SLOTS: |
51 | |
52 | void tst_sensor_plugins_shake(); |
53 | void tst_sensor_plugins_qtsensors_data(); |
54 | void tst_sensor_plugins_qtsensors(); |
55 | void tst_sensor_plugins_qtsensors_all(); |
56 | |
57 | }; |
58 | |
59 | Tst_qsensorgesturePluginsTest::Tst_qsensorgesturePluginsTest() |
60 | { |
61 | qputenv(varName: "QT_SENSORS_LOAD_PLUGINS" , value: "0" ); // Do not load plugins |
62 | register_test_backends(); |
63 | } |
64 | |
65 | void Tst_qsensorgesturePluginsTest::tst_sensor_plugins_shake() |
66 | { |
67 | |
68 | QSensorGestureManager manager; |
69 | QVERIFY(manager.gestureIds().contains("QtSensors.shake" )); |
70 | |
71 | QSensorGestureRecognizer *recognizer = manager.sensorGestureRecognizer(id: "QtSensors.shake" ); |
72 | |
73 | QCOMPARE(recognizer->isActive(), false); |
74 | |
75 | QTest::ignoreMessage(type: QtWarningMsg, message: "Not starting. Gesture Recognizer not initialized" ); |
76 | recognizer->startBackend(); |
77 | QCOMPARE(recognizer->isActive(), false); |
78 | |
79 | QTest::ignoreMessage(type: QtWarningMsg, message: "Not stopping. Gesture Recognizer not initialized" ); |
80 | recognizer->stopBackend(); |
81 | QCOMPARE(recognizer->isActive(), false); |
82 | |
83 | QScopedPointer<QSensorGesture> gesture(new QSensorGesture(QStringList() << "QtSensors.shake" )); |
84 | |
85 | QCOMPARE(gesture->isActive(),false); |
86 | QCOMPARE(gesture->validIds(), QStringList() << "QtSensors.shake" ); |
87 | |
88 | QCOMPARE(QStringList() << recognizer->id(), gesture->validIds()); |
89 | |
90 | gesture->startDetection(); |
91 | QCOMPARE(gesture->isActive(),true); |
92 | QCOMPARE(recognizer->isActive(), true); |
93 | |
94 | gesture->stopDetection(); |
95 | QCOMPARE(recognizer->isActive(), false); |
96 | } |
97 | |
98 | void Tst_qsensorgesturePluginsTest::tst_sensor_plugins_qtsensors_data() |
99 | { |
100 | QTest::addColumn<QString>(name: "gestureId" ); |
101 | |
102 | QTest::newRow(dataTag: "QtSensors.cover" ) << "QtSensors.cover" ; |
103 | |
104 | QTest::newRow(dataTag: "QtSensors.doubletap" ) << "QtSensors.doubletap" ; |
105 | |
106 | QTest::newRow(dataTag: "QtSensors.freefall" ) << "QtSensors.freefall" ; |
107 | QTest::newRow(dataTag: "QtSensors.hover" ) << "QtSensors.hover" ; |
108 | QTest::newRow(dataTag: "QtSensors.shake2" ) << "QtSensors.shake2" ; |
109 | QTest::newRow(dataTag: "QtSensors.slam" ) << "QtSensors.slam" ; |
110 | QTest::newRow(dataTag: "QtSensors.turnover" ) << "QtSensors.turnover" ; |
111 | QTest::newRow(dataTag: "QtSensors.twist" ) << "QtSensors.twist" ; |
112 | QTest::newRow(dataTag: "QtSensors.whip" ) << "QtSensors.whip" ; |
113 | } |
114 | |
115 | void Tst_qsensorgesturePluginsTest::tst_sensor_plugins_qtsensors() |
116 | { |
117 | QFETCH(QString, gestureId); |
118 | QSensorGestureManager manager; |
119 | |
120 | QVERIFY(manager.gestureIds().contains(gestureId)); |
121 | |
122 | QScopedPointer<QSensorGesture> gesture(new QSensorGesture(QStringList() << gestureId)); |
123 | QScopedPointer<QSensorGesture> gesture2(new QSensorGesture(QStringList() << gestureId)); |
124 | |
125 | QCOMPARE(gesture->isActive(),false); |
126 | QCOMPARE(gesture->validIds(), QStringList() << gestureId); |
127 | |
128 | QSensorGestureRecognizer *recognizer = manager.sensorGestureRecognizer(id: gestureId); |
129 | |
130 | QCOMPARE(QStringList() << recognizer->id(), gesture->validIds()); |
131 | |
132 | QCOMPARE(QStringList() << QStringList(), gesture->invalidIds()); |
133 | |
134 | QVERIFY(recognizer->isActive() == false); |
135 | QVERIFY(gesture->isActive() == false); |
136 | QVERIFY(gesture2->isActive() == false); |
137 | |
138 | gesture->startDetection(); |
139 | |
140 | QVERIFY(gesture->isActive() == true); |
141 | QVERIFY(gesture2->isActive() == false); |
142 | |
143 | QVERIFY(recognizer->isActive() == true); |
144 | |
145 | gesture2->startDetection(); |
146 | |
147 | QVERIFY(gesture->isActive() == true); |
148 | QVERIFY(gesture2->isActive() == true); |
149 | QVERIFY(recognizer->isActive() == true); |
150 | |
151 | gesture2->stopDetection(); |
152 | |
153 | QVERIFY(gesture->isActive() == true); |
154 | QVERIFY(gesture2->isActive() == false); |
155 | QVERIFY(recognizer->isActive() == true); |
156 | |
157 | gesture->stopDetection(); |
158 | |
159 | QVERIFY(gesture->isActive() == false); |
160 | QVERIFY(gesture2->isActive() == false); |
161 | QVERIFY(recognizer->isActive() == false); |
162 | |
163 | |
164 | QVERIFY(recognizer->gestureSignals().count() > 1); |
165 | QVERIFY(recognizer->gestureSignals().contains("detected(QString)" )); |
166 | |
167 | } |
168 | |
169 | void Tst_qsensorgesturePluginsTest::tst_sensor_plugins_qtsensors_all() |
170 | { |
171 | QStringList list; |
172 | list << "QtSensors.cover" ; |
173 | list << "QtSensors.doubletap" ; |
174 | list << "QtSensors.hover" ; |
175 | list << "QtSensors.pickup" ; |
176 | list << "QtSensors.shake2" ; |
177 | list << "QtSensors.slam" ; |
178 | list << "QtSensors.turnover" ; |
179 | list << "QtSensors.twist" ; |
180 | list << "QtSensors.whip" ; |
181 | |
182 | QSensorGestureManager manager; |
183 | |
184 | QScopedPointer<QSensorGesture> gesture(new QSensorGesture(list)); |
185 | QVERIFY(gesture->invalidIds().count() == 0); |
186 | QVERIFY(gesture->validIds().count() == 9); |
187 | QVERIFY(gesture->gestureSignals().count() == 14); |
188 | |
189 | gesture->startDetection(); |
190 | QVERIFY(gesture->isActive()); |
191 | |
192 | QSensorGestureRecognizer *recognizer = manager.sensorGestureRecognizer(id: "QtSensors.turnover" ); |
193 | recognizer->stopBackend(); |
194 | QVERIFY(!recognizer->isActive()); |
195 | QVERIFY(gesture->isActive()); |
196 | |
197 | } |
198 | |
199 | |
200 | QTEST_MAIN(Tst_qsensorgesturePluginsTest); |
201 | |
202 | #include "tst_qsensorgesturepluginstest.moc" |
203 | |