QmlDebugging: Remove QQmlDebugClient
[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 "debugutil_p.h"
47 #include "qqmldebugclient.h"
48 #include "../../../shared/util.h"
49
50 #define PORT 13773
51 #define STR_PORT "13773"
52
53 class QQmlProfilerClient : public QQmlDebugClient
54 {
55     Q_OBJECT
56
57 public:
58     QQmlProfilerClient(QQmlDebugConnection *connection)
59         : QQmlDebugClient(QLatin1String("CanvasFrameRate"), connection)
60     {
61     }
62
63     QList<QQmlProfilerData> traceMessages;
64
65     void setTraceState(bool enabled) {
66         QByteArray message;
67         QDataStream stream(&message, QIODevice::WriteOnly);
68         stream << enabled;
69         sendMessage(message);
70     }
71
72 signals:
73     void complete();
74
75 protected:
76     void messageReceived(const QByteArray &message);
77 };
78
79 class tst_QQmlProfilerService : public QQmlDataTest
80 {
81     Q_OBJECT
82
83 public:
84     tst_QQmlProfilerService()
85         : m_process(0)
86         , m_connection(0)
87         , m_client(0)
88     {
89     }
90
91 private:
92     QQmlDebugProcess *m_process;
93     QQmlDebugConnection *m_connection;
94     QQmlProfilerClient *m_client;
95
96     void connect(bool block, const QString &testFile);
97
98 private slots:
99     void cleanup();
100
101     void blockingConnectWithTraceEnabled();
102     void blockingConnectWithTraceDisabled();
103     void nonBlockingConnect();
104     void profileOnExit();
105 };
106
107 void QQmlProfilerClient::messageReceived(const QByteArray &message)
108 {
109     QByteArray msg = message;
110     QDataStream stream(&msg, QIODevice::ReadOnly);
111
112
113     QQmlProfilerData data;
114     data.time = -2;
115     data.messageType = -1;
116     data.detailType = -1;
117     data.line = -1;
118     data.framerate = -1;
119     data.animationcount = -1;
120
121     stream >> data.time >> data.messageType;
122
123     QVERIFY(data.time >= -1);
124
125     switch (data.messageType) {
126     case (QQmlProfilerService::Event): {
127         stream >> data.detailType;
128
129         switch (data.detailType) {
130         case QQmlProfilerService::AnimationFrame: {
131             stream >> data.framerate >> data.animationcount;
132             QVERIFY(data.framerate != -1);
133             QVERIFY(data.animationcount != -1);
134             break;
135         }
136         case QQmlProfilerService::FramePaint:
137         case QQmlProfilerService::Mouse:
138         case QQmlProfilerService::Key:
139         case QQmlProfilerService::StartTrace:
140         case QQmlProfilerService::EndTrace:
141             break;
142         default: {
143             QString failMsg = QString("Unknown event type:") + data.detailType;
144             QFAIL(qPrintable(failMsg));
145             break;
146         }
147         }
148         break;
149     }
150     case QQmlProfilerService::Complete: {
151         emit complete();
152         return;
153     }
154     case QQmlProfilerService::RangeStart: {
155         stream >> data.detailType;
156         QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
157         break;
158     }
159     case QQmlProfilerService::RangeEnd: {
160         stream >> data.detailType;
161         QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
162         break;
163     }
164     case QQmlProfilerService::RangeData: {
165         stream >> data.detailType >> data.detailData;
166         QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
167         break;
168     }
169     case QQmlProfilerService::RangeLocation: {
170         stream >> data.detailType >> data.detailData >> data.line >> data.column;
171         QVERIFY(data.detailType >= 0 && data.detailType < QQmlProfilerService::MaximumRangeType);
172         QVERIFY(data.line >= -2);
173         break;
174     }
175     default:
176         QString failMsg = QString("Unknown message type:") + data.messageType;
177         QFAIL(qPrintable(failMsg));
178         break;
179     }
180     QVERIFY(stream.atEnd());
181     traceMessages.append(data);
182 }
183
184 void tst_QQmlProfilerService::connect(bool block, const QString &testFile)
185 {
186     const QString executable = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene";
187     QStringList arguments;
188
189     if (block)
190         arguments << QString("-qmljsdebugger=port:"STR_PORT",block");
191     else
192         arguments << QString("-qmljsdebugger=port:"STR_PORT);
193
194     arguments << QQmlDataTest::instance()->testFile(testFile);
195
196     m_process = new QQmlDebugProcess(executable);
197     m_process->start(QStringList() << arguments);
198     if (!m_process->waitForSessionStart()) {
199         QString failMsg = QString("Could not launch app '%1'.\nApplication output:\n%2").arg(
200                     executable, m_process->output());
201         QFAIL(qPrintable(failMsg));
202     }
203
204     QQmlDebugConnection *m_connection = new QQmlDebugConnection();
205     m_client = new QQmlProfilerClient(m_connection);
206
207     m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
208 }
209
210 void tst_QQmlProfilerService::cleanup()
211 {
212     delete m_process;
213     delete m_connection;
214     delete m_client;
215 }
216
217 void tst_QQmlProfilerService::blockingConnectWithTraceEnabled()
218 {
219     connect(true, "test.qml");
220     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
221
222     m_client->setTraceState(true);
223     m_client->setTraceState(false);
224     if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
225         QString failMsg
226                 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
227         QFAIL(qPrintable(failMsg));
228     }
229
230     QVERIFY(m_client->traceMessages.count());
231     // must start with "StartTrace"
232     QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
233     QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
234
235     // must end with "EndTrace"
236     QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
237     QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
238 }
239
240 void tst_QQmlProfilerService::blockingConnectWithTraceDisabled()
241 {
242     connect(true, "test.qml");
243     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
244
245     m_client->setTraceState(false);
246     m_client->setTraceState(true);
247     m_client->setTraceState(false);
248     if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
249         QString failMsg
250                 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
251         QFAIL(qPrintable(failMsg));
252     }
253
254     QVERIFY(m_client->traceMessages.count());
255
256     // must start with "StartTrace"
257     QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
258     QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
259
260     // must end with "EndTrace"
261     QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
262     QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
263 }
264
265 void tst_QQmlProfilerService::nonBlockingConnect()
266 {
267     connect(false, "test.qml");
268     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
269
270     m_client->setTraceState(true);
271     m_client->setTraceState(false);
272     if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
273         QString failMsg
274                 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
275         QFAIL(qPrintable(failMsg));
276     }
277
278     // must start with "StartTrace"
279     QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
280     QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
281
282     // must end with "EndTrace"
283     QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
284     QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
285 }
286
287 void tst_QQmlProfilerService::profileOnExit()
288 {
289     connect(true, "exit.qml");
290     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
291
292     m_client->setTraceState(true);
293
294     if (!QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()))) {
295         QString failMsg
296                 = QString("No trace received in time. App output: \n%1\n").arg(m_process->output());
297         QFAIL(qPrintable(failMsg));
298     }
299
300     // must start with "StartTrace"
301     QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerService::Event);
302     QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerService::StartTrace);
303
304     // must end with "EndTrace"
305     QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerService::Event);
306     QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerService::EndTrace);
307 }
308
309 QTEST_MAIN(tst_QQmlProfilerService)
310
311 #include "tst_qqmlprofilerservice.moc"