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 "../shared/debugutil_p.h"
47 #include "../../../shared/util.h"
50 #define STR_PORT "13773"
52 class QQmlProfilerClient : public QQmlDebugClient
57 QQmlProfilerClient(QQmlDebugConnection *connection)
58 : QQmlDebugClient(QLatin1String("CanvasFrameRate"), connection)
62 QList<QQmlProfilerData> traceMessages;
64 void setTraceState(bool enabled) {
66 QDataStream stream(&message, QIODevice::WriteOnly);
75 void messageReceived(const QByteArray &message);
78 class tst_QQmlProfilerService : public QQmlDataTest
83 tst_QQmlProfilerService()
91 QQmlDebugProcess *m_process;
92 QQmlDebugConnection *m_connection;
93 QQmlProfilerClient *m_client;
95 void connect(bool block, const QString &testFile);
100 void blockingConnectWithTraceEnabled();
101 void blockingConnectWithTraceDisabled();
102 void nonBlockingConnect();
103 void profileOnExit();
106 void QQmlProfilerClient::messageReceived(const QByteArray &message)
108 QByteArray msg = message;
109 QDataStream stream(&msg, QIODevice::ReadOnly);
112 QQmlProfilerData data;
114 data.messageType = -1;
115 data.detailType = -1;
118 data.animationcount = -1;
120 stream >> data.time >> data.messageType;
122 QVERIFY(data.time >= -1);
124 switch (data.messageType) {
125 case (QQmlProfilerService::Event): {
126 stream >> data.detailType;
128 switch (data.detailType) {
129 case QQmlProfilerService::AnimationFrame: {
130 stream >> data.framerate >> data.animationcount;
131 QVERIFY(data.framerate != -1);
132 QVERIFY(data.animationcount != -1);
135 case QQmlProfilerService::FramePaint:
136 case QQmlProfilerService::Mouse:
137 case QQmlProfilerService::Key:
138 case QQmlProfilerService::StartTrace:
139 case QQmlProfilerService::EndTrace:
142 QString failMsg = QString("Unknown event type:") + data.detailType;
143 QFAIL(qPrintable(failMsg));
149 case QQmlProfilerService::Complete: {
153 case QQmlProfilerService::RangeStart: {
154 stream >> data.detailType;
155 QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
158 case QQmlProfilerService::RangeEnd: {
159 stream >> data.detailType;
160 QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
163 case QQmlProfilerService::RangeData: {
164 stream >> data.detailType >> data.detailData;
165 QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
168 case QQmlProfilerService::RangeLocation: {
169 stream >> data.detailType >> data.detailData >> data.line >> data.column;
170 QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
171 QVERIFY(data.line >= -2);
175 QString failMsg = QString("Unknown message type:") + data.messageType;
176 QFAIL(qPrintable(failMsg));
179 QVERIFY(stream.atEnd());
180 traceMessages.append(data);
183 void tst_QQmlProfilerService::connect(bool block, const QString &testFile)
185 const QString executable = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene";
186 QStringList arguments;
189 arguments << QString("-qmljsdebugger=port:"STR_PORT",block");
191 arguments << QString("-qmljsdebugger=port:"STR_PORT);
193 arguments << QQmlDataTest::instance()->testFile(testFile);
195 m_process = new QQmlDebugProcess(executable);
196 m_process->start(QStringList() << arguments);
197 if (!m_process->waitForSessionStart()) {
198 QString failMsg = QString("Could not launch app '%1'.\nApplication output:\n%2").arg(
199 executable, m_process->output());
200 QFAIL(qPrintable(failMsg));
203 QQmlDebugConnection *m_connection = new QQmlDebugConnection();
204 m_client = new QQmlProfilerClient(m_connection);
206 m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
209 void tst_QQmlProfilerService::cleanup()
216 void tst_QQmlProfilerService::blockingConnectWithTraceEnabled()
218 connect(true, "test.qml");
219 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
221 m_client->setTraceState(true);
222 m_client->setTraceState(false);
223 if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
225 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
226 QFAIL(qPrintable(failMsg));
229 QVERIFY(m_client->traceMessages.count());
230 // must start with "StartTrace"
231 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
232 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
234 // must end with "EndTrace"
235 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
236 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
239 void tst_QQmlProfilerService::blockingConnectWithTraceDisabled()
241 connect(true, "test.qml");
242 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
244 m_client->setTraceState(false);
245 m_client->setTraceState(true);
246 m_client->setTraceState(false);
247 if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
249 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
250 QFAIL(qPrintable(failMsg));
253 QVERIFY(m_client->traceMessages.count());
255 // must start with "StartTrace"
256 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
257 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
259 // must end with "EndTrace"
260 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
261 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
264 void tst_QQmlProfilerService::nonBlockingConnect()
266 connect(false, "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 // must start with "StartTrace"
278 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
279 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
281 // must end with "EndTrace"
282 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
283 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
286 void tst_QQmlProfilerService::profileOnExit()
288 connect(true, "exit.qml");
289 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
291 m_client->setTraceState(true);
293 if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
295 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
296 QFAIL(qPrintable(failMsg));
299 // must start with "StartTrace"
300 QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
301 QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
303 // must end with "EndTrace"
304 QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
305 QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
308 QTEST_MAIN(tst_QQmlProfilerService)
310 #include "tst_qqmlprofilerservice.moc"