Update copyright year in Nokia copyright headers.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / debugger / qdebugmessageservice / tst_qdebugmessageservice.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtDeclarative/private/qdeclarativedebugclient_p.h>
43
44 //QDeclarativeDebugTest
45 #include "../shared/debugutil_p.h"
46 #include "../../../shared/util.h"
47
48 #include <QtCore/QString>
49 #include <QtTest/QtTest>
50
51 const char *NORMALMODE = "-qmljsdebugger=port:3777,block";
52 const char *QMLFILE = "test.qml";
53
54 class QDeclarativeDebugMsgClient;
55 class tst_QDebugMessageService : public QDeclarativeDataTest
56 {
57     Q_OBJECT
58
59 public:
60     tst_QDebugMessageService();
61
62 private slots:
63     void initTestCase();
64     void cleanupTestCase();
65
66     void init();
67     void cleanup();
68
69     void retrieveDebugOutput();
70
71 private:
72     QDeclarativeDebugProcess *m_process;
73     QDeclarativeDebugMsgClient *m_client;
74     QDeclarativeDebugConnection *m_connection;
75 };
76
77 struct LogEntry {
78     LogEntry(QtMsgType _type, QString _message)
79         : type(_type), message(_message) {}
80
81     QtMsgType type;
82     QString message;
83
84     QString toString() const { return QString::number(type) + ": " + message; }
85 };
86
87 class QDeclarativeDebugMsgClient : public QDeclarativeDebugClient
88 {
89     Q_OBJECT
90 public:
91     QDeclarativeDebugMsgClient(QDeclarativeDebugConnection *connection)
92         : QDeclarativeDebugClient(QLatin1String("DebugMessages"), connection)
93     {
94     }
95
96     QList<LogEntry> logBuffer;
97
98 protected:
99     //inherited from QDeclarativeDebugClient
100     void statusChanged(Status status);
101     void messageReceived(const QByteArray &data);
102
103 signals:
104     void enabled();
105     void debugOutput();
106 };
107
108 void QDeclarativeDebugMsgClient::statusChanged(Status status)
109 {
110     if (status == Enabled) {
111         emit enabled();
112     }
113 }
114
115 void QDeclarativeDebugMsgClient::messageReceived(const QByteArray &data)
116 {
117     QDataStream ds(data);
118     QByteArray command;
119     ds >> command;
120
121     if (command == "MESSAGE") {
122         QByteArray container;
123         ds >> container;
124
125         QVERIFY(ds.atEnd());
126
127         QDataStream containerDs(container);
128         int type;
129         QByteArray message;
130         containerDs >> type >> message;
131         QVERIFY(containerDs.atEnd());
132
133         QVERIFY(type >= QtDebugMsg);
134         QVERIFY(type <= QtFatalMsg);
135
136         logBuffer << LogEntry((QtMsgType)type, QString::fromUtf8(message));
137         emit debugOutput();
138     } else {
139         QFAIL("Unknown message");
140     }
141 }
142
143 tst_QDebugMessageService::tst_QDebugMessageService()
144 {
145 }
146
147 void tst_QDebugMessageService::initTestCase()
148 {
149     QDeclarativeDataTest::initTestCase();
150     m_process = 0;
151     m_client = 0;
152     m_connection = 0;
153 }
154
155 void tst_QDebugMessageService::cleanupTestCase()
156 {
157     if (m_process)
158         delete m_process;
159
160     if (m_client)
161         delete m_client;
162
163     if (m_connection)
164         delete m_connection;
165 }
166
167 void tst_QDebugMessageService::init()
168 {
169     m_connection = new QDeclarativeDebugConnection();
170     m_process = new QDeclarativeDebugProcess(QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene");
171     m_client = new QDeclarativeDebugMsgClient(m_connection);
172
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());
177     }
178
179     m_connection->connectToHost("127.0.0.1", 3777);
180     QVERIFY(m_connection->waitForConnected());
181
182     QVERIFY(QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(enabled())));
183 }
184
185 void tst_QDebugMessageService::cleanup()
186 {
187     if (QTest::currentTestFailed())
188         qDebug() << m_process->output();
189     if (m_process)
190         delete m_process;
191
192     if (m_client)
193         delete m_client;
194
195     if (m_connection)
196         delete m_connection;
197
198     m_process = 0;
199     m_client = 0;
200     m_connection = 0;
201 }
202
203 void tst_QDebugMessageService::retrieveDebugOutput()
204 {
205     if (m_client->logBuffer.isEmpty())
206         QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(debugOutput()));
207     QVERIFY(!m_client->logBuffer.isEmpty());
208
209
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());
214 }
215
216 QTEST_MAIN(tst_QDebugMessageService)
217
218 #include "tst_qdebugmessageservice.moc"