Debugger: Fix trace service for tracing on startup in block mode
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / debugger / qdeclarativedebugtrace / tst_qdeclarativedebugtrace.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 QObject
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 private slots:
96     void init();
97     void cleanup();
98
99     void connectWithTraceEnabled();
100     void connectWithTraceDisabled();
101 };
102
103 void QDeclarativeDebugTraceClient::messageReceived(const QByteArray &message)
104 {
105     QByteArray msg = message;
106     QDataStream stream(&msg, QIODevice::ReadOnly);
107
108
109     QDeclarativeDebugData data;
110     data.time = -2;
111     data.messageType = -1;
112     data.detailType = -1;
113     data.line = -1;
114     data.framerate = -1;
115     data.animationcount = -1;
116
117     stream >> data.time >> data.messageType;
118
119     QVERIFY(data.time >= -1);
120
121     switch (data.messageType) {
122     case (QDeclarativeDebugTrace::Event): {
123         stream >> data.detailType;
124
125         switch (data.detailType) {
126         case QDeclarativeDebugTrace::AnimationFrame: {
127             stream >> data.framerate >> data.animationcount;
128             QVERIFY(data.framerate != -1);
129             QVERIFY(data.animationcount != -1);
130             break;
131         }
132         case QDeclarativeDebugTrace::FramePaint:
133         case QDeclarativeDebugTrace::Mouse:
134         case QDeclarativeDebugTrace::Key:
135         case QDeclarativeDebugTrace::StartTrace:
136         case QDeclarativeDebugTrace::EndTrace:
137             break;
138         default: {
139             QString failMsg = QString("Unknown event type:") + data.detailType;
140             QFAIL(qPrintable(failMsg));
141             break;
142         }
143         }
144         break;
145     }
146     case QDeclarativeDebugTrace::Complete: {
147         emit complete();
148         QVERIFY(stream.atEnd());
149         return;
150     }
151     case QDeclarativeDebugTrace::RangeStart: {
152         stream >> data.detailType;
153         QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::MaximumRangeType);
154         break;
155     }
156     case QDeclarativeDebugTrace::RangeEnd: {
157         stream >> data.detailType;
158         QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::MaximumRangeType);
159         break;
160     }
161     case QDeclarativeDebugTrace::RangeData: {
162         stream >> data.detailType >> data.detailData;
163         QVERIFY(data.detailType >= 0 && data.detailType < QDeclarativeDebugTrace::MaximumRangeType);
164         break;
165     }
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);
170         break;
171     }
172     default:
173         QString failMsg = QString("Unknown message type:") + data.messageType;
174         QFAIL(qPrintable(failMsg));
175         break;
176     }
177     QVERIFY(stream.atEnd());
178     traceMessages.append(data);
179 }
180
181 void tst_QDeclarativeDebugTrace::init()
182 {
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")));
187
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));
194     }
195
196     QDeclarativeDebugConnection *m_connection = new QDeclarativeDebugConnection();
197     m_client = new QDeclarativeDebugTraceClient(m_connection);
198
199     m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
200 }
201
202 void tst_QDeclarativeDebugTrace::cleanup()
203 {
204     delete m_process;
205     delete m_connection;
206     delete m_client;
207 }
208
209 void tst_QDeclarativeDebugTrace::connectWithTraceEnabled()
210 {
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()))) {
215         QString failMsg
216                 = QString("No trace received in time. App output: \n\n").arg(m_process->output());
217         QFAIL(qPrintable(failMsg));
218     }
219
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);
223
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);
227 }
228
229 void tst_QDeclarativeDebugTrace::connectWithTraceDisabled()
230 {
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()))) {
236         QString failMsg
237                 = QString("No trace received in time. App output: \n\n").arg(m_process->output());
238         QFAIL(qPrintable(failMsg));
239     }
240
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);
244
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);
248 }
249
250 QTEST_MAIN(tst_QDeclarativeDebugTrace)
251
252 #include "tst_qdeclarativedebugtrace.moc"