summaryrefslogtreecommitdiffstats
path: root/tests/auto/qmediastreamscontrol/tst_qmediastreamscontrol.cpp
blob: d728691853728ff62ea71cdbeacb3a82cf1549a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

//TESTED_COMPONENT=src/multimedia

#include <QtCore/QString>
#include <QtTest/QtTest>
#include <QtCore/QCoreApplication>


#include <qmediaplayercontrol.h>
#include <qmediaservice.h>

#include <qmediastreamscontrol.h>

#include <QtGui/QImage>
#include <QtCore/QPointer>

QT_USE_NAMESPACE


#define WAIT_FOR_CONDITION(a,e)            \
    for (int _i = 0; _i < 500; _i += 1) {  \
    if ((a) == (e)) break;             \
    QTest::qWait(10);}

class tst_qmediastreamscontrol : public QObject
{
    Q_OBJECT

public:
    tst_qmediastreamscontrol();

private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();
    void control_iid();
    void control();
    void isActive();
    void streamCount();
    void streamsChanged();
    void metadata();
};



class mediaStatusList : public QObject, public QList<QMediaStreamsControl::StreamType>
{
    Q_OBJECT
public slots:
    void mediaStatus(QMediaStreamsControl::StreamType status) {
        append(status);
    }

public:
    mediaStatusList(QObject *obj, const char *aSignal)
        : QObject()
    {
        QObject::connect(obj, aSignal, this, SLOT(mediaStatus(QMediaStreamsControl::StreamType)));
    }
};

class QtTestMediaStreamsControl: public QMediaStreamsControl
{
public:
    QtTestMediaStreamsControl(QObject *parent = 0)
        : QMediaStreamsControl(parent)
    {
    }

    int streamCount()
    {
        QList <StreamType> m_stype;

        return streams.count();
    }
    void setStreamCount(int count)
    {
        streams.resize(count);
    }

    StreamType streamType(int index)
    {
        return streams.at(index).type;
    }
    void setStreamType(int index, StreamType type)
    {
        streams[index].type = type;
    }

    QVariant metaData(int index, QtMultimedia::MetaData key)
    {
        QtMultimedia::MetaData keys = key;
        return keys;
    }

    void setMetaData(int index, QtMultimedia::MetaData key, const QVariant &value)
    {
        streams[index].metaData.insert(key, value);
    }

    bool isActive(int index)
    {
        return streams.at(index).active;
    }
    void setActive(int index, bool state)
    {
        streams[index].active = state;
    }

    void setAudioOnlyContent()
    {
        mediaContent = audioOnlyContent;

        m_player->setMedia(*mediaContent);
    }

    void setVideoOnlyContent()
    {
        mediaContent = videoOnlyContent;
        duration = 60000;

        m_player->setMedia(*mediaContent);
    }

    void setAudioVideoContent()
    {
        if (mediaContent == audioVideoContent)
        {
            mediaContent = audioVideoAltContent;
            duration = 101840;
        }
        else
        {
            mediaContent = audioVideoContent;
            duration = 141000;
        }

        m_player->setMedia(*mediaContent);
    }

    void setStreamingContent()
    {
        mediaContent = streamingContent;

        m_player->setMedia(*mediaContent);
    }



public:
    struct Stream
    {
        Stream() : type(UnknownStream), active(false) {}
        StreamType type;
        QMap<QtMultimedia::MetaData, QVariant> metaData;
        bool active;
    };

    QVector<Stream> streams;
    QMediaContent* audioOnlyContent;
    QMediaContent* videoOnlyContent;
    QMediaContent* audioVideoContent;
    QMediaContent* audioVideoAltContent;
    QMediaContent* mediaContent;
    QMediaContent* streamingContent;

    qint64 duration;
    QMediaPlayer *m_player;
    QVideoWidget *m_widget;
    QWidget *m_windowWidget;


};

class QTestMediaStreamsControlA : public QMediaControl
{
    Q_OBJECT
};

#define QTestMediaStreamsControlA_iid "com.nokia.QTestMediaStreamsControlA"
Q_MEDIA_DECLARE_CONTROL(QTestMediaStreamsControlA, QTestMediaStreamsControlA_iid)

class QTestMediaStreamsControlB : public QMediaControl
{
    Q_OBJECT
public:
    QTestMediaStreamsControlB()
        : QMediaControl(0)
        ,ctrlA(0)
        ,ctrlB(0)
        ,ctrlC(0) {}

    bool isActive(int stream)
    {
        return 1;
    }

    int ctrlA;
    int ctrlB;
    int ctrlC;
};

#define QTestMediaStreamsControlB_iid "com.nokia.QTestMediaStreamsControlB"
Q_MEDIA_DECLARE_CONTROL(QTestMediaStreamsControlB, QTestMediaStreamsControlB_iid)


class QTestMediaStreamsControlC : public QMediaControl
{
    Q_OBJECT
};

#define QTestMediaStreamsControlC_iid "com.nokia.QTestMediaStreamsControlC"
Q_MEDIA_DECLARE_CONTROL(QTestMediaStreamsControlC, QTestMediaStreamsControlC_iid) // Yes A.

class QTestMediaStreamsControlD : public QMediaControl
{
    Q_OBJECT
};

#define QTestMediaStreamsControlD_iid "com.nokia.QTestMediaStreamsControlD"
Q_MEDIA_DECLARE_CONTROL(QTestMediaStreamsControlD, QTestMediaStreamsControlD_iid)


class QtTestMediaService : public QMediaService
{
    Q_OBJECT
public:
    QtTestMediaService()
        : QMediaService(0)
        , refA(0)
        , refB(0)
        , refC(0)
    {
    }

    QMediaControl *requestControl(const char *name)
    {
        if (strcmp(name, QTestMediaStreamsControlA_iid) == 0) {
            refA += 1;

            return &controlA;
        } else if (strcmp(name, QTestMediaStreamsControlB_iid) == 0) {
            refB += 1;

            return &controlB;
        } else if (strcmp(name, QTestMediaStreamsControlC_iid) == 0) {
            refA += 1;

            return &controlA;
        } else {
            return 0;
        }
    }

    void releaseControl(QMediaControl *control)
    {
        if (control == &controlA)
            refA -= 1;
        else if (control == &controlB)
            refB -= 1;
        else if (control == &controlC)
            refC -= 1;
    }

    using QMediaService::requestControl;

    int refA;
    int refB;
    int refC;
    QTestMediaStreamsControlA controlA;
    QTestMediaStreamsControlB controlB;
    QTestMediaStreamsControlC controlC;
};


tst_qmediastreamscontrol::tst_qmediastreamscontrol()
{
}

void tst_qmediastreamscontrol::initTestCase()
{
}

void tst_qmediastreamscontrol::cleanupTestCase()
{
}

void tst_qmediastreamscontrol::control_iid()
{

    // Default implementation.
    QCOMPARE(qmediacontrol_iid<QTestMediaStreamsControlA *>(), QTestMediaStreamsControlA_iid);

    // Partial template.
    QVERIFY(qstrcmp(qmediacontrol_iid<QTestMediaStreamsControlA *>(), QTestMediaStreamsControlA_iid) == 0);
}

void tst_qmediastreamscontrol::control()
{
    QtTestMediaService *service = new QtTestMediaService();
    QMediaStreamsControl *control = qobject_cast<QMediaStreamsControl *>
            (service->requestControl("com.nokia.Qt.MediaStreamsControl/1.0"));
    //    QCOMPARE(control,service->controlA.objectName());
    QTestMediaStreamsControlA *controlA = (QTestMediaStreamsControlA *)service->requestControl("controlA");
    //    QCOMPARE(controlA,service->controlA);
    QVERIFY(service->requestControl<QTestMediaStreamsControlA *>());

    service->releaseControl(controlA);
    delete service;
}

void tst_qmediastreamscontrol::isActive()
{
    QTestMediaStreamsControlB ser;
    QVERIFY(ser.isActive(1));
    QtTestMediaStreamsControl m_active;
    //setActive
    m_active.setActive(1,1);
    QVERIFY(m_active.isActive(1));
    //set InActive
    m_active.setActive(2,0);
    QVERIFY(!m_active.isActive(0));
}

//Returns the number of media streams.
void tst_qmediastreamscontrol::streamCount()
{
    QtTestMediaStreamsControl m_cnt;
    m_cnt.setStreamType(0,QMediaStreamsControl::UnknownStream);
    m_cnt.setStreamType(1,QMediaStreamsControl::VideoStream);
    m_cnt.setStreamType(2,QMediaStreamsControl::AudioStream);
    m_cnt.setStreamType(3,QMediaStreamsControl::SubPictureStream);
    m_cnt.setStreamType(4,QMediaStreamsControl::DataStream);
    m_cnt.setStreamCount(5);
    QVERIFY(m_cnt.streamCount() == m_cnt.streams.count());
}

//The signal is emitted when the available streams list is changed.
void tst_qmediastreamscontrol::streamsChanged()
{
    QMediaPlayer *m_player = new QMediaPlayer(0);
    QMediaStreamsControl* m_streamControl = (QMediaStreamsControl*)
            (m_player->service()->requestControl(QTestMediaStreamsControlA_iid));

    QMediaContent videoOnlyContent;

    m_player->setMedia(videoOnlyContent);
    if (m_streamControl) {
        QSignalSpy m_strm_lst_chgSpy(m_streamControl,SIGNAL(streamsChanged()));
        QVERIFY(m_strm_lst_chgSpy.isValid());
        QVERIFY(m_strm_lst_chgSpy.isEmpty());
        WAIT_FOR_CONDITION(m_player->mediaStatus(),QMediaPlayer::LoadedMedia);
        QVERIFY(m_streamControl->streamCount() == 1);
        QVERIFY(m_strm_lst_chgSpy.count() == 1);
    }

    delete m_player;
    m_player = NULL;
}

void tst_qmediastreamscontrol::metadata()
{
    QtTestMediaStreamsControl m_metadata;
    m_metadata.metaData(1,QtMultimedia::AlbumArtist);
    qDebug() << m_metadata.metaData(1,QtMultimedia::AlbumArtist);
}
QTEST_MAIN(tst_qmediastreamscontrol);

#include "tst_qmediastreamscontrol.moc"