a19fd4b766a4a77784d972383fea0916837d8272
[profile/ivi/qtdeclarative.git] / tests / auto / qml / debugger / qdebugmessageservice / tst_qdebugmessageservice.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtQml/private/qqmldebugclient_p.h>
43
44 //QQmlDebugTest
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 QQmlDebugMsgClient;
55 class tst_QDebugMessageService : public QQmlDataTest
56 {
57     Q_OBJECT
58
59 public:
60     tst_QDebugMessageService();
61
62     void init();
63
64 private slots:
65     void initTestCase();
66     void cleanupTestCase();
67
68     void cleanup();
69
70     void retrieveDebugOutput();
71
72 private:
73     QQmlDebugProcess *m_process;
74     QQmlDebugMsgClient *m_client;
75     QQmlDebugConnection *m_connection;
76 };
77
78 struct LogEntry {
79     LogEntry(QtMsgType _type, QString _message)
80         : type(_type), message(_message) {}
81
82     QtMsgType type;
83     QString message;
84     int line;
85     QString file;
86     QString function;
87
88     QString toString() const { return QString::number(type) + ": " + message; }
89 };
90
91 bool operator==(const LogEntry &t1, const LogEntry &t2)
92 {
93     return t1.type == t2.type && t1.message == t2.message
94             && t1.line == t2.line && t1.file == t2.file
95             && t1.function == t2.function;
96 }
97
98 class QQmlDebugMsgClient : public QQmlDebugClient
99 {
100     Q_OBJECT
101 public:
102     QQmlDebugMsgClient(QQmlDebugConnection *connection)
103         : QQmlDebugClient(QLatin1String("DebugMessages"), connection)
104     {
105     }
106
107     QList<LogEntry> logBuffer;
108
109 protected:
110     //inherited from QQmlDebugClient
111     void stateChanged(State state);
112     void messageReceived(const QByteArray &data);
113
114 signals:
115     void enabled();
116     void debugOutput();
117 };
118
119 void QQmlDebugMsgClient::stateChanged(State state)
120 {
121     if (state == Enabled) {
122         emit enabled();
123     }
124 }
125
126 void QQmlDebugMsgClient::messageReceived(const QByteArray &data)
127 {
128     QDataStream ds(data);
129     QByteArray command;
130     ds >> command;
131
132     if (command == "MESSAGE") {
133         int type;
134         QByteArray message;
135         QByteArray file;
136         QByteArray function;
137         int line;
138         ds >> type >> message >> file >> line >> function;
139         QVERIFY(ds.atEnd());
140
141         QVERIFY(type >= QtDebugMsg);
142         QVERIFY(type <= QtFatalMsg);
143
144         LogEntry entry((QtMsgType)type, QString::fromUtf8(message));
145         entry.line = line;
146         entry.file = QString::fromUtf8(file);
147         entry.function = QString::fromUtf8(function);
148         logBuffer << entry;
149         emit debugOutput();
150     } else {
151         QFAIL("Unknown message");
152     }
153 }
154
155 tst_QDebugMessageService::tst_QDebugMessageService()
156 {
157 }
158
159 void tst_QDebugMessageService::initTestCase()
160 {
161     QQmlDataTest::initTestCase();
162     m_process = 0;
163     m_client = 0;
164     m_connection = 0;
165 }
166
167 void tst_QDebugMessageService::cleanupTestCase()
168 {
169     if (m_process)
170         delete m_process;
171
172     if (m_client)
173         delete m_client;
174
175     if (m_connection)
176         delete m_connection;
177 }
178
179 void tst_QDebugMessageService::init()
180 {
181     m_connection = new QQmlDebugConnection();
182     m_process = new QQmlDebugProcess(QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene");
183     m_client = new QQmlDebugMsgClient(m_connection);
184
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());
188     }
189
190     m_connection->connectToHost("127.0.0.1", 3777);
191     QVERIFY(m_connection->waitForConnected());
192
193     QVERIFY(QQmlDebugTest::waitForSignal(m_client, SIGNAL(enabled())));
194 }
195
196 void tst_QDebugMessageService::cleanup()
197 {
198     if (QTest::currentTestFailed())
199         qDebug() << m_process->output();
200     if (m_process)
201         delete m_process;
202
203     if (m_client)
204         delete m_client;
205
206     if (m_connection)
207         delete m_connection;
208
209     m_process = 0;
210     m_client = 0;
211     m_connection = 0;
212 }
213
214 void tst_QDebugMessageService::retrieveDebugOutput()
215 {
216     init();
217
218     int maxTries = 2;
219     while ((m_client->logBuffer.size() < 2)
220            || (maxTries-- > 0))
221         QQmlDebugTest::waitForSignal(m_client, SIGNAL(debugOutput()), 1000);
222
223     QVERIFY(m_client->logBuffer.size() >= 2);
224
225     const QString path =
226             QUrl::fromLocalFile(QQmlDataTest::instance()->testFile(QMLFILE)).toString();
227     LogEntry entry1(QtDebugMsg, QLatin1String("console.log"));
228     entry1.line = 48;
229     entry1.file = path;
230     entry1.function = QLatin1String("onCompleted");
231     LogEntry entry2(QtDebugMsg, QLatin1String("console.count: 1"));
232     entry2.line = 49;
233     entry2.file = path;
234     entry2.function = QLatin1String("onCompleted");
235
236     QVERIFY(m_client->logBuffer.contains(entry1));
237     QVERIFY(m_client->logBuffer.contains(entry2));
238 }
239
240 QTEST_MAIN(tst_QDebugMessageService)
241
242 #include "tst_qdebugmessageservice.moc"