Debian Codegraph @

src/imports/Test/plugin/uctestextras.cpp

raw
1 /*
2 * Copyright 2016 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 "uctestextras.h"
18
19 #include <QtCore/QSysInfo>
20 #include <QtCore/private/qobject_p.h>
21 #include <QtGui/QOpenGLContext>
22 #include <QtGui/qpa/qwindowsysteminterface.h>
23 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
24 #include <LomiriToolkit-Qt6/private/mousetouchadaptor_p.h>
25 #else
26 #include <LomiriToolkit/private/mousetouchadaptor_p.h>
27 #endif
28 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
29 #include <LomiriGestures-Qt6/private/ucswipearea_p_p.h>
30 #else
31 #include <LomiriGestures/private/ucswipearea_p_p.h>
32 #endif
33
34 #include "uctestcase.h"
35
36 UT_USE_NAMESPACE
37
38 const char *DEVICE_MISSING_MSG = "No touch device registered. Register one using registerTouchDevice() before using %1";
39
40 #define CHECK_TOUCH_DEVICE(touchId, item) \
41 if (!touchDevicePresent()) { \
42 qWarning() << QString(DEVICE_MISSING_MSG).arg(__FUNCTION__); \
43 return; \
44 } \
45 if (touchId < 0) { \
46 qWarning() << "Invalid touchId specified."; \
47 return; \
48 } \
49 if (!item) { \
50 qWarning() << "Invalid item specified."; \
51 return; \
52 }
53
54 UCTestExtras *UCTestExtras::m_testExtras = 0;
55
56 /*!
57 * \qmltype TestExtras
58 * \instantiates UCTestExtras
59 * \inqmlmodule Lomiri.Test 1.0
60 * \ingroup lomiri-test
61 * \brief Singleton type providing additional test functions.
62 *
63 * The component provides additional test functions like touch handling, registering
64 * touch device on non-touch screen enabled environment.
65 */
66
67 UCTestExtras::UCTestExtras(QObject *parent) :
68 QObject(parent)
69 {
70 m_testExtras = this;
71 }
72
73 /*!
74 * \qmlmethod TestExtras::openGLflavor()
75 * Returns "opengl" or "opengles2".
76 */
77 QString UCTestExtras::openGLflavor()
78 {
79 if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGLES)
80 return QStringLiteral("opengles2");
81 else
82 return QStringLiteral("opengl");
83
84 }
85
86 /*!
87 * \qmlmethod TestExtras::cpuArchitecture()
88 * Returns the build architecure, including but not limited to
89 * "arm", "arm64", "i386" and "x86_64".
90 */
91 QString UCTestExtras::cpuArchitecture()
92 {
93 #if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
94 return QSysInfo::buildCpuArchitecture();
95 #elif defined(Q_PROCESSOR_X86_64)
96 return "x86_64";
97 #elif defined(Q_PROCESSOR_X86_32)
98 return "i386";
99 #elif defined(Q_PROCESSOR_ARM_64)
100 return "arm64";
101 #elif defined(Q_PROCESSOR_ARM)
102 return "arm";
103 #else
104 return "unknown";
105 #endif
106 }
107
108 /*!
109 * \qmlmethod TestExtras::touchDevicePresent()
110 * Returns true if the system has a touch device registered.
111 */
112 bool UCTestExtras::touchDevicePresent()
113 {
114 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
115 QList<const QInputDevice*> touchDevices = QInputDevice::devices();
116 Q_FOREACH(const QInputDevice *device, touchDevices) {
117 if (device->type() == QInputDevice::DeviceType::TouchScreen) {
118 #else
119 QList<const QTouchDevice*> touchDevices = QTouchDevice::devices();
120 Q_FOREACH(const QTouchDevice *device, touchDevices) {
121 if (device->type() == QTouchDevice::TouchScreen) {
122 #endif
123 return true;
124 }
125 }
126 return false;
127 }
128
129 /*!
130 * \qmlmethod TestExtras::registerTouchDevice()
131 * Registers a touch device if there's none registered. Calling the function in
132 * touch enabled environment has no effect. The function must be called in initTestCase()
133 * in order to perform touch related tests.
134 */
135 void UCTestExtras::registerTouchDevice()
136 {
137 if (MouseTouchAdaptor::registerTouchDevice() && m_testExtras) {
138 Q_EMIT m_testExtras->touchDevicePresentChanged();
139 }
140 }
141
142 /*!
143 * \qmlmethod TestExtras::touchPress(touchId, item, point)
144 * The function triggers a touch press event for a given \a touchId on a specific
145 * \a item. The \a point contains the (x,y) coordinates of the event in \a item
146 * coordinates.
147 */
148 void UCTestExtras::touchPress(int touchId, QQuickItem *item, const QPoint &point)
149 {
150 CHECK_TOUCH_DEVICE(touchId, item);
151 QTest::touchEvent(item->window(), MouseTouchAdaptor::touchDevice()).press(touchId, item->mapToScene(point).toPoint(), item->window());
152 }
153 /*!
154 * \qmlmethod TestExtras::touchRelease(touchId, item, point)
155 * The function produces a touch release event on a given \a touchId performed on
156 * \a item at a \a point.
157 */
158 void UCTestExtras::touchRelease(int touchId, QQuickItem *item, const QPoint &point)
159 {
160 CHECK_TOUCH_DEVICE(touchId, item);
161 QTest::touchEvent(item->window(), MouseTouchAdaptor::touchDevice()).release(touchId, item->mapToScene(point).toPoint(), item->window());
162 }
163 /*!
164 * \qmlmethod TestExtras::touchClick(touchId, item, point)
165 * The function performs a pair of \l touchPress and \l touchRelease calls on the same
166 * point resulting in a click like event.
167 */
168 void UCTestExtras::touchClick(int touchId, QQuickItem *item, const QPoint &point)
169 {
170 CHECK_TOUCH_DEVICE(touchId, item);
171 touchPress(touchId, item, point);
172 QTest::qWait(100);
173 touchRelease(touchId, item, point);
174 }
175 /*!
176 * \qmlmethod TestExtras::touchLongPress(touchId, item, point)
177 * The function produces a \l touchPress event with a timeout equivalent to the
178 * mouse \c pressAndHold timeout, after which the function returns.
179 */
180 void UCTestExtras::touchLongPress(int touchId, QQuickItem *item, const QPoint &point)
181 {
182 CHECK_TOUCH_DEVICE(touchId, item);
183 touchPress(touchId, item, point);
184 // 800 milliseconds + 200 to let events processed
185 QTest::qWait(1000);
186 }
187 /*!
188 * \qmlmethod TestExtras::touchDoubleClick(touchId, item, point)
189 * The function performs two consecutive \l touchClick events with a slight delay
190 * in between each click event.
191 */
192 void UCTestExtras::touchDoubleClick(int touchId, QQuickItem *item, const QPoint &point)
193 {
194 CHECK_TOUCH_DEVICE(touchId, item);
195 touchClick(touchId, item, point);
196 QTest::qWait(100);
197 touchClick(touchId, item, point);
198 }
199 /*!
200 * \qmlmethod TestExtras::touchMove(touchId, item, point)
201 * The function moves the touch point identified by the \a touchId to the destination
202 * \a point. The point is in \a item coordinates. The touch point identified by the
203 * \a touchId must be pressed before calling this function in order to produce the
204 * desired functionality. The event can be captured in a \c MultiPointTouchArea through
205 * \c updated() signal.
206 */
207 void UCTestExtras::touchMove(int touchId, QQuickItem *item, const QPoint &point)
208 {
209 CHECK_TOUCH_DEVICE(touchId, item);
210 QTest::touchEvent(item->window(), MouseTouchAdaptor::touchDevice()).move(touchId, item->mapToScene(point).toPoint(), item->window());
211 }
212 /*!
213 * \qmlmethod TestExtras::touchDrag(touchId, item, from, delta, steps = 5)
214 * The function performs a drag gesture on a touch point identified by \a touchId
215 * over an \a item from the starting point \a from with a \a delta. The gesture
216 * is realized with a touch press, \a step moves and a release event.
217 *
218 * By default the function uses 5 steps to produce the gesture. This value can be any
219 * positive number, driving the gesture appliance to be faster (less than 5 moves) or
220 * slower (more than 5 moves). If a negative or 0 value is given, the function will
221 * use the default 5 steps to produce the gesture.
222 */
223 void UCTestExtras::touchDrag(int touchId, QQuickItem *item, const QPoint &from, const QPoint &delta, int steps)
224 {
225 CHECK_TOUCH_DEVICE(touchId, item);
226 if (delta.isNull()) {
227 qWarning() << "delta point is invalid";
228 return;
229 }
230 if (steps <= 0) {
231 steps = 5;
232 }
233 touchPress(touchId, item, from);
234 QPoint movePoint(from);
235 qreal stepDx = delta.x() / steps;
236 qreal stepDy = delta.y() / steps;
237 for (int i = 0; i < steps; i++) {
238 QTest::qWait(20);
239 movePoint += QPoint(stepDx, stepDy);
240 touchMove(touchId, item, movePoint);
241 }
242 QTest::qWait(20);
243 touchRelease(touchId, item, from + delta);
244 }
245
246 /*!
247 * \qmlmethod void TestExtras::mouseDrag(touchId, item, from, delta, button, stateKey, steps = 5, delay = 20)
248 * The function performs a drag with a mouse over an \a item from the starting
249 * point \a from with a \a delta. The gesture is realized with a mouse press,
250 * \a step moves and a release event, with a \e delay in between each mouse event.
251 *
252 * By default the function uses 5 steps to produce the drag. This value can be any
253 * positive number, driving the gesture appliance to be faster (less than 5 moves) or
254 * slower (more than 5 moves). If a negative or 0 value is given, the function will
255 * use the default 5 steps to produce the gesture.
256 */
257
258 void UCTestExtras::mouseDrag(QQuickItem *item, const QPoint &from, const QPoint &delta, Qt::MouseButton button, Qt::KeyboardModifiers stateKey, int steps, int delay)
259 {
260 if (delta.isNull()) {
261 qWarning() << "delta point is invalid";
262 return;
263 }
264 if (steps <= 0) {
265 steps = 5;
266 }
267 if (delay < 20) {
268 delay = 20;
269 }
270 QTest::mousePress(item->window(), button, stateKey, from, delay);
271 QPoint movePoint(from);
272 qreal stepDx = delta.x() / steps;
273 qreal stepDy = delta.y() / steps;
274 if (!delta.isNull()) {
275 for (int i = 0; i < steps; i++) {
276 QTest::mouseMove(item->window(), movePoint, delay);
277 movePoint += QPoint(stepDx, stepDy);
278 }
279 }
280 QTest::mouseRelease(item->window(), button, stateKey, movePoint, delay);
281 }
282
283 /*
284 * void TestExtras::touchDragWithPoints(touchId, item, list<point> ponts, delay = 20)
285 * Similar to \l touchDrag function, but here the points must be specified in \e points property.
286 * The first point is expected to be the starting position, after which each point is a relative
287 * point defining the move. A minimum of 5 points are needed to properly produce a drag.
288 * \qml
289 * import Qt.Test 1.0
290 * import Lomiri.Test 1.0
291 * Item {
292 * id: testItem
293 * LomiriTestCase {
294 *
295 * function test_vertical_drag_upwards() {
296 * var points = [];
297 * points.push(centerOf(testItem));
298 * points.push(Qt.point(0, -1));
299 * points.push(Qt.point(0, -3));
300 * points.push(Qt.point(0, -6));
301 * points.push(Qt.point(0, -5));
302 * points.push(Qt.point(0, -2));
303 * TestExtras.touchDragWithPoints(0, testItem, points);
304 * }
305 * }
306 * }
307 * \endqml
308 * The delay must be a minimum of 20 milliseconds.
309 */
310 void UCTestExtras::touchDragWithPoints(int touchId, QQuickItem *item, QList<QPoint> points, int delay)
311 {
312 if (points.size() < 5) {
313 qWarning() << "minimum 5 points are needed.";
314 return;
315 }
316 if (delay < 20) {
317 delay = 20;
318 }
319
320 QPoint movePoint(points[0]);
321 touchPress(touchId, item, movePoint);
322 QTest::qWait(delay);
323 touchMove(touchId, item, movePoint);
324 for (int i = 1; i < points.size(); ++i) {
325 QTest::qWait(delay);
326 movePoint += points[i];
327 touchMove(touchId, item, movePoint);
328 }
329 QTest::qWait(delay);
330 touchRelease(touchId, item, movePoint);
331 }
332
333 /*
334 * void TestExtras::mouseDragWithPoints(item, button, list<point> ponts, stateKey = 0, delay = 20)
335 * Similar to \l mouseDrag function, but here the points must be specified in \e points property.
336 * The first point is expected to be the starting position, after which each point is a relative
337 * point defining the move. A minimum of 5 points are needed to properly produce a drag.
338 * \qml
339 * import Qt.Test 1.0
340 * import Lomiri.Test 1.0
341 * Item {
342 * id: testItem
343 * LomiriTestCase {
344 *
345 * function test_vertical_drag_upwards() {
346 * var points = [];
347 * points.push(centerOf(testItem));
348 * points.push(Qt.point(0, -1));
349 * points.push(Qt.point(0, -3));
350 * points.push(Qt.point(0, -6));
351 * points.push(Qt.point(0, -5));
352 * points.push(Qt.point(0, -2));
353 * TestExtras.mouseDragWithPoints(testItem, Qt.LeftButton, points);
354 * }
355 * }
356 * }
357 * \endqml
358 * The delay must be a minimum of 20 milliseconds.
359 */
360 void UCTestExtras::mouseDragWithPoints(QQuickItem *item, QList<QPoint> points, Qt::MouseButton button, Qt::KeyboardModifiers stateKey, int delay)
361 {
362 if (points.size() < 5) {
363 qWarning() << "minimum 5 points are needed.";
364 return;
365 }
366 if (delay < 20) {
367 delay = 20;
368 }
369 QTest::mousePress(item->window(), button, stateKey, item->mapToScene(points[0]).toPoint(), delay);
370 QPoint movePoint(item->mapToScene(points[0]).toPoint());
371 QTest::mouseMove(item->window(), movePoint, delay);
372 for (int i = 1; i < points.size(); ++i) {
373 movePoint += points[i];
374 QTest::mouseMove(item->window(), movePoint, delay);
375 }
376 QTest::mouseRelease(item->window(), button, stateKey, movePoint, delay);
377 }
378
379
380 void UCTestExtras::removeTimeConstraintsFromSwipeArea(QQuickItem *item)
381 {
382 UG_PREPEND_NAMESPACE(UCSwipeArea) *swipeArea = dynamic_cast<UG_PREPEND_NAMESPACE(UCSwipeArea)*>(item);
383 if (!swipeArea) {
384 qWarning() << item << "is not a SwipeArea";
385 return;
386 }
387
388 UG_PREPEND_NAMESPACE(UCSwipeAreaPrivate) *priv =
389 static_cast<UG_PREPEND_NAMESPACE(UCSwipeAreaPrivate)*>(QObjectPrivate::get(swipeArea));
390 priv->setMaxTime(60 * 60 * 1000);
391 priv->setCompositionTime(0);
392 }