972c3ac6f47586d452e93f19965ddd17c510ee34
[profile/ivi/qtdeclarative.git] / tests / auto / qml / debugger / qqmlprofilerservice / tst_qqmlprofilerservice.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <QLibraryInfo>
44
45 #include "debugutil_p.h"
46 #include "qqmldebugclient.h"
47 #include "../../../shared/util.h"
48
49 #define PORT 13773
50 #define STR_PORT "13773"
51
52 struct QQmlProfilerData
53 {
54     qint64 time;
55     int messageType;
56     int detailType;
57
58     //###
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
64
65     QByteArray toByteArray() const;
66 };
67
68 class QQmlProfilerClient : public QQmlDebugClient
69 {
70     Q_OBJECT
71
72 public:
73     enum Message {
74         Event,
75         RangeStart,
76         RangeData,
77         RangeLocation,
78         RangeEnd,
79         Complete, // end of transmission
80
81         MaximumMessage
82     };
83
84     enum EventType {
85         FramePaint,
86         Mouse,
87         Key,
88         AnimationFrame,
89         EndTrace,
90         StartTrace,
91
92         MaximumEventType
93     };
94
95     enum RangeType {
96         Painting,
97         Compiling,
98         Creating,
99         Binding,            //running a binding
100         HandlingSignal,     //running a signal handler
101
102         MaximumRangeType
103     };
104
105     QQmlProfilerClient(QQmlDebugConnection *connection)
106         : QQmlDebugClient(QLatin1String("CanvasFrameRate"), connection)
107     {
108     }
109
110     QList<QQmlProfilerData> traceMessages;
111
112     void setTraceState(bool enabled) {
113         QByteArray message;
114         QDataStream stream(&message, QIODevice::WriteOnly);
115         stream << enabled;
116         sendMessage(message);
117     }
118
119 signals:
120     void complete();
121
122 protected:
123     void messageReceived(const QByteArray &message);
124 };
125
126 class tst_QQmlProfilerService : public QQmlDataTest
127 {
128     Q_OBJECT
129
130 public:
131     tst_QQmlProfilerService()
132         : m_process(0)
133         , m_connection(0)
134         , m_client(0)
135     {
136     }
137
138 private:
139     QQmlDebugProcess *m_process;
140     QQmlDebugConnection *m_connection;
141     QQmlProfilerClient *m_client;
142
143     void connect(bool block, const QString &testFile);
144
145 private slots:
146     void cleanup();
147
148     void blockingConnectWithTraceEnabled();
149     void blockingConnectWithTraceDisabled();
150     void nonBlockingConnect();
151     void profileOnExit();
152 };
153
154 void QQmlProfilerClient::messageReceived(const QByteArray &message)
155 {
156     QByteArray msg = message;
157     QDataStream stream(&msg, QIODevice::ReadOnly);
158
159
160     QQmlProfilerData data;
161     data.time = -2;
162     data.messageType = -1;
163     data.detailType = -1;
164     data.line = -1;
165     data.framerate = -1;
166     data.animationcount = -1;
167
168     stream >> data.time >> data.messageType;
169
170     QVERIFY(data.time >= -1);
171
172     switch (data.messageType) {
173     case (QQmlProfilerClient::Event): {
174         stream >> data.detailType;
175
176         switch (data.detailType) {
177         case QQmlProfilerClient::AnimationFrame: {
178             stream >> data.framerate >> data.animationcount;
179             QVERIFY(data.framerate != -1);
180             QVERIFY(data.animationcount != -1);
181             break;
182         }
183         case QQmlProfilerClient::FramePaint:
184         case QQmlProfilerClient::Mouse:
185         case QQmlProfilerClient::Key:
186         case QQmlProfilerClient::StartTrace:
187         case QQmlProfilerClient::EndTrace:
188             break;
189         default: {
190             QString failMsg = QString("Unknown event type:") + data.detailType;
191             QFAIL(qPrintable(failMsg));
192             break;
193         }
194         }
195         break;
196     }
197     case QQmlProfilerClient::Complete: {
198         emit complete();
199         return;
200     }
201     case QQmlProfilerClient::RangeStart: {
202         stream >> data.detailType;
203         QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerClient::MaximumRangeType);
204         break;
205     }
206     case QQmlProfilerClient::RangeEnd: {
207         stream >> data.detailType;
208         QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerClient::MaximumRangeType);
209         break;
210     }
211     case QQmlProfilerClient::RangeData: {
212         stream >> data.detailType >> data.detailData;
213         QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerClient::MaximumRangeType);
214         break;
215     }
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);
220         break;
221     }
222     default:
223         QString failMsg = QString("Unknown message type:") + data.messageType;
224         QFAIL(qPrintable(failMsg));
225         break;
226     }
227     QVERIFY(stream.atEnd());
228     traceMessages.append(data);
229 }
230
231 void tst_QQmlProfilerService::connect(bool block, const QString &testFile)
232 {
233     const QString executable = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene";
234     QStringList arguments;
235
236     if (block)
237         arguments << QString("-qmljsdebugger=port:"STR_PORT",block");
238     else
239         arguments << QString("-qmljsdebugger=port:"STR_PORT);
240
241     arguments << QQmlDataTest::instance()->testFile(testFile);
242
243     m_process = new QQmlDebugProcess(executable);
244     m_process->start(QStringList() << arguments);
245     if (!m_process->waitForSessionStart()) {
246         QString failMsg = QString("Could not launch app '%1'.\nApplication output:\n%2").arg(
247                     executable, m_process->output());
248         QFAIL(qPrintable(failMsg));
249     }
250
251     QQmlDebugConnection *m_connection = new QQmlDebugConnection();
252     m_client = new QQmlProfilerClient(m_connection);
253
254     m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
255 }
256
257 void tst_QQmlProfilerService::cleanup()
258 {
259     delete m_process;
260     delete m_connection;
261     delete m_client;
262 }
263
264 void tst_QQmlProfilerService::blockingConnectWithTraceEnabled()
265 {
266     connect(true, "test.qml");
267     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
268
269     m_client->setTraceState(true);
270     m_client->setTraceState(false);
271     if (!QQmlDebugTest::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     QVERIFY(m_client->traceMessages.count());
278     // must start with "StartTrace"
279     QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
280     QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
281
282     // must end with "EndTrace"
283     QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
284     QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
285 }
286
287 void tst_QQmlProfilerService::blockingConnectWithTraceDisabled()
288 {
289     connect(true, "test.qml");
290     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
291
292     m_client->setTraceState(false);
293     m_client->setTraceState(true);
294     m_client->setTraceState(false);
295     if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
296         QString failMsg
297                 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
298         QFAIL(qPrintable(failMsg));
299     }
300
301     QVERIFY(m_client->traceMessages.count());
302
303     // must start with "StartTrace"
304     QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
305     QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
306
307     // must end with "EndTrace"
308     QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
309     QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
310 }
311
312 void tst_QQmlProfilerService::nonBlockingConnect()
313 {
314     connect(false, "test.qml");
315     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
316
317     m_client->setTraceState(true);
318     m_client->setTraceState(false);
319     if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
320         QString failMsg
321                 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
322         QFAIL(qPrintable(failMsg));
323     }
324
325     // must start with "StartTrace"
326     QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
327     QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
328
329     // must end with "EndTrace"
330     QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
331     QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
332 }
333
334 void tst_QQmlProfilerService::profileOnExit()
335 {
336     connect(true, "exit.qml");
337     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
338
339     m_client->setTraceState(true);
340
341     if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
342         QString failMsg
343                 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
344         QFAIL(qPrintable(failMsg));
345     }
346
347     // must start with "StartTrace"
348     QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
349     QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
350
351     // must end with "EndTrace"
352     QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
353     QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
354 }
355
356 QTEST_MAIN(tst_QQmlProfilerService)
357
358 #include "tst_qqmlprofilerservice.moc"