| 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 | { return QUrl::fromLocalFile(localfile: testFile(fileName)); } | 
| 52 | inline QUrl testFileUrl(const char *fileName) const | 
| 53 | { return testFileUrl(fileName: QLatin1String(fileName)); } | 
| 54 | |
| 55 | inline QString dataDirectory() const { return m_dataDirectory; } | 
| 56 | inline QUrl dataDirectoryUrl() const { return m_dataDirectoryUrl; } | 
| 57 | inline QString directory() const { return m_directory; } | 
| 58 | |
| 59 | static inline QQmlDataTest *instance() { return m_instance; } | 
| 60 | |
| 61 | public slots: | 
| 62 | virtual void initTestCase(); | 
| 63 | |
| 64 | private: | 
| 65 | static QQmlDataTest *m_instance; | 
| 66 | |
| 67 | const QString m_dataDirectory; | 
| 68 | const QUrl m_dataDirectoryUrl; | 
| 69 | QString m_directory; | 
| 70 | }; | 
| 71 | |
| 72 | class QQmlTestMessageHandler | 
| 73 | { | 
| 74 | Q_DISABLE_COPY(QQmlTestMessageHandler) | 
| 75 | public: | 
| 76 | QQmlTestMessageHandler(); | 
| 77 | ~QQmlTestMessageHandler(); | 
| 78 | |
| 79 | const QStringList &messages() const { return m_messages; } | 
| 80 | const QString messageString() const { return m_messages.join(sep: QLatin1Char('\n')); } | 
| 81 | |
| 82 | void clear() { m_messages.clear(); } | 
| 83 | |
| 84 | void setIncludeCategoriesEnabled(bool enabled) { m_includeCategories = enabled; } | 
| 85 | |
| 86 | private: | 
| 87 | static void messageHandler(QtMsgType, const QMessageLogContext &context, const QString &message); | 
| 88 | |
| 89 | static QQmlTestMessageHandler *m_instance; | 
| 90 | QStringList m_messages; | 
| 91 | QtMessageHandler m_oldHandler; | 
| 92 | bool m_includeCategories; | 
| 93 | }; | 
| 94 | |
| 95 | #endif // QQMLTESTUTILS_H | 
| 96 | 
