2 * Copyright (C) 2013 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17#include <QtTest/QtTest>
18#include <QtCore/QObject>
19#include <qpa/qwindowsysteminterface.h>
20#include <QtQuick/QQuickView>
21#include <QtQml/QQmlEngine>
23#include <DirectionalDragArea.h>
25class FakeTimer : public UbuntuGestures::AbstractTimer
29 FakeTimer(QObject *parent = 0)
30 : UbuntuGestures::AbstractTimer(parent)
33 virtual int interval() const { return m_duration; }
34 virtual void setInterval(int msecs) { m_duration = msecs; }
36 void emitTimeout() {Q_EMIT timeout();}
41class FakeTimeSource : public AxisVelocityCalculator::TimeSource
44 FakeTimeSource() { m_msecsSinceReference = 0; }
45 virtual qint64 msecsSinceReference() {return m_msecsSinceReference;}
46 qint64 m_msecsSinceReference;
49class tst_DirectionalDragArea: public QObject
53 tst_DirectionalDragArea() : device(0) { }
55 void initTestCase(); // will be called before the first test function is executed
56 void cleanupTestCase(); // will be called after the last test function was executed.
58 void init(); // called right before each and every test function is executed
59 void cleanup(); // called right after each and every test function is executed
63 void dragWithShortDirectionChange();
66 void recognitionTimerUsage();
67 void maxSilenceTime();
70 QQuickView *createView();
74 FakeTimeSource *fakeTimeSource;
75 AxisVelocityCalculator *velCalcWithFakeTiming;
78void tst_DirectionalDragArea::initTestCase()
81 device = new QTouchDevice;
82 device->setType(QTouchDevice::TouchScreen);
83 QWindowSystemInterface::registerTouchDevice(device);
89void tst_DirectionalDragArea::cleanupTestCase()
93void tst_DirectionalDragArea::init()
96 view->setSource(QUrl::fromLocalFile("edgeDragExample.qml"));
98 QVERIFY(QTest::qWaitForWindowExposed(view));
99 QVERIFY(view->rootObject() != 0);
100 qApp->processEvents();
102 fakeTimer = new FakeTimer;
103 fakeTimeSource = new FakeTimeSource;
104 velCalcWithFakeTiming = new AxisVelocityCalculator(fakeTimeSource);
107void tst_DirectionalDragArea::cleanup()
115 // will bring fakeTimeSource down with it
116 delete velCalcWithFakeTiming;
118 velCalcWithFakeTiming = 0;
121QQuickView *tst_DirectionalDragArea::createView()
123 QQuickView *window = new QQuickView(0);
124 window->setResizeMode(QQuickView::SizeRootObjectToView);
125 window->resize(600, 600);
126 window->engine()->addImportPath(QLatin1String(UBUNTU_GESTURES_PLUGIN_DIR));
132QPointF calculateInitialTouchPos(DirectionalDragArea *edgeDragArea, QQuickView *view)
134 switch (edgeDragArea->direction()) {
135 case Direction::Upwards:
136 return QPointF(view->width()/2.0f, view->height() - (edgeDragArea->height()/2.0f));
137 case Direction::Downwards:
138 return QPointF(view->width()/2.0f, edgeDragArea->height()/2.0f);
139 case Direction::Leftwards:
140 return QPointF(view->width() - (edgeDragArea->width()/2.0f), view->height()/2.0f);
141 default: // Direction::Rightwards:
142 return QPointF(edgeDragArea->width()/2.0f, view->height()/2.0f);
146QPointF calculateDirectionVector(DirectionalDragArea *edgeDragArea,
147 qreal wideningAngleMultiplier)
149 qreal angleRadians = edgeDragArea->wideningAngle() * wideningAngleMultiplier
152 qreal angleCos = qCos(angleRadians);
153 qreal angleSin = qSin(angleRadians);
155 switch (edgeDragArea->direction()) {
156 case Direction::Upwards:
157 return QPointF(angleSin, -angleCos);
158 case Direction::Downwards:
159 return QPointF(angleSin, angleCos);
160 case Direction::Leftwards:
161 return QPointF(-angleCos, angleSin);
162 default: // Direction::Rightwards:
163 return QPointF(angleCos, angleSin);
167QPointF createTouchDeviation(DirectionalDragArea *edgeDragArea)
169 qreal deviation = edgeDragArea->maxDeviation() * 0.8;
171 if (Direction::isHorizontal(edgeDragArea->direction())) {
172 return QPointF(0, deviation);
174 return QPointF(deviation, 0);
179void tst_DirectionalDragArea::edgeDrag()
181 QFETCH(QString, dragAreaObjectName);
182 QFETCH(qreal, wideningAngleMultiplier);
183 QFETCH(qreal, dragDistanceFactor);
184 QFETCH(int, expectedGestureRecognition);
186 DirectionalDragArea *edgeDragArea =
187 view->rootObject()->findChild<DirectionalDragArea*>(dragAreaObjectName);
188 QVERIFY(edgeDragArea != 0);
189 edgeDragArea->setRecognitionTimer(fakeTimer);
190 edgeDragArea->setAxisVelocityCalculator(velCalcWithFakeTiming);
192 QSignalSpy draggingSpy(edgeDragArea, SIGNAL(draggingChanged(bool)));
194 QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, view);
195 QPointF touchPoint = initialTouchPos;
197 qreal desiredDragDistance = edgeDragArea->distanceThreshold()*dragDistanceFactor;
198 QPointF dragDirectionVector = calculateDirectionVector(edgeDragArea,
199 wideningAngleMultiplier);
200 QPointF touchMovement = dragDirectionVector * (edgeDragArea->distanceThreshold() * 0.1f);
202 QTest::touchEvent(view, device).press(0, touchPoint.toPoint());
204 QCOMPARE(draggingSpy.count(), 1);
205 QCOMPARE(edgeDragArea->dragging(), true);
207 if (wideningAngleMultiplier > 0) {
208 // go close to the border of the valid area for this touch point
209 // in order to make it easier to leave it by dragging at an angle
210 // slightly bigger than the widening angle
211 touchPoint += createTouchDeviation(edgeDragArea);
212 QTest::touchEvent(view, device).move(0, touchPoint.toPoint());
216 touchPoint += touchMovement;
217 QTest::touchEvent(view, device).move(0, touchPoint.toPoint());
218 } while ((touchPoint - initialTouchPos).manhattanLength() < desiredDragDistance);
220 if (expectedGestureRecognition)
221 QCOMPARE((int)edgeDragArea->status(), (int)DirectionalDragArea::Recognized);
223 if (edgeDragArea->status() == DirectionalDragArea::Rejected) {
224 QCOMPARE(edgeDragArea->dragging(), false);
225 QCOMPARE(draggingSpy.count(), 2);
228 QTest::touchEvent(view, device).release(0, touchPoint.toPoint());
230 QCOMPARE(draggingSpy.count(), 2);
231 QCOMPARE(edgeDragArea->dragging(), false);
234void tst_DirectionalDragArea::edgeDrag_data()
236 QTest::addColumn<QString>("dragAreaObjectName");
237 QTest::addColumn<qreal>("wideningAngleMultiplier");
238 QTest::addColumn<qreal>("dragDistanceFactor");
239 QTest::addColumn<int>("expectedGestureRecognition");
241 QTest::newRow("rightwards, tiny drag")
242 << "hpDragArea" << 0.0 << 0.2 << 0;
244 QTest::newRow("rightwards, straight drag")
245 << "hpDragArea" << 0.0 << 3.0 << 1;
247 QTest::newRow("rightwards, diagonal drag")
248 << "hpDragArea" << 0.9 << 3.0 << 1;
250 QTest::newRow("rightwards, overly diagonal drag")
251 << "hpDragArea" << 2.0 << 3.0 << 0;
253 QTest::newRow("leftwards, tiny drag")
254 << "hnDragArea" << 0.0 << 0.2 << 0;
256 QTest::newRow("leftwards, straight drag")
257 << "hnDragArea" << 0.0 << 3.0 << 1;
259 QTest::newRow("leftwards, diagonal drag")
260 << "hnDragArea" << 0.9 << 3.0 << 1;
262 QTest::newRow("downwards, tiny drag")
263 << "vpDragArea" << 0.0 << 0.2 << 0;
265 QTest::newRow("downwards, straight drag")
266 << "vpDragArea" << 0.0 << 3.0 << 1;
268 QTest::newRow("downwards, diagonal drag")
269 << "vpDragArea" << 0.9 << 3.0 << 1;
271 QTest::newRow("upwards, tiny drag")
272 << "vnDragArea" << 0.0 << 0.2 << 0;
274 QTest::newRow("upwards, straight drag")
275 << "vnDragArea" << 0.0 << 3.0 << 1;
277 QTest::newRow("upwards, diagonal drag")
278 << "vnDragArea" << 0.9 << 3.0 << 1;
280 QTest::newRow("upwards, overly diagonal drag")
281 << "vnDragArea" << 2.0 << 3.0 << 0;
285 A directional drag should still be recognized if there is a momentaneous, small,
286 change in the direction of a drag. That should be accounted as input noise and
289void tst_DirectionalDragArea::dragWithShortDirectionChange()
291 DirectionalDragArea *edgeDragArea =
292 view->rootObject()->findChild<DirectionalDragArea*>("hpDragArea");
293 QVERIFY(edgeDragArea != 0);
294 edgeDragArea->setRecognitionTimer(fakeTimer);
295 edgeDragArea->setAxisVelocityCalculator(velCalcWithFakeTiming);
297 QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, view);
298 QPointF touchPoint = initialTouchPos;
300 qreal desiredDragDistance = edgeDragArea->distanceThreshold()*2.0;
301 QPointF dragDirectionVector(1.0, 0.0);
302 QPointF touchMovement = dragDirectionVector * (edgeDragArea->distanceThreshold() * 0.1f);
304 QTest::touchEvent(view, device).press(0, touchPoint.toPoint());
306 // Move a bit in the proper direction
307 for (int i=0; i < 3; ++i) {
308 touchPoint += touchMovement;
309 QTest::touchEvent(view, device).move(0, touchPoint.toPoint());
312 // Then a sudden and small movement to the opposite direction
313 touchPoint -= dragDirectionVector * (edgeDragArea->maxDeviation() * 0.7);
314 QTest::touchEvent(view, device).move(0, touchPoint.toPoint());
316 // And then resume movment in the correct direction until it crosses the distance threshold.
318 touchPoint += touchMovement;
319 QTest::touchEvent(view, device).move(0, touchPoint.toPoint());
320 } while ((touchPoint - initialTouchPos).manhattanLength() < desiredDragDistance);
322 QCOMPARE((int)edgeDragArea->status(), (int)DirectionalDragArea::Recognized);
324 QTest::touchEvent(view, device).release(0, touchPoint.toPoint());
328 Checks that a gesture will be rejected if it's slower than minSpeed while
331void tst_DirectionalDragArea::minSpeed()
333 QFETCH(int, minSpeedMsecsDeviation);
334 QFETCH(int, expectedStatusAfterSpeedCheck);
336 DirectionalDragArea *edgeDragArea =
337 view->rootObject()->findChild<DirectionalDragArea*>("hpDragArea");
338 QVERIFY(edgeDragArea != 0);
339 edgeDragArea->setRecognitionTimer(fakeTimer);
340 edgeDragArea->setAxisVelocityCalculator(velCalcWithFakeTiming);
342 QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, view);
343 QPointF touchPoint = initialTouchPos;
345 QPointF dragDirectionVector(1.0, 0.0);
346 qreal distanceStep = edgeDragArea->distanceThreshold() * 0.1f;
347 QPointF touchMovement = dragDirectionVector * distanceStep;
348 qreal minSpeedMsecs = edgeDragArea->minSpeed() / 1000.0;
349 qint64 timeStepMsecs = qFloor(distanceStep / minSpeedMsecs) + minSpeedMsecsDeviation;
351 // if it fails it means the params set in the QML file are not in harmony with the values
353 Q_ASSERT(timeStepMsecs > 0);
355 fakeTimeSource->m_msecsSinceReference = 0;
356 QTest::touchEvent(view, device).press(0, touchPoint.toPoint());
358 // Move a bit in the proper direction
359 for (int i=0; i < 4; ++i) {
360 touchPoint += touchMovement;
361 fakeTimeSource->m_msecsSinceReference += timeStepMsecs;
362 QTest::touchEvent(view, device).move(0, touchPoint.toPoint());
365 QCOMPARE((int)edgeDragArea->status(), (int)DirectionalDragArea::Undecided);
367 // Force the periodic speed check.
368 fakeTimer->emitTimeout();
370 QCOMPARE((int)edgeDragArea->status(), expectedStatusAfterSpeedCheck);
373void tst_DirectionalDragArea::minSpeed_data()
375 QTest::addColumn<int>("minSpeedMsecsDeviation");
376 QTest::addColumn<int>("expectedStatusAfterSpeedCheck");
378 QTest::newRow("slower than minSpeed") << 20 << (int)DirectionalDragArea::Rejected;
379 QTest::newRow("faster than minSpeed") << -20 << (int)DirectionalDragArea::Undecided;
383 Checks that the recognition timer is started and stopped appropriately.
384 I.e., check that it's running only while gesture recognition is taking place
385 (status == Undecided)
387void tst_DirectionalDragArea::recognitionTimerUsage()
389 DirectionalDragArea *edgeDragArea =
390 view->rootObject()->findChild<DirectionalDragArea*>("hpDragArea");
391 QVERIFY(edgeDragArea != 0);
392 edgeDragArea->setRecognitionTimer(fakeTimer);
393 edgeDragArea->setAxisVelocityCalculator(velCalcWithFakeTiming);
395 QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, view);
396 QPointF touchPoint = initialTouchPos;
398 QPointF dragDirectionVector(1.0, 0.0);
399 QPointF touchMovement = dragDirectionVector * (edgeDragArea->distanceThreshold() * 0.2f);
401 QVERIFY(!fakeTimer->isRunning());
403 QTest::touchEvent(view, device).press(0, touchPoint.toPoint());
405 QVERIFY(fakeTimer->isRunning());
407 // Move a bit in the proper direction
408 for (int i=0; i < 3; ++i) {
409 touchPoint += touchMovement;
410 QTest::touchEvent(view, device).move(0, touchPoint.toPoint());
413 QVERIFY(fakeTimer->isRunning());
415 // Move beyond distance threshold
416 touchPoint += 3*touchMovement;
417 QTest::touchEvent(view, device).move(0, touchPoint.toPoint());
419 QCOMPARE((int)edgeDragArea->status(), (int)DirectionalDragArea::Recognized);
421 QVERIFY(!fakeTimer->isRunning());
425 A gesture should be rejected if too much time has passed without any new input
428void tst_DirectionalDragArea::maxSilenceTime()
430 DirectionalDragArea *edgeDragArea =
431 view->rootObject()->findChild<DirectionalDragArea*>("hpDragArea");
432 QVERIFY(edgeDragArea != 0);
433 edgeDragArea->setRecognitionTimer(fakeTimer);
434 edgeDragArea->setAxisVelocityCalculator(velCalcWithFakeTiming);
436 QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, view);
437 QPointF touchPoint = initialTouchPos;
439 fakeTimeSource->m_msecsSinceReference = 0;
440 QTest::touchEvent(view, device).press(0, touchPoint.toPoint());
442 QCOMPARE((int)edgeDragArea->status(), (int)DirectionalDragArea::Undecided);
444 int neededTimeouts = qCeil((qreal)edgeDragArea->maxSilenceTime() / (qreal)fakeTimer->interval());
445 if ((edgeDragArea->maxSilenceTime() % fakeTimer->interval()) == 0) {
449 // Force the periodic speed check.
450 for (int i = 0; i < neededTimeouts; ++i) {
451 fakeTimer->emitTimeout();
453 if (i < neededTimeouts - 1) {
454 QCOMPARE((int)edgeDragArea->status(), (int)DirectionalDragArea::Undecided);
458 QCOMPARE((int)edgeDragArea->status(), (int)DirectionalDragArea::Rejected);
461QTEST_MAIN(tst_DirectionalDragArea)
463#include "tst_DirectionalDragArea.moc"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list