1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QLibraryInfo>
45 #include "QtDeclarative/private/qdeclarativedebugtrace_p.h"
46 #include "../shared/debugutil_p.h"
47 #include "../../shared/util.h"
50 #define STR_PORT "13773"
52 class QDeclarativeDebugTraceClient : public QDeclarativeDebugClient
57 QDeclarativeDebugTraceClient(QDeclarativeDebugConnection *connection)
58 : QDeclarativeDebugClient(QLatin1String("CanvasFrameRate"), connection)
62 QList<QDeclarativeDebugData> traceMessages;
64 void setTraceStatus(bool enabled) {
66 QDataStream stream(&message, QIODevice::WriteOnly);
75 void messageReceived(const QByteArray &message);
78 class tst_QDeclarativeDebugTrace : public QObject
83 tst_QDeclarativeDebugTrace()
91 QDeclarativeDebugProcess *m_process;
92 QDeclarativeDebugConnection *m_connection;
93 QDeclarativeDebugTraceClient *m_client;
95 void connect(bool block);
100 void blockingConnectWithTraceEnabled();
101 void blockingConnectWithTraceDisabled();
102 void nonBlockingConnect();
105 void QDeclarativeDebugTraceClient::messageReceived(const QByteArray &message)
107 QByteArray msg = message;
108 QDataStream stream(&msg, QIODevice::ReadOnly);
111 QDeclarativeDebugData data;
113 data.messageType = -1;
114 data.detailType = -1;
117 data.animationcount = -1;
119 stream >> data.time >> data.messageType;
121 QVERIFY(data.time >= -1);
123 switch (data.messageType) {
124 case (QDeclarativeDebugTrace::Event): {
125 stream >> data.detailType;
127 switch (data.detailType) {
128 case QDeclarativeDebugTrace::AnimationFrame: {
129 stream >> data.framerate >> data.animationcount;
130 QVERIFY(data.framerate != -1);
131 QVERIFY(data.animationcount != -1);
134 case QDeclarativeDebugTrace::FramePaint:
135 case QDeclarativeDebugTrace::Mouse:
136 case QDeclarativeDebugTrace::Key:
137 case QDeclarativeDebugTrace::StartTrace:
138 case QDeclarativeDebugTrace::EndTrace:
141 QString failMsg = QString("Unknown event type:") + data.detailType;
142 QFAIL(qPrintable(failMsg));
148 case QDeclarativeDebugTrace::Complete: {
150 QVERIFY(stream.atEnd());
153 case QDeclarativeDebugTrace::RangeStart: {
154 stream >> data.detailType;
155 QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::MaximumRangeType);
158 case QDeclarativeDebugTrace::RangeEnd: {
159 stream >> data.detailType;
160 QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::MaximumRangeType);
163 case QDeclarativeDebugTrace::RangeData: {
164 stream >> data.detailType >> data.detailData;
165 QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::MaximumRangeType);
168 case QDeclarativeDebugTrace::RangeLocation: {
169 stream >> data.detailType >> data.detailData >> data.line;
170 QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::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_QDeclarativeDebugTrace::connect(bool block)
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 << QString(TESTDATA(QLatin1String("test.qml")));
195 m_process = new QDeclarativeDebugProcess(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 QDeclarativeDebugConnection *m_connection = new QDeclarativeDebugConnection();
204 m_client = new QDeclarativeDebugTraceClient(m_connection);
206 m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
209 void tst_QDeclarativeDebugTrace::cleanup()
216 void tst_QDeclarativeDebugTrace::blockingConnectWithTraceEnabled()
219 QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
221 m_client->setTraceStatus(true);
222 m_client->setTraceStatus(false);
223 if (!QDeclarativeDebugTest::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 for (int i = 0; i < m_client->traceMessages.size(); ++i)
230 qDebug() << m_client->traceMessages.at(i).messageType << m_client->traceMessages.at(i).detailType;
232 // must start with "StartTrace"
233 QCOMPARE(m_client->traceMessages.first().messageType, (int)QDeclarativeDebugTrace::Event);
234 QCOMPARE(m_client->traceMessages.first().detailType, (int)QDeclarativeDebugTrace::StartTrace);
236 // must end with "EndTrace"
237 QCOMPARE(m_client->traceMessages.last().messageType, (int)QDeclarativeDebugTrace::Event);
238 QCOMPARE(m_client->traceMessages.last().detailType, (int)QDeclarativeDebugTrace::EndTrace);
241 void tst_QDeclarativeDebugTrace::blockingConnectWithTraceDisabled()
244 QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
246 m_client->setTraceStatus(false);
247 m_client->setTraceStatus(true);
248 m_client->setTraceStatus(false);
249 if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
251 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
252 QFAIL(qPrintable(failMsg));
255 // must start with "StartTrace"
256 QCOMPARE(m_client->traceMessages.first().messageType, (int)QDeclarativeDebugTrace::Event);
257 QCOMPARE(m_client->traceMessages.first().detailType, (int)QDeclarativeDebugTrace::StartTrace);
259 // must end with "EndTrace"
260 QCOMPARE(m_client->traceMessages.last().messageType, (int)QDeclarativeDebugTrace::Event);
261 QCOMPARE(m_client->traceMessages.last().detailType, (int)QDeclarativeDebugTrace::EndTrace);
264 void tst_QDeclarativeDebugTrace::nonBlockingConnect()
267 QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
269 m_client->setTraceStatus(true);
270 m_client->setTraceStatus(false);
271 if (!QDeclarativeDebugTest::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)QDeclarativeDebugTrace::Event);
279 QCOMPARE(m_client->traceMessages.first().detailType, (int)QDeclarativeDebugTrace::StartTrace);
281 // must end with "EndTrace"
282 QCOMPARE(m_client->traceMessages.last().messageType, (int)QDeclarativeDebugTrace::Event);
283 QCOMPARE(m_client->traceMessages.last().detailType, (int)QDeclarativeDebugTrace::EndTrace);
286 QTEST_MAIN(tst_QDeclarativeDebugTrace)
288 #include "tst_qdeclarativedebugtrace.moc"