| 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 | /* Base class for tests with data that are located in a "data" subfolder. */ |
| 39 | |
| 40 | class QQmlDataTest : public QObject |
| 41 | { |
| 42 | Q_OBJECT |
| 43 | public: |
| 44 | QQmlDataTest(); |
| 45 | virtual ~QQmlDataTest(); |
| 46 | |
| 47 | QString testFile(const QString &fileName) const; |
| 48 | inline QString testFile(const char *fileName) const |
| 49 | { return testFile(fileName: QLatin1String(fileName)); } |
| 50 | inline QUrl testFileUrl(const QString &fileName) const |
| 51 | { |
| 52 | const QString fn = testFile(fileName); |
| 53 | return fn.startsWith(c: QLatin1Char(':')) |
| 54 | ? QUrl(QLatin1String("qrc") + fn) |
| 55 | : QUrl::fromLocalFile(localfile: fn); |
| 56 | } |
| 57 | inline QUrl testFileUrl(const char *fileName) const |
| 58 | { return testFileUrl(fileName: QLatin1String(fileName)); } |
| 59 | |
| 60 | inline QString dataDirectory() const { return m_dataDirectory; } |
| 61 | inline QUrl dataDirectoryUrl() const { return m_dataDirectoryUrl; } |
| 62 | inline QString directory() const { return m_directory; } |
| 63 | |
| 64 | static inline QQmlDataTest *instance() { return m_instance; } |
| 65 | |
| 66 | public slots: |
| 67 | virtual void initTestCase(); |
| 68 | |
| 69 | private: |
| 70 | static QQmlDataTest *m_instance; |
| 71 | |
| 72 | const QString m_dataDirectory; |
| 73 | const QUrl m_dataDirectoryUrl; |
| 74 | QString m_directory; |
| 75 | }; |
| 76 | |
| 77 | class QQmlTestMessageHandler |
| 78 | { |
| 79 | Q_DISABLE_COPY(QQmlTestMessageHandler) |
| 80 | public: |
| 81 | QQmlTestMessageHandler(); |
| 82 | ~QQmlTestMessageHandler(); |
| 83 | |
| 84 | const QStringList &messages() const { return m_messages; } |
| 85 | const QString messageString() const { return m_messages.join(sep: QLatin1Char('\n')); } |
| 86 | |
| 87 | void clear() { m_messages.clear(); } |
| 88 | |
| 89 | void setIncludeCategoriesEnabled(bool enabled) { m_includeCategories = enabled; } |
| 90 | |
| 91 | private: |
| 92 | static void messageHandler(QtMsgType, const QMessageLogContext &context, const QString &message); |
| 93 | |
| 94 | static QQmlTestMessageHandler *m_instance; |
| 95 | QStringList m_messages; |
| 96 | QtMessageHandler m_oldHandler; |
| 97 | bool m_includeCategories; |
| 98 | }; |
| 99 | |
| 100 | #endif // QQMLTESTUTILS_H |
| 101 |
