Debian Codegraph @

src/imports/Test/plugin/uctestcase.h

raw
1 /*
2 * Copyright 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 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 * Author: Christian Dywan <[email protected]>
17 */
18
19 #ifndef UCTESTCASE_H
20 #define UCTESTCASE_H
21
22 #include <QtQuick/QQuickItem>
23 #include <QtQuick/QQuickView>
24 #include <QtTest/QtTest>
25 #include <QtTest/QSignalSpy>
26
27 class LomiriTestCase : public QQuickView
28 {
29 Q_OBJECT
30 public:
31 LomiriTestCase(const QString& file, ResizeMode resize = SizeViewToRootObject, bool assertOnFailure = true, QWindow* parent = 0);
32 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
33 ~LomiriTestCase();
34 #endif
35 int warnings() const;
36 // getter
37 template<class T>
38 inline T findItem(const QString& objectName) const {
39 T item = rootObject()->findChild<T>(objectName);
40 if (item)
41 return item;
42 if (rootObject()->findChild<QObject*>(objectName))
43 qFatal("Item '%s' found with unexpected type", qPrintable(objectName));
44 qFatal("No item '%s' found", qPrintable(objectName));
45 }
46
47 static inline QPointF centerOf(QQuickItem *item, bool windowPos = false)
48 {
49 QPointF center;
50 if (item) {
51 center = item->boundingRect().center();
52 if (windowPos) {
53 center = item->mapToScene(center);
54 }
55 }
56 return center;
57 }
58
59 static void ignoreWarning(const QString& fileName, uint line, uint column, const QString& message, uint occurences=1);
60 static void ignoreWarning(const QString& fileName, uint line, const QString& message, uint occurences=1);
61
62 static inline void waitForSignal(QSignalSpy *spy, int timeout = 5000)
63 {
64 QVERIFY(spy);
65 if (!spy->count()) {
66 QVERIFY(spy->wait(timeout));
67 } else {
68 QCOMPARE(spy->count(), 1);
69 }
70 }
71
72 private:
73 QSignalSpy* m_spy;
74 };
75
76 #endif // UCTESTCASE_H