1 | // Copyright (C) 2020 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QUICKTEST_P_H |
5 | #define QUICKTEST_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtQuickTest/private/quicktestglobal_p.h> |
19 | #include <QtQuickTest/quicktest.h> |
20 | |
21 | #include <QtQml/qqmlpropertymap.h> |
22 | #include <QtQml/qqml.h> |
23 | |
24 | #include <QtCore/qpointer.h> |
25 | |
26 | QT_BEGIN_NAMESPACE |
27 | |
28 | class Q_QMLTEST_EXPORT QTestRootObject : public QObject |
29 | { |
30 | Q_OBJECT |
31 | Q_PROPERTY(bool windowShown READ windowShown NOTIFY windowShownChanged) |
32 | Q_PROPERTY(bool hasTestCase READ hasTestCase WRITE setHasTestCase NOTIFY hasTestCaseChanged) |
33 | Q_PROPERTY(QObject *defined READ defined) |
34 | |
35 | QML_SINGLETON |
36 | QML_ELEMENT |
37 | QML_ADDED_IN_VERSION(1, 0) |
38 | |
39 | public: |
40 | static QTestRootObject *create(QQmlEngine *q, QJSEngine *) |
41 | { |
42 | QTestRootObject *result = instance(); |
43 | QQmlEngine *engine = qmlEngine(result); |
44 | // You can only test on one engine at a time |
45 | return (engine == nullptr || engine == q) ? result : nullptr; |
46 | } |
47 | |
48 | static QTestRootObject *instance() { |
49 | static QPointer<QTestRootObject> object = new QTestRootObject; |
50 | if (!object) { |
51 | // QTestRootObject was deleted when previous test ended, create a new one |
52 | object = new QTestRootObject; |
53 | } |
54 | return object; |
55 | } |
56 | |
57 | bool hasQuit:1; |
58 | bool hasTestCase() const { return m_hasTestCase; } |
59 | void setHasTestCase(bool value) { m_hasTestCase = value; Q_EMIT hasTestCaseChanged(); } |
60 | |
61 | bool windowShown() const { return m_windowShown; } |
62 | void setWindowShown(bool value) { m_windowShown = value; Q_EMIT windowShownChanged(); } |
63 | QQmlPropertyMap *defined() const { return m_defined; } |
64 | |
65 | void init() { setWindowShown(false); setHasTestCase(false); hasQuit = false; } |
66 | |
67 | Q_SIGNALS: |
68 | void windowShownChanged(); |
69 | void hasTestCaseChanged(); |
70 | |
71 | private Q_SLOTS: |
72 | void quit() { hasQuit = true; } |
73 | |
74 | private: |
75 | QTestRootObject(QObject *parent = nullptr) |
76 | : QObject(parent), hasQuit(false), m_windowShown(false), m_hasTestCase(false) { |
77 | m_defined = new QQmlPropertyMap(this); |
78 | #if defined(QT_OPENGL_ES_2_ANGLE) |
79 | m_defined->insert(QLatin1String("QT_OPENGL_ES_2_ANGLE" ), QVariant(true)); |
80 | #endif |
81 | } |
82 | |
83 | bool m_windowShown : 1; |
84 | bool m_hasTestCase :1; |
85 | QQmlPropertyMap *m_defined; |
86 | }; |
87 | |
88 | bool qWaitForSignal(QObject *obj, const char* signal, int timeout = 5000); |
89 | |
90 | QT_END_NAMESPACE |
91 | |
92 | #endif // QUICKTEST_P_H |
93 | |