1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
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, this);
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"