1 | /**************************************************************************** |
---|---|
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #ifndef QQMLTESTUTILS_H |
30 | #define QQMLTESTUTILS_H |
31 | |
32 | #include <QtCore/QDir> |
33 | #include <QtCore/QUrl> |
34 | #include <QtCore/QCoreApplication> |
35 | #include <QtCore/QStringList> |
36 | #include <QtTest/QTest> |
37 | |
38 | QT_FORWARD_DECLARE_CLASS(QQmlComponent) |
39 | QT_FORWARD_DECLARE_CLASS(QQmlEngine) |
40 | |
41 | /* Base class for tests with data that are located in a "data" subfolder. */ |
42 | |
43 | class QQmlDataTest : public QObject |
44 | { |
45 | Q_OBJECT |
46 | public: |
47 | QQmlDataTest(); |
48 | virtual ~QQmlDataTest(); |
49 | |
50 | QString testFile(const QString &fileName) const; |
51 | inline QString testFile(const char *fileName) const |
52 | { return testFile(fileName: QLatin1String(fileName)); } |
53 | inline QUrl testFileUrl(const QString &fileName) const |
54 | { return QUrl::fromLocalFile(localfile: testFile(fileName)); } |
55 | inline QUrl testFileUrl(const char *fileName) const |
56 | { return testFileUrl(fileName: QLatin1String(fileName)); } |
57 | |
58 | inline QString dataDirectory() const { return m_dataDirectory; } |
59 | inline QUrl dataDirectoryUrl() const { return m_dataDirectoryUrl; } |
60 | inline QString directory() const { return m_directory; } |
61 | |
62 | static inline QQmlDataTest *instance() { return m_instance; } |
63 | |
64 | static QByteArray msgComponentError(const QQmlComponent &, |
65 | const QQmlEngine *engine = 0); |
66 | |
67 | public slots: |
68 | virtual void initTestCase(); |
69 | |
70 | private: |
71 | static QQmlDataTest *m_instance; |
72 | |
73 | const QString m_dataDirectory; |
74 | const QUrl m_dataDirectoryUrl; |
75 | QString m_directory; |
76 | }; |
77 | |
78 | class QQmlTestMessageHandler |
79 | { |
80 | Q_DISABLE_COPY(QQmlTestMessageHandler) |
81 | public: |
82 | QQmlTestMessageHandler(); |
83 | ~QQmlTestMessageHandler(); |
84 | |
85 | const QStringList &messages() const { return m_messages; } |
86 | const QString messageString() const { return m_messages.join(sep: QLatin1Char('\n')); } |
87 | |
88 | void clear() { m_messages.clear(); } |
89 | |
90 | private: |
91 | static void messageHandler(QtMsgType, const QMessageLogContext &, const QString &message); |
92 | |
93 | static QQmlTestMessageHandler *m_instance; |
94 | QStringList m_messages; |
95 | QtMessageHandler m_oldHandler; |
96 | }; |
97 | |
98 | #endif // QQMLTESTUTILS_H |
99 |