on dfb5fab help sign in
105 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>
23#include <private/qquickbehavior_p.h>
24#include <private/qquickanimation_p.h>
26// UbuntuGestures lib
27#include <TouchRegistry>
28#include <Timer>
30using namespace UbuntuGestures;
32TestUtil::TestUtil(QObject *parent)
33 : QObject(parent)
34 , m_targetWindow(0)
35 , m_touchDevice(0)
36 , m_putFakeTimerFactoryInTouchRegistry(false)
40TestUtil::~TestUtil()
44bool
45TestUtil::isInstanceOf(QObject *obj, QString name)
47 if (!obj) return false;
48 bool result = obj->inherits(name.toUtf8());
49 if (!result) {
50 const QMetaObject *metaObject = obj->metaObject();
51 while (!result && metaObject) {
52 const QString className = metaObject->className();
53 const QString qmlName = className.left(className.indexOf("_QMLTYPE_"));
54 result = qmlName == name;
55 metaObject = metaObject->superClass();
56 }
57 }
58 return result;
61void
62TestUtil::waitForBehaviors(QObject *obj)
64 if (!obj) return;
66 Q_FOREACH(auto c, obj->children()) {
67 if (auto *b = dynamic_cast<QQuickBehavior*>(c)) {
68 if (b->animation()) {
69 QTRY_COMPARE(b->animation()->isRunning(), false);
70 }
71 }
72 waitForBehaviors(c);
73 }
76TouchEventSequenceWrapper *TestUtil::touchEvent(QQuickItem *item)
78 ensureTargetWindow();
79 ensureTouchDevice();
81 // Tests can be *very* slow to run and we don't want things timing out because
82 // of that. So give it fake timers to use (they will never time out)
83 if (!m_putFakeTimerFactoryInTouchRegistry) {
84 TouchRegistry::instance()->setTimerFactory(new FakeTimerFactory);
85 m_putFakeTimerFactoryInTouchRegistry = true;
86 }
88 return new TouchEventSequenceWrapper(
89 QTest::touchEvent(m_targetWindow, m_touchDevice, /* autoCommit */ false), item);
92void TestUtil::ensureTargetWindow()
94 if (!m_targetWindow && !QGuiApplication::topLevelWindows().isEmpty())
95 m_targetWindow = QGuiApplication::topLevelWindows()[0];
98void TestUtil::ensureTouchDevice()
100 if (!m_touchDevice) {
101 m_touchDevice = new QTouchDevice;
102 m_touchDevice->setType(QTouchDevice::TouchScreen);
103 QWindowSystemInterface::registerTouchDevice(m_touchDevice);
104 }