on 2fe5ed7 help sign in
99 lines 3.0 kB history raw
1/*
2 * Copyright (C) 2012, 2013, 2014 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 */
18#include "testutil.h"
20#include <qpa/qwindowsysteminterface.h>
21#include <QtGui/QGuiApplication>
22#include <QQuickView>
24// UbuntuGestures lib
25#include <TouchRegistry>
26#include <Timer>
28using namespace UbuntuGestures;
30TestUtil::TestUtil(QObject *parent)
31 : QObject(parent)
32 , m_targetWindow(0)
33 , m_touchDevice(0)
34 , m_putFakeTimerFactoryInTouchRegistry(false)
38TestUtil::~TestUtil()
42bool
43TestUtil::isInstanceOf(QObject *obj, QString name)
45 if (!obj) return false;
46 bool result = obj->inherits(name.toUtf8());
47 if (!result) {
48 const QMetaObject *metaObject = obj->metaObject();
49 while (!result && metaObject) {
50 const QString className = metaObject->className();
51 const QString qmlName = className.left(className.indexOf("_QMLTYPE_"));
52 result = qmlName == name;
53 metaObject = metaObject->superClass();
54 }
55 }
56 return result;
59TouchEventSequenceWrapper *TestUtil::touchEvent(QQuickItem *item)
61 ensureTargetWindow();
62 ensureTouchDevice();
64 // Tests can be *very* slow to run and we don't want things timing out because
65 // of that. So give it fake timers to use (they will never time out)
66 if (!m_putFakeTimerFactoryInTouchRegistry) {
67 TouchRegistry::instance()->setTimerFactory(new FakeTimerFactory);
68 m_putFakeTimerFactoryInTouchRegistry = true;
69 }
71 return new TouchEventSequenceWrapper(
72 QTest::touchEvent(m_targetWindow, m_touchDevice, /* autoCommit */ false), item);
75void TestUtil::removeTimeConstraintsFromSwipeArea(QObject *object)
77 auto children = object->findChildren<QObject*>();
78 Q_FOREACH(QObject *child, children) {
79 if (child->metaObject()->className() == QByteArray("UCSwipeAreaPrivate")) {
80 QMetaObject::invokeMethod(child, "setMaxTime", Q_ARG(int, 60 * 60 * 1000));
81 QMetaObject::invokeMethod(child, "setCompositionTime", Q_ARG(int, 0));
82 }
83 }
86void TestUtil::ensureTargetWindow()
88 if (!m_targetWindow && !QGuiApplication::topLevelWindows().isEmpty())
89 m_targetWindow = QGuiApplication::topLevelWindows()[0];
92void TestUtil::ensureTouchDevice()
94 if (!m_touchDevice) {
95 m_touchDevice = new QTouchDevice;
96 m_touchDevice->setType(QTouchDevice::TouchScreen);
97 QWindowSystemInterface::registerTouchDevice(m_touchDevice);
98 }