Unity 8
testutil.cpp
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  */
16 
17 
18 #include "testutil.h"
19 
20 #include <qpa/qwindowsysteminterface.h>
21 #include <QtGui/QGuiApplication>
22 #include <QQuickView>
23 #include <private/qquickbehavior_p.h>
24 #include <private/qquickanimation_p.h>
25 
26 // UbuntuGestures lib
27 #include <TouchRegistry>
28 #include <Timer>
29 
30 using namespace UbuntuGestures;
31 
32 TestUtil::TestUtil(QObject *parent)
33  : QObject(parent)
34  , m_targetWindow(0)
35  , m_touchDevice(0)
36  , m_putFakeTimerFactoryInTouchRegistry(false)
37 {
38 }
39 
40 TestUtil::~TestUtil()
41 {
42 }
43 
44 bool
45 TestUtil::isInstanceOf(QObject *obj, QString name)
46 {
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;
59 }
60 
61 void
62 TestUtil::waitForBehaviors(QObject *obj)
63 {
64  if (!obj) return;
65 
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  }
74 }
75 
76 TouchEventSequenceWrapper *TestUtil::touchEvent(QQuickItem *item)
77 {
78  ensureTargetWindow();
79  ensureTouchDevice();
80 
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  }
87 
88  return new TouchEventSequenceWrapper(
89  QTest::touchEvent(m_targetWindow, m_touchDevice, /* autoCommit */ false), item);
90 }
91 
92 void TestUtil::ensureTargetWindow()
93 {
94  if (!m_targetWindow && !QGuiApplication::topLevelWindows().isEmpty())
95  m_targetWindow = QGuiApplication::topLevelWindows()[0];
96 }
97 
98 void TestUtil::ensureTouchDevice()
99 {
100  if (!m_touchDevice) {
101  m_touchDevice = new QTouchDevice;
102  m_touchDevice->setType(QTouchDevice::TouchScreen);
103  QWindowSystemInterface::registerTouchDevice(m_touchDevice);
104  }
105 }