1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
43 #include <QLibraryInfo>
45 #include "QtDeclarative/private/qv8profilerservice_p.h"
46 #include "../shared/debugutil_p.h"
47 #include "../../../shared/util.h"
50 #define STR_PORT "13774"
52 class QV8ProfilerClient : public QDeclarativeDebugClient
57 QV8ProfilerClient(QDeclarativeDebugConnection *connection)
58 : QDeclarativeDebugClient(QLatin1String("V8Profiler"), connection)
62 void startProfiling(const QString &name) {
64 QDataStream stream(&message, QIODevice::WriteOnly);
65 stream << QByteArray("V8PROFILER") << QByteArray("start") << name;
69 void stopProfiling(const QString &name) {
71 QDataStream stream(&message, QIODevice::WriteOnly);
72 stream << QByteArray("V8PROFILER") << QByteArray("stop") << name;
78 QDataStream stream(&message, QIODevice::WriteOnly);
79 stream << QByteArray("V8SNAPSHOT") << QByteArray("full");
83 void deleteSnapshots() {
85 QDataStream stream(&message, QIODevice::WriteOnly);
86 stream << QByteArray("V8SNAPSHOT") << QByteArray("delete");
90 QList<QV8ProfilerData> traceMessages;
91 QList<QByteArray> snapshotMessages;
98 void messageReceived(const QByteArray &message);
101 class tst_QV8ProfilerService : public QDeclarativeDataTest
106 tst_QV8ProfilerService()
114 QDeclarativeDebugProcess *m_process;
115 QDeclarativeDebugConnection *m_connection;
116 QV8ProfilerClient *m_client;
118 void connect(bool block);
123 void blockingConnectWithTraceEnabled();
124 void blockingConnectWithTraceDisabled();
125 void nonBlockingConnect();
129 void QV8ProfilerClient::messageReceived(const QByteArray &message)
131 QByteArray msg = message;
132 QDataStream stream(&msg, QIODevice::ReadOnly);
135 stream >> messageType;
137 QVERIFY(messageType >= 0);
138 QVERIFY(messageType < QV8ProfilerService::V8MaximumMessage);
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);
147 case QV8ProfilerService::V8Complete:
150 case QV8ProfilerService::V8SnapshotChunk: {
153 snapshotMessages.append(json);
156 case QV8ProfilerService::V8SnapshotComplete:
160 QString failMessage = QString("Unknown message type: %1").arg(messageType);
161 QFAIL(qPrintable(failMessage));
164 QVERIFY(stream.atEnd());
167 void tst_QV8ProfilerService::connect(bool block)
169 const QString executable = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene";
170 QStringList arguments;
173 arguments << QString("-qmljsdebugger=port:"STR_PORT",block");
175 arguments << QString("-qmljsdebugger=port:"STR_PORT);
177 arguments << QDeclarativeDataTest::instance()->testFile("test.qml");
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));
187 QDeclarativeDebugConnection *m_connection = new QDeclarativeDebugConnection();
188 m_client = new QV8ProfilerClient(m_connection);
190 m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
193 void tst_QV8ProfilerService::cleanup()
200 void tst_QV8ProfilerService::blockingConnectWithTraceEnabled()
203 QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
205 m_client->startProfiling("");
206 m_client->stopProfiling("");
207 if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
209 = QString("No trace received in time. App output: %1\n\n").arg(m_process->output());
210 QFAIL(qPrintable(failMsg));
214 void tst_QV8ProfilerService::blockingConnectWithTraceDisabled()
217 QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
219 m_client->startProfiling("");
220 m_client->stopProfiling("");
221 m_client->startProfiling("");
222 if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
224 = QString("No trace received in time. App output: %1\n\n").arg(m_process->output());
225 QFAIL(qPrintable(failMsg));
229 void tst_QV8ProfilerService::nonBlockingConnect()
232 QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
234 m_client->stopProfiling("");
235 m_client->startProfiling("");
236 if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
238 = QString("No trace received in time. App output: %1\n\n").arg(m_process->output());
239 QFAIL(qPrintable(failMsg));
243 void tst_QV8ProfilerService::snapshot()
246 QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
248 m_client->takeSnapshot();
249 if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(snapshot()))) {
251 = QString("No snapshot received in time. App output: %1\n\n").arg(m_process->output());
252 QFAIL(qPrintable(failMsg));
256 QTEST_MAIN(tst_QV8ProfilerService)
258 #include "tst_qv8profilerservice.moc"