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