15fd0c597d20c3a5fc49a3566489856da871d704
[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: http://www.qt-project.org/
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     void init(bool extendedOutput);
63
64 private slots:
65     void initTestCase();
66     void cleanupTestCase();
67
68     void cleanup();
69
70     void retrieveDebugOutput();
71     void retrieveDebugOutputExtended();
72
73 private:
74     QDeclarativeDebugProcess *m_process;
75     QDeclarativeDebugMsgClient *m_client;
76     QDeclarativeDebugConnection *m_connection;
77 };
78
79 struct LogEntry {
80     LogEntry(QtMsgType _type, QString _message)
81         : type(_type), message(_message) {}
82
83     QtMsgType type;
84     QString message;
85
86     QString toString() const { return QString::number(type) + ": " + message; }
87 };
88
89 class QDeclarativeDebugMsgClient : public QDeclarativeDebugClient
90 {
91     Q_OBJECT
92 public:
93     QDeclarativeDebugMsgClient(QDeclarativeDebugConnection *connection)
94         : QDeclarativeDebugClient(QLatin1String("DebugMessages"), connection)
95     {
96     }
97
98     QList<LogEntry> logBuffer;
99
100 protected:
101     //inherited from QDeclarativeDebugClient
102     void statusChanged(Status status);
103     void messageReceived(const QByteArray &data);
104
105 signals:
106     void enabled();
107     void debugOutput();
108 };
109
110 void QDeclarativeDebugMsgClient::statusChanged(Status status)
111 {
112     if (status == Enabled) {
113         emit enabled();
114     }
115 }
116
117 void QDeclarativeDebugMsgClient::messageReceived(const QByteArray &data)
118 {
119     QDataStream ds(data);
120     QByteArray command;
121     ds >> command;
122
123     if (command == "MESSAGE") {
124         int type;
125         QByteArray message;
126         ds >> type >> message;
127         QVERIFY(ds.atEnd());
128
129         QVERIFY(type >= QtDebugMsg);
130         QVERIFY(type <= QtFatalMsg);
131
132         logBuffer << LogEntry((QtMsgType)type, QString::fromUtf8(message));
133         emit debugOutput();
134     } else {
135         QFAIL("Unknown message");
136     }
137 }
138
139 tst_QDebugMessageService::tst_QDebugMessageService()
140 {
141 }
142
143 void tst_QDebugMessageService::initTestCase()
144 {
145     QDeclarativeDataTest::initTestCase();
146     m_process = 0;
147     m_client = 0;
148     m_connection = 0;
149 }
150
151 void tst_QDebugMessageService::cleanupTestCase()
152 {
153     if (m_process)
154         delete m_process;
155
156     if (m_client)
157         delete m_client;
158
159     if (m_connection)
160         delete m_connection;
161 }
162
163 void tst_QDebugMessageService::init(bool extendedOutput)
164 {
165     m_connection = new QDeclarativeDebugConnection();
166     m_process = new QDeclarativeDebugProcess(QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene");
167     m_client = new QDeclarativeDebugMsgClient(m_connection);
168
169     if (extendedOutput)
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());
174     }
175
176     m_connection->connectToHost("127.0.0.1", 3777);
177     QVERIFY(m_connection->waitForConnected());
178
179     QVERIFY(QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(enabled())));
180 }
181
182 void tst_QDebugMessageService::cleanup()
183 {
184     if (QTest::currentTestFailed())
185         qDebug() << m_process->output();
186     if (m_process)
187         delete m_process;
188
189     if (m_client)
190         delete m_client;
191
192     if (m_connection)
193         delete m_connection;
194
195     m_process = 0;
196     m_client = 0;
197     m_connection = 0;
198 }
199
200 void tst_QDebugMessageService::retrieveDebugOutput()
201 {
202     init(false);
203
204     int maxTries = 2;
205     while ((m_client->logBuffer.size() < 2)
206            && (maxTries-- > 0))
207         QVERIFY(QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(debugOutput())));
208
209     QCOMPARE(m_client->logBuffer.size(), 2);
210
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());
215 }
216
217 void tst_QDebugMessageService::retrieveDebugOutputExtended()
218 {
219     init(true);
220
221     int maxTries = 2;
222     while ((m_client->logBuffer.size() < 2)
223            && (maxTries-- > 0))
224         QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(debugOutput()));
225
226     QCOMPARE(m_client->logBuffer.size(), 2);
227
228     const QString path =
229             QUrl::fromLocalFile(QDeclarativeDataTest::instance()->testFile(QMLFILE)).toString();
230
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);
233
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());
238 }
239
240 QTEST_MAIN(tst_QDebugMessageService)
241
242 #include "tst_qdebugmessageservice.moc"