File indexing completed on 2024-05-05 16:07:09

0001 /*
0002     This file is part of the test suite of the Qt Toolkit.
0003     SPDX-FileCopyrightText: 2014 Digia Plc and/or its subsidiary(-ies) <http://www.qt-project.org/legal>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only WITH Qt-LGPL-exception-1.1 OR LGPL-3.0-only WITH Qt-LGPL-exception-1.1 OR LicenseRef-Qt-Commercial
0006 */
0007 
0008 #ifndef QQMLTESTUTILS_H
0009 #define QQMLTESTUTILS_H
0010 
0011 #include <QObject>
0012 #include <QStringList>
0013 #include <QUrl>
0014 
0015 QT_FORWARD_DECLARE_CLASS(QQmlComponent)
0016 QT_FORWARD_DECLARE_CLASS(QQmlEngine)
0017 
0018 /* Base class for tests with data that are located in a "data" subfolder. */
0019 
0020 class QQmlDataTest : public QObject
0021 {
0022     Q_OBJECT
0023 public:
0024     QQmlDataTest();
0025     ~QQmlDataTest() override;
0026 
0027     QString testFile(const QString &fileName) const;
0028     inline QString testFile(const char *fileName) const
0029     {
0030         return testFile(QLatin1String(fileName));
0031     }
0032     inline QUrl testFileUrl(const QString &fileName) const
0033     {
0034         return QUrl::fromLocalFile(testFile(fileName));
0035     }
0036     inline QUrl testFileUrl(const char *fileName) const
0037     {
0038         return testFileUrl(QLatin1String(fileName));
0039     }
0040 
0041     inline QString dataDirectory() const
0042     {
0043         return m_dataDirectory;
0044     }
0045     inline QUrl dataDirectoryUrl() const
0046     {
0047         return m_dataDirectoryUrl;
0048     }
0049     inline QString directory() const
0050     {
0051         return m_directory;
0052     }
0053 
0054     static inline QQmlDataTest *instance()
0055     {
0056         return m_instance;
0057     }
0058 
0059     static QByteArray msgComponentError(const QQmlComponent &, const QQmlEngine *engine = nullptr);
0060 
0061 public Q_SLOTS:
0062     virtual void initTestCase();
0063 
0064 private:
0065     static QQmlDataTest *m_instance;
0066 
0067     const QString m_dataDirectory;
0068     const QUrl m_dataDirectoryUrl;
0069     QString m_directory;
0070 };
0071 
0072 class QQmlTestMessageHandler
0073 {
0074     Q_DISABLE_COPY(QQmlTestMessageHandler)
0075 public:
0076     QQmlTestMessageHandler();
0077     ~QQmlTestMessageHandler();
0078 
0079     const QStringList &messages() const
0080     {
0081         return m_messages;
0082     }
0083     const QString messageString() const
0084     {
0085         return m_messages.join(QLatin1Char('\n'));
0086     }
0087 
0088     void clear()
0089     {
0090         m_messages.clear();
0091     }
0092 
0093 private:
0094     static void messageHandler(QtMsgType, const QMessageLogContext &, const QString &message);
0095 
0096     static QQmlTestMessageHandler *m_instance;
0097     QStringList m_messages;
0098     QtMessageHandler m_oldHandler;
0099 };
0100 
0101 #endif // QQMLTESTUTILS_H