summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoïc Molinari <loic.molinari@canonical.com>2015-08-09 21:35:07 +0200
committerYoann Lopes <yoann.lopes@theqtcompany.com>2015-08-10 11:37:44 +0000
commit475a14ccc39aad0f73b039990412b00f52e0c81e (patch)
tree9a1fdfc09da47fbf624c450478f974dfe2f223c4
parent414b748b23dee51af1e54acf2369639c38256161 (diff)
downloadqtmultimedia-475a14ccc39aad0f73b039990412b00f52e0c81e.tar.gz
qtmultimedia-475a14ccc39aad0f73b039990412b00f52e0c81e.tar.bz2
qtmultimedia-475a14ccc39aad0f73b039990412b00f52e0c81e.zip
Check and fix up bounds in QMediaPlaylist methods.
Change-Id: I665d665139dbe9663b20ecb08fa3dab9cbe3f899 Reviewed-by: Yoann Lopes <yoann.lopes@theqtcompany.com>
-rw-r--r--src/multimedia/playback/qmediaplaylist.cpp22
-rw-r--r--tests/auto/unit/qmediaplaylist/tst_qmediaplaylist.cpp117
2 files changed, 133 insertions, 6 deletions
diff --git a/src/multimedia/playback/qmediaplaylist.cpp b/src/multimedia/playback/qmediaplaylist.cpp
index 3a08b7e5..9b5d9a68 100644
--- a/src/multimedia/playback/qmediaplaylist.cpp
+++ b/src/multimedia/playback/qmediaplaylist.cpp
@@ -358,7 +358,8 @@ bool QMediaPlaylist::addMedia(const QList<QMediaContent> &items)
bool QMediaPlaylist::insertMedia(int pos, const QMediaContent &content)
{
- return d_func()->playlist()->insertMedia(pos, content);
+ QMediaPlaylistProvider *playlist = d_func()->playlist();
+ return playlist->insertMedia(qBound(0, pos, playlist->mediaCount()), content);
}
/*!
@@ -369,7 +370,8 @@ bool QMediaPlaylist::insertMedia(int pos, const QMediaContent &content)
bool QMediaPlaylist::insertMedia(int pos, const QList<QMediaContent> &items)
{
- return d_func()->playlist()->insertMedia(pos, items);
+ QMediaPlaylistProvider *playlist = d_func()->playlist();
+ return playlist->insertMedia(qBound(0, pos, playlist->mediaCount()), items);
}
/*!
@@ -379,8 +381,11 @@ bool QMediaPlaylist::insertMedia(int pos, const QList<QMediaContent> &items)
*/
bool QMediaPlaylist::removeMedia(int pos)
{
- Q_D(QMediaPlaylist);
- return d->playlist()->removeMedia(pos);
+ QMediaPlaylistProvider *playlist = d_func()->playlist();
+ if (pos >= 0 && pos < playlist->mediaCount())
+ return playlist->removeMedia(pos);
+ else
+ return false;
}
/*!
@@ -390,8 +395,13 @@ bool QMediaPlaylist::removeMedia(int pos)
*/
bool QMediaPlaylist::removeMedia(int start, int end)
{
- Q_D(QMediaPlaylist);
- return d->playlist()->removeMedia(start, end);
+ QMediaPlaylistProvider *playlist = d_func()->playlist();
+ start = qMax(0, start);
+ end = qMin(end, playlist->mediaCount() - 1);
+ if (start <= end)
+ return playlist->removeMedia(start, end);
+ else
+ return false;
}
/*!
diff --git a/tests/auto/unit/qmediaplaylist/tst_qmediaplaylist.cpp b/tests/auto/unit/qmediaplaylist/tst_qmediaplaylist.cpp
index f96529c1..daecda82 100644
--- a/tests/auto/unit/qmediaplaylist/tst_qmediaplaylist.cpp
+++ b/tests/auto/unit/qmediaplaylist/tst_qmediaplaylist.cpp
@@ -234,6 +234,71 @@ void tst_QMediaPlaylist::insert()
playlist.insertMedia(1, QList<QMediaContent>());
QCOMPARE(aboutToBeInsertedSignalSpy.count(), 0);
QCOMPARE(insertedSignalSpy.count(), 0);
+
+ playlist.clear();
+ playlist.addMedia(content1);
+ aboutToBeInsertedSignalSpy.clear();
+ insertedSignalSpy.clear();
+
+ playlist.insertMedia(-10, content2);
+ QCOMPARE(playlist.media(0), content2);
+ QCOMPARE(playlist.media(1), content1);
+ QCOMPARE(aboutToBeInsertedSignalSpy.count(), 1);
+ QCOMPARE(aboutToBeInsertedSignalSpy.first()[0].toInt(), 0);
+ QCOMPARE(aboutToBeInsertedSignalSpy.first()[1].toInt(), 0);
+ QCOMPARE(insertedSignalSpy.count(), 1);
+ QCOMPARE(insertedSignalSpy.first()[0].toInt(), 0);
+ QCOMPARE(insertedSignalSpy.first()[1].toInt(), 0);
+
+ aboutToBeInsertedSignalSpy.clear();
+ insertedSignalSpy.clear();
+
+ playlist.insertMedia(10, content3);
+ QCOMPARE(playlist.media(0), content2);
+ QCOMPARE(playlist.media(1), content1);
+ QCOMPARE(playlist.media(2), content3);
+ QCOMPARE(aboutToBeInsertedSignalSpy.count(), 1);
+ QCOMPARE(aboutToBeInsertedSignalSpy.first()[0].toInt(), 2);
+ QCOMPARE(aboutToBeInsertedSignalSpy.first()[1].toInt(), 2);
+ QCOMPARE(insertedSignalSpy.count(), 1);
+ QCOMPARE(insertedSignalSpy.first()[0].toInt(), 2);
+ QCOMPARE(insertedSignalSpy.first()[1].toInt(), 2);
+
+ aboutToBeInsertedSignalSpy.clear();
+ insertedSignalSpy.clear();
+
+ playlist.insertMedia(-10, QList<QMediaContent>() << content4 << content5);
+ QCOMPARE(playlist.media(0), content4);
+ QCOMPARE(playlist.media(1), content5);
+ QCOMPARE(playlist.media(2), content2);
+ QCOMPARE(playlist.media(3), content1);
+ QCOMPARE(playlist.media(4), content3);
+ QCOMPARE(aboutToBeInsertedSignalSpy.count(), 1);
+ QCOMPARE(aboutToBeInsertedSignalSpy.first()[0].toInt(), 0);
+ QCOMPARE(aboutToBeInsertedSignalSpy.first()[1].toInt(), 1);
+ QCOMPARE(insertedSignalSpy.count(), 1);
+ QCOMPARE(insertedSignalSpy.first()[0].toInt(), 0);
+ QCOMPARE(insertedSignalSpy.first()[1].toInt(), 1);
+
+ aboutToBeInsertedSignalSpy.clear();
+ insertedSignalSpy.clear();
+
+ QMediaContent content6(QUrl(QLatin1String("file:///6")));
+ QMediaContent content7(QUrl(QLatin1String("file:///7")));
+ playlist.insertMedia(10, QList<QMediaContent>() << content6 << content7);
+ QCOMPARE(playlist.media(0), content4);
+ QCOMPARE(playlist.media(1), content5);
+ QCOMPARE(playlist.media(2), content2);
+ QCOMPARE(playlist.media(3), content1);
+ QCOMPARE(playlist.media(4), content3);
+ QCOMPARE(playlist.media(5), content6);
+ QCOMPARE(playlist.media(6), content7);
+ QCOMPARE(aboutToBeInsertedSignalSpy.count(), 1);
+ QCOMPARE(aboutToBeInsertedSignalSpy.first()[0].toInt(), 5);
+ QCOMPARE(aboutToBeInsertedSignalSpy.first()[1].toInt(), 6);
+ QCOMPARE(insertedSignalSpy.count(), 1);
+ QCOMPARE(insertedSignalSpy.first()[0].toInt(), 5);
+ QCOMPARE(insertedSignalSpy.first()[1].toInt(), 6);
}
@@ -329,6 +394,58 @@ void tst_QMediaPlaylist::removeMedia()
playlist.removeMedia(0,1);
QCOMPARE(playlist.mediaCount(), 1);
QCOMPARE(playlist.media(0), content3);
+
+ QCOMPARE(playlist.removeMedia(-1), false);
+ QCOMPARE(playlist.removeMedia(1), false);
+
+ playlist.addMedia(content1);
+ aboutToBeRemovedSignalSpy.clear();
+ removedSignalSpy.clear();
+
+ playlist.removeMedia(-10, 10);
+ QCOMPARE(aboutToBeRemovedSignalSpy.count(), 1);
+ QCOMPARE(aboutToBeRemovedSignalSpy.first()[0].toInt(), 0);
+ QCOMPARE(aboutToBeRemovedSignalSpy.first()[1].toInt(), 1);
+ QCOMPARE(removedSignalSpy.count(), 1);
+ QCOMPARE(removedSignalSpy.first()[0].toInt(), 0);
+ QCOMPARE(removedSignalSpy.first()[1].toInt(), 1);
+
+ playlist.addMedia(content1);
+ playlist.addMedia(content2);
+ playlist.addMedia(content3);
+ aboutToBeRemovedSignalSpy.clear();
+ removedSignalSpy.clear();
+
+ QCOMPARE(playlist.removeMedia(10, -10), false);
+ QCOMPARE(aboutToBeRemovedSignalSpy.count(), 0);
+ QCOMPARE(removedSignalSpy.count(), 0);
+ QCOMPARE(playlist.removeMedia(-10, -5), false);
+ QCOMPARE(aboutToBeRemovedSignalSpy.count(), 0);
+ QCOMPARE(removedSignalSpy.count(), 0);
+ QCOMPARE(playlist.removeMedia(5, 10), false);
+ QCOMPARE(aboutToBeRemovedSignalSpy.count(), 0);
+ QCOMPARE(removedSignalSpy.count(), 0);
+
+ playlist.removeMedia(1, 10);
+ QCOMPARE(aboutToBeRemovedSignalSpy.count(), 1);
+ QCOMPARE(aboutToBeRemovedSignalSpy.first()[0].toInt(), 1);
+ QCOMPARE(aboutToBeRemovedSignalSpy.first()[1].toInt(), 2);
+ QCOMPARE(removedSignalSpy.count(), 1);
+ QCOMPARE(removedSignalSpy.first()[0].toInt(), 1);
+ QCOMPARE(removedSignalSpy.first()[1].toInt(), 2);
+
+ playlist.addMedia(content2);
+ playlist.addMedia(content3);
+ aboutToBeRemovedSignalSpy.clear();
+ removedSignalSpy.clear();
+
+ playlist.removeMedia(-10, 1);
+ QCOMPARE(aboutToBeRemovedSignalSpy.count(), 1);
+ QCOMPARE(aboutToBeRemovedSignalSpy.first()[0].toInt(), 0);
+ QCOMPARE(aboutToBeRemovedSignalSpy.first()[1].toInt(), 1);
+ QCOMPARE(removedSignalSpy.count(), 1);
+ QCOMPARE(removedSignalSpy.first()[0].toInt(), 0);
+ QCOMPARE(removedSignalSpy.first()[1].toInt(), 1);
}
void tst_QMediaPlaylist::saveAndLoad()