1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef QQMLTESTUTILS_P_H
5#define QQMLTESTUTILS_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 <QtCore/QTemporaryDir>
19#include <QtCore/QDir>
20#include <QtCore/QUrl>
21#include <QtCore/QCoreApplication>
22#include <QtCore/QStringList>
23#include <QtTest/QTest>
24#include <QtCore/private/qglobal_p.h>
25
26QT_BEGIN_NAMESPACE
27
28/* Base class for tests with data that are located in a "data" subfolder. */
29
30class QQmlDataTest : public QObject
31{
32 Q_OBJECT
33public:
34 enum class FailOnWarningsPolicy {
35 DoNotFailOnWarnings,
36 FailOnWarnings
37 };
38
39 QQmlDataTest(
40 const char *qmlTestDataDir,
41 FailOnWarningsPolicy failOnWarningsPolicy = FailOnWarningsPolicy::DoNotFailOnWarnings,
42 const char *dataSubdir = "data");
43 virtual ~QQmlDataTest();
44
45 QString testFile(const QString &fileName) const;
46 inline QString testFile(const char *fileName) const
47 { return testFile(fileName: QLatin1String(fileName)); }
48 inline QUrl testFileUrl(const QString &fileName) const
49 {
50 const QString fn = testFile(fileName);
51 return fn.startsWith(c: QLatin1Char(':'))
52 ? QUrl(QLatin1String("qrc") + fn)
53 : QUrl::fromLocalFile(localfile: fn);
54 }
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 bool canImportModule(const QString &importTestQmlSource) const;
65
66public Q_SLOTS:
67 virtual void initTestCase();
68 virtual void init();
69
70private:
71 static QQmlDataTest *m_instance;
72
73 // The directory in which to search for m_dataSubDir.
74 const char *m_qmlTestDataDir = nullptr;
75 // The subdirectory containing the actual data. Typically "data"
76 const char *m_dataSubDir = nullptr;
77 // The path to m_dataSubDir, if found.
78 const QString m_dataDirectory;
79 const QUrl m_dataDirectoryUrl;
80 QTemporaryDir m_cacheDir;
81 QString m_directory;
82 bool m_usesOwnCacheDir = false;
83 FailOnWarningsPolicy m_failOnWarningsPolicy = FailOnWarningsPolicy::DoNotFailOnWarnings;
84};
85
86class QQmlTestMessageHandler
87{
88 Q_DISABLE_COPY(QQmlTestMessageHandler)
89public:
90 QQmlTestMessageHandler();
91 ~QQmlTestMessageHandler();
92
93 const QStringList &messages() const { return m_messages; }
94 const QString messageString() const { return m_messages.join(sep: QLatin1Char('\n')); }
95
96 void clear() { m_messages.clear(); }
97
98 void setIncludeCategoriesEnabled(bool enabled) { m_includeCategories = enabled; }
99
100private:
101 static void messageHandler(QtMsgType, const QMessageLogContext &context, const QString &message);
102
103 static QQmlTestMessageHandler *m_instance;
104 QStringList m_messages;
105 QtMessageHandler m_oldHandler;
106 bool m_includeCategories;
107};
108
109QT_END_NAMESPACE
110
111#endif // QQMLTESTUTILS_P_H
112

source code of qtdeclarative/src/quicktestutils/qml/qmlutils_p.h