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//QQmlDebugTest
30#include "debugutil_p.h"
31
32#include <private/qqmldebugclient_p.h>
33#include <private/qqmldebugconnection_p.h>
34#include <private/qpacket_p.h>
35
36#include <QtCore/qstring.h>
37#include <QtCore/qlibraryinfo.h>
38#include <QtTest/qtest.h>
39
40const char *QMLFILE = "test.qml";
41
42class QQmlDebugMsgClient;
43class tst_QDebugMessageService : public QQmlDebugTest
44{
45 Q_OBJECT
46
47private slots:
48 void retrieveDebugOutput();
49
50private:
51 QList<QQmlDebugClient *> createClients() override;
52 QPointer<QQmlDebugMsgClient> m_client;
53};
54
55struct LogEntry {
56 LogEntry(QtMsgType _type, QString _message)
57 : type(_type), message(_message) {}
58
59 QtMsgType type;
60 QString message;
61 int line;
62 QString file;
63 QString function;
64 QString category;
65
66 QString toString() const
67 {
68 return QString::number(type) + ": " + message + " (" + category + ")";
69 }
70};
71
72bool operator==(const LogEntry &t1, const LogEntry &t2)
73{
74 return t1.type == t2.type && t1.message == t2.message
75 && t1.line == t2.line && t1.file == t2.file
76 && t1.function == t2.function && t1.category == t2.category;
77}
78
79class QQmlDebugMsgClient : public QQmlDebugClient
80{
81 Q_OBJECT
82public:
83 QQmlDebugMsgClient(QQmlDebugConnection *connection)
84 : QQmlDebugClient(QLatin1String("DebugMessages"), connection)
85 {
86 }
87
88 QList<LogEntry> logBuffer;
89
90protected:
91 void messageReceived(const QByteArray &data) override;
92
93signals:
94 void debugOutput();
95};
96
97void QQmlDebugMsgClient::messageReceived(const QByteArray &data)
98{
99 QPacket ds(connection()->currentDataStreamVersion(), data);
100 QByteArray command;
101 ds >> command;
102
103 if (command == "MESSAGE") {
104 int type;
105 QByteArray message;
106 QByteArray file;
107 QByteArray function;
108 QByteArray category;
109 qint64 timestamp;
110 int line;
111 ds >> type >> message >> file >> line >> function >> category >> timestamp;
112 QVERIFY(ds.atEnd());
113
114 QVERIFY(type >= QtDebugMsg);
115 QVERIFY(type <= QtInfoMsg);
116 QVERIFY(timestamp > 0);
117
118 LogEntry entry((QtMsgType)type, QString::fromUtf8(str: message));
119 entry.line = line;
120 entry.file = QString::fromUtf8(str: file);
121 entry.function = QString::fromUtf8(str: function);
122 entry.category = QString::fromUtf8(str: category);
123 logBuffer << entry;
124 emit debugOutput();
125 } else {
126 QFAIL("Unknown message");
127 }
128}
129
130QList<QQmlDebugClient *> tst_QDebugMessageService::createClients()
131{
132 m_client = new QQmlDebugMsgClient(m_connection);
133 return QList<QQmlDebugClient *>({m_client});
134}
135
136void tst_QDebugMessageService::retrieveDebugOutput()
137{
138 QCOMPARE(QQmlDebugTest::connectTo(QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qml",
139 QString(), testFile(QMLFILE), true), ConnectSuccess);
140
141 QTRY_VERIFY(m_client->logBuffer.size() >= 2);
142
143 const QString path =
144 QUrl::fromLocalFile(localfile: QQmlDataTest::instance()->testFile(fileName: QMLFILE)).toString();
145 LogEntry entry1(QtDebugMsg, QLatin1String("console.log"));
146 entry1.line = 35;
147 entry1.file = path;
148 entry1.function = QLatin1String("onCompleted");
149 entry1.category = QLatin1String("qml");
150 LogEntry entry2(QtDebugMsg, QLatin1String("console.count: 1"));
151 entry2.line = 36;
152 entry2.file = path;
153 entry2.function = QLatin1String("onCompleted");
154 entry2.category = QLatin1String("default");
155
156 QVERIFY(m_client->logBuffer.contains(entry1));
157 QVERIFY(m_client->logBuffer.contains(entry2));
158}
159
160QTEST_MAIN(tst_QDebugMessageService)
161
162#include "tst_qdebugmessageservice.moc"
163

source code of qtdeclarative/tests/auto/qml/debugger/qdebugmessageservice/tst_qdebugmessageservice.cpp