1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
43 #include <QLibraryInfo>
45 #include "debugutil_p.h"
46 #include "qqmldebugclient.h"
47 #include "../../../shared/util.h"
50 #define STR_PORT "13773"
52 struct QQmlProfilerData
59 QString detailData; //used by RangeData and RangeLocation
60 int line; //used by RangeLocation
61 int column; //used by RangeLocation
62 int framerate; //used by animation events
63 int animationcount; //used by animation events
65 QByteArray toByteArray() const;
68 class QQmlProfilerClient : public QQmlDebugClient
79 Complete, // end of transmission
99 Binding, //running a binding
100 HandlingSignal, //running a signal handler
105 QQmlProfilerClient(QQmlDebugConnection *connection)
106 : QQmlDebugClient(QLatin1String("CanvasFrameRate"), connection)
110 QList<QQmlProfilerData> traceMessages;
112 void setTraceState(bool enabled) {
114 QDataStream stream(&message, QIODevice::WriteOnly);
116 sendMessage(message);
123 void messageReceived(const QByteArray &message);
126 class tst_QQmlProfilerService : public QQmlDataTest
131 tst_QQmlProfilerService()
139 QQmlDebugProcess *m_process;
140 QQmlDebugConnection *m_connection;
141 QQmlProfilerClient *m_client;
143 void connect(bool block, const QString &testFile);
148 void blockingConnectWithTraceEnabled();
149 void blockingConnectWithTraceDisabled();
150 void nonBlockingConnect();
151 void profileOnExit();
154 void QQmlProfilerClient::messageReceived(const QByteArray &message)
156 QByteArray msg = message;
157 QDataStream stream(&msg, QIODevice::ReadOnly);
160 QQmlProfilerData data;
162 data.messageType = -1;
163 data.detailType = -1;
166 data.animationcount = -1;
168 stream >> data.time >> data.messageType;
170 QVERIFY(data.time >= -1);
172 switch (data.messageType) {
173 case (QQmlProfilerClient::Event): {
174 stream >> data.detailType;
176 switch (data.detailType) {
177 case QQmlProfilerClient::AnimationFrame: {
178 stream >> data.framerate >> data.animationcount;
179 QVERIFY(data.framerate != -1);
180 QVERIFY(data.animationcount != -1);
183 case QQmlProfilerClient::FramePaint:
184 case QQmlProfilerClient::Mouse:
185 case QQmlProfilerClient::Key:
186 case QQmlProfilerClient::StartTrace:
187 case QQmlProfilerClient::EndTrace:
190 QString failMsg = QString("Unknown event type:") + data.detailType;
191 QFAIL(qPrintable(failMsg));
197 case QQmlProfilerClient::Complete: {
201 case QQmlProfilerClient::RangeStart: {
202 stream >> data.detailType;
203 QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerClient::MaximumRangeType);
206 case QQmlProfilerClient::RangeEnd: {
207 stream >> data.detailType;
208 QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerClient::MaximumRangeType);
211 case QQmlProfilerClient::RangeData: {
212 stream >> data.detailType >> data.detailData;
213 QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerClient::MaximumRangeType);
216 case QQmlProfilerClient::RangeLocation: {
217 stream >> data.detailType >> data.detailData >> data.line >> data.column;
218 QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerClient::MaximumRangeType);
219 QVERIFY(data.line >= -2);
223 QString failMsg = QString("Unknown message type:") + data.messageType;
224 QFAIL(qPrintable(failMsg));
227 QVERIFY(stream.atEnd());
228 traceMessages.append(data);
231 void tst_QQmlProfilerService::connect(bool block, const QString &testFile)
233 const QString executable = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene";
234 QStringList arguments;
237 arguments << QString("-qmljsdebugger=port:"STR_PORT",block");
239 arguments << QString("-qmljsdebugger=port:"STR_PORT);
241 arguments << QQmlDataTest::instance()->testFile(testFile);
243 m_process = new QQmlDebugProcess(executable);
244 m_process->start(QStringList() << arguments);
245 if (!m_process->waitForSessionStart()) {
246 QString failMsg = QString("Could not launch app '%1'.\nApplication output:\n%2").arg(
247 executable, m_process->output());
248 QFAIL(qPrintable(failMsg));
251 QQmlDebugConnection *m_connection = new QQmlDebugConnection();
252 m_client = new QQmlProfilerClient(m_connection);
254 m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
257 void tst_QQmlProfilerService::cleanup()
264 void tst_QQmlProfilerService::blockingConnectWithTraceEnabled()
266 connect(true, "test.qml");
267 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
269 m_client->setTraceState(true);
270 m_client->setTraceState(false);
271 if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
273 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
274 QFAIL(qPrintable(failMsg));
277 QVERIFY(m_client->traceMessages.count());
278 // must start with "StartTrace"
279 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
280 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
282 // must end with "EndTrace"
283 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
284 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
287 void tst_QQmlProfilerService::blockingConnectWithTraceDisabled()
289 connect(true, "test.qml");
290 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
292 m_client->setTraceState(false);
293 m_client->setTraceState(true);
294 m_client->setTraceState(false);
295 if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
297 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
298 QFAIL(qPrintable(failMsg));
301 QVERIFY(m_client->traceMessages.count());
303 // must start with "StartTrace"
304 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
305 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
307 // must end with "EndTrace"
308 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
309 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
312 void tst_QQmlProfilerService::nonBlockingConnect()
314 connect(false, "test.qml");
315 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
317 m_client->setTraceState(true);
318 m_client->setTraceState(false);
319 if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
321 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
322 QFAIL(qPrintable(failMsg));
325 // must start with "StartTrace"
326 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
327 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
329 // must end with "EndTrace"
330 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
331 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
334 void tst_QQmlProfilerService::profileOnExit()
336 connect(true, "exit.qml");
337 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
339 m_client->setTraceState(true);
341 if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
343 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
344 QFAIL(qPrintable(failMsg));
347 // must start with "StartTrace"
348 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
349 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
351 // must end with "EndTrace"
352 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
353 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
356 QTEST_MAIN(tst_QQmlProfilerService)
358 #include "tst_qqmlprofilerservice.moc"