Changeset View
Changeset View
Standalone View
Standalone View
autotests/test_gestures.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | KWin - the KDE window manager | ||||
3 | This file is part of the KDE project. | ||||
4 | | ||||
5 | Copyright (C) 2017 Martin Gräßlin <mgraesslin@kde.org> | ||||
6 | | ||||
7 | This program is free software; you can redistribute it and/or modify | ||||
8 | it under the terms of the GNU General Public License as published by | ||||
9 | the Free Software Foundation; either version 2 of the License, or | ||||
10 | (at your option) any later version. | ||||
11 | | ||||
12 | This program is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | GNU General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU General Public License | ||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | *********************************************************************/ | ||||
20 | #include "../gestures.h" | ||||
21 | | ||||
22 | #include <QtTest/QTest> | ||||
23 | #include <QSignalSpy> | ||||
24 | | ||||
25 | using namespace KWin; | ||||
26 | | ||||
27 | class GestureTest : public QObject | ||||
28 | { | ||||
29 | Q_OBJECT | ||||
30 | private Q_SLOTS: | ||||
31 | void testSwipeMinFinger_data(); | ||||
32 | void testSwipeMinFinger(); | ||||
33 | void testSwipeMaxFinger_data(); | ||||
34 | void testSwipeMaxFinger(); | ||||
35 | void testDirection_data(); | ||||
36 | void testDirection(); | ||||
37 | void testUnregisterSwipeCancels(); | ||||
38 | void testDeleteSwipeCancels(); | ||||
39 | void testSwipeCancel_data(); | ||||
40 | void testSwipeCancel(); | ||||
41 | void testSwipeUpdateCancel(); | ||||
42 | void testSwipeUpdateTrigger_data(); | ||||
43 | void testSwipeUpdateTrigger(); | ||||
44 | void testSwipeMinFingerStart_data(); | ||||
45 | void testSwipeMinFingerStart(); | ||||
46 | void testSwipeMaxFingerStart_data(); | ||||
47 | void testSwipeMaxFingerStart(); | ||||
48 | void testSwipeDiagonalCancels_data(); | ||||
49 | void testSwipeDiagonalCancels(); | ||||
50 | }; | ||||
51 | | ||||
52 | void GestureTest::testSwipeMinFinger_data() | ||||
53 | { | ||||
54 | QTest::addColumn<uint>("count"); | ||||
55 | QTest::addColumn<uint>("expectedCount"); | ||||
56 | | ||||
57 | QTest::newRow("0") << 0u << 0u; | ||||
58 | QTest::newRow("1") << 1u << 1u; | ||||
59 | QTest::newRow("10") << 10u << 10u; | ||||
60 | } | ||||
61 | | ||||
62 | void GestureTest::testSwipeMinFinger() | ||||
63 | { | ||||
64 | SwipeGesture gesture; | ||||
65 | QCOMPARE(gesture.minimumFingerCountIsRelevant(), false); | ||||
66 | QCOMPARE(gesture.minimumFingerCount(), 0u); | ||||
67 | QFETCH(uint, count); | ||||
68 | gesture.setMinimumFingerCount(count); | ||||
69 | QCOMPARE(gesture.minimumFingerCountIsRelevant(), true); | ||||
70 | QTEST(gesture.minimumFingerCount(), "expectedCount"); | ||||
71 | gesture.setMinimumFingerCount(0); | ||||
72 | QCOMPARE(gesture.minimumFingerCountIsRelevant(), true); | ||||
73 | QCOMPARE(gesture.minimumFingerCount(), 0u); | ||||
74 | } | ||||
75 | | ||||
76 | void GestureTest::testSwipeMaxFinger_data() | ||||
77 | { | ||||
78 | QTest::addColumn<uint>("count"); | ||||
79 | QTest::addColumn<uint>("expectedCount"); | ||||
80 | | ||||
81 | QTest::newRow("0") << 0u << 0u; | ||||
82 | QTest::newRow("1") << 1u << 1u; | ||||
83 | QTest::newRow("10") << 10u << 10u; | ||||
84 | } | ||||
85 | | ||||
86 | void GestureTest::testSwipeMaxFinger() | ||||
87 | { | ||||
88 | SwipeGesture gesture; | ||||
89 | QCOMPARE(gesture.maximumFingerCountIsRelevant(), false); | ||||
90 | QCOMPARE(gesture.maximumFingerCount(), 0u); | ||||
91 | QFETCH(uint, count); | ||||
92 | gesture.setMaximumFingerCount(count); | ||||
93 | QCOMPARE(gesture.maximumFingerCountIsRelevant(), true); | ||||
94 | QTEST(gesture.maximumFingerCount(), "expectedCount"); | ||||
95 | gesture.setMaximumFingerCount(0); | ||||
96 | QCOMPARE(gesture.maximumFingerCountIsRelevant(), true); | ||||
97 | QCOMPARE(gesture.maximumFingerCount(), 0u); | ||||
98 | } | ||||
99 | | ||||
100 | void GestureTest::testDirection_data() | ||||
101 | { | ||||
102 | QTest::addColumn<KWin::SwipeGesture::Direction>("direction"); | ||||
103 | | ||||
104 | QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up; | ||||
105 | QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left; | ||||
106 | QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right; | ||||
107 | QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down; | ||||
108 | } | ||||
109 | | ||||
110 | void GestureTest::testDirection() | ||||
111 | { | ||||
112 | SwipeGesture gesture; | ||||
113 | QCOMPARE(gesture.direction(), SwipeGesture::Direction::Down); | ||||
114 | QFETCH(KWin::SwipeGesture::Direction, direction); | ||||
115 | gesture.setDirection(direction); | ||||
116 | QCOMPARE(gesture.direction(), direction); | ||||
117 | // back to down | ||||
118 | gesture.setDirection(SwipeGesture::Direction::Down); | ||||
119 | QCOMPARE(gesture.direction(), SwipeGesture::Direction::Down); | ||||
120 | } | ||||
121 | | ||||
122 | void GestureTest::testUnregisterSwipeCancels() | ||||
123 | { | ||||
124 | GestureRecognizer recognizer; | ||||
125 | QScopedPointer<SwipeGesture> gesture(new SwipeGesture); | ||||
126 | QSignalSpy startedSpy(gesture.data(), &SwipeGesture::started); | ||||
127 | QVERIFY(startedSpy.isValid()); | ||||
128 | QSignalSpy cancelledSpy(gesture.data(), &SwipeGesture::cancelled); | ||||
129 | QVERIFY(cancelledSpy.isValid()); | ||||
130 | | ||||
131 | recognizer.registerGesture(gesture.data()); | ||||
132 | recognizer.startSwipeGesture(1); | ||||
133 | QCOMPARE(startedSpy.count(), 1); | ||||
134 | QCOMPARE(cancelledSpy.count(), 0); | ||||
135 | recognizer.unregisterGesture(gesture.data()); | ||||
136 | QCOMPARE(cancelledSpy.count(), 1); | ||||
137 | | ||||
138 | // delete the gesture should not trigger cancel | ||||
139 | gesture.reset(); | ||||
140 | QCOMPARE(cancelledSpy.count(), 1); | ||||
141 | } | ||||
142 | | ||||
143 | void GestureTest::testDeleteSwipeCancels() | ||||
144 | { | ||||
145 | GestureRecognizer recognizer; | ||||
146 | QScopedPointer<SwipeGesture> gesture(new SwipeGesture); | ||||
147 | QSignalSpy startedSpy(gesture.data(), &SwipeGesture::started); | ||||
148 | QVERIFY(startedSpy.isValid()); | ||||
149 | QSignalSpy cancelledSpy(gesture.data(), &SwipeGesture::cancelled); | ||||
150 | QVERIFY(cancelledSpy.isValid()); | ||||
151 | | ||||
152 | recognizer.registerGesture(gesture.data()); | ||||
153 | recognizer.startSwipeGesture(1); | ||||
154 | QCOMPARE(startedSpy.count(), 1); | ||||
155 | QCOMPARE(cancelledSpy.count(), 0); | ||||
156 | gesture.reset(); | ||||
157 | QCOMPARE(cancelledSpy.count(), 1); | ||||
158 | } | ||||
159 | | ||||
160 | void GestureTest::testSwipeCancel_data() | ||||
161 | { | ||||
162 | QTest::addColumn<KWin::SwipeGesture::Direction>("direction"); | ||||
163 | | ||||
164 | QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up; | ||||
165 | QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left; | ||||
166 | QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right; | ||||
167 | QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down; | ||||
168 | } | ||||
169 | | ||||
170 | void GestureTest::testSwipeCancel() | ||||
171 | { | ||||
172 | GestureRecognizer recognizer; | ||||
173 | QScopedPointer<SwipeGesture> gesture(new SwipeGesture); | ||||
174 | QFETCH(SwipeGesture::Direction, direction); | ||||
175 | gesture->setDirection(direction); | ||||
176 | QSignalSpy startedSpy(gesture.data(), &SwipeGesture::started); | ||||
177 | QVERIFY(startedSpy.isValid()); | ||||
178 | QSignalSpy cancelledSpy(gesture.data(), &SwipeGesture::cancelled); | ||||
179 | QVERIFY(cancelledSpy.isValid()); | ||||
180 | QSignalSpy triggeredSpy(gesture.data(), &SwipeGesture::triggered); | ||||
181 | QVERIFY(triggeredSpy.isValid()); | ||||
182 | | ||||
183 | recognizer.registerGesture(gesture.data()); | ||||
184 | recognizer.startSwipeGesture(1); | ||||
185 | QCOMPARE(startedSpy.count(), 1); | ||||
186 | QCOMPARE(cancelledSpy.count(), 0); | ||||
187 | recognizer.cancelSwipeGesture(); | ||||
188 | QCOMPARE(cancelledSpy.count(), 1); | ||||
189 | QCOMPARE(triggeredSpy.count(), 0); | ||||
190 | } | ||||
191 | | ||||
192 | void GestureTest::testSwipeUpdateCancel() | ||||
193 | { | ||||
194 | GestureRecognizer recognizer; | ||||
195 | SwipeGesture upGesture; | ||||
196 | upGesture.setDirection(SwipeGesture::Direction::Up); | ||||
197 | SwipeGesture downGesture; | ||||
198 | downGesture.setDirection(SwipeGesture::Direction::Down); | ||||
199 | SwipeGesture rightGesture; | ||||
200 | rightGesture.setDirection(SwipeGesture::Direction::Right); | ||||
201 | SwipeGesture leftGesture; | ||||
202 | leftGesture.setDirection(SwipeGesture::Direction::Left); | ||||
203 | | ||||
204 | QSignalSpy upCancelledSpy(&upGesture, &SwipeGesture::cancelled); | ||||
205 | QVERIFY(upCancelledSpy.isValid()); | ||||
206 | QSignalSpy downCancelledSpy(&downGesture, &SwipeGesture::cancelled); | ||||
207 | QVERIFY(downCancelledSpy.isValid()); | ||||
208 | QSignalSpy rightCancelledSpy(&rightGesture, &SwipeGesture::cancelled); | ||||
209 | QVERIFY(rightCancelledSpy.isValid()); | ||||
210 | QSignalSpy leftCancelledSpy(&leftGesture, &SwipeGesture::cancelled); | ||||
211 | QVERIFY(leftCancelledSpy.isValid()); | ||||
212 | | ||||
213 | QSignalSpy upTriggeredSpy(&upGesture, &SwipeGesture::triggered); | ||||
214 | QVERIFY(upTriggeredSpy.isValid()); | ||||
215 | QSignalSpy downTriggeredSpy(&downGesture, &SwipeGesture::triggered); | ||||
216 | QVERIFY(downTriggeredSpy.isValid()); | ||||
217 | QSignalSpy rightTriggeredSpy(&rightGesture, &SwipeGesture::triggered); | ||||
218 | QVERIFY(rightTriggeredSpy.isValid()); | ||||
219 | QSignalSpy leftTriggeredSpy(&leftGesture, &SwipeGesture::triggered); | ||||
220 | QVERIFY(leftTriggeredSpy.isValid()); | ||||
221 | | ||||
222 | recognizer.registerGesture(&upGesture); | ||||
223 | recognizer.registerGesture(&downGesture); | ||||
224 | recognizer.registerGesture(&rightGesture); | ||||
225 | recognizer.registerGesture(&leftGesture); | ||||
226 | | ||||
227 | recognizer.startSwipeGesture(4); | ||||
228 | | ||||
229 | // first a down gesture | ||||
230 | recognizer.updateSwipeGesture(QSizeF(1, 20)); | ||||
231 | QCOMPARE(upCancelledSpy.count(), 1); | ||||
232 | QCOMPARE(downCancelledSpy.count(), 0); | ||||
233 | QCOMPARE(leftCancelledSpy.count(), 1); | ||||
234 | QCOMPARE(rightCancelledSpy.count(), 1); | ||||
235 | // another down gesture | ||||
236 | recognizer.updateSwipeGesture(QSizeF(-2, 10)); | ||||
237 | QCOMPARE(downCancelledSpy.count(), 0); | ||||
238 | // and an up gesture | ||||
239 | recognizer.updateSwipeGesture(QSizeF(-2, -10)); | ||||
240 | QCOMPARE(upCancelledSpy.count(), 1); | ||||
241 | QCOMPARE(downCancelledSpy.count(), 1); | ||||
242 | QCOMPARE(leftCancelledSpy.count(), 1); | ||||
243 | QCOMPARE(rightCancelledSpy.count(), 1); | ||||
244 | | ||||
245 | recognizer.endSwipeGesture(); | ||||
246 | QCOMPARE(upCancelledSpy.count(), 1); | ||||
247 | QCOMPARE(downCancelledSpy.count(), 1); | ||||
248 | QCOMPARE(leftCancelledSpy.count(), 1); | ||||
249 | QCOMPARE(rightCancelledSpy.count(), 1); | ||||
250 | QCOMPARE(upTriggeredSpy.count(), 0); | ||||
251 | QCOMPARE(downTriggeredSpy.count(), 0); | ||||
252 | QCOMPARE(leftTriggeredSpy.count(), 0); | ||||
253 | QCOMPARE(rightTriggeredSpy.count(), 0); | ||||
254 | } | ||||
255 | | ||||
256 | void GestureTest::testSwipeUpdateTrigger_data() | ||||
257 | { | ||||
258 | QTest::addColumn<KWin::SwipeGesture::Direction>("direction"); | ||||
259 | QTest::addColumn<QSizeF>("delta"); | ||||
260 | | ||||
261 | QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up << QSizeF(2, -3); | ||||
262 | QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left << QSizeF(-3, 1); | ||||
263 | QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right << QSizeF(20, -19); | ||||
264 | QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down << QSizeF(0, 50); | ||||
265 | } | ||||
266 | | ||||
267 | void GestureTest::testSwipeUpdateTrigger() | ||||
268 | { | ||||
269 | GestureRecognizer recognizer; | ||||
270 | SwipeGesture gesture; | ||||
271 | QFETCH(SwipeGesture::Direction, direction); | ||||
272 | gesture.setDirection(direction); | ||||
273 | | ||||
274 | QSignalSpy triggeredSpy(&gesture, &SwipeGesture::triggered); | ||||
275 | QVERIFY(triggeredSpy.isValid()); | ||||
276 | QSignalSpy cancelledSpy(&gesture, &SwipeGesture::cancelled); | ||||
277 | QVERIFY(cancelledSpy.isValid()); | ||||
278 | | ||||
279 | recognizer.registerGesture(&gesture); | ||||
280 | | ||||
281 | recognizer.startSwipeGesture(1); | ||||
282 | QFETCH(QSizeF, delta); | ||||
283 | recognizer.updateSwipeGesture(delta); | ||||
284 | QCOMPARE(cancelledSpy.count(), 0); | ||||
285 | QCOMPARE(triggeredSpy.count(), 0); | ||||
286 | | ||||
287 | recognizer.endSwipeGesture(); | ||||
288 | QCOMPARE(cancelledSpy.count(), 0); | ||||
289 | QCOMPARE(triggeredSpy.count(), 1); | ||||
290 | } | ||||
291 | | ||||
292 | void GestureTest::testSwipeMinFingerStart_data() | ||||
293 | { | ||||
294 | QTest::addColumn<uint>("min"); | ||||
295 | QTest::addColumn<uint>("count"); | ||||
296 | QTest::addColumn<bool>("started"); | ||||
297 | | ||||
298 | QTest::newRow("same") << 1u << 1u << true; | ||||
299 | QTest::newRow("less") << 2u << 1u << false; | ||||
300 | QTest::newRow("more") << 1u << 2u << true; | ||||
301 | } | ||||
302 | | ||||
303 | void GestureTest::testSwipeMinFingerStart() | ||||
304 | { | ||||
305 | GestureRecognizer recognizer; | ||||
306 | SwipeGesture gesture; | ||||
307 | QFETCH(uint, min); | ||||
308 | gesture.setMinimumFingerCount(min); | ||||
309 | | ||||
310 | QSignalSpy startedSpy(&gesture, &SwipeGesture::started); | ||||
311 | QVERIFY(startedSpy.isValid()); | ||||
312 | | ||||
313 | recognizer.registerGesture(&gesture); | ||||
314 | QFETCH(uint, count); | ||||
315 | recognizer.startSwipeGesture(count); | ||||
316 | QTEST(!startedSpy.isEmpty(), "started"); | ||||
317 | } | ||||
318 | | ||||
319 | void GestureTest::testSwipeMaxFingerStart_data() | ||||
320 | { | ||||
321 | QTest::addColumn<uint>("max"); | ||||
322 | QTest::addColumn<uint>("count"); | ||||
323 | QTest::addColumn<bool>("started"); | ||||
324 | | ||||
325 | QTest::newRow("same") << 1u << 1u << true; | ||||
326 | QTest::newRow("less") << 2u << 1u << true; | ||||
327 | QTest::newRow("more") << 1u << 2u << false; | ||||
328 | } | ||||
329 | | ||||
330 | void GestureTest::testSwipeMaxFingerStart() | ||||
331 | { | ||||
332 | GestureRecognizer recognizer; | ||||
333 | SwipeGesture gesture; | ||||
334 | QFETCH(uint, max); | ||||
335 | gesture.setMaximumFingerCount(max); | ||||
336 | | ||||
337 | QSignalSpy startedSpy(&gesture, &SwipeGesture::started); | ||||
338 | QVERIFY(startedSpy.isValid()); | ||||
339 | | ||||
340 | recognizer.registerGesture(&gesture); | ||||
341 | QFETCH(uint, count); | ||||
342 | recognizer.startSwipeGesture(count); | ||||
343 | QTEST(!startedSpy.isEmpty(), "started"); | ||||
344 | } | ||||
345 | | ||||
346 | void GestureTest::testSwipeDiagonalCancels_data() | ||||
347 | { | ||||
348 | QTest::addColumn<KWin::SwipeGesture::Direction>("direction"); | ||||
349 | | ||||
350 | QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up; | ||||
351 | QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left; | ||||
352 | QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right; | ||||
353 | QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down; | ||||
354 | } | ||||
355 | | ||||
356 | void GestureTest::testSwipeDiagonalCancels() | ||||
357 | { | ||||
358 | GestureRecognizer recognizer; | ||||
359 | SwipeGesture gesture; | ||||
360 | QFETCH(SwipeGesture::Direction, direction); | ||||
361 | gesture.setDirection(direction); | ||||
362 | | ||||
363 | QSignalSpy triggeredSpy(&gesture, &SwipeGesture::triggered); | ||||
364 | QVERIFY(triggeredSpy.isValid()); | ||||
365 | QSignalSpy cancelledSpy(&gesture, &SwipeGesture::cancelled); | ||||
366 | QVERIFY(cancelledSpy.isValid()); | ||||
367 | | ||||
368 | recognizer.registerGesture(&gesture); | ||||
369 | | ||||
370 | recognizer.startSwipeGesture(1); | ||||
371 | recognizer.updateSwipeGesture(QSizeF(1, 1)); | ||||
372 | QCOMPARE(cancelledSpy.count(), 1); | ||||
373 | QCOMPARE(triggeredSpy.count(), 0); | ||||
374 | | ||||
375 | recognizer.endSwipeGesture(); | ||||
376 | QCOMPARE(cancelledSpy.count(), 1); | ||||
377 | QCOMPARE(triggeredSpy.count(), 0); | ||||
378 | | ||||
379 | } | ||||
380 | | ||||
381 | QTEST_MAIN(GestureTest) | ||||
382 | #include "test_gestures.moc" |