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 <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();
62 void init(bool extendedOutput);
66 void cleanupTestCase();
70 void retrieveDebugOutput();
71 void retrieveDebugOutputExtended();
74 QDeclarativeDebugProcess *m_process;
75 QDeclarativeDebugMsgClient *m_client;
76 QDeclarativeDebugConnection *m_connection;
80 LogEntry(QtMsgType _type, QString _message)
81 : type(_type), message(_message) {}
86 QString toString() const { return QString::number(type) + ": " + message; }
89 class QDeclarativeDebugMsgClient : public QDeclarativeDebugClient
93 QDeclarativeDebugMsgClient(QDeclarativeDebugConnection *connection)
94 : QDeclarativeDebugClient(QLatin1String("DebugMessages"), connection)
98 QList<LogEntry> logBuffer;
101 //inherited from QDeclarativeDebugClient
102 void statusChanged(Status status);
103 void messageReceived(const QByteArray &data);
110 void QDeclarativeDebugMsgClient::statusChanged(Status status)
112 if (status == Enabled) {
117 void QDeclarativeDebugMsgClient::messageReceived(const QByteArray &data)
119 QDataStream ds(data);
123 if (command == "MESSAGE") {
126 ds >> type >> message;
129 QVERIFY(type >= QtDebugMsg);
130 QVERIFY(type <= QtFatalMsg);
132 logBuffer << LogEntry((QtMsgType)type, QString::fromUtf8(message));
135 QFAIL("Unknown message");
139 tst_QDebugMessageService::tst_QDebugMessageService()
143 void tst_QDebugMessageService::initTestCase()
145 QDeclarativeDataTest::initTestCase();
151 void tst_QDebugMessageService::cleanupTestCase()
163 void tst_QDebugMessageService::init(bool extendedOutput)
165 m_connection = new QDeclarativeDebugConnection();
166 m_process = new QDeclarativeDebugProcess(QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene");
167 m_client = new QDeclarativeDebugMsgClient(m_connection);
170 m_process->setEnvironment(QProcess::systemEnvironment() << "QML_CONSOLE_EXTENDED=1");
171 m_process->start(QStringList() << QLatin1String(NORMALMODE) << QDeclarativeDataTest::instance()->testFile(QMLFILE));
172 if (!m_process->waitForSessionStart()) {
173 QFAIL(QString("Could not launch app. Application output: \n%1").arg(m_process->output()).toAscii());
176 m_connection->connectToHost("127.0.0.1", 3777);
177 QVERIFY(m_connection->waitForConnected());
179 QVERIFY(QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(enabled())));
182 void tst_QDebugMessageService::cleanup()
184 if (QTest::currentTestFailed())
185 qDebug() << m_process->output();
200 void tst_QDebugMessageService::retrieveDebugOutput()
205 while ((m_client->logBuffer.size() < 2)
207 QVERIFY(QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(debugOutput())));
209 QCOMPARE(m_client->logBuffer.size(), 2);
211 QCOMPARE(m_client->logBuffer.at(0).toString(),
212 LogEntry(QtDebugMsg, QLatin1String("console.log")).toString());
213 QCOMPARE(m_client->logBuffer.at(1).toString(),
214 LogEntry(QtDebugMsg, QLatin1String("console.count: 1")).toString());
217 void tst_QDebugMessageService::retrieveDebugOutputExtended()
222 while ((m_client->logBuffer.size() < 2)
224 QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(debugOutput()));
226 QCOMPARE(m_client->logBuffer.size(), 2);
229 QUrl::fromLocalFile(QDeclarativeDataTest::instance()->testFile(QMLFILE)).toString();
231 QString logMsg = QString::fromLatin1("console.log (%1:%2)").arg(path).arg(48);
232 QString countMsg = QString::fromLatin1("console.count: 1 (%1:%2)").arg(path).arg(49);
234 QCOMPARE(m_client->logBuffer.at(0).toString(),
235 LogEntry(QtDebugMsg, logMsg).toString());
236 QCOMPARE(m_client->logBuffer.at(1).toString(),
237 LogEntry(QtDebugMsg, countMsg).toString());
240 QTEST_MAIN(tst_QDebugMessageService)
242 #include "tst_qdebugmessageservice.moc"