1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
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 http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "util.h"
38
39#include <QtQml/QQmlComponent>
40#include <QtQml/QQmlError>
41#include <QtQml/QQmlContext>
42#include <QtQml/QQmlEngine>
43#include <QtCore/QTextStream>
44#include <QtCore/QDebug>
45#include <QtCore/QMutexLocker>
46
47QQmlDataTest *QQmlDataTest::m_instance = 0;
48
49QQmlDataTest::QQmlDataTest() :
50#ifdef QT_TESTCASE_BUILDDIR
51 m_dataDirectory(QTest::qFindTestData(basepath: "data", QT_QMLTEST_DATADIR, line: 0, QT_TESTCASE_BUILDDIR)),
52#else
53 m_dataDirectory(QTest::qFindTestData("data", QT_QMLTEST_DATADIR, 0)),
54#endif
55
56 m_dataDirectoryUrl(m_dataDirectory.startsWith(c: QLatin1Char(':'))
57 ? QUrl(QLatin1String("qrc") + m_dataDirectory)
58 : QUrl::fromLocalFile(localfile: m_dataDirectory + QLatin1Char('/')))
59{
60 m_instance = this;
61}
62
63QQmlDataTest::~QQmlDataTest()
64{
65 m_instance = 0;
66}
67
68void QQmlDataTest::initTestCase()
69{
70 QVERIFY2(!m_dataDirectory.isEmpty(), "'data' directory not found");
71 m_directory = QFileInfo(m_dataDirectory).absolutePath();
72 if (m_dataDirectoryUrl.scheme() != QLatin1String("qrc"))
73 QVERIFY2(QDir::setCurrent(m_directory), qPrintable(QLatin1String("Could not chdir to ") + m_directory));
74}
75
76QString QQmlDataTest::testFile(const QString &fileName) const
77{
78 if (m_directory.isEmpty())
79 qFatal(msg: "QQmlDataTest::initTestCase() not called.");
80 QString result = m_dataDirectory;
81 result += QLatin1Char('/');
82 result += fileName;
83 return result;
84}
85
86QByteArray QQmlDataTest::msgComponentError(const QQmlComponent &c,
87 const QQmlEngine *engine /* = 0 */)
88{
89 QString result;
90 const QList<QQmlError> errors = c.errors();
91 QTextStream str(&result);
92 str << "Component '" << c.url().toString() << "' has " << errors.size()
93 << " errors: '";
94 for (int i = 0; i < errors.size(); ++i) {
95 if (i)
96 str << ", '";
97 str << errors.at(i).toString() << '\'';
98
99 }
100 if (!engine)
101 if (QQmlContext *context = c.creationContext())
102 engine = context->engine();
103 if (engine) {
104 str << " Import paths: (" << engine->importPathList().join(QStringLiteral(", "))
105 << ") Plugin paths: (" << engine->pluginPathList().join(QStringLiteral(", "))
106 << ')';
107 }
108 return result.toLocal8Bit();
109}
110
111Q_GLOBAL_STATIC(QMutex, qQmlTestMessageHandlerMutex)
112
113QQmlTestMessageHandler *QQmlTestMessageHandler::m_instance = 0;
114
115void QQmlTestMessageHandler::messageHandler(QtMsgType, const QMessageLogContext &, const QString &message)
116{
117 QMutexLocker locker(qQmlTestMessageHandlerMutex());
118 if (QQmlTestMessageHandler::m_instance)
119 QQmlTestMessageHandler::m_instance->m_messages.push_back(t: message);
120}
121
122QQmlTestMessageHandler::QQmlTestMessageHandler()
123{
124 QMutexLocker locker(qQmlTestMessageHandlerMutex());
125 Q_ASSERT(!QQmlTestMessageHandler::m_instance);
126 QQmlTestMessageHandler::m_instance = this;
127 m_oldHandler = qInstallMessageHandler(messageHandler);
128}
129
130QQmlTestMessageHandler::~QQmlTestMessageHandler()
131{
132 QMutexLocker locker(qQmlTestMessageHandlerMutex());
133 Q_ASSERT(QQmlTestMessageHandler::m_instance);
134 qInstallMessageHandler(m_oldHandler);
135 QQmlTestMessageHandler::m_instance = 0;
136}
137

source code of qtquickcontrols2/tests/auto/shared/util.cpp