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 | #include "qmlutils_p.h" |
5 | |
6 | #include <QtCore/QDebug> |
7 | #include <QtCore/QMutexLocker> |
8 | #include <QtQml/QQmlComponent> |
9 | #include <QtQml/QQmlEngine> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | QQmlDataTest *QQmlDataTest::m_instance = nullptr; |
14 | |
15 | QQmlDataTest::QQmlDataTest( |
16 | const char *qmlTestDataDir, FailOnWarningsPolicy failOnWarningsPolicy, |
17 | const char *dataSubDir) |
18 | : m_qmlTestDataDir(qmlTestDataDir) |
19 | #ifdef QT_TESTCASE_BUILDDIR |
20 | , m_dataDirectory(QTest::qFindTestData(basepath: dataSubDir, file: m_qmlTestDataDir, line: 0, QT_TESTCASE_BUILDDIR)) |
21 | #else |
22 | , m_dataDirectory(QTest::qFindTestData(dataSubDir, m_qmlTestDataDir, 0)) |
23 | #endif |
24 | , m_dataDirectoryUrl(m_dataDirectory.startsWith(c: QLatin1Char(':')) |
25 | ? QUrl(QLatin1String("qrc" ) + m_dataDirectory + QLatin1Char('/')) |
26 | : QUrl::fromLocalFile(localfile: m_dataDirectory + QLatin1Char('/'))) |
27 | , m_failOnWarningsPolicy(failOnWarningsPolicy) |
28 | { |
29 | m_instance = this; |
30 | if (m_cacheDir.isValid() && !qEnvironmentVariableIsSet(varName: "QML_DISK_CACHE_PATH" )) { |
31 | m_usesOwnCacheDir = true; |
32 | qputenv(varName: "QML_DISK_CACHE_PATH" , value: m_cacheDir.path().toLocal8Bit()); |
33 | } |
34 | } |
35 | |
36 | QQmlDataTest::~QQmlDataTest() |
37 | { |
38 | m_instance = nullptr; |
39 | if (m_usesOwnCacheDir) |
40 | qunsetenv(varName: "QML_DISK_CACHE_PATH" ); |
41 | } |
42 | |
43 | void QQmlDataTest::initTestCase() |
44 | { |
45 | QVERIFY2(!m_dataDirectory.isEmpty(), qPrintable( |
46 | QLatin1String("'%1' directory not found in %2" ).arg( |
47 | QString::fromUtf8(m_dataSubDir), |
48 | QFileInfo(QString::fromUtf8(m_qmlTestDataDir)).absolutePath()))); |
49 | m_directory = QFileInfo(m_dataDirectory).absolutePath(); |
50 | if (m_dataDirectoryUrl.scheme() != QLatin1String("qrc" )) |
51 | QVERIFY2(QDir::setCurrent(m_directory), qPrintable(QLatin1String("Could not chdir to " ) + m_directory)); |
52 | } |
53 | |
54 | void QQmlDataTest::init() |
55 | { |
56 | if (m_failOnWarningsPolicy == FailOnWarningsPolicy::FailOnWarnings) |
57 | QTest::failOnWarning(messagePattern: QRegularExpression(QStringLiteral(".?" ))); |
58 | } |
59 | |
60 | QString QQmlDataTest::testFile(const QString &fileName) const |
61 | { |
62 | if (m_directory.isEmpty()) |
63 | qFatal(msg: "QQmlDataTest::initTestCase() not called." ); |
64 | QString result = m_dataDirectory; |
65 | result += QLatin1Char('/'); |
66 | result += fileName; |
67 | return result; |
68 | } |
69 | |
70 | bool QQmlDataTest::canImportModule(const QString &importTestQmlSource) const |
71 | { |
72 | QQmlEngine engine; |
73 | QQmlComponent component(&engine); |
74 | component.setData(importTestQmlSource.toLatin1(), baseUrl: QUrl()); |
75 | return !component.isError(); |
76 | } |
77 | |
78 | Q_GLOBAL_STATIC(QMutex, qQmlTestMessageHandlerMutex) |
79 | |
80 | QQmlTestMessageHandler *QQmlTestMessageHandler::m_instance = nullptr; |
81 | |
82 | void QQmlTestMessageHandler::messageHandler(QtMsgType, const QMessageLogContext &context, const QString &message) |
83 | { |
84 | QMutexLocker locker(qQmlTestMessageHandlerMutex()); |
85 | if (QQmlTestMessageHandler::m_instance) { |
86 | if (QQmlTestMessageHandler::m_instance->m_includeCategories) { |
87 | QQmlTestMessageHandler::m_instance->m_messages.push_back( |
88 | t: QString::fromLatin1(ba: "%1: %2" ).arg(args: QString::fromUtf8(utf8: context.category), args: message)); |
89 | } else { |
90 | QQmlTestMessageHandler::m_instance->m_messages.push_back(t: message); |
91 | } |
92 | } |
93 | } |
94 | |
95 | QQmlTestMessageHandler::QQmlTestMessageHandler() |
96 | { |
97 | QMutexLocker locker(qQmlTestMessageHandlerMutex()); |
98 | Q_ASSERT(!QQmlTestMessageHandler::m_instance); |
99 | QQmlTestMessageHandler::m_instance = this; |
100 | m_oldHandler = qInstallMessageHandler(messageHandler); |
101 | m_includeCategories = false; |
102 | } |
103 | |
104 | QQmlTestMessageHandler::~QQmlTestMessageHandler() |
105 | { |
106 | QMutexLocker locker(qQmlTestMessageHandlerMutex()); |
107 | Q_ASSERT(QQmlTestMessageHandler::m_instance); |
108 | qInstallMessageHandler(m_oldHandler); |
109 | QQmlTestMessageHandler::m_instance = nullptr; |
110 | } |
111 | |
112 | QT_END_NAMESPACE |
113 | |
114 | #include "moc_qmlutils_p.cpp" |
115 | |