~ci-train-bot/qtubuntu-media/qtubuntu-media-ubuntu-xenial-landing-035

« back to all changes in this revision

Viewing changes to tests/integration/tst_mediaplaylist.cpp

  • Committer: Alfonso Sanchez-Beato
  • Date: 2015-09-02 08:02:08 UTC
  • mfrom: (83.1.2 qtubuntu-media)
  • Revision ID: alfonso.sanchez-beato@canonical.com-20150902080208-tsxsb1ixf7qph5fm
[ Jim Hodapp ]
Added background playlist support connecting qtmultimedia with media-hub.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2015 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
#include "tst_mediaplaylist.h"
 
18
#include "aalutility.h"
 
19
 
 
20
#include <thread>
 
21
#include <unistd.h>
 
22
 
 
23
#include <QMediaPlayer>
 
24
#include <QMediaPlaylist>
 
25
 
 
26
#include <QtTest/QtTest>
 
27
 
 
28
void tst_MediaPlaylist::initTestCase()
 
29
{
 
30
}
 
31
 
 
32
void tst_MediaPlaylist::cleanupTestCase()
 
33
{
 
34
}
 
35
 
 
36
void tst_MediaPlaylist::init()
 
37
{
 
38
    // NOTE: This sleep is currently needed in order to give media-hub a bit of time
 
39
    // between our different tests to cleanup and come back in a state where it can
 
40
    // respond to our requests.
 
41
    sleep(1);
 
42
}
 
43
 
 
44
void tst_MediaPlaylist::constructDestroyRepeat()
 
45
{
 
46
    for (int i=0; i<25; i++)
 
47
    {
 
48
        QMediaPlayer *player = new QMediaPlayer;
 
49
        QMediaPlaylist *playlist = new QMediaPlaylist;
 
50
        player->setPlaylist(playlist);
 
51
 
 
52
        delete playlist;
 
53
        delete player;
 
54
    }
 
55
}
 
56
 
 
57
void tst_MediaPlaylist::addTwoTracksAndVerify()
 
58
{
 
59
    QMediaPlayer *player = new QMediaPlayer;
 
60
    QMediaPlaylist *playlist = new QMediaPlaylist;
 
61
    player->setPlaylist(playlist);
 
62
 
 
63
    playlist->addMedia(QUrl(QFINDTESTDATA("testdata/testfile.ogg")));
 
64
    playlist->addMedia(QUrl(QFINDTESTDATA("testdata/testfile.mp4")));
 
65
 
 
66
    QCOMPARE(playlist->mediaCount(), 2);
 
67
 
 
68
    delete playlist;
 
69
    delete player;
 
70
}
 
71
 
 
72
void tst_MediaPlaylist::addListOfTracksAndVerify()
 
73
{
 
74
    QMediaPlayer *player = new QMediaPlayer;
 
75
    QMediaPlaylist *playlist = new QMediaPlaylist;
 
76
    player->setPlaylist(playlist);
 
77
 
 
78
    QList<QMediaContent> content;
 
79
    content.push_back(QUrl(QFINDTESTDATA("testdata/testfile.ogg")));
 
80
    content.push_back(QUrl(QFINDTESTDATA("testdata/testfile.mp4")));
 
81
 
 
82
    playlist->addMedia(content);
 
83
 
 
84
    QCOMPARE(playlist->mediaCount(), 2);
 
85
 
 
86
    delete playlist;
 
87
    delete player;
 
88
}
 
89
 
 
90
void tst_MediaPlaylist::goToNextTrack()
 
91
{
 
92
    QMediaPlayer *player = new QMediaPlayer;
 
93
    QMediaPlaylist *playlist = new QMediaPlaylist;
 
94
    player->setPlaylist(playlist);
 
95
 
 
96
    const QUrl audio(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
97
    const QUrl video(QUrl("file://" + QFINDTESTDATA("testdata/testfile.mp4")));
 
98
    qDebug() << "audio URL: " << audio.toString();
 
99
    qDebug() << "video URL: " << video.toString();
 
100
    playlist->addMedia(audio);
 
101
    playlist->addMedia(video);
 
102
 
 
103
    QCOMPARE(playlist->mediaCount(), 2);
 
104
 
 
105
    player->play();
 
106
 
 
107
    QCoreApplication::processEvents();
 
108
 
 
109
    const QUrl audioToVerify(playlist->currentMedia().canonicalUrl());
 
110
    QCOMPARE(audioToVerify, audio);
 
111
 
 
112
    playlist->next();
 
113
 
 
114
    QCoreApplication::processEvents();
 
115
 
 
116
    const QUrl videoToVerify(playlist->currentMedia().canonicalUrl());
 
117
    QCOMPARE(videoToVerify, video);
 
118
 
 
119
    delete playlist;
 
120
    delete player;
 
121
}
 
122
 
 
123
void tst_MediaPlaylist::goToPreviousTrack()
 
124
{
 
125
    QMediaPlayer *player = new QMediaPlayer;
 
126
    QMediaPlaylist *playlist = new QMediaPlaylist;
 
127
    player->setPlaylist(playlist);
 
128
 
 
129
    const QUrl audio1(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
130
    const QUrl audio2(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
131
    playlist->addMedia(audio1);
 
132
    playlist->addMedia(audio2);
 
133
 
 
134
    QCOMPARE(playlist->mediaCount(), 2);
 
135
    playlist->setCurrentIndex(1);
 
136
 
 
137
    player->play();
 
138
 
 
139
    QCoreApplication::processEvents();
 
140
 
 
141
    const QUrl audio2ToVerify(playlist->currentMedia().canonicalUrl());
 
142
    QCOMPARE(audio2ToVerify, audio2);
 
143
 
 
144
    playlist->previous();
 
145
 
 
146
    QCoreApplication::processEvents();
 
147
 
 
148
    const QUrl audio1ToVerify(playlist->currentMedia().canonicalUrl());
 
149
    QCOMPARE(audio2ToVerify, audio1);
 
150
    QCOMPARE(playlist->currentIndex(), 0);
 
151
 
 
152
    delete playlist;
 
153
    delete player;
 
154
}
 
155
 
 
156
void tst_MediaPlaylist::verifyMedia()
 
157
{
 
158
    QMediaPlayer *player = new QMediaPlayer;
 
159
    QMediaPlaylist *playlist = new QMediaPlaylist;
 
160
    player->setPlaylist(playlist);
 
161
 
 
162
    const QUrl audio(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
163
    const QUrl video(QUrl("file://" + QFINDTESTDATA("testdata/testfile.mp4")));
 
164
    qDebug() << "audio URL: " << audio.toString();
 
165
    qDebug() << "video URL: " << video.toString();
 
166
    playlist->addMedia(audio);
 
167
    playlist->addMedia(video);
 
168
 
 
169
    QCOMPARE(playlist->mediaCount(), 2);
 
170
 
 
171
    const QUrl audioToVerify(playlist->media(0).canonicalUrl());
 
172
    QCOMPARE(audioToVerify, audio);
 
173
 
 
174
    const QUrl videoToVerify(playlist->media(1).canonicalUrl());
 
175
    QCOMPARE(videoToVerify, video);
 
176
 
 
177
    delete playlist;
 
178
    delete player;
 
179
}
 
180
 
 
181
void tst_MediaPlaylist::removeTrackAndVerify()
 
182
{
 
183
    QMediaPlayer *player = new QMediaPlayer;
 
184
    QMediaPlaylist *playlist = new QMediaPlaylist;
 
185
    player->setPlaylist(playlist);
 
186
 
 
187
    playlist->addMedia(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
188
    const QUrl video(QUrl("file://" + QFINDTESTDATA("testdata/testfile.mp4")));
 
189
    playlist->addMedia(video);
 
190
 
 
191
    QCOMPARE(playlist->mediaCount(), 2);
 
192
 
 
193
    playlist->removeMedia(0);
 
194
 
 
195
    QCOMPARE(playlist->mediaCount(), 1);
 
196
 
 
197
    const QUrl videoToVerify(playlist->media(0).canonicalUrl());
 
198
    QCOMPARE(videoToVerify, video);
 
199
 
 
200
    delete playlist;
 
201
    delete player;
 
202
}
 
203
 
 
204
void tst_MediaPlaylist::verifyCurrentIndex()
 
205
{
 
206
    QMediaPlayer *player = new QMediaPlayer;
 
207
    QMediaPlaylist *playlist = new QMediaPlaylist;
 
208
    player->setPlaylist(playlist);
 
209
 
 
210
    QList<QMediaContent> content;
 
211
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
212
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.mp4")));
 
213
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
214
    playlist->addMedia(content);
 
215
 
 
216
    QCOMPARE(playlist->mediaCount(), 3);
 
217
 
 
218
    QMediaContent current_media;
 
219
    std::promise<QMediaContent> promise;
 
220
    std::future<QMediaContent> future{promise.get_future()};
 
221
    QMetaObject::Connection c = connect(playlist, &QMediaPlaylist::currentMediaChanged, [&](const QMediaContent& content)
 
222
    {
 
223
            qDebug() << "currentMediaChanged to: " << content.canonicalUrl().toString();
 
224
            current_media = content;
 
225
            promise.set_value(current_media);
 
226
            // Make sure the promise is not fulfilled twice
 
227
            QObject::disconnect(c);
 
228
    });
 
229
 
 
230
    qDebug() << "Setting current index to be 1";
 
231
    playlist->setCurrentIndex(1);
 
232
 
 
233
    // Wait for the currentMediaChanged signal to be emited
 
234
    wait_for_signal(future);
 
235
 
 
236
    qDebug() << "Checking if current index is 1";
 
237
    QCOMPARE(playlist->currentIndex(), 1);
 
238
 
 
239
    delete playlist;
 
240
    delete player;
 
241
}
 
242
 
 
243
void tst_MediaPlaylist::verifyNextIndex()
 
244
{
 
245
    QMediaPlayer *player = new QMediaPlayer;
 
246
    QMediaPlaylist *playlist = new QMediaPlaylist;
 
247
    player->setPlaylist(playlist);
 
248
 
 
249
    QList<QMediaContent> content;
 
250
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
251
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.mp4")));
 
252
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
253
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.mp4")));
 
254
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
255
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.mp4")));
 
256
    playlist->addMedia(content);
 
257
 
 
258
    QCoreApplication::processEvents();
 
259
 
 
260
    QCOMPARE(playlist->mediaCount(), 6);
 
261
 
 
262
    QCOMPARE(playlist->nextIndex(1), 1);
 
263
    QCOMPARE(playlist->nextIndex(4), 4);
 
264
    QCOMPARE(playlist->nextIndex(6), 0);
 
265
    QCOMPARE(playlist->nextIndex(7), 1);
 
266
    QCOMPARE(playlist->nextIndex(11), 5);
 
267
 
 
268
    delete playlist;
 
269
    delete player;
 
270
}
 
271
 
 
272
void tst_MediaPlaylist::verifyPreviousIndex()
 
273
{
 
274
    QMediaPlayer *player = new QMediaPlayer;
 
275
    QMediaPlaylist *playlist = new QMediaPlaylist;
 
276
    player->setPlaylist(playlist);
 
277
 
 
278
    QList<QMediaContent> content;
 
279
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
280
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.mp4")));
 
281
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
282
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.mp4")));
 
283
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
284
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.mp4")));
 
285
    playlist->addMedia(content);
 
286
 
 
287
    QCoreApplication::processEvents();
 
288
 
 
289
    QCOMPARE(playlist->mediaCount(), 6);
 
290
 
 
291
    QCOMPARE(playlist->previousIndex(1), 5);
 
292
    QCOMPARE(playlist->previousIndex(4), 2);
 
293
    QCOMPARE(playlist->previousIndex(6), 0);
 
294
    QCOMPARE(playlist->previousIndex(11), 1);
 
295
    QCOMPARE(playlist->previousIndex(21), 3);
 
296
    QCOMPARE(playlist->previousIndex(19), 5);
 
297
 
 
298
    delete playlist;
 
299
    delete player;
 
300
}
 
301
 
 
302
void tst_MediaPlaylist::verifyPlaybackModeCurrentItemInLoop()
 
303
{
 
304
    QMediaPlayer *player = new QMediaPlayer;
 
305
    QMediaPlaylist *playlist = new QMediaPlaylist;
 
306
    player->setPlaylist(playlist);
 
307
 
 
308
    QList<QMediaContent> content;
 
309
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
310
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
311
    playlist->addMedia(content);
 
312
 
 
313
    QCOMPARE(playlist->mediaCount(), 2);
 
314
 
 
315
    QMediaContent current_media;
 
316
    std::promise<QMediaContent> promise;
 
317
    std::future<QMediaContent> future{promise.get_future()};
 
318
    QMetaObject::Connection c = connect(playlist, &QMediaPlaylist::currentMediaChanged, [&](const QMediaContent& content)
 
319
    {
 
320
            qDebug() << "currentMediaChanged to: " << content.canonicalUrl().toString();
 
321
            current_media = content;
 
322
            promise.set_value(current_media);
 
323
            // Make sure the promise is not fulfilled twice
 
324
            QObject::disconnect(c);
 
325
    });
 
326
 
 
327
    playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
 
328
 
 
329
    qDebug() << "Call player->play()";
 
330
    player->play();
 
331
 
 
332
    // Wait for the currentMediaChanged signal to be emited
 
333
    wait_for_signal(future);
 
334
 
 
335
    QCOMPARE(playlist->currentIndex(), 0);
 
336
 
 
337
    QObject::disconnect(c);
 
338
 
 
339
    delete playlist;
 
340
    delete player;
 
341
}
 
342
 
 
343
void tst_MediaPlaylist::verifyPlaybackModeSequential()
 
344
{
 
345
    QMediaPlayer *player = new QMediaPlayer;
 
346
    QMediaPlaylist *playlist = new QMediaPlaylist;
 
347
    player->setPlaylist(playlist);
 
348
 
 
349
    QMediaContent current_media;
 
350
    std::promise<QMediaContent> promise;
 
351
    std::future<QMediaContent> future{promise.get_future()};
 
352
 
 
353
    QMetaObject::Connection c = connect(playlist, &QMediaPlaylist::currentMediaChanged, [&](const QMediaContent& content)
 
354
    {
 
355
            qDebug() << "currentMediaChanged to: " << content.canonicalUrl().toString();
 
356
            current_media = content;
 
357
            promise.set_value(current_media);
 
358
            // Make sure the promise is not fulfilled twice
 
359
            QObject::disconnect(c);
 
360
    });
 
361
 
 
362
    QList<QMediaContent> content;
 
363
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
364
    content.push_back(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
 
365
    playlist->addMedia(content);
 
366
 
 
367
    QCOMPARE(playlist->mediaCount(), 2);
 
368
 
 
369
    playlist->setPlaybackMode(QMediaPlaylist::Sequential);
 
370
 
 
371
    player->play();
 
372
 
 
373
    // Wait for the currentMediaChanged signal to be emited
 
374
    wait_for_signal(future);
 
375
 
 
376
    QCOMPARE(playlist->currentIndex(), 1);
 
377
 
 
378
    QObject::disconnect(c);
 
379
 
 
380
    delete playlist;
 
381
    delete player;
 
382
}
 
383
 
 
384
template<typename R>
 
385
void tst_MediaPlaylist::wait_for_signal(std::future<R> const& f)
 
386
{
 
387
    while (!is_ready<QMediaContent>(f))
 
388
    {
 
389
        // Make sure we don't block the main QEventLoop, which
 
390
        // would hinder receiving the currentMediaChanged event above
 
391
        QCoreApplication::processEvents();
 
392
        std::this_thread::yield();
 
393
    }
 
394
}
 
395
 
 
396
QTEST_GUILESS_MAIN(tst_MediaPlaylist)