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 "QtQml/private/qqmlprofilerservice_p.h"
46 #include "debugutil_p.h"
47 #include "qqmldebugclient.h"
48 #include "../../../shared/util.h"
51 #define STR_PORT "13773"
53 class QQmlProfilerClient : public QQmlDebugClient
58 QQmlProfilerClient(QQmlDebugConnection *connection)
59 : QQmlDebugClient(QLatin1String("CanvasFrameRate"), connection)
63 QList<QQmlProfilerData> traceMessages;
65 void setTraceState(bool enabled) {
67 QDataStream stream(&message, QIODevice::WriteOnly);
76 void messageReceived(const QByteArray &message);
79 class tst_QQmlProfilerService : public QQmlDataTest
84 tst_QQmlProfilerService()
92 QQmlDebugProcess *m_process;
93 QQmlDebugConnection *m_connection;
94 QQmlProfilerClient *m_client;
96 void connect(bool block, const QString &testFile);
101 void blockingConnectWithTraceEnabled();
102 void blockingConnectWithTraceDisabled();
103 void nonBlockingConnect();
104 void profileOnExit();
107 void QQmlProfilerClient::messageReceived(const QByteArray &message)
109 QByteArray msg = message;
110 QDataStream stream(&msg, QIODevice::ReadOnly);
113 QQmlProfilerData data;
115 data.messageType = -1;
116 data.detailType = -1;
119 data.animationcount = -1;
121 stream >> data.time >> data.messageType;
123 QVERIFY(data.time >= -1);
125 switch (data.messageType) {
126 case (QQmlProfilerService::Event): {
127 stream >> data.detailType;
129 switch (data.detailType) {
130 case QQmlProfilerService::AnimationFrame: {
131 stream >> data.framerate >> data.animationcount;
132 QVERIFY(data.framerate != -1);
133 QVERIFY(data.animationcount != -1);
136 case QQmlProfilerService::FramePaint:
137 case QQmlProfilerService::Mouse:
138 case QQmlProfilerService::Key:
139 case QQmlProfilerService::StartTrace:
140 case QQmlProfilerService::EndTrace:
143 QString failMsg = QString("Unknown event type:") + data.detailType;
144 QFAIL(qPrintable(failMsg));
150 case QQmlProfilerService::Complete: {
154 case QQmlProfilerService::RangeStart: {
155 stream >> data.detailType;
156 QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
159 case QQmlProfilerService::RangeEnd: {
160 stream >> data.detailType;
161 QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
164 case QQmlProfilerService::RangeData: {
165 stream >> data.detailType >> data.detailData;
166 QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
169 case QQmlProfilerService::RangeLocation: {
170 stream >> data.detailType >> data.detailData >> data.line >> data.column;
171 QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
172 QVERIFY(data.line >= -2);
176 QString failMsg = QString("Unknown message type:") + data.messageType;
177 QFAIL(qPrintable(failMsg));
180 QVERIFY(stream.atEnd());
181 traceMessages.append(data);
184 void tst_QQmlProfilerService::connect(bool block, const QString &testFile)
186 const QString executable = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene";
187 QStringList arguments;
190 arguments << QString("-qmljsdebugger=port:"STR_PORT",block");
192 arguments << QString("-qmljsdebugger=port:"STR_PORT);
194 arguments << QQmlDataTest::instance()->testFile(testFile);
196 m_process = new QQmlDebugProcess(executable);
197 m_process->start(QStringList() << arguments);
198 if (!m_process->waitForSessionStart()) {
199 QString failMsg = QString("Could not launch app '%1'.\nApplication output:\n%2").arg(
200 executable, m_process->output());
201 QFAIL(qPrintable(failMsg));
204 QQmlDebugConnection *m_connection = new QQmlDebugConnection();
205 m_client = new QQmlProfilerClient(m_connection);
207 m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
210 void tst_QQmlProfilerService::cleanup()
217 void tst_QQmlProfilerService::blockingConnectWithTraceEnabled()
219 connect(true, "test.qml");
220 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
222 m_client->setTraceState(true);
223 m_client->setTraceState(false);
224 if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
226 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
227 QFAIL(qPrintable(failMsg));
230 QVERIFY(m_client->traceMessages.count());
231 // must start with "StartTrace"
232 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
233 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
235 // must end with "EndTrace"
236 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
237 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
240 void tst_QQmlProfilerService::blockingConnectWithTraceDisabled()
242 connect(true, "test.qml");
243 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
245 m_client->setTraceState(false);
246 m_client->setTraceState(true);
247 m_client->setTraceState(false);
248 if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
250 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
251 QFAIL(qPrintable(failMsg));
254 QVERIFY(m_client->traceMessages.count());
256 // must start with "StartTrace"
257 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
258 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
260 // must end with "EndTrace"
261 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
262 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
265 void tst_QQmlProfilerService::nonBlockingConnect()
267 connect(false, "test.qml");
268 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
270 m_client->setTraceState(true);
271 m_client->setTraceState(false);
272 if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
274 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
275 QFAIL(qPrintable(failMsg));
278 // must start with "StartTrace"
279 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
280 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
282 // must end with "EndTrace"
283 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
284 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
287 void tst_QQmlProfilerService::profileOnExit()
289 connect(true, "exit.qml");
290 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
292 m_client->setTraceState(true);
294 if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
296 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
297 QFAIL(qPrintable(failMsg));
300 // must start with "StartTrace"
301 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
302 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
304 // must end with "EndTrace"
305 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
306 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
309 QTEST_MAIN(tst_QQmlProfilerService)
311 #include "tst_qqmlprofilerservice.moc"