on b210fee help sign in
unity8/tests/plugins/Ubuntu/Gestures/tst_TouchDispatcher.cpp
148 lines 4.8 kB history raw
1/*
2 * Copyright (C) 2014-2015 Canonical, Ltd.
3 *
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.
7 *
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.
12 *
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/>.
15 */
17#include <QtTest>
18#include <QQuickView>
19#include <QStyleHints>
21#include "GestureTest.h"
23#include <TouchDispatcher.h>
25class tst_TouchDispatcher : public GestureTest
27 Q_OBJECT
28public:
29 tst_TouchDispatcher();
30private Q_SLOTS:
31 void sendMouseEventIfTouchIgnored_data();
32 void sendMouseEventIfTouchIgnored();
33 void mouseDoubleClick_data();
34 void mouseDoubleClick();
35};
37tst_TouchDispatcher::tst_TouchDispatcher()
38 : GestureTest(QStringLiteral("empty.qml"))
42void tst_TouchDispatcher::sendMouseEventIfTouchIgnored_data()
44 QTest::addColumn<bool>("itemAcceptsTouch");
45 QTest::addColumn<int>("acceptedMouseButtons");
46 QTest::addColumn<bool>("shouldGetMousePress");
48 QTest::newRow("accepts touch and doesn't accept mouse buttons") << true << (int)Qt::NoButton << false;
49 QTest::newRow("ignores touch and accepts mouse buttons") << false << (int)Qt::LeftButton << true;
50 QTest::newRow("ignores touch and doesn't accept mouse buttons") << false << (int)Qt::NoButton << false;
53void tst_TouchDispatcher::sendMouseEventIfTouchIgnored()
55 QFETCH(bool, itemAcceptsTouch);
56 QFETCH(int, acceptedMouseButtons);
57 QFETCH(bool, shouldGetMousePress);
58 DummyItem *dummyItem = new DummyItem(m_view->rootObject());
60 TouchDispatcher touchDispatcher;
61 touchDispatcher.setTargetItem(dummyItem);
63 QList<QTouchEvent::TouchPoint> touchPoints;
64 QTouchEvent::TouchPoint touchPoint;
65 touchPoint.setId(0);
66 touchPoint.setState(Qt::TouchPointPressed);
67 touchPoints.append(touchPoint);
69 bool gotTouchEvent = false;
70 bool gotMousePressEvent = false;
72 dummyItem->setAcceptedMouseButtons((Qt::MouseButtons)acceptedMouseButtons);
74 dummyItem->touchEventHandler = [&](QTouchEvent *event) {
75 gotTouchEvent = true;
76 event->setAccepted(itemAcceptsTouch);
77 };
79 dummyItem->mousePressEventHandler = [&](QMouseEvent *event) {
80 gotMousePressEvent = true;
81 event->accept();
82 };
84 touchDispatcher.dispatch(m_device, Qt::NoModifier, touchPoints, m_view, 12345 /* timestamp */);
86 QVERIFY(gotTouchEvent);
87 QCOMPARE(gotMousePressEvent, shouldGetMousePress);
90void tst_TouchDispatcher::mouseDoubleClick_data()
92 QTest::addColumn<ulong>("timeBetweenClicks");
93 QTest::addColumn<bool>("shouldSendDoubleClick");
95 QTest::newRow("double click") << static_cast<ulong>(qApp->styleHints()->mouseDoubleClickInterval() / 10) << true;
96 QTest::newRow("two separate clicks") << static_cast<ulong>(qApp->styleHints()->mouseDoubleClickInterval() * 2) << false;
99void tst_TouchDispatcher::mouseDoubleClick()
101 QFETCH(ulong, timeBetweenClicks);
102 QFETCH(bool, shouldSendDoubleClick);
103 DummyItem *dummyItem = new DummyItem(m_view->rootObject());
104 dummyItem->setAcceptedMouseButtons(Qt::LeftButton);
106 TouchDispatcher touchDispatcher;
107 touchDispatcher.setTargetItem(dummyItem);
109 bool gotDoubleClickEvent = false;
110 dummyItem->mouseDoubleClickEventHandler = [&](QMouseEvent *event) {
111 gotDoubleClickEvent = true;
112 event->accept();
113 };
115 dummyItem->touchEventHandler = [&](QTouchEvent *event) {
116 event->ignore();
117 };
119 ulong doubleClickInterval = static_cast<ulong>(qApp->styleHints()->mouseDoubleClickInterval());
121 QList<QTouchEvent::TouchPoint> touchPoints;
122 {
123 QTouchEvent::TouchPoint touchPoint;
124 touchPoint.setId(0);
125 touchPoint.setState(Qt::TouchPointPressed);
126 touchPoints.append(touchPoint);
127 }
128 ulong timestamp = 12345;
130 touchDispatcher.dispatch(m_device, Qt::NoModifier, touchPoints, m_view, timestamp);
132 touchPoints[0].setState(Qt::TouchPointReleased);
133 timestamp += doubleClickInterval / 10;
135 touchDispatcher.dispatch(m_device, Qt::NoModifier, touchPoints, m_view, timestamp);
137 touchPoints[0].setId(1);
138 touchPoints[0].setState(Qt::TouchPointPressed);
139 timestamp += timeBetweenClicks;
141 touchDispatcher.dispatch(m_device, Qt::NoModifier, touchPoints, m_view, timestamp);
143 QCOMPARE(gotDoubleClickEvent, shouldSendDoubleClick);
146QTEST_MAIN(tst_TouchDispatcher)
148#include "tst_TouchDispatcher.moc"