2
* Copyright (C) 2015 Canonical, Ltd.
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.
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.
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/>.
17
#include "tst_mediaplaylist.h"
18
#include "aalutility.h"
23
#include <QMediaPlayer>
24
#include <QMediaPlaylist>
26
#include <QtTest/QtTest>
28
void tst_MediaPlaylist::initTestCase()
32
void tst_MediaPlaylist::cleanupTestCase()
36
void tst_MediaPlaylist::init()
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.
44
void tst_MediaPlaylist::constructDestroyRepeat()
46
for (int i=0; i<25; i++)
48
QMediaPlayer *player = new QMediaPlayer;
49
QMediaPlaylist *playlist = new QMediaPlaylist;
50
player->setPlaylist(playlist);
57
void tst_MediaPlaylist::addTwoTracksAndVerify()
59
QMediaPlayer *player = new QMediaPlayer;
60
QMediaPlaylist *playlist = new QMediaPlaylist;
61
player->setPlaylist(playlist);
63
playlist->addMedia(QUrl(QFINDTESTDATA("testdata/testfile.ogg")));
64
playlist->addMedia(QUrl(QFINDTESTDATA("testdata/testfile.mp4")));
66
QCOMPARE(playlist->mediaCount(), 2);
72
void tst_MediaPlaylist::addListOfTracksAndVerify()
74
QMediaPlayer *player = new QMediaPlayer;
75
QMediaPlaylist *playlist = new QMediaPlaylist;
76
player->setPlaylist(playlist);
78
QList<QMediaContent> content;
79
content.push_back(QUrl(QFINDTESTDATA("testdata/testfile.ogg")));
80
content.push_back(QUrl(QFINDTESTDATA("testdata/testfile.mp4")));
82
playlist->addMedia(content);
84
QCOMPARE(playlist->mediaCount(), 2);
90
void tst_MediaPlaylist::goToNextTrack()
92
QMediaPlayer *player = new QMediaPlayer;
93
QMediaPlaylist *playlist = new QMediaPlaylist;
94
player->setPlaylist(playlist);
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);
103
QCOMPARE(playlist->mediaCount(), 2);
107
QCoreApplication::processEvents();
109
const QUrl audioToVerify(playlist->currentMedia().canonicalUrl());
110
QCOMPARE(audioToVerify, audio);
114
QCoreApplication::processEvents();
116
const QUrl videoToVerify(playlist->currentMedia().canonicalUrl());
117
QCOMPARE(videoToVerify, video);
123
void tst_MediaPlaylist::goToPreviousTrack()
125
QMediaPlayer *player = new QMediaPlayer;
126
QMediaPlaylist *playlist = new QMediaPlaylist;
127
player->setPlaylist(playlist);
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);
134
QCOMPARE(playlist->mediaCount(), 2);
135
playlist->setCurrentIndex(1);
139
QCoreApplication::processEvents();
141
const QUrl audio2ToVerify(playlist->currentMedia().canonicalUrl());
142
QCOMPARE(audio2ToVerify, audio2);
144
playlist->previous();
146
QCoreApplication::processEvents();
148
const QUrl audio1ToVerify(playlist->currentMedia().canonicalUrl());
149
QCOMPARE(audio2ToVerify, audio1);
150
QCOMPARE(playlist->currentIndex(), 0);
156
void tst_MediaPlaylist::verifyMedia()
158
QMediaPlayer *player = new QMediaPlayer;
159
QMediaPlaylist *playlist = new QMediaPlaylist;
160
player->setPlaylist(playlist);
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);
169
QCOMPARE(playlist->mediaCount(), 2);
171
const QUrl audioToVerify(playlist->media(0).canonicalUrl());
172
QCOMPARE(audioToVerify, audio);
174
const QUrl videoToVerify(playlist->media(1).canonicalUrl());
175
QCOMPARE(videoToVerify, video);
181
void tst_MediaPlaylist::removeTrackAndVerify()
183
QMediaPlayer *player = new QMediaPlayer;
184
QMediaPlaylist *playlist = new QMediaPlaylist;
185
player->setPlaylist(playlist);
187
playlist->addMedia(QUrl("file://" + QFINDTESTDATA("testdata/testfile.ogg")));
188
const QUrl video(QUrl("file://" + QFINDTESTDATA("testdata/testfile.mp4")));
189
playlist->addMedia(video);
191
QCOMPARE(playlist->mediaCount(), 2);
193
playlist->removeMedia(0);
195
QCOMPARE(playlist->mediaCount(), 1);
197
const QUrl videoToVerify(playlist->media(0).canonicalUrl());
198
QCOMPARE(videoToVerify, video);
204
void tst_MediaPlaylist::verifyCurrentIndex()
206
QMediaPlayer *player = new QMediaPlayer;
207
QMediaPlaylist *playlist = new QMediaPlaylist;
208
player->setPlaylist(playlist);
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);
216
QCOMPARE(playlist->mediaCount(), 3);
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)
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);
230
qDebug() << "Setting current index to be 1";
231
playlist->setCurrentIndex(1);
233
// Wait for the currentMediaChanged signal to be emited
234
wait_for_signal(future);
236
qDebug() << "Checking if current index is 1";
237
QCOMPARE(playlist->currentIndex(), 1);
243
void tst_MediaPlaylist::verifyNextIndex()
245
QMediaPlayer *player = new QMediaPlayer;
246
QMediaPlaylist *playlist = new QMediaPlaylist;
247
player->setPlaylist(playlist);
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);
258
QCoreApplication::processEvents();
260
QCOMPARE(playlist->mediaCount(), 6);
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);
272
void tst_MediaPlaylist::verifyPreviousIndex()
274
QMediaPlayer *player = new QMediaPlayer;
275
QMediaPlaylist *playlist = new QMediaPlaylist;
276
player->setPlaylist(playlist);
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);
287
QCoreApplication::processEvents();
289
QCOMPARE(playlist->mediaCount(), 6);
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);
302
void tst_MediaPlaylist::verifyPlaybackModeCurrentItemInLoop()
304
QMediaPlayer *player = new QMediaPlayer;
305
QMediaPlaylist *playlist = new QMediaPlaylist;
306
player->setPlaylist(playlist);
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);
313
QCOMPARE(playlist->mediaCount(), 2);
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)
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);
327
playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
329
qDebug() << "Call player->play()";
332
// Wait for the currentMediaChanged signal to be emited
333
wait_for_signal(future);
335
QCOMPARE(playlist->currentIndex(), 0);
337
QObject::disconnect(c);
343
void tst_MediaPlaylist::verifyPlaybackModeSequential()
345
QMediaPlayer *player = new QMediaPlayer;
346
QMediaPlaylist *playlist = new QMediaPlaylist;
347
player->setPlaylist(playlist);
349
QMediaContent current_media;
350
std::promise<QMediaContent> promise;
351
std::future<QMediaContent> future{promise.get_future()};
353
QMetaObject::Connection c = connect(playlist, &QMediaPlaylist::currentMediaChanged, [&](const QMediaContent& content)
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);
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);
367
QCOMPARE(playlist->mediaCount(), 2);
369
playlist->setPlaybackMode(QMediaPlaylist::Sequential);
373
// Wait for the currentMediaChanged signal to be emited
374
wait_for_signal(future);
376
QCOMPARE(playlist->currentIndex(), 1);
378
QObject::disconnect(c);
385
void tst_MediaPlaylist::wait_for_signal(std::future<R> const& f)
387
while (!is_ready<QMediaContent>(f))
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();
396
QTEST_GUILESS_MAIN(tst_MediaPlaylist)