1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtDeclarative/private/qdeclarativedebugclient_p.h>
44 //QDeclarativeDebugTest
45 #include "../shared/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 QDeclarativeDebugMsgClient;
55 class tst_QDebugMessageService : public QDeclarativeDataTest
60 tst_QDebugMessageService();
64 void cleanupTestCase();
69 void retrieveDebugOutput();
72 QDeclarativeDebugProcess *m_process;
73 QDeclarativeDebugMsgClient *m_client;
74 QDeclarativeDebugConnection *m_connection;
78 LogEntry(QtMsgType _type, QString _message)
79 : type(_type), message(_message) {}
84 QString toString() const { return QString::number(type) + ": " + message; }
87 class QDeclarativeDebugMsgClient : public QDeclarativeDebugClient
91 QDeclarativeDebugMsgClient(QDeclarativeDebugConnection *connection)
92 : QDeclarativeDebugClient(QLatin1String("DebugMessages"), connection)
96 QList<LogEntry> logBuffer;
99 //inherited from QDeclarativeDebugClient
100 void statusChanged(Status status);
101 void messageReceived(const QByteArray &data);
108 void QDeclarativeDebugMsgClient::statusChanged(Status status)
110 if (status == Enabled) {
115 void QDeclarativeDebugMsgClient::messageReceived(const QByteArray &data)
117 QDataStream ds(data);
121 if (command == "MESSAGE") {
122 QByteArray container;
127 QDataStream containerDs(container);
130 containerDs >> type >> message;
131 QVERIFY(containerDs.atEnd());
133 QVERIFY(type >= QtDebugMsg);
134 QVERIFY(type <= QtFatalMsg);
136 logBuffer << LogEntry((QtMsgType)type, QString::fromUtf8(message));
139 QFAIL("Unknown message");
143 tst_QDebugMessageService::tst_QDebugMessageService()
147 void tst_QDebugMessageService::initTestCase()
149 QDeclarativeDataTest::initTestCase();
155 void tst_QDebugMessageService::cleanupTestCase()
167 void tst_QDebugMessageService::init()
169 m_connection = new QDeclarativeDebugConnection();
170 m_process = new QDeclarativeDebugProcess(QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene");
171 m_client = new QDeclarativeDebugMsgClient(m_connection);
173 m_process->setEnvironment(QProcess::systemEnvironment() << "QML_CONSOLE_EXTENDED=1");
174 m_process->start(QStringList() << QLatin1String(NORMALMODE) << QDeclarativeDataTest::instance()->testFile(QMLFILE));
175 if (!m_process->waitForSessionStart()) {
176 QFAIL(QString("Could not launch app. Application output: \n%1").arg(m_process->output()).toAscii());
179 m_connection->connectToHost("127.0.0.1", 3777);
180 QVERIFY(m_connection->waitForConnected());
182 QVERIFY(QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(enabled())));
185 void tst_QDebugMessageService::cleanup()
187 if (QTest::currentTestFailed())
188 qDebug() << m_process->output();
203 void tst_QDebugMessageService::retrieveDebugOutput()
205 if (m_client->logBuffer.isEmpty())
206 QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(debugOutput()));
207 QVERIFY(!m_client->logBuffer.isEmpty());
210 QString msg = QString::fromLatin1("console.log (%1:%2)").arg(
211 QUrl::fromLocalFile(QDeclarativeDataTest::instance()->testFile(QMLFILE)).toString()).arg(48);
212 QCOMPARE(m_client->logBuffer.last().toString(),
213 LogEntry(QtDebugMsg, msg).toString());
216 QTEST_MAIN(tst_QDebugMessageService)
218 #include "tst_qdebugmessageservice.moc"