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 <QtCore/QDebug>
32#include <QtCore/QMutexLocker>
33
34QQmlDataTest *QQmlDataTest::m_instance = 0;
35
36QQmlDataTest::QQmlDataTest() :
37#ifdef QT_TESTCASE_BUILDDIR
38 m_dataDirectory(QTest::qFindTestData(basepath: "data", QT_QMLTEST_DATADIR, line: 0, QT_TESTCASE_BUILDDIR)),
39#else
40 m_dataDirectory(QTest::qFindTestData("data", QT_QMLTEST_DATADIR, 0)),
41#endif
42
43 m_dataDirectoryUrl(m_dataDirectory.startsWith(c: QLatin1Char(':'))
44 ? QUrl(QLatin1String("qrc") + m_dataDirectory)
45 : QUrl::fromLocalFile(localfile: m_dataDirectory + QLatin1Char('/')))
46{
47 m_instance = this;
48}
49
50QQmlDataTest::~QQmlDataTest()
51{
52 m_instance = 0;
53}
54
55void QQmlDataTest::initTestCase()
56{
57 QVERIFY2(!m_dataDirectory.isEmpty(), "'data' directory not found");
58 m_directory = QFileInfo(m_dataDirectory).absolutePath();
59 if (m_dataDirectoryUrl.scheme() != QLatin1String("qrc"))
60 QVERIFY2(QDir::setCurrent(m_directory), qPrintable(QLatin1String("Could not chdir to ") + m_directory));
61}
62
63QString QQmlDataTest::testFile(const QString &fileName) const
64{
65 if (m_directory.isEmpty())
66 qFatal(msg: "QQmlDataTest::initTestCase() not called.");
67 QString result = m_dataDirectory;
68 result += QLatin1Char('/');
69 result += fileName;
70 return result;
71}
72
73Q_GLOBAL_STATIC(QMutex, qQmlTestMessageHandlerMutex)
74
75QQmlTestMessageHandler *QQmlTestMessageHandler::m_instance = 0;
76
77void QQmlTestMessageHandler::messageHandler(QtMsgType, const QMessageLogContext &context, const QString &message)
78{
79 QMutexLocker locker(qQmlTestMessageHandlerMutex());
80 if (QQmlTestMessageHandler::m_instance) {
81 if (QQmlTestMessageHandler::m_instance->m_includeCategories)
82 QQmlTestMessageHandler::m_instance->m_messages.push_back(t: QString("%1: %2").arg(args: context.category, args: message));
83 else
84 QQmlTestMessageHandler::m_instance->m_messages.push_back(t: message);
85 }
86}
87
88QQmlTestMessageHandler::QQmlTestMessageHandler()
89{
90 QMutexLocker locker(qQmlTestMessageHandlerMutex());
91 Q_ASSERT(!QQmlTestMessageHandler::m_instance);
92 QQmlTestMessageHandler::m_instance = this;
93 m_oldHandler = qInstallMessageHandler(messageHandler);
94 m_includeCategories = false;
95}
96
97QQmlTestMessageHandler::~QQmlTestMessageHandler()
98{
99 QMutexLocker locker(qQmlTestMessageHandlerMutex());
100 Q_ASSERT(QQmlTestMessageHandler::m_instance);
101 qInstallMessageHandler(m_oldHandler);
102 QQmlTestMessageHandler::m_instance = 0;
103}
104

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