80a8d0ce5a6a1e2bc583f4610bf9e5b2de04e46c
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / debugger / qv8profilerservice / tst_qv8profilerservice.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 <qtest.h>
43 #include <QLibraryInfo>
44
45 #include "QtDeclarative/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 QDeclarativeDebugClient
53 {
54     Q_OBJECT
55
56 public:
57     QV8ProfilerClient(QDeclarativeDebugConnection *connection)
58         : QDeclarativeDebugClient(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 complete();
95     void snapshot();
96
97 protected:
98     void messageReceived(const QByteArray &message);
99 };
100
101 class tst_QV8ProfilerService : public QDeclarativeDataTest
102 {
103     Q_OBJECT
104
105 public:
106     tst_QV8ProfilerService()
107         : m_process(0)
108         , m_connection(0)
109         , m_client(0)
110     {
111     }
112
113 private:
114     QDeclarativeDebugProcess *m_process;
115     QDeclarativeDebugConnection *m_connection;
116     QV8ProfilerClient *m_client;
117
118     void connect(bool block);
119
120 private slots:
121     void cleanup();
122
123     void blockingConnectWithTraceEnabled();
124     void blockingConnectWithTraceDisabled();
125     void nonBlockingConnect();
126     void snapshot();
127 };
128
129 void QV8ProfilerClient::messageReceived(const QByteArray &message)
130 {
131     QByteArray msg = message;
132     QDataStream stream(&msg, QIODevice::ReadOnly);
133
134     int messageType;
135     stream >> messageType;
136
137     QVERIFY(messageType >= 0);
138     QVERIFY(messageType < QV8ProfilerService::V8MaximumMessage);
139
140     switch (messageType) {
141     case QV8ProfilerService::V8Entry: {
142         QV8ProfilerData entry;
143         stream >> entry.filename >> entry.functionname >> entry.lineNumber >> entry.totalTime >> entry.selfTime >> entry.treeLevel;
144         traceMessages.append(entry);
145         break;
146     }
147     case QV8ProfilerService::V8Complete:
148         emit complete();
149         break;
150     case QV8ProfilerService::V8SnapshotChunk: {
151         QByteArray json;
152         stream >> json;
153         snapshotMessages.append(json);
154         break;
155     }
156     case QV8ProfilerService::V8SnapshotComplete:
157         emit snapshot();
158         break;
159     default:
160         QString failMessage = QString("Unknown message type: %1").arg(messageType);
161         QFAIL(qPrintable(failMessage));
162     }
163
164     QVERIFY(stream.atEnd());
165 }
166
167 void tst_QV8ProfilerService::connect(bool block)
168 {
169     const QString executable = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene";
170     QStringList arguments;
171
172     if (block)
173         arguments << QString("-qmljsdebugger=port:"STR_PORT",block");
174     else
175         arguments << QString("-qmljsdebugger=port:"STR_PORT);
176
177     arguments << QDeclarativeDataTest::instance()->testFile("test.qml");
178
179     m_process = new QDeclarativeDebugProcess(executable);
180     m_process->start(QStringList() << arguments);
181     if (!m_process->waitForSessionStart()) {
182         QString failMsg = QString("Could not launch app '%1'.\nApplication output:\n%2").arg(
183                     executable, m_process->output());
184         QFAIL(qPrintable(failMsg));
185     }
186
187     QDeclarativeDebugConnection *m_connection = new QDeclarativeDebugConnection();
188     m_client = new QV8ProfilerClient(m_connection);
189
190     m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
191 }
192
193 void tst_QV8ProfilerService::cleanup()
194 {
195     delete m_process;
196     delete m_connection;
197     delete m_client;
198 }
199
200 void tst_QV8ProfilerService::blockingConnectWithTraceEnabled()
201 {
202     connect(true);
203     QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
204
205     m_client->startProfiling("");
206     m_client->stopProfiling("");
207     if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
208         QString failMsg
209                 = QString("No trace received in time. App output: %1\n\n").arg(m_process->output());
210         QFAIL(qPrintable(failMsg));
211     }
212 }
213
214 void tst_QV8ProfilerService::blockingConnectWithTraceDisabled()
215 {
216     connect(true);
217     QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
218
219     m_client->startProfiling("");
220     m_client->stopProfiling("");
221     m_client->startProfiling("");
222     if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
223         QString failMsg
224                 = QString("No trace received in time. App output: %1\n\n").arg(m_process->output());
225         QFAIL(qPrintable(failMsg));
226     }
227 }
228
229 void tst_QV8ProfilerService::nonBlockingConnect()
230 {
231     connect(false);
232     QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
233
234     m_client->stopProfiling("");
235     m_client->startProfiling("");
236     if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
237         QString failMsg
238                 = QString("No trace received in time. App output: %1\n\n").arg(m_process->output());
239         QFAIL(qPrintable(failMsg));
240     }
241 }
242
243 void tst_QV8ProfilerService::snapshot()
244 {
245     connect(false);
246     QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
247
248     m_client->takeSnapshot();
249     if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(snapshot()))) {
250         QString failMsg
251                 = QString("No snapshot received in time. App output: %1\n\n").arg(m_process->output());
252         QFAIL(qPrintable(failMsg));
253     }
254 }
255
256 QTEST_MAIN(tst_QV8ProfilerService)
257
258 #include "tst_qv8profilerservice.moc"