on ccd07fa help sign in
184 lines 5.1 kB history raw
1/*
2 * Copyright (C) 2013,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 "GestureTest.h"
19#include <qpa/qwindowsysteminterface.h>
20#include <private/qquickwindow_p.h>
21#include <QQmlEngine>
22#include <QQuickView>
23#include <QtTest>
25#include <UbuntuGestures/private/timer_p.h>
26#include <UbuntuGestures/private/touchregistry_p.h>
27#include <paths.h>
29UG_USE_NAMESPACE
31GestureTest::GestureTest(const QString &qmlFilename)
32 : QObject(), m_device(nullptr), m_view(nullptr), m_qmlFilename(qmlFilename)
36void GestureTest::initTestCase()
38 if (!m_device) {
39 m_device = new QTouchDevice;
40 m_device->setType(QTouchDevice::TouchScreen);
41 QWindowSystemInterface::registerTouchDevice(m_device);
42 }
45void GestureTest::init()
47 m_view = new QQuickView;
48 m_view->setResizeMode(QQuickView::SizeRootObjectToView);
49 m_view->setSource(QUrl::fromLocalFile(testDataDir() + "/plugins/Ubuntu/Gestures/" + m_qmlFilename));
50 m_view->show();
51 QVERIFY(QTest::qWaitForWindowExposed(m_view));
52 QVERIFY(m_view->rootObject() != 0);
54 m_fakeTimerFactory = new FakeTimerFactory;
56 m_touchRegistry = TouchRegistry::instance();
57 m_touchRegistry->setTimerFactory(m_fakeTimerFactory);
58 m_view->installEventFilter(m_touchRegistry);
60 qApp->processEvents();
63void GestureTest::cleanup()
65 m_view->removeEventFilter(m_touchRegistry);
66 delete m_touchRegistry;
67 m_touchRegistry = nullptr;
69 // TouchRegistry will take down the timer factory along with him
70 // delete m_fakeTimerFactory;
71 m_fakeTimerFactory = nullptr;
73 delete m_view;
74 m_view = nullptr;
77void GestureTest::sendTouchPress(qint64 timestamp, int id, QPointF pos)
79 sendTouch(timestamp, id, pos, Qt::TouchPointPressed, QEvent::TouchBegin);
82void GestureTest::sendTouchUpdate(qint64 timestamp, int id, QPointF pos)
84 sendTouch(timestamp, id, pos, Qt::TouchPointMoved, QEvent::TouchUpdate);
87void GestureTest::sendTouchRelease(qint64 timestamp, int id, QPointF pos)
89 sendTouch(timestamp, id, pos, Qt::TouchPointReleased, QEvent::TouchEnd);
92void GestureTest::sendTouch(qint64 timestamp, int id, QPointF pos,
93 Qt::TouchPointState pointState, QEvent::Type eventType)
95 m_fakeTimerFactory->updateTime(timestamp);
97 QTouchEvent::TouchPoint point;
99 point.setState(pointState);
100 point.setId(id);
101 point.setScenePos(pos);
102 point.setPos(pos);
104 QList<QTouchEvent::TouchPoint> points;
105 points << point;
107 QTouchEvent touchEvent(eventType, m_device, Qt::NoModifier, Qt::TouchPointPressed, points);
108 QCoreApplication::sendEvent(m_view, &touchEvent);
110 QQuickWindowPrivate *windowPrivate = QQuickWindowPrivate::get(m_view);
111 windowPrivate->flushDelayedTouchEvent();
114void GestureTest::passTime(qint64 timeSpanMs)
116 qint64 finalTime = m_fakeTimerFactory->timeSource()->msecsSinceReference() + timeSpanMs;
117 m_fakeTimerFactory->updateTime(finalTime);
120////////////////////////// TouchMemento /////////////////////////////
122TouchMemento::TouchMemento(const QTouchEvent *touchEvent)
123 : touchPointStates(touchEvent->touchPointStates()), touchPoints(touchEvent->touchPoints())
128bool TouchMemento::containsTouchWithId(int touchId) const
130 for (int i = 0; i < touchPoints.count(); ++i) {
131 if (touchPoints.at(i).id() == touchId) {
132 return true;
133 }
134 }
135 return false;
138////////////////////////// DummyItem /////////////////////////////
140DummyItem::DummyItem(QQuickItem *parent)
141 : QQuickItem(parent)
143 touchEventHandler = defaultTouchEventHandler;
144 mousePressEventHandler = defaultMouseEventHandler;
145 mouseMoveEventHandler = defaultMouseEventHandler;
146 mouseReleaseEventHandler = defaultMouseEventHandler;
147 mouseDoubleClickEventHandler = defaultMouseEventHandler;
150void DummyItem::touchEvent(QTouchEvent *event)
152 touchEvents.append(TouchMemento(event));
153 touchEventHandler(event);
156void DummyItem::mousePressEvent(QMouseEvent *event)
158 mousePressEventHandler(event);
161void DummyItem::mouseMoveEvent(QMouseEvent *event)
163 mouseMoveEventHandler(event);
166void DummyItem::mouseReleaseEvent(QMouseEvent *event)
168 mouseReleaseEventHandler(event);
171void DummyItem::mouseDoubleClickEvent(QMouseEvent *event)
173 mouseDoubleClickEventHandler(event);
176void DummyItem::defaultTouchEventHandler(QTouchEvent *event)
178 event->accept();
181void DummyItem::defaultMouseEventHandler(QMouseEvent *event)
183 event->accept();