1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qqmldebugclient.h"
45 #include "debugutil_p.h"
46 #include "../../../shared/util.h"
48 #include <QtCore/QString>
49 #include <QtTest/QtTest>
51 const char *NORMALMODE = "-qmljsdebugger=port:3777,block";
52 const char *QMLFILE = "test.qml";
54 class QQmlDebugMsgClient;
55 class tst_QDebugMessageService : public QQmlDataTest
60 tst_QDebugMessageService();
66 void cleanupTestCase();
70 void retrieveDebugOutput();
73 QQmlDebugProcess *m_process;
74 QQmlDebugMsgClient *m_client;
75 QQmlDebugConnection *m_connection;
79 LogEntry(QtMsgType _type, QString _message)
80 : type(_type), message(_message) {}
88 QString toString() const { return QString::number(type) + ": " + message; }
91 bool operator==(const LogEntry &t1, const LogEntry &t2)
93 return t1.type == t2.type && t1.message == t2.message
94 && t1.line == t2.line && t1.file == t2.file
95 && t1.function == t2.function;
98 class QQmlDebugMsgClient : public QQmlDebugClient
102 QQmlDebugMsgClient(QQmlDebugConnection *connection)
103 : QQmlDebugClient(QLatin1String("DebugMessages"), connection)
107 QList<LogEntry> logBuffer;
110 //inherited from QQmlDebugClient
111 void stateChanged(State state);
112 void messageReceived(const QByteArray &data);
119 void QQmlDebugMsgClient::stateChanged(State state)
121 if (state == Enabled) {
126 void QQmlDebugMsgClient::messageReceived(const QByteArray &data)
128 QDataStream ds(data);
132 if (command == "MESSAGE") {
138 ds >> type >> message >> file >> line >> function;
141 QVERIFY(type >= QtDebugMsg);
142 QVERIFY(type <= QtFatalMsg);
144 LogEntry entry((QtMsgType)type, QString::fromUtf8(message));
146 entry.file = QString::fromUtf8(file);
147 entry.function = QString::fromUtf8(function);
151 QFAIL("Unknown message");
155 tst_QDebugMessageService::tst_QDebugMessageService()
159 void tst_QDebugMessageService::initTestCase()
161 QQmlDataTest::initTestCase();
167 void tst_QDebugMessageService::cleanupTestCase()
179 void tst_QDebugMessageService::init()
181 m_connection = new QQmlDebugConnection();
182 m_process = new QQmlDebugProcess(QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene");
183 m_client = new QQmlDebugMsgClient(m_connection);
185 m_process->start(QStringList() << QLatin1String(NORMALMODE) << QQmlDataTest::instance()->testFile(QMLFILE));
186 if (!m_process->waitForSessionStart()) {
187 QFAIL(QString("Could not launch app. Application output: \n%1").arg(m_process->output()).toAscii());
190 m_connection->connectToHost("127.0.0.1", 3777);
191 QVERIFY(m_connection->waitForConnected());
193 QVERIFY(QQmlDebugTest::waitForSignal(m_client, SIGNAL(enabled())));
196 void tst_QDebugMessageService::cleanup()
198 if (QTest::currentTestFailed())
199 qDebug() << m_process->output();
214 void tst_QDebugMessageService::retrieveDebugOutput()
219 while ((m_client->logBuffer.size() < 2)
221 QQmlDebugTest::waitForSignal(m_client, SIGNAL(debugOutput()), 1000);
223 QVERIFY(m_client->logBuffer.size() >= 2);
226 QUrl::fromLocalFile(QQmlDataTest::instance()->testFile(QMLFILE)).toString();
227 LogEntry entry1(QtDebugMsg, QLatin1String("console.log"));
230 entry1.function = QLatin1String("onCompleted");
231 LogEntry entry2(QtDebugMsg, QLatin1String("console.count: 1"));
234 entry2.function = QLatin1String("onCompleted");
236 QVERIFY(m_client->logBuffer.contains(entry1));
237 QVERIFY(m_client->logBuffer.contains(entry2));
240 QTEST_MAIN(tst_QDebugMessageService)
242 #include "tst_qdebugmessageservice.moc"