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#include "util.h"
30
31#include <QtQml/QQmlComponent>
32#include <QtQml/QQmlError>
33#include <QtQml/QQmlContext>
34#include <QtQml/QQmlEngine>
35#include <QtCore/QTextStream>
36#include <QtCore/QDebug>
37#include <QtCore/QMutexLocker>
38
39QQmlDataTest *QQmlDataTest::m_instance = 0;
40
41QQmlDataTest::QQmlDataTest() :
42#ifdef QT_TESTCASE_BUILDDIR
43 m_dataDirectory(QTest::qFindTestData(basepath: "data", QT_QMLTEST_DATADIR, line: 0, QT_TESTCASE_BUILDDIR)),
44#else
45 m_dataDirectory(QTest::qFindTestData("data", QT_QMLTEST_DATADIR, 0)),
46#endif
47
48 m_dataDirectoryUrl(QUrl::fromLocalFile(localfile: m_dataDirectory + QLatin1Char('/')))
49{
50 m_instance = this;
51}
52
53QQmlDataTest::~QQmlDataTest()
54{
55 m_instance = 0;
56}
57
58void QQmlDataTest::initTestCase()
59{
60 QVERIFY2(!m_dataDirectory.isEmpty(), "'data' directory not found");
61 m_directory = QFileInfo(m_dataDirectory).absolutePath();
62 QVERIFY2(QDir::setCurrent(m_directory), qPrintable(QLatin1String("Could not chdir to ") + m_directory));
63}
64
65QString QQmlDataTest::testFile(const QString &fileName) const
66{
67 if (m_directory.isEmpty())
68 qFatal(msg: "QQmlDataTest::initTestCase() not called.");
69 QString result = m_dataDirectory;
70 result += QLatin1Char('/');
71 result += fileName;
72 return result;
73}
74
75QByteArray QQmlDataTest::msgComponentError(const QQmlComponent &c,
76 const QQmlEngine *engine /* = 0 */)
77{
78 QString result;
79 const QList<QQmlError> errors = c.errors();
80 QTextStream str(&result);
81 str << "Component '" << c.url().toString() << "' has " << errors.size()
82 << " errors: '";
83 for (int i = 0; i < errors.size(); ++i) {
84 if (i)
85 str << ", '";
86 str << errors.at(i).toString() << '\'';
87
88 }
89 if (!engine)
90 if (QQmlContext *context = c.creationContext())
91 engine = context->engine();
92 if (engine) {
93 str << " Import paths: (" << engine->importPathList().join(QStringLiteral(", "))
94 << ") Plugin paths: (" << engine->pluginPathList().join(QStringLiteral(", "))
95 << ')';
96 }
97 return result.toLocal8Bit();
98}
99
100Q_GLOBAL_STATIC(QMutex, qQmlTestMessageHandlerMutex)
101
102QQmlTestMessageHandler *QQmlTestMessageHandler::m_instance = 0;
103
104void QQmlTestMessageHandler::messageHandler(QtMsgType, const QMessageLogContext &, const QString &message)
105{
106 QMutexLocker locker(qQmlTestMessageHandlerMutex());
107 if (QQmlTestMessageHandler::m_instance)
108 QQmlTestMessageHandler::m_instance->m_messages.push_back(t: message);
109}
110
111QQmlTestMessageHandler::QQmlTestMessageHandler()
112{
113 QMutexLocker locker(qQmlTestMessageHandlerMutex());
114 Q_ASSERT(!QQmlTestMessageHandler::m_instance);
115 QQmlTestMessageHandler::m_instance = this;
116 m_oldHandler = qInstallMessageHandler(messageHandler);
117}
118
119QQmlTestMessageHandler::~QQmlTestMessageHandler()
120{
121 QMutexLocker locker(qQmlTestMessageHandlerMutex());
122 Q_ASSERT(QQmlTestMessageHandler::m_instance);
123 qInstallMessageHandler(m_oldHandler);
124 QQmlTestMessageHandler::m_instance = 0;
125}
126

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