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;
99 void connectWithTraceEnabled();
100 void connectWithTraceDisabled();
103 void QDeclarativeDebugTraceClient::messageReceived(const QByteArray &message)
105 QByteArray msg = message;
106 QDataStream stream(&msg, QIODevice::ReadOnly);
109 QDeclarativeDebugData data;
111 data.messageType = -1;
112 data.detailType = -1;
115 data.animationcount = -1;
117 stream >> data.time >> data.messageType;
119 QVERIFY(data.time >= -1);
121 switch (data.messageType) {
122 case (QDeclarativeDebugTrace::Event): {
123 stream >> data.detailType;
125 switch (data.detailType) {
126 case QDeclarativeDebugTrace::AnimationFrame: {
127 stream >> data.framerate >> data.animationcount;
128 QVERIFY(data.framerate != -1);
129 QVERIFY(data.animationcount != -1);
132 case QDeclarativeDebugTrace::FramePaint:
133 case QDeclarativeDebugTrace::Mouse:
134 case QDeclarativeDebugTrace::Key:
135 case QDeclarativeDebugTrace::StartTrace:
136 case QDeclarativeDebugTrace::EndTrace:
139 QString failMsg = QString("Unknown event type:") + data.detailType;
140 QFAIL(qPrintable(failMsg));
146 case QDeclarativeDebugTrace::Complete: {
148 QVERIFY(stream.atEnd());
151 case QDeclarativeDebugTrace::RangeStart: {
152 stream >> data.detailType;
153 QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::MaximumRangeType);
156 case QDeclarativeDebugTrace::RangeEnd: {
157 stream >> data.detailType;
158 QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::MaximumRangeType);
161 case QDeclarativeDebugTrace::RangeData: {
162 stream >> data.detailType >> data.detailData;
163 QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::MaximumRangeType);
166 case QDeclarativeDebugTrace::RangeLocation: {
167 stream >> data.detailType >> data.detailData >> data.line;
168 QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::MaximumRangeType);
169 QVERIFY(data.line >= -2);
173 QString failMsg = QString("Unknown message type:") + data.messageType;
174 QFAIL(qPrintable(failMsg));
177 QVERIFY(stream.atEnd());
178 traceMessages.append(data);
181 void tst_QDeclarativeDebugTrace::init()
183 const QString executable = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene";
184 QStringList arguments;
185 arguments << QString("-qmljsdebugger=port:"STR_PORT",block");
186 arguments << QString(TESTDATA(QLatin1String("test.qml")));
188 m_process = new QDeclarativeDebugProcess(executable);
189 m_process->start(QStringList() << arguments);
190 if (!m_process->waitForSessionStart()) {
191 QString failMsg = QString("Could not launch app '%1'.\nApplication output:\n%2").arg(
192 executable, m_process->output());
193 QFAIL(qPrintable(failMsg));
196 QDeclarativeDebugConnection *m_connection = new QDeclarativeDebugConnection();
197 m_client = new QDeclarativeDebugTraceClient(m_connection);
199 m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
202 void tst_QDeclarativeDebugTrace::cleanup()
209 void tst_QDeclarativeDebugTrace::connectWithTraceEnabled()
211 QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
212 m_client->setTraceStatus(true);
213 m_client->setTraceStatus(false);
214 if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
216 = QString("No trace received in time. App output: \n\n").arg(m_process->output());
217 QFAIL(qPrintable(failMsg));
220 // must start with "StartTrace"
221 QCOMPARE(m_client->traceMessages.first().messageType, (int)QDeclarativeDebugTrace::Event);
222 QCOMPARE(m_client->traceMessages.first().detailType, (int)QDeclarativeDebugTrace::StartTrace);
224 // must end with "EndTrace"
225 QCOMPARE(m_client->traceMessages.last().messageType, (int)QDeclarativeDebugTrace::Event);
226 QCOMPARE(m_client->traceMessages.last().detailType, (int)QDeclarativeDebugTrace::EndTrace);
229 void tst_QDeclarativeDebugTrace::connectWithTraceDisabled()
231 QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
232 m_client->setTraceStatus(false);
233 m_client->setTraceStatus(true);
234 m_client->setTraceStatus(false);
235 if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
237 = QString("No trace received in time. App output: \n\n").arg(m_process->output());
238 QFAIL(qPrintable(failMsg));
241 // must start with "StartTrace"
242 QCOMPARE(m_client->traceMessages.first().messageType, (int)QDeclarativeDebugTrace::Event);
243 QCOMPARE(m_client->traceMessages.first().detailType, (int)QDeclarativeDebugTrace::StartTrace);
245 // must end with "EndTrace"
246 QCOMPARE(m_client->traceMessages.last().messageType, (int)QDeclarativeDebugTrace::Event);
247 QCOMPARE(m_client->traceMessages.last().detailType, (int)QDeclarativeDebugTrace::EndTrace);
250 QTEST_MAIN(tst_QDeclarativeDebugTrace)
252 #include "tst_qdeclarativedebugtrace.moc"