Debian Codegraph @

tests/unit/swipearea13/GestureTest.cpp

raw
1 /*
2 * Copyright 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 Lesser 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 Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include "GestureTest.h"
18
19 #include <QtGui/qpa/qwindowsysteminterface.h>
20 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
21 #include <QtGui/QTouchDevice>
22 #else
23 #include <QtGui/QPointingDevice>
24 #endif
25 #include <QtQml/QQmlEngine>
26 #include <QtQuick/QQuickView>
27 #include <QtTest/QtTest>
28 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
29 #include <LomiriGestures-Qt6/private/timer_p.h>
30 #else
31 #include <LomiriGestures/private/timer_p.h>
32 #endif
33 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
34 #include <LomiriGestures-Qt6/private/touchregistry_p.h>
35 #else
36 #include <LomiriGestures/private/touchregistry_p.h>
37 #endif
38
39 #include "uctestcase.h"
40 #include "uctestextras.h"
41
42 UG_USE_NAMESPACE
43
44 GestureTest::GestureTest(const QString &qmlFilename)
45 : QObject(), m_device(nullptr), m_view(nullptr), m_qmlFilename(qmlFilename)
46 {
47 }
48
49 void GestureTest::initTestCase()
50 {
51 if (!m_device) {
52 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
53 m_device = new QTouchDevice;
54 m_device->setType(QTouchDevice::TouchScreen);
55 QWindowSystemInterface::registerTouchDevice(m_device);
56 #else
57 m_device = new QPointingDevice(
58 "TestTouchDevice",
59 1,
60 QInputDevice::DeviceType::TouchScreen,
61 QPointingDevice::PointerType::Finger,
62 QInputDevice::Capability::Position,
63 1,
64 0
65 );
66 QWindowSystemInterface::registerInputDevice(m_device);
67 #endif
68 }
69 }
70
71 void GestureTest::init()
72 {
73 m_view = new LomiriTestCase(m_qmlFilename, QQuickView::SizeRootObjectToView, true);
74
75 m_fakeTimerFactory = new FakeTimerFactory;
76
77 m_touchRegistry = TouchRegistry::instance();
78 m_touchRegistry->setTimerFactory(m_fakeTimerFactory);
79 m_view->installEventFilter(m_touchRegistry);
80
81 qApp->processEvents();
82 }
83
84 void GestureTest::cleanup()
85 {
86 m_view->removeEventFilter(m_touchRegistry);
87 delete m_touchRegistry;
88 m_touchRegistry = nullptr;
89
90 // TouchRegistry will take down the timer factory along with him
91 // delete m_fakeTimerFactory;
92 m_fakeTimerFactory = nullptr;
93
94 delete m_view;
95 m_view = nullptr;
96
97 // add a small timeout after each run to have a proper cleanup
98 QTest::qWait(400);
99 }
100
101 ////////////////////////// TouchMemento /////////////////////////////
102
103 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
104 TouchMemento::TouchMemento(const QTouchEvent *touchEvent)
105 : touchPointStates(touchEvent->touchPointStates()), touchPoints(touchEvent->touchPoints())
106 {
107 }
108 #else
109 TouchMemento::TouchMemento(const QTouchEvent *touchEvent)
110 : touchPointStates(touchEvent->touchPointStates()), touchPoints(touchEvent->points())
111 {
112 }
113 #endif
114
115 bool TouchMemento::containsTouchWithId(int touchId) const
116 {
117 for (int i = 0; i < touchPoints.count(); ++i) {
118 if (touchPoints.at(i).id() == touchId) {
119 return true;
120 }
121 }
122 return false;
123 }
124
125 ////////////////////////// DummyItem /////////////////////////////
126
127 DummyItem::DummyItem(QQuickItem *parent)
128 : QQuickItem(parent)
129 {
130 touchEventHandler = defaultTouchEventHandler;
131 mousePressEventHandler = defaultMouseEventHandler;
132 mouseMoveEventHandler = defaultMouseEventHandler;
133 mouseReleaseEventHandler = defaultMouseEventHandler;
134 mouseDoubleClickEventHandler = defaultMouseEventHandler;
135 }
136
137 void DummyItem::touchEvent(QTouchEvent *event)
138 {
139 touchEvents.append(TouchMemento(event));
140 touchEventHandler(event);
141 }
142
143 void DummyItem::mousePressEvent(QMouseEvent *event)
144 {
145 mousePressEventHandler(event);
146 }
147
148 void DummyItem::mouseMoveEvent(QMouseEvent *event)
149 {
150 mouseMoveEventHandler(event);
151 }
152
153 void DummyItem::mouseReleaseEvent(QMouseEvent *event)
154 {
155 mouseReleaseEventHandler(event);
156 }
157
158 void DummyItem::mouseDoubleClickEvent(QMouseEvent *event)
159 {
160 mouseDoubleClickEventHandler(event);
161 }
162
163 void DummyItem::defaultTouchEventHandler(QTouchEvent *event)
164 {
165 event->accept();
166 }
167
168 void DummyItem::defaultMouseEventHandler(QMouseEvent *event)
169 {
170 event->accept();
171 }