~ubuntu-branches/ubuntu/trusty/qtdeclarative-opensource-src/trusty-updates

« back to all changes in this revision

Viewing changes to tests/auto/qml/debugger/qqmlprofilerservice/tst_qqmlprofilerservice.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 14:17:19 UTC
  • Revision ID: package-import@ubuntu.com-20130205141719-qqeyml8wslpyez52
Tags: upstream-5.0.1
ImportĀ upstreamĀ versionĀ 5.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
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, this);
 
244
    m_process->start(QStringList() << arguments);
 
245
    QVERIFY2(m_process->waitForSessionStart(), "Could not launch application, or did not get 'Waiting for connection'.");
 
246
 
 
247
    QQmlDebugConnection *m_connection = new QQmlDebugConnection();
 
248
    m_client = new QQmlProfilerClient(m_connection);
 
249
 
 
250
    m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
 
251
}
 
252
 
 
253
void tst_QQmlProfilerService::cleanup()
 
254
{
 
255
    if (QTest::currentTestFailed()) {
 
256
        qDebug() << "Process State:" << m_process->state();
 
257
        qDebug() << "Application Output:" << m_process->output();
 
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
    QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())), "No trace received in time.");
 
272
 
 
273
    QVERIFY(m_client->traceMessages.count());
 
274
    // must start with "StartTrace"
 
275
    QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
 
276
    QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
 
277
 
 
278
    // must end with "EndTrace"
 
279
    QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
 
280
    QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
 
281
}
 
282
 
 
283
void tst_QQmlProfilerService::blockingConnectWithTraceDisabled()
 
284
{
 
285
    connect(true, "test.qml");
 
286
    QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
 
287
 
 
288
    m_client->setTraceState(false);
 
289
    m_client->setTraceState(true);
 
290
    m_client->setTraceState(false);
 
291
    QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())), "No trace received in time.");
 
292
 
 
293
    QVERIFY(m_client->traceMessages.count());
 
294
 
 
295
    // must start with "StartTrace"
 
296
    QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
 
297
    QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
 
298
 
 
299
    // must end with "EndTrace"
 
300
    QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
 
301
    QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
 
302
}
 
303
 
 
304
void tst_QQmlProfilerService::nonBlockingConnect()
 
305
{
 
306
    connect(false, "test.qml");
 
307
    QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
 
308
 
 
309
    m_client->setTraceState(true);
 
310
    m_client->setTraceState(false);
 
311
    QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())), "No trace received in time.");
 
312
 
 
313
    // must start with "StartTrace"
 
314
    QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
 
315
    QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
 
316
 
 
317
    // must end with "EndTrace"
 
318
    QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
 
319
    QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
 
320
}
 
321
 
 
322
void tst_QQmlProfilerService::profileOnExit()
 
323
{
 
324
    connect(true, "exit.qml");
 
325
    QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
 
326
 
 
327
    m_client->setTraceState(true);
 
328
 
 
329
    QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())), "No trace received in time.");
 
330
 
 
331
    // must start with "StartTrace"
 
332
    QCOMPARE(m_client->traceMessages.first().messageType, (int)QQmlProfilerClient::Event);
 
333
    QCOMPARE(m_client->traceMessages.first().detailType, (int)QQmlProfilerClient::StartTrace);
 
334
 
 
335
    // must end with "EndTrace"
 
336
    QCOMPARE(m_client->traceMessages.last().messageType, (int)QQmlProfilerClient::Event);
 
337
    QCOMPARE(m_client->traceMessages.last().detailType, (int)QQmlProfilerClient::EndTrace);
 
338
}
 
339
 
 
340
QTEST_MAIN(tst_QQmlProfilerService)
 
341
 
 
342
#include "tst_qqmlprofilerservice.moc"