dece1f52d0a642c5a7d7c14a50fc42734baf4fb0
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / debugger / qdeclarativedebugtrace / tst_qdeclarativedebugtrace.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <QLibraryInfo>
44
45 #include "QtDeclarative/private/qdeclarativedebugtrace_p.h"
46 #include "../shared/debugutil_p.h"
47 #include "../../../shared/util.h"
48
49 #define PORT 13773
50 #define STR_PORT "13773"
51
52 class QDeclarativeDebugTraceClient : public QDeclarativeDebugClient
53 {
54     Q_OBJECT
55
56 public:
57     QDeclarativeDebugTraceClient(QDeclarativeDebugConnection *connection)
58         : QDeclarativeDebugClient(QLatin1String("CanvasFrameRate"), connection)
59     {
60     }
61
62     QList<QDeclarativeDebugData> traceMessages;
63
64     void setTraceStatus(bool enabled) {
65         QByteArray message;
66         QDataStream stream(&message, QIODevice::WriteOnly);
67         stream << enabled;
68         sendMessage(message);
69     }
70
71 signals:
72     void complete();
73
74 protected:
75     void messageReceived(const QByteArray &message);
76 };
77
78 class tst_QDeclarativeDebugTrace : public QDeclarativeDataTest
79 {
80     Q_OBJECT
81
82 public:
83     tst_QDeclarativeDebugTrace()
84         : m_process(0)
85         , m_connection(0)
86         , m_client(0)
87     {
88     }
89
90 private:
91     QDeclarativeDebugProcess *m_process;
92     QDeclarativeDebugConnection *m_connection;
93     QDeclarativeDebugTraceClient *m_client;
94
95     void connect(bool block);
96
97 private slots:
98     void cleanup();
99
100     void blockingConnectWithTraceEnabled();
101     void blockingConnectWithTraceDisabled();
102     void nonBlockingConnect();
103 };
104
105 void QDeclarativeDebugTraceClient::messageReceived(const QByteArray &message)
106 {
107     QByteArray msg = message;
108     QDataStream stream(&msg, QIODevice::ReadOnly);
109
110
111     QDeclarativeDebugData data;
112     data.time = -2;
113     data.messageType = -1;
114     data.detailType = -1;
115     data.line = -1;
116     data.framerate = -1;
117     data.animationcount = -1;
118
119     stream >> data.time >> data.messageType;
120
121     QVERIFY(data.time >= -1);
122
123     switch (data.messageType) {
124     case (QDeclarativeDebugTrace::Event): {
125         stream >> data.detailType;
126
127         switch (data.detailType) {
128         case QDeclarativeDebugTrace::AnimationFrame: {
129             stream >> data.framerate >> data.animationcount;
130             QVERIFY(data.framerate != -1);
131             QVERIFY(data.animationcount != -1);
132             break;
133         }
134         case QDeclarativeDebugTrace::FramePaint:
135         case QDeclarativeDebugTrace::Mouse:
136         case QDeclarativeDebugTrace::Key:
137         case QDeclarativeDebugTrace::StartTrace:
138         case QDeclarativeDebugTrace::EndTrace:
139             break;
140         default: {
141             QString failMsg = QString("Unknown event type:") + data.detailType;
142             QFAIL(qPrintable(failMsg));
143             break;
144         }
145         }
146         break;
147     }
148     case QDeclarativeDebugTrace::Complete: {
149         emit complete();
150         QVERIFY(stream.atEnd());
151         return;
152     }
153     case QDeclarativeDebugTrace::RangeStart: {
154         stream >> data.detailType;
155         QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::MaximumRangeType);
156         break;
157     }
158     case QDeclarativeDebugTrace::RangeEnd: {
159         stream >> data.detailType;
160         QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::MaximumRangeType);
161         break;
162     }
163     case QDeclarativeDebugTrace::RangeData: {
164         stream >> data.detailType >> data.detailData;
165         QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::MaximumRangeType);
166         break;
167     }
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);
172         break;
173     }
174     default:
175         QString failMsg = QString("Unknown message type:") + data.messageType;
176         QFAIL(qPrintable(failMsg));
177         break;
178     }
179     QVERIFY(stream.atEnd());
180     traceMessages.append(data);
181 }
182
183 void tst_QDeclarativeDebugTrace::connect(bool block)
184 {
185     const QString executable = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene";
186     QStringList arguments;
187
188     if (block)
189         arguments << QString("-qmljsdebugger=port:"STR_PORT",block");
190     else
191         arguments << QString("-qmljsdebugger=port:"STR_PORT);
192
193     arguments << testFile("test.qml");
194
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));
201     }
202
203     QDeclarativeDebugConnection *m_connection = new QDeclarativeDebugConnection();
204     m_client = new QDeclarativeDebugTraceClient(m_connection);
205
206     m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
207 }
208
209 void tst_QDeclarativeDebugTrace::cleanup()
210 {
211     delete m_process;
212     delete m_connection;
213     delete m_client;
214 }
215
216 void tst_QDeclarativeDebugTrace::blockingConnectWithTraceEnabled()
217 {
218     connect(true);
219     QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
220
221     m_client->setTraceStatus(true);
222     m_client->setTraceStatus(false);
223     if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
224         QString failMsg
225                 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
226         QFAIL(qPrintable(failMsg));
227     }
228
229     QVERIFY(m_client->traceMessages.count());
230     // must start with "StartTrace"
231     QCOMPARE(m_client->traceMessages.first().messageType, (int)QDeclarativeDebugTrace::Event);
232     QCOMPARE(m_client->traceMessages.first().detailType, (int)QDeclarativeDebugTrace::StartTrace);
233
234     // must end with "EndTrace"
235     QCOMPARE(m_client->traceMessages.last().messageType, (int)QDeclarativeDebugTrace::Event);
236     QCOMPARE(m_client->traceMessages.last().detailType, (int)QDeclarativeDebugTrace::EndTrace);
237 }
238
239 void tst_QDeclarativeDebugTrace::blockingConnectWithTraceDisabled()
240 {
241     connect(true);
242     QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
243
244     m_client->setTraceStatus(false);
245     m_client->setTraceStatus(true);
246     m_client->setTraceStatus(false);
247     if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
248         QString failMsg
249                 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
250         QFAIL(qPrintable(failMsg));
251     }
252
253     QVERIFY(m_client->traceMessages.count());
254
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);
258
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);
262 }
263
264 void tst_QDeclarativeDebugTrace::nonBlockingConnect()
265 {
266     connect(false);
267     QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
268
269     m_client->setTraceStatus(true);
270     m_client->setTraceStatus(false);
271     if (!QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
272         QString failMsg
273                 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
274         QFAIL(qPrintable(failMsg));
275     }
276
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);
280
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);
284 }
285
286 QTEST_MAIN(tst_QDeclarativeDebugTrace)
287
288 #include "tst_qdeclarativedebugtrace.moc"