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 QVERIFY2(m_process->waitForSessionStart(), "Could not launch application, or did not get 'Waiting for connection'.");
247 QQmlDebugConnection *m_connection = new QQmlDebugConnection();
248 m_client = new QQmlProfilerClient(m_connection);
250 m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
253 void tst_QQmlProfilerService::cleanup()
255 if (QTest::currentTestFailed()) {
256 qDebug() << "Process State:" << m_process->state();
257 qDebug() << "Application Output:" << m_process->output();
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 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())), "No trace received in time.");
273 QVERIFY(m_client->traceMessages.count());
274 // must start with "StartTrace"
275 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
276 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
278 // must end with "EndTrace"
279 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
280 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
283 void tst_QQmlProfilerService::blockingConnectWithTraceDisabled()
285 connect(true, "test.qml");
286 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
288 m_client->setTraceState(false);
289 m_client->setTraceState(true);
290 m_client->setTraceState(false);
291 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())), "No trace received in time.");
293 QVERIFY(m_client->traceMessages.count());
295 // must start with "StartTrace"
296 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
297 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
299 // must end with "EndTrace"
300 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
301 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
304 void tst_QQmlProfilerService::nonBlockingConnect()
306 connect(false, "test.qml");
307 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
309 m_client->setTraceState(true);
310 m_client->setTraceState(false);
311 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())), "No trace received in time.");
313 // must start with "StartTrace"
314 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
315 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
317 // must end with "EndTrace"
318 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
319 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
322 void tst_QQmlProfilerService::profileOnExit()
324 connect(true, "exit.qml");
325 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
327 m_client->setTraceState(true);
329 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())), "No trace received in time.");
331 // must start with "StartTrace"
332 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
333 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
335 // must end with "EndTrace"
336 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
337 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
340 QTEST_MAIN(tst_QQmlProfilerService)
342 #include "tst_qqmlprofilerservice.moc"