edc120ba85ef00127719a8df9ec320ae379ae702
[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 "QtQml/private/qqmlprofilerservice_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 QQmlProfilerClient : public QQmlDebugClient
53 {
54     Q_OBJECT
55
56 public:
57     QQmlProfilerClient(QQmlDebugConnection *connection)
58         : QQmlDebugClient(QLatin1String("CanvasFrameRate"), connection)
59     {
60     }
61
62     QList<QQmlProfilerData> traceMessages;
63
64     void setTraceState(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_QQmlProfilerService : public QQmlDataTest
79 {
80     Q_OBJECT
81
82 public:
83     tst_QQmlProfilerService()
84         : m_process(0)
85         , m_connection(0)
86         , m_client(0)
87     {
88     }
89
90 private:
91     QQmlDebugProcess *m_process;
92     QQmlDebugConnection *m_connection;
93     QQmlProfilerClient *m_client;
94
95     void connect(bool block, const QString &testFile);
96
97 private slots:
98     void cleanup();
99
100     void blockingConnectWithTraceEnabled();
101     void blockingConnectWithTraceDisabled();
102     void nonBlockingConnect();
103     void profileOnExit();
104 };
105
106 void QQmlProfilerClient::messageReceived(const QByteArray &message)
107 {
108     QByteArray msg = message;
109     QDataStream stream(&msg, QIODevice::ReadOnly);
110
111
112     QQmlProfilerData data;
113     data.time = -2;
114     data.messageType = -1;
115     data.detailType = -1;
116     data.line = -1;
117     data.framerate = -1;
118     data.animationcount = -1;
119
120     stream >> data.time >> data.messageType;
121
122     QVERIFY(data.time >= -1);
123
124     switch (data.messageType) {
125     case (QQmlProfilerService::Event): {
126         stream >> data.detailType;
127
128         switch (data.detailType) {
129         case QQmlProfilerService::AnimationFrame: {
130             stream >> data.framerate >> data.animationcount;
131             QVERIFY(data.framerate != -1);
132             QVERIFY(data.animationcount != -1);
133             break;
134         }
135         case QQmlProfilerService::FramePaint:
136         case QQmlProfilerService::Mouse:
137         case QQmlProfilerService::Key:
138         case QQmlProfilerService::StartTrace:
139         case QQmlProfilerService::EndTrace:
140             break;
141         default: {
142             QString failMsg = QString("Unknown event type:") + data.detailType;
143             QFAIL(qPrintable(failMsg));
144             break;
145         }
146         }
147         break;
148     }
149     case QQmlProfilerService::Complete: {
150         emit complete();
151         return;
152     }
153     case QQmlProfilerService::RangeStart: {
154         stream >> data.detailType;
155         QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
156         break;
157     }
158     case QQmlProfilerService::RangeEnd: {
159         stream >> data.detailType;
160         QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
161         break;
162     }
163     case QQmlProfilerService::RangeData: {
164         stream >> data.detailType >> data.detailData;
165         QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
166         break;
167     }
168     case QQmlProfilerService::RangeLocation: {
169         stream >> data.detailType >> data.detailData >> data.line >> data.column;
170         QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::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_QQmlProfilerService::connect(bool block, const QString &testFile)
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 << QQmlDataTest::instance()->testFile(testFile);
194
195     m_process = new QQmlDebugProcess(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     QQmlDebugConnection *m_connection = new QQmlDebugConnection();
204     m_client = new QQmlProfilerClient(m_connection);
205
206     m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
207 }
208
209 void tst_QQmlProfilerService::cleanup()
210 {
211     delete m_process;
212     delete m_connection;
213     delete m_client;
214 }
215
216 void tst_QQmlProfilerService::blockingConnectWithTraceEnabled()
217 {
218     connect(true, "test.qml");
219     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
220
221     m_client->setTraceState(true);
222     m_client->setTraceState(false);
223     if (!QQmlDebugTest::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)QQmlProfilerService::Event);
232     QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
233
234     // must end with "EndTrace"
235     QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
236     QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
237 }
238
239 void tst_QQmlProfilerService::blockingConnectWithTraceDisabled()
240 {
241     connect(true, "test.qml");
242     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
243
244     m_client->setTraceState(false);
245     m_client->setTraceState(true);
246     m_client->setTraceState(false);
247     if (!QQmlDebugTest::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)QQmlProfilerService::Event);
257     QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
258
259     // must end with "EndTrace"
260     QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
261     QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
262 }
263
264 void tst_QQmlProfilerService::nonBlockingConnect()
265 {
266     connect(false, "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     // must start with "StartTrace"
278     QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
279     QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
280
281     // must end with "EndTrace"
282     QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
283     QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
284 }
285
286 void tst_QQmlProfilerService::profileOnExit()
287 {
288     connect(true, "exit.qml");
289     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
290
291     m_client->setTraceState(true);
292
293     if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
294         QString failMsg
295                 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
296         QFAIL(qPrintable(failMsg));
297     }
298
299     // must start with "StartTrace"
300     QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
301     QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
302
303     // must end with "EndTrace"
304     QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
305     QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
306 }
307
308 QTEST_MAIN(tst_QQmlProfilerService)
309
310 #include "tst_qqmlprofilerservice.moc"