QmlDebugging: Remove QQmlDebugClient
[profile/ivi/qtdeclarative.git] / tests / auto / qml / debugger / qv8profilerservice / tst_qv8profilerservice.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 <qtest.h>
43 #include <QLibraryInfo>
44
45 #include "QtQml/private/qv8profilerservice_p.h"
46 #include "debugutil_p.h"
47 #include "qqmldebugclient.h"
48 #include "../../../shared/util.h"
49
50 #define PORT 13774
51 #define STR_PORT "13774"
52
53 class QV8ProfilerClient : public QQmlDebugClient
54 {
55     Q_OBJECT
56
57 public:
58     QV8ProfilerClient(QQmlDebugConnection *connection)
59         : QQmlDebugClient(QLatin1String("V8Profiler"), connection)
60     {
61     }
62
63     void startProfiling(const QString &name) {
64         QByteArray message;
65         QDataStream stream(&message, QIODevice::WriteOnly);
66         stream << QByteArray("V8PROFILER") << QByteArray("start") << name;
67         sendMessage(message);
68     }
69
70     void stopProfiling(const QString &name) {
71         QByteArray message;
72         QDataStream stream(&message, QIODevice::WriteOnly);
73         stream << QByteArray("V8PROFILER") << QByteArray("stop") << name;
74         sendMessage(message);
75     }
76
77     void takeSnapshot() {
78         QByteArray message;
79         QDataStream stream(&message, QIODevice::WriteOnly);
80         stream << QByteArray("V8SNAPSHOT") << QByteArray("full");
81         sendMessage(message);
82     }
83
84     void deleteSnapshots() {
85         QByteArray message;
86         QDataStream stream(&message, QIODevice::WriteOnly);
87         stream << QByteArray("V8SNAPSHOT") << QByteArray("delete");
88         sendMessage(message);
89     }
90
91     QList<QV8ProfilerData> traceMessages;
92     QList<QByteArray> snapshotMessages;
93
94 signals:
95     void started();
96     void complete();
97     void snapshot();
98
99 protected:
100     void messageReceived(const QByteArray &message);
101 };
102
103 class tst_QV8ProfilerService : public QQmlDataTest
104 {
105     Q_OBJECT
106
107 public:
108     tst_QV8ProfilerService()
109         : m_process(0)
110         , m_connection(0)
111         , m_client(0)
112     {
113     }
114
115 private:
116     QQmlDebugProcess *m_process;
117     QQmlDebugConnection *m_connection;
118     QV8ProfilerClient *m_client;
119
120     void connect(bool block, const QString &testFile);
121
122 private slots:
123     void cleanup();
124
125     void blockingConnectWithTraceEnabled();
126     void blockingConnectWithTraceDisabled();
127     void nonBlockingConnect();
128     void snapshot();
129     void profileOnExit();
130     void console();
131 };
132
133 void QV8ProfilerClient::messageReceived(const QByteArray &message)
134 {
135     QByteArray msg = message;
136     QDataStream stream(&msg, QIODevice::ReadOnly);
137
138     int messageType;
139     stream >> messageType;
140
141     QVERIFY(messageType >= 0);
142     QVERIFY(messageType < QV8ProfilerService::V8MaximumMessage);
143
144     switch (messageType) {
145     case QV8ProfilerService::V8Entry: {
146         QV8ProfilerData entry;
147         stream >> entry.filename >> entry.functionname >> entry.lineNumber >> entry.totalTime >> entry.selfTime >> entry.treeLevel;
148         traceMessages.append(entry);
149         break;
150     }
151     case QV8ProfilerService::V8Complete:
152         emit complete();
153         break;
154     case QV8ProfilerService::V8SnapshotChunk: {
155         QByteArray json;
156         stream >> json;
157         snapshotMessages.append(json);
158         break;
159     }
160     case QV8ProfilerService::V8SnapshotComplete:
161         emit snapshot();
162         break;
163     case QV8ProfilerService::V8Started:
164         emit started();
165         break;
166     default:
167         QString failMessage = QString("Unknown message type: %1").arg(messageType);
168         QFAIL(qPrintable(failMessage));
169     }
170
171     QVERIFY(stream.atEnd());
172 }
173
174 void tst_QV8ProfilerService::connect(bool block, const QString &testFile)
175 {
176     const QString executable = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene";
177     QStringList arguments;
178
179     if (block)
180         arguments << QString("-qmljsdebugger=port:"STR_PORT",block");
181     else
182         arguments << QString("-qmljsdebugger=port:"STR_PORT);
183
184     arguments << QQmlDataTest::instance()->testFile(testFile);
185
186     m_process = new QQmlDebugProcess(executable);
187     m_process->start(QStringList() << arguments);
188     if (!m_process->waitForSessionStart()) {
189         QString failMsg = QString("Could not launch app '%1'.\nApplication output:\n%2").arg(
190                     executable, m_process->output());
191         QFAIL(qPrintable(failMsg));
192     }
193
194     QQmlDebugConnection *m_connection = new QQmlDebugConnection();
195     m_client = new QV8ProfilerClient(m_connection);
196
197     m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
198 }
199
200 void tst_QV8ProfilerService::cleanup()
201 {
202     if (QTest::currentTestFailed())
203         qDebug() << "Application Output:" << m_process->output();
204
205     delete m_process;
206     delete m_connection;
207     delete m_client;
208 }
209
210 void tst_QV8ProfilerService::blockingConnectWithTraceEnabled()
211 {
212     connect(true, "test.qml");
213     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
214
215     m_client->startProfiling("");
216     QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
217              "No start signal received in time.");
218     m_client->stopProfiling("");
219     QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
220              "No trace received in time.");
221 }
222
223 void tst_QV8ProfilerService::blockingConnectWithTraceDisabled()
224 {
225     connect(true, "test.qml");
226     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
227
228     m_client->stopProfiling("");
229     if (QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()), 1000)) {
230         QString failMsg
231                 = QString("Unexpected trace received! App output: %1\n\n").arg(m_process->output());
232         QFAIL(qPrintable(failMsg));
233     }
234     m_client->startProfiling("");
235     QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
236              "No start signal received in time.");
237     m_client->stopProfiling("");
238     QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
239              "No trace received in time.");
240 }
241
242 void tst_QV8ProfilerService::nonBlockingConnect()
243 {
244     connect(false, "test.qml");
245     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
246
247     m_client->startProfiling("");
248     QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
249              "No start signal received in time.");
250     m_client->stopProfiling("");
251     QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
252              "No trace received in time.");
253 }
254
255 void tst_QV8ProfilerService::snapshot()
256 {
257     connect(false, "test.qml");
258     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
259
260     m_client->takeSnapshot();
261     QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(snapshot())),
262              "No trace received in time.");
263 }
264
265 void tst_QV8ProfilerService::profileOnExit()
266 {
267     connect(true, "exit.qml");
268     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
269
270     m_client->startProfiling("");
271     QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
272              "No start signal received in time.");
273
274     QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
275              "No trace received in time.");
276     //QVERIFY(!m_client->traceMessages.isEmpty());
277 }
278
279 void tst_QV8ProfilerService::console()
280 {
281     connect(true, "console.qml");
282     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
283
284     m_client->stopProfiling("");
285
286     QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
287              "No start signal received in time.");
288     QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
289              "No trace received in time.");
290     QVERIFY(!m_client->traceMessages.isEmpty());
291 }
292
293 QTEST_MAIN(tst_QV8ProfilerService)
294
295 #include "tst_qv8profilerservice.moc"