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 <QString> |
30 | #include <QtTest> |
31 | #include <QtSensors/QSensorGestureManager> |
32 | #include <QtSensors/QSensorGesture> |
33 | #include <QFile> |
34 | |
35 | #include "mockbackends.h" |
36 | |
37 | class tst_sensorgestures_gestures : public QObject |
38 | { |
39 | Q_OBJECT |
40 | |
41 | public: |
42 | tst_sensorgestures_gestures(); |
43 | |
44 | private Q_SLOTS: |
45 | void initTestCase(); |
46 | |
47 | void testTiltedTwist(); |
48 | void testNotPickup(); |
49 | |
50 | void testNotHover2(); |
51 | void testNotHover(); |
52 | void testNotWhip(); |
53 | |
54 | void testSingleGestures(); |
55 | void testSingleGestures_data(); |
56 | |
57 | void testSingleDataset2Gestures(); |
58 | void testSingleDataset2Gestures_data(); |
59 | |
60 | void testTwist(); |
61 | void testTwist_data(); |
62 | |
63 | void testShake2(); |
64 | void testShake2_data(); |
65 | |
66 | void testShake(); |
67 | |
68 | void testAllGestures(); |
69 | void testAllGestures_data(); |
70 | |
71 | |
72 | protected: |
73 | mockSensorPlugin plugin; |
74 | |
75 | }; |
76 | |
77 | tst_sensorgestures_gestures::tst_sensorgestures_gestures() |
78 | { |
79 | } |
80 | |
81 | void tst_sensorgestures_gestures::initTestCase() |
82 | { |
83 | qputenv(varName: "QT_SENSORS_LOAD_PLUGINS" , value: "0" ); // Do not load plugins |
84 | plugin.registerSensors(); |
85 | } |
86 | |
87 | void tst_sensorgestures_gestures::testSingleGestures() |
88 | { |
89 | QFETCH(QString, gestureId); |
90 | QFETCH(QStringList, gestureSignals); |
91 | |
92 | QString name = "mock_data/sensordata_" + gestureId + ".dat" ; |
93 | |
94 | QSensorGestureManager manager; |
95 | QStringList idList = manager.gestureIds(); |
96 | QString gestStr = QLatin1String("QtSensors." ) + gestureId; |
97 | |
98 | QVERIFY(idList.contains(gestStr)); |
99 | |
100 | QScopedPointer<QSensorGesture> gesture(new QSensorGesture(QStringList() << gestStr)); |
101 | QVERIFY(gesture.data()->validIds().contains(gestStr)); |
102 | |
103 | QSignalSpy spy_gesture(gesture.data(), SIGNAL(detected(QString))); |
104 | |
105 | QCOMPARE(mockcommonPrivate::instance()->setFile(name), true); |
106 | gesture.data()->startDetection(); |
107 | QCOMPARE(gesture->isActive(),true); |
108 | |
109 | auto toStringList = [](const QList<QList<QVariant>> list) { |
110 | QStringList result; |
111 | for (const QList<QVariant> &item: list) { |
112 | if (!item.isEmpty()) |
113 | result << item.first().toString(); |
114 | } |
115 | return result; |
116 | }; |
117 | |
118 | QTRY_COMPARE_WITH_TIMEOUT(toStringList(spy_gesture), gestureSignals, 7000); |
119 | } |
120 | |
121 | void tst_sensorgestures_gestures::testSingleGestures_data() |
122 | { |
123 | QTest::addColumn<QString>(name: "gestureId" ); |
124 | QTest::addColumn<QStringList>(name: "gestureSignals" ); |
125 | QTest::newRow(dataTag: "cover" ) << "cover" << QStringList({ "cover" }); |
126 | QTest::newRow(dataTag: "doubletap" ) << "doubletap" << QStringList({ "doubletap" }); |
127 | QTest::newRow(dataTag: "hover" ) << "hover" << QStringList({ "hover" }); |
128 | QTest::newRow(dataTag: "pickup" ) << "pickup" << QStringList({ "pickup" }); |
129 | QTest::newRow(dataTag: "shake2" ) << "shake2" << QStringList({ "shakeRight" }); |
130 | QTest::newRow(dataTag: "slam" ) << "slam" << QStringList({ "slam" }); |
131 | QTest::newRow(dataTag: "turnover" ) << "turnover" << QStringList({ "turnover" }); |
132 | QTest::newRow(dataTag: "twist" ) << "twist" << QStringList({ "twistLeft" , "twistLeft" , "twistRight" }); |
133 | QTest::newRow(dataTag: "whip" ) << "whip" << QStringList({ "whip" }); |
134 | } |
135 | |
136 | void tst_sensorgestures_gestures::testSingleDataset2Gestures() |
137 | { |
138 | QFETCH(QString, gestureId); |
139 | |
140 | QString name = "dataset2_mock_data/sensordata_" + gestureId + ".dat" ; |
141 | QSensorGestureManager manager; |
142 | QStringList idList = manager.gestureIds(); |
143 | |
144 | QString gestStr = QLatin1String("QtSensors." ) + gestureId; |
145 | |
146 | QVERIFY(idList.contains(gestStr)); |
147 | |
148 | QScopedPointer<QSensorGesture> gesture(new QSensorGesture(QStringList() << gestStr)); |
149 | QVERIFY(gesture.data()->validIds().contains(gestStr)); |
150 | |
151 | QSignalSpy spy_gesture(gesture.data(), SIGNAL(detected(QString))); |
152 | |
153 | QCOMPARE(mockcommonPrivate::instance()->setFile(name), true); |
154 | gesture.data()->startDetection(); |
155 | QCOMPARE(gesture->isActive(),true); |
156 | |
157 | QTRY_COMPARE_WITH_TIMEOUT(spy_gesture.count(),1, 7000); |
158 | } |
159 | |
160 | void tst_sensorgestures_gestures::testSingleDataset2Gestures_data() |
161 | { |
162 | QTest::addColumn<QString>(name: "gestureId" ); |
163 | QTest::newRow(dataTag: "cover" ) << "cover" ; |
164 | QTest::newRow(dataTag: "doubletap" ) << "doubletap" ; |
165 | QTest::newRow(dataTag: "hover" ) << "hover" ; |
166 | QTest::newRow(dataTag: "pickup" ) << "pickup" ; |
167 | QTest::newRow(dataTag: "shake2" ) << "shake2" ; //multi? |
168 | QTest::newRow(dataTag: "slam" ) << "slam" ; |
169 | QTest::newRow(dataTag: "turnover" ) << "turnover" ; |
170 | QTest::newRow(dataTag: "twist" ) << "twist" ; //multi? |
171 | QTest::newRow(dataTag: "whip" ) << "whip" ; |
172 | } |
173 | |
174 | void tst_sensorgestures_gestures::testTwist() |
175 | { |
176 | QFETCH(QString, gestureSignal); |
177 | |
178 | QString name = "mock_data/sensordata_" + gestureSignal + ".dat" ; |
179 | |
180 | QSensorGestureManager manager; |
181 | QStringList idList = manager.gestureIds(); |
182 | |
183 | QString gestStr = QLatin1String("QtSensors.twist" ); |
184 | |
185 | QVERIFY(idList.contains(gestStr)); |
186 | |
187 | QScopedPointer<QSensorGesture> gesture2(new QSensorGesture(QStringList() << gestStr)); |
188 | QVERIFY(gesture2.data()->validIds().contains(gestStr)); |
189 | |
190 | QSignalSpy spy_gesture(gesture2.data(), SIGNAL(detected(QString))); |
191 | |
192 | QCOMPARE(mockcommonPrivate::instance()->setFile(name), true); |
193 | gesture2.data()->startDetection(); |
194 | QCOMPARE(gesture2->isActive(),true); |
195 | |
196 | QTRY_COMPARE_WITH_TIMEOUT(spy_gesture.count(),1, 7000); |
197 | |
198 | QList<QVariant> arguments = spy_gesture.takeFirst(); |
199 | QCOMPARE(arguments.at(0).toString(), QString(gestureSignal)); |
200 | } |
201 | |
202 | void tst_sensorgestures_gestures::testTwist_data() |
203 | { |
204 | QTest::addColumn<QString>(name: "gestureSignal" ); |
205 | QTest::newRow(dataTag: "twistLeft" ) << "twistLeft" ; |
206 | QTest::newRow(dataTag: "twistRight" ) << "twistRight" ; |
207 | } |
208 | |
209 | void tst_sensorgestures_gestures::testShake2() |
210 | { |
211 | QFETCH(QString, gestureSignal); |
212 | |
213 | QString name = "mock_data/sensordata_" + gestureSignal + ".dat" ; |
214 | |
215 | QSensorGestureManager manager; |
216 | QStringList idList = manager.gestureIds(); |
217 | |
218 | QString gestStr = QLatin1String("QtSensors.shake2" ); |
219 | |
220 | QVERIFY(idList.contains(gestStr)); |
221 | |
222 | QScopedPointer<QSensorGesture> gesture(new QSensorGesture(QStringList() << gestStr)); |
223 | QVERIFY(gesture.data()->validIds().contains(gestStr)); |
224 | |
225 | QSignalSpy spy_gesture(gesture.data(), SIGNAL(detected(QString))); |
226 | |
227 | QCOMPARE(mockcommonPrivate::instance()->setFile(name), true); |
228 | gesture.data()->startDetection(); |
229 | QCOMPARE(gesture->isActive(),true); |
230 | |
231 | QTRY_COMPARE_WITH_TIMEOUT(spy_gesture.count(),1, 7000); |
232 | QList<QVariant> arguments = spy_gesture.takeFirst(); |
233 | QCOMPARE(arguments.at(0).toString(), QString(gestureSignal)); |
234 | |
235 | gesture.data()->stopDetection(); |
236 | } |
237 | |
238 | void tst_sensorgestures_gestures::testShake2_data() |
239 | { |
240 | QTest::addColumn<QString>(name: "gestureSignal" ); |
241 | QTest::newRow(dataTag: "shakeLeft" ) << "shakeLeft" ; |
242 | QTest::newRow(dataTag: "shakeRight" ) << "shakeRight" ; |
243 | QTest::newRow(dataTag: "shakeUp" ) << "shakeUp" ; |
244 | QTest::newRow(dataTag: "shakeDown" ) << "shakeDown" ; |
245 | } |
246 | |
247 | void tst_sensorgestures_gestures::testShake() |
248 | { |
249 | QString gestureSignal = "shake" ; |
250 | |
251 | QString name = "mock_data/sensordata_shake2.dat" ; |
252 | |
253 | QSensorGestureManager manager; |
254 | QStringList idList = manager.gestureIds(); |
255 | |
256 | QString gestStr = QLatin1String("QtSensors.shake" ); |
257 | |
258 | QVERIFY(idList.contains(gestStr)); |
259 | |
260 | QScopedPointer<QSensorGesture> gesture(new QSensorGesture(QStringList() << gestStr)); |
261 | QVERIFY(gesture.data()->validIds().contains(gestStr)); |
262 | |
263 | QSignalSpy spy_gesture(gesture.data(), SIGNAL(detected(QString))); |
264 | |
265 | QCOMPARE(mockcommonPrivate::instance()->setFile(name), true); |
266 | gesture.data()->startDetection(); |
267 | QCOMPARE(gesture->isActive(),true); |
268 | |
269 | QTRY_COMPARE_WITH_TIMEOUT(spy_gesture.count(),1, 7000); |
270 | QList<QVariant> arguments = spy_gesture.takeFirst(); |
271 | |
272 | QCOMPARE(arguments.at(0).toString(), QString(gestureSignal)); |
273 | gesture.data()->stopDetection(); |
274 | } |
275 | |
276 | |
277 | void tst_sensorgestures_gestures::testAllGestures_data() |
278 | { |
279 | testSingleGestures_data(); |
280 | } |
281 | |
282 | void tst_sensorgestures_gestures::testAllGestures() |
283 | { |
284 | QFETCH(QString, gestureId); |
285 | |
286 | QString name = "dataset2_mock_data/sensordata_" + gestureId + ".dat" ; |
287 | |
288 | QSensorGestureManager manager; |
289 | QStringList idList = manager.gestureIds(); |
290 | |
291 | QStringList gestStringList; |
292 | |
293 | gestStringList << "QtSensors.cover" |
294 | << "QtSensors.doubletap" |
295 | << "QtSensors.hover" |
296 | << "QtSensors.pickup" |
297 | << "QtSensors.shake2" |
298 | << "QtSensors.slam" |
299 | << "QtSensors.turnover" |
300 | << "QtSensors.twist" |
301 | << "QtSensors.whip" ; |
302 | |
303 | QScopedPointer<QSensorGesture> gesture(new QSensorGesture(gestStringList)); |
304 | |
305 | QCOMPARE(gesture->invalidIds().count(),0); |
306 | QSignalSpy spy_gesture(gesture.data(), SIGNAL(detected(QString))); |
307 | |
308 | QCOMPARE(mockcommonPrivate::instance()->setFile(name), true); |
309 | gesture.data()->startDetection(); |
310 | QCOMPARE(gesture->isActive(),true); |
311 | |
312 | QTRY_COMPARE_WITH_TIMEOUT(spy_gesture.count(),1, 7000); |
313 | |
314 | gesture.data()->stopDetection(); |
315 | |
316 | QList<QVariant> arguments = spy_gesture.takeFirst(); |
317 | QString gestureSignal; |
318 | if (gestureId.right(n: 1) == QLatin1String("2" )) { |
319 | gestureSignal = "shakeLeft" ; |
320 | } else if (gestureId.contains(s: "twist" )) { |
321 | gestureSignal = "twistLeft" ; |
322 | } else { |
323 | gestureSignal = gestureId; |
324 | } |
325 | |
326 | QCOMPARE(arguments.at(0).toString(), QString(gestureSignal)); |
327 | } |
328 | |
329 | void tst_sensorgestures_gestures::testNotHover() |
330 | { |
331 | QString name = "mock_data/sensordata_nothover.dat" ; |
332 | |
333 | QSensorGestureManager manager; |
334 | QStringList idList = manager.gestureIds(); |
335 | |
336 | QStringList gestStringList; |
337 | |
338 | gestStringList << "QtSensors.hover" ; |
339 | QScopedPointer<QSensorGesture> gesture(new QSensorGesture(gestStringList)); |
340 | |
341 | QCOMPARE(gesture->invalidIds().count(),0); |
342 | QSignalSpy spy_gesture(gesture.data(), SIGNAL(detected(QString))); |
343 | |
344 | QCOMPARE(mockcommonPrivate::instance()->setFile(name), true); |
345 | gesture.data()->startDetection(); |
346 | QCOMPARE(gesture->isActive(),true); |
347 | |
348 | QTRY_COMPARE_WITH_TIMEOUT(spy_gesture.count(),0, 2000); |
349 | |
350 | } |
351 | |
352 | void tst_sensorgestures_gestures::testNotWhip() |
353 | { |
354 | |
355 | QString name = "mock_data/sensordata_notwhip.dat" ; |
356 | |
357 | QSensorGestureManager manager; |
358 | QStringList idList = manager.gestureIds(); |
359 | |
360 | QStringList gestStringList; |
361 | |
362 | gestStringList << "QtSensors.whip" ; |
363 | QScopedPointer<QSensorGesture> gesture(new QSensorGesture(gestStringList)); |
364 | |
365 | QCOMPARE(gesture->invalidIds().count(),0); |
366 | QSignalSpy spy_gesture(gesture.data(), SIGNAL(detected(QString))); |
367 | |
368 | QCOMPARE(mockcommonPrivate::instance()->setFile(name), true); |
369 | gesture.data()->startDetection(); |
370 | QCOMPARE(gesture->isActive(),true); |
371 | |
372 | QTRY_COMPARE_WITH_TIMEOUT(spy_gesture.count(),0, 2000); |
373 | |
374 | } |
375 | |
376 | void tst_sensorgestures_gestures::testNotHover2() |
377 | { |
378 | // test slam when coming to close to head |
379 | QString name = "dataset2_mock_data/sensordata_nothover2.dat" ; |
380 | |
381 | QStringList gestStringList; |
382 | |
383 | gestStringList << "QtSensors.hover" ; |
384 | gestStringList << "QtSensors.slam" ; |
385 | |
386 | QScopedPointer<QSensorGesture> gesture(new QSensorGesture(gestStringList)); |
387 | |
388 | QCOMPARE(gesture->invalidIds().count(),0); |
389 | QSignalSpy spy_gesture(gesture.data(), SIGNAL(detected(QString))); |
390 | |
391 | QCOMPARE(mockcommonPrivate::instance()->setFile(name), true); |
392 | gesture.data()->startDetection(); |
393 | QCOMPARE(gesture->isActive(),true); |
394 | |
395 | QTRY_COMPARE_WITH_TIMEOUT(spy_gesture.count(),1, 2000); |
396 | |
397 | QList<QVariant> arguments = spy_gesture.takeFirst(); |
398 | QCOMPARE(arguments.at(0).toString(), QLatin1String("slam" )); |
399 | } |
400 | |
401 | void tst_sensorgestures_gestures::testTiltedTwist() |
402 | { |
403 | QString name = "mock_data/sensordata_tiltedtwist.dat" ; |
404 | |
405 | QStringList gestStringList; |
406 | |
407 | gestStringList << "QtSensors.twist" ; |
408 | QScopedPointer<QSensorGesture> gesture(new QSensorGesture(gestStringList)); |
409 | |
410 | QCOMPARE(gesture->invalidIds().count(),0); |
411 | QSignalSpy spy_gesture(gesture.data(), SIGNAL(detected(QString))); |
412 | |
413 | QCOMPARE(mockcommonPrivate::instance()->setFile(name), true); |
414 | gesture.data()->startDetection(); |
415 | QCOMPARE(gesture->isActive(),true); |
416 | |
417 | QTRY_COMPARE_WITH_TIMEOUT(spy_gesture.count(),1, 7000); |
418 | |
419 | QList<QVariant> arguments = spy_gesture.takeFirst(); |
420 | QCOMPARE(arguments.at(0).toString(), QLatin1String("twistLeft" )); |
421 | } |
422 | |
423 | void tst_sensorgestures_gestures::testNotPickup() |
424 | { |
425 | // QString name = "mock_data/sensordata_notpickup.dat"; |
426 | |
427 | // QStringList gestStringList; |
428 | // gestStringList << "QtSensors.pickup" << "QtSensors.twist"; |
429 | |
430 | // QScopedPointer<QSensorGesture> gesture(new QSensorGesture(gestStringList)); |
431 | |
432 | // QCOMPARE(gesture->invalidIds().count(),0); |
433 | // QSignalSpy spy_gesture(gesture.data(), SIGNAL(detected(QString))); |
434 | |
435 | // QCOMPARE(mockcommonPrivate::instance()->setFile(name), true); |
436 | // gesture.data()->startDetection(); |
437 | // QCOMPARE(gesture->isActive(),true); |
438 | |
439 | // QTRY_COMPARE_WITH_TIMEOUT(spy_gesture.count(),1, 7000); |
440 | |
441 | // QList<QVariant> arguments = spy_gesture.takeFirst(); |
442 | // QCOMPARE(arguments.at(0).toString(), QLatin1String("twistLeft")); |
443 | } |
444 | |
445 | |
446 | |
447 | QTEST_MAIN(tst_sensorgestures_gestures) |
448 | |
449 | #include "tst_sensorgestures_gestures.moc" |
450 | |