1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29//TESTED_COMPONENT=src/multimedia
30
31#include <QtTest/QtTest>
32#include <QDebug>
33#include <private/qmedianetworkplaylistprovider_p.h>
34#include <private/qmediaplaylistnavigator_p.h>
35
36QT_USE_NAMESPACE
37class tst_QMediaPlaylistNavigator : public QObject
38{
39 Q_OBJECT
40public slots:
41 void init();
42 void cleanup();
43
44private slots:
45 void construction();
46 void setPlaylist();
47 void linearPlayback();
48 void loopPlayback();
49 void currentItemOnce();
50 void currentItemInLoop();
51 void randomPlayback();
52
53 void testItemAt();
54 void testNextIndex();
55 void testPreviousIndex();
56 void testCurrentIndexChangedSignal();
57 void testPlaybackModeChangedSignal();
58 void testSurroundingItemsChangedSignal();
59 void testActivatedSignal();
60};
61
62void tst_QMediaPlaylistNavigator::init()
63{
64 qRegisterMetaType<QMediaPlaylist::PlaybackMode>(typeName: "QMediaPlaylist::PlaybackMode");
65 qRegisterMetaType<QMediaContent>(typeName: "QMediaContent");
66}
67
68void tst_QMediaPlaylistNavigator::cleanup()
69{
70}
71
72void tst_QMediaPlaylistNavigator::construction()
73{
74 QMediaNetworkPlaylistProvider playlist;
75 QCOMPARE(playlist.mediaCount(), 0);
76
77 QMediaPlaylistNavigator navigator(&playlist);
78 QVERIFY(navigator.currentItem().isNull());
79 QCOMPARE(navigator.currentIndex(), -1);
80}
81
82void tst_QMediaPlaylistNavigator::setPlaylist()
83{
84 QMediaPlaylistNavigator navigator(0);
85 QVERIFY(navigator.playlist() != 0);
86 QCOMPARE(navigator.playlist()->mediaCount(), 0);
87 QCOMPARE(navigator.playlist()->media(0), QMediaContent());
88 QVERIFY(navigator.playlist()->isReadOnly() );
89
90 QMediaNetworkPlaylistProvider playlist;
91 QCOMPARE(playlist.mediaCount(), 0);
92
93 navigator.setPlaylist(&playlist);
94 QCOMPARE(navigator.playlist(), (QMediaPlaylistProvider*)&playlist);
95 QCOMPARE(navigator.playlist()->mediaCount(), 0);
96 QVERIFY(!navigator.playlist()->isReadOnly() );
97}
98
99void tst_QMediaPlaylistNavigator::linearPlayback()
100{
101 QMediaNetworkPlaylistProvider playlist;
102 QMediaPlaylistNavigator navigator(&playlist);
103
104 navigator.setPlaybackMode(QMediaPlaylist::Sequential);
105 navigator.jump(0);
106 QVERIFY(navigator.currentItem().isNull());
107 QCOMPARE(navigator.currentIndex(), -1);
108
109 QMediaContent content1(QUrl(QLatin1String("file:///1")));
110 playlist.addMedia(content: content1);
111 navigator.jump(0);
112 QVERIFY(!navigator.currentItem().isNull());
113
114 QCOMPARE(navigator.currentIndex(), 0);
115 QCOMPARE(navigator.currentItem(), content1);
116 QCOMPARE(navigator.nextItem(), QMediaContent());
117 QCOMPARE(navigator.nextItem(2), QMediaContent());
118 QCOMPARE(navigator.previousItem(), QMediaContent());
119 QCOMPARE(navigator.previousItem(2), QMediaContent());
120
121 QMediaContent content2(QUrl(QLatin1String("file:///2")));
122 playlist.addMedia(content: content2);
123 QCOMPARE(navigator.currentIndex(), 0);
124 QCOMPARE(navigator.currentItem(), content1);
125 QCOMPARE(navigator.nextItem(), content2);
126 QCOMPARE(navigator.nextItem(2), QMediaContent());
127 QCOMPARE(navigator.previousItem(), QMediaContent());
128 QCOMPARE(navigator.previousItem(2), QMediaContent());
129
130 navigator.jump(1);
131 QCOMPARE(navigator.currentIndex(), 1);
132 QCOMPARE(navigator.currentItem(), content2);
133 QCOMPARE(navigator.nextItem(), QMediaContent());
134 QCOMPARE(navigator.nextItem(2), QMediaContent());
135 QCOMPARE(navigator.previousItem(), content1);
136 QCOMPARE(navigator.previousItem(2), QMediaContent());
137
138 navigator.jump(0);
139 navigator.next();
140 QCOMPARE(navigator.currentIndex(), 1);
141 navigator.next();
142 QCOMPARE(navigator.currentIndex(), -1);
143 navigator.next();//jump to the first item
144 QCOMPARE(navigator.currentIndex(), 0);
145
146 navigator.previous();
147 QCOMPARE(navigator.currentIndex(), -1);
148 navigator.previous();//jump to the last item
149 QCOMPARE(navigator.currentIndex(), 1);
150}
151
152void tst_QMediaPlaylistNavigator::loopPlayback()
153{
154 QMediaNetworkPlaylistProvider playlist;
155 QMediaPlaylistNavigator navigator(&playlist);
156
157 navigator.setPlaybackMode(QMediaPlaylist::Loop);
158 navigator.jump(0);
159 QVERIFY(navigator.currentItem().isNull());
160 QCOMPARE(navigator.currentIndex(), -1);
161
162 QMediaContent content1(QUrl(QLatin1String("file:///1")));
163 playlist.addMedia(content: content1);
164 navigator.jump(0);
165 QVERIFY(!navigator.currentItem().isNull());
166
167 QCOMPARE(navigator.currentIndex(), 0);
168 QCOMPARE(navigator.currentItem(), content1);
169 QCOMPARE(navigator.nextItem(), content1);
170 QCOMPARE(navigator.nextItem(2), content1);
171 QCOMPARE(navigator.previousItem(), content1);
172 QCOMPARE(navigator.previousItem(2), content1);
173
174 QMediaContent content2(QUrl(QLatin1String("file:///2")));
175 playlist.addMedia(content: content2);
176 QCOMPARE(navigator.currentIndex(), 0);
177 QCOMPARE(navigator.currentItem(), content1);
178 QCOMPARE(navigator.nextItem(), content2);
179 QCOMPARE(navigator.nextItem(2), content1); //loop over end of the list
180 QCOMPARE(navigator.previousItem(), content2);
181 QCOMPARE(navigator.previousItem(2), content1);
182
183 navigator.jump(1);
184 QCOMPARE(navigator.currentIndex(), 1);
185 QCOMPARE(navigator.currentItem(), content2);
186 QCOMPARE(navigator.nextItem(), content1);
187 QCOMPARE(navigator.nextItem(2), content2);
188 QCOMPARE(navigator.previousItem(), content1);
189 QCOMPARE(navigator.previousItem(2), content2);
190
191 navigator.jump(0);
192 navigator.next();
193 QCOMPARE(navigator.currentIndex(), 1);
194 navigator.next();
195 QCOMPARE(navigator.currentIndex(), 0);
196 navigator.previous();
197 QCOMPARE(navigator.currentIndex(), 1);
198 navigator.previous();
199 QCOMPARE(navigator.currentIndex(), 0);
200}
201
202void tst_QMediaPlaylistNavigator::currentItemOnce()
203{
204 QMediaNetworkPlaylistProvider playlist;
205 QMediaPlaylistNavigator navigator(&playlist);
206
207 navigator.setPlaybackMode(QMediaPlaylist::CurrentItemOnce);
208
209 QCOMPARE(navigator.playbackMode(), QMediaPlaylist::CurrentItemOnce);
210 QCOMPARE(navigator.currentIndex(), -1);
211
212 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///1"))));
213 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///2"))));
214 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///3"))));
215
216 QCOMPARE(navigator.currentIndex(), -1);
217 navigator.next();
218 QCOMPARE(navigator.currentIndex(), -1);
219
220 navigator.jump(1);
221 QCOMPARE(navigator.currentIndex(), 1);
222 navigator.next();
223 QCOMPARE(navigator.currentIndex(), -1);
224 navigator.next();
225 QCOMPARE(navigator.currentIndex(), -1);
226 navigator.previous();
227 QCOMPARE(navigator.currentIndex(), -1);
228 navigator.jump(1);
229 navigator.previous();
230 QCOMPARE(navigator.currentIndex(), -1);
231}
232
233void tst_QMediaPlaylistNavigator::currentItemInLoop()
234{
235 QMediaNetworkPlaylistProvider playlist;
236 QMediaPlaylistNavigator navigator(&playlist);
237
238 navigator.setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
239
240 QCOMPARE(navigator.playbackMode(), QMediaPlaylist::CurrentItemInLoop);
241 QCOMPARE(navigator.currentIndex(), -1);
242
243 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///1"))));
244 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///2"))));
245 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///3"))));
246
247 QCOMPARE(navigator.currentIndex(), -1);
248 navigator.next();
249 QCOMPARE(navigator.currentIndex(), -1);
250 navigator.jump(1);
251 navigator.next();
252 QCOMPARE(navigator.currentIndex(), 1);
253 navigator.next();
254 QCOMPARE(navigator.currentIndex(), 1);
255 navigator.previous();
256 QCOMPARE(navigator.currentIndex(), 1);
257 navigator.previous();
258 QCOMPARE(navigator.currentIndex(), 1);
259}
260
261void tst_QMediaPlaylistNavigator::randomPlayback()
262{
263 QMediaNetworkPlaylistProvider playlist;
264 QMediaPlaylistNavigator navigator(&playlist);
265
266 navigator.setPlaybackMode(QMediaPlaylist::Random);
267
268 QCOMPARE(navigator.playbackMode(), QMediaPlaylist::Random);
269 QCOMPARE(navigator.currentIndex(), -1);
270
271 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///1"))));
272 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///2"))));
273 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///3"))));
274
275 playlist.shuffle();
276
277 QCOMPARE(navigator.currentIndex(), -1);
278 navigator.next();
279 int pos1 = navigator.currentIndex();
280 navigator.next();
281 int pos2 = navigator.currentIndex();
282 navigator.next();
283 int pos3 = navigator.currentIndex();
284
285 QVERIFY(pos1 != -1);
286 QVERIFY(pos2 != -1);
287 QVERIFY(pos3 != -1);
288
289 navigator.previous();
290 QCOMPARE(navigator.currentIndex(), pos2);
291 navigator.next();
292 QCOMPARE(navigator.currentIndex(), pos3);
293 navigator.next();
294 int pos4 = navigator.currentIndex();
295 navigator.previous();
296 QCOMPARE(navigator.currentIndex(), pos3);
297 navigator.previous();
298 QCOMPARE(navigator.currentIndex(), pos2);
299 navigator.previous();
300 QCOMPARE(navigator.currentIndex(), pos1);
301 navigator.previous();
302 int pos0 = navigator.currentIndex();
303 QVERIFY(pos0 != -1);
304 navigator.next();
305 navigator.next();
306 navigator.next();
307 navigator.next();
308 QCOMPARE(navigator.currentIndex(), pos4);
309
310}
311
312void tst_QMediaPlaylistNavigator::testItemAt()
313{
314 QMediaNetworkPlaylistProvider playlist;
315 QMediaPlaylistNavigator navigator(&playlist);
316 navigator.setPlaybackMode(QMediaPlaylist::Random);
317 QCOMPARE(navigator.playbackMode(), QMediaPlaylist::Random);
318 QCOMPARE(navigator.currentIndex(), -1);
319
320 //Adding the media to the playlist
321 QMediaContent content = QMediaContent(QUrl(QLatin1String("file:///1")));
322 playlist.addMedia(content);
323
324 //Currently it is not pointing to any index , Returns Null mediacontent
325 QCOMPARE(navigator.currentIndex(), -1);
326 QCOMPARE(navigator.itemAt(navigator.currentIndex()),QMediaContent());
327 navigator.next();
328
329 //Points to the added media
330 int pos1 = navigator.currentIndex();
331 QCOMPARE(content,navigator.itemAt(pos1));
332}
333
334void tst_QMediaPlaylistNavigator::testNextIndex()
335{
336 QMediaNetworkPlaylistProvider playlist;
337 QMediaPlaylistNavigator navigator(&playlist);
338 navigator.setPlaybackMode(QMediaPlaylist::Random);
339 QCOMPARE(navigator.playbackMode(), QMediaPlaylist::Random);
340 QCOMPARE(navigator.currentIndex(), -1);
341
342 //Adding the media to the playlist
343 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///1"))));
344 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///2"))));
345 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///3"))));
346
347 playlist.shuffle();
348
349 //Currently it is not pointing to any index
350 QCOMPARE(navigator.currentIndex(), -1);
351 navigator.next();
352 int pos1 = navigator.currentIndex();
353 //Pointing to the next index
354 navigator.next();
355 int pos2 = navigator.currentIndex();
356 navigator.next();
357 int pos3 = navigator.currentIndex();
358
359 //Pointing to the previous index
360 navigator.previous();
361 QCOMPARE(navigator.nextIndex(1), pos3);
362 navigator.previous();
363 QCOMPARE(navigator.nextIndex(1), pos2);
364 QCOMPARE(navigator.nextIndex(2), pos3);
365 navigator.previous();
366 QCOMPARE(navigator.nextIndex(1), pos1);
367}
368
369void tst_QMediaPlaylistNavigator::testPreviousIndex()
370{
371 QMediaNetworkPlaylistProvider playlist;
372 QMediaPlaylistNavigator navigator(&playlist);
373 navigator.setPlaybackMode(QMediaPlaylist::Random);
374 QCOMPARE(navigator.playbackMode(), QMediaPlaylist::Random);
375 QCOMPARE(navigator.currentIndex(), -1);
376
377 //Adding the media to the playlist
378 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///1"))));
379 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///2"))));
380 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///3"))));
381 playlist.shuffle();
382
383 //Currently it is not pointing to any index
384 QCOMPARE(navigator.currentIndex(), -1);
385
386 //pointing to next index
387 navigator.next();
388 int pos1 = navigator.currentIndex();
389 navigator.next();
390 int pos2 = navigator.currentIndex();
391 navigator.next();
392 int pos3 = navigator.currentIndex();
393 QCOMPARE(navigator.previousIndex(1), pos2);
394 QCOMPARE(navigator.previousIndex(2), pos1);
395 navigator.next();
396 QCOMPARE(navigator.previousIndex(1), pos3);
397}
398
399void tst_QMediaPlaylistNavigator::testCurrentIndexChangedSignal()
400{
401 QMediaNetworkPlaylistProvider playlist;
402 QMediaPlaylistNavigator navigator(&playlist);
403 QCOMPARE(navigator.playbackMode(), QMediaPlaylist::Sequential);
404 QCOMPARE(navigator.currentIndex(), -1);
405
406 //Creating a QSignalSpy object for currentIndexChanged() signal
407 QSignalSpy spy(&navigator,SIGNAL(currentIndexChanged(int)));
408 QVERIFY(spy.count() == 0);
409
410 //Adding the media to the playlist
411 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///1"))));
412 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///2"))));
413 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///3"))));
414
415 //Currently it is not pointing to any index
416 QCOMPARE(navigator.currentIndex(), -1);
417 navigator.next();
418 QVERIFY(spy.count() == 1);
419 int pos1 = navigator.currentIndex();
420 //Pointing to the next index
421 navigator.next();
422 QVERIFY(navigator.previousIndex(1) == pos1);
423 QVERIFY(spy.count() == 2);
424}
425
426void tst_QMediaPlaylistNavigator::testPlaybackModeChangedSignal()
427{
428 QMediaNetworkPlaylistProvider playlist;
429 QMediaPlaylistNavigator navigator(&playlist);
430 navigator.setPlaybackMode(QMediaPlaylist::Random);
431 QCOMPARE(navigator.playbackMode(), QMediaPlaylist::Random);
432 QCOMPARE(navigator.currentIndex(), -1);
433
434 //Creating a QSignalSpy object for currentIndexChanged() signal
435 QSignalSpy spy(&navigator,SIGNAL(playbackModeChanged(QMediaPlaylist::PlaybackMode)));
436 QVERIFY(spy.count() == 0);
437
438 //Adding the media to the playlist
439 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///1"))));
440
441 //set the play back mode to sequential
442 navigator.setPlaybackMode(QMediaPlaylist::Sequential);
443 QCOMPARE(navigator.playbackMode(), QMediaPlaylist::Sequential);
444 QVERIFY(spy.count() == 1);
445
446 //set the play back mode to loop
447 navigator.setPlaybackMode(QMediaPlaylist::Loop);
448 QCOMPARE(navigator.playbackMode(), QMediaPlaylist::Loop);
449 QVERIFY(spy.count() == 2);
450}
451
452void tst_QMediaPlaylistNavigator::testSurroundingItemsChangedSignal()
453{
454 QMediaNetworkPlaylistProvider playlist;
455 QMediaPlaylistNavigator navigator(&playlist);
456 navigator.setPlaybackMode(QMediaPlaylist::Random);
457 QCOMPARE(navigator.playbackMode(), QMediaPlaylist::Random);
458 QCOMPARE(navigator.currentIndex(), -1);
459
460 //Creating a QSignalSpy object for surroundingItemsChanged()signal
461 QSignalSpy spy(&navigator,SIGNAL(surroundingItemsChanged()));
462 QVERIFY(spy.count() == 0);
463
464 //Adding the media to the playlist
465 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///1"))));
466 QVERIFY(spy.count() == 1);
467
468 //set the play back mode to sequential
469 navigator.setPlaybackMode(QMediaPlaylist::Sequential);
470 QCOMPARE(navigator.playbackMode(), QMediaPlaylist::Sequential);
471 QVERIFY(spy.count() == 2);
472
473 //Point to the next index
474 navigator.next();
475 QVERIFY(spy.count() == 3);
476}
477
478void tst_QMediaPlaylistNavigator::testActivatedSignal()
479{
480 QMediaNetworkPlaylistProvider playlist;
481 QMediaPlaylistNavigator navigator(&playlist);
482 navigator.setPlaybackMode(QMediaPlaylist::Random);
483 QCOMPARE(navigator.playbackMode(), QMediaPlaylist::Random);
484 QCOMPARE(navigator.currentIndex(), -1);
485
486 //Creating a QSignalSpy object for surroundingItemsChanged()signal
487 QSignalSpy spy(&navigator,SIGNAL(activated(QMediaContent)));
488 QVERIFY(spy.count() == 0);
489
490 //Adding the media to the playlist
491 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///1"))));
492 playlist.addMedia(content: QMediaContent(QUrl(QLatin1String("file:///2"))));
493 playlist.shuffle();
494
495 //Point to the next index
496 navigator.next();
497 QVERIFY(spy.count() == 1);
498
499 //Jump to 0th item
500 navigator.jump(0);
501 QVERIFY(spy.count() == 2);
502
503 //move to previous item
504 navigator.previous();
505 QVERIFY(spy.count() == 3);
506}
507
508QTEST_MAIN(tst_QMediaPlaylistNavigator)
509#include "tst_qmediaplaylistnavigator.moc"
510

source code of qtmultimedia/tests/auto/unit/qmediaplaylistnavigator/tst_qmediaplaylistnavigator.cpp