From 63cff37741dca71f3db45ee06bc5bb06488c51f4 Mon Sep 17 00:00:00 2001 From: Yoann Lopes Date: Wed, 18 Feb 2015 16:58:02 +0100 Subject: QMediaPlayer: handle resource files in a cross-platform way. It was the backend's responsibility to handle resource files in an appropriate way. In practice, it was either not handled at all, or implemented in an almost identical manner in every backend that does handle it. This is now dealt with in QMediaPlayer, always passing to the backend something it will be able to play. If the backend has the StreamPlayback capability, we pass a QFile from which it streams the data. If it doesn't, we copy the resource to a temporary file and pass its path to the backend. Task-number: QTBUG-36175 Task-number: QTBUG-42263 Task-number: QTBUG-43839 Change-Id: I57b355c72692d02661baeaf74e66581ca0a0bd1d Reviewed-by: Andrew Knight Reviewed-by: Peng Wu Reviewed-by: Christian Stromme --- tests/auto/unit/qmediaplayer/qmediaplayer.pro | 1 + tests/auto/unit/qmediaplayer/testdata.qrc | 5 ++ .../auto/unit/qmediaplayer/testdata/nokia-tune.mp3 | Bin 0 -> 62715 bytes tests/auto/unit/qmediaplayer/tst_qmediaplayer.cpp | 90 ++++++++++++++++++++- .../qmultimedia_common/mockmediaplayercontrol.h | 7 +- .../qmultimedia_common/mockmediaserviceprovider.h | 11 +++ 6 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 tests/auto/unit/qmediaplayer/testdata.qrc create mode 100644 tests/auto/unit/qmediaplayer/testdata/nokia-tune.mp3 (limited to 'tests/auto/unit') diff --git a/tests/auto/unit/qmediaplayer/qmediaplayer.pro b/tests/auto/unit/qmediaplayer/qmediaplayer.pro index 52568c071..cbdbf71f4 100644 --- a/tests/auto/unit/qmediaplayer/qmediaplayer.pro +++ b/tests/auto/unit/qmediaplayer/qmediaplayer.pro @@ -2,6 +2,7 @@ CONFIG += testcase no_private_qt_headers_warning TARGET = tst_qmediaplayer QT += network multimedia-private testlib SOURCES += tst_qmediaplayer.cpp +RESOURCES += testdata.qrc include (../qmultimedia_common/mock.pri) include (../qmultimedia_common/mockplayer.pri) diff --git a/tests/auto/unit/qmediaplayer/testdata.qrc b/tests/auto/unit/qmediaplayer/testdata.qrc new file mode 100644 index 000000000..1afc630dd --- /dev/null +++ b/tests/auto/unit/qmediaplayer/testdata.qrc @@ -0,0 +1,5 @@ + + + testdata/nokia-tune.mp3 + + diff --git a/tests/auto/unit/qmediaplayer/testdata/nokia-tune.mp3 b/tests/auto/unit/qmediaplayer/testdata/nokia-tune.mp3 new file mode 100644 index 000000000..2435f65b8 Binary files /dev/null and b/tests/auto/unit/qmediaplayer/testdata/nokia-tune.mp3 differ diff --git a/tests/auto/unit/qmediaplayer/tst_qmediaplayer.cpp b/tests/auto/unit/qmediaplayer/tst_qmediaplayer.cpp index 2082fe14a..0271f1a8f 100644 --- a/tests/auto/unit/qmediaplayer/tst_qmediaplayer.cpp +++ b/tests/auto/unit/qmediaplayer/tst_qmediaplayer.cpp @@ -133,6 +133,8 @@ private slots: void testPlayerFlags(); void testDestructor(); void testSupportedMimeTypes(); + void testQrc_data(); + void testQrc(); private: void setupCommonTestData(); @@ -976,12 +978,17 @@ void tst_QMediaPlayer::testPlaylist() player->setPlaylist(playlist); player->play(); QCOMPARE(ss.count(), 1); + QCOMPARE(ms.count(), 1); + QCOMPARE(qvariant_cast(ms.last().value(0)), QMediaPlayer::LoadingMedia); + ms.clear(); mockService->setState(QMediaPlayer::StoppedState, QMediaPlayer::InvalidMedia); QCOMPARE(player->state(), QMediaPlayer::PlayingState); - QCOMPARE(player->mediaStatus(), QMediaPlayer::InvalidMedia); + QCOMPARE(player->mediaStatus(), QMediaPlayer::LoadingMedia); QCOMPARE(ss.count(), 1); - QCOMPARE(ms.count(), 1); + QCOMPARE(ms.count(), 2); + QCOMPARE(qvariant_cast(ms.at(0).value(0)), QMediaPlayer::InvalidMedia); + QCOMPARE(qvariant_cast(ms.at(1).value(0)), QMediaPlayer::LoadingMedia); // NOTE: status should begin transitioning through to BufferedMedia. QCOMPARE(player->currentMedia(), content1); @@ -1210,5 +1217,84 @@ void tst_QMediaPlayer::testSupportedMimeTypes() // This is empty on some platforms, and not on others, so can't test something here at the moment. } +void tst_QMediaPlayer::testQrc_data() +{ + QTest::addColumn("mediaContent"); + QTest::addColumn("status"); + QTest::addColumn("error"); + QTest::addColumn("errorCount"); + QTest::addColumn("hasStreamFeature"); + QTest::addColumn("backendMediaContentScheme"); + QTest::addColumn("backendHasStream"); + + QTest::newRow("invalid") << QMediaContent(QUrl(QLatin1String("qrc:/invalid.mp3"))) + << QMediaPlayer::InvalidMedia + << QMediaPlayer::ResourceError + << 1 // error count + << false // No StreamPlayback support + << QString() // backend should not have got any media (empty URL scheme) + << false; // backend should not have got any stream + + QTest::newRow("valid+nostream") << QMediaContent(QUrl(QLatin1String("qrc:/testdata/nokia-tune.mp3"))) + << QMediaPlayer::LoadingMedia + << QMediaPlayer::NoError + << 0 // error count + << false // No StreamPlayback support + << QStringLiteral("file") // backend should have a got a temporary file + << false; // backend should not have got any stream + + QTest::newRow("valid+stream") << QMediaContent(QUrl(QLatin1String("qrc:/testdata/nokia-tune.mp3"))) + << QMediaPlayer::LoadingMedia + << QMediaPlayer::NoError + << 0 // error count + << true // StreamPlayback support + << QStringLiteral("qrc") + << true; // backend should have got a stream (QFile opened from the resource) +} + +void tst_QMediaPlayer::testQrc() +{ + QFETCH(QMediaContent, mediaContent); + QFETCH(QMediaPlayer::MediaStatus, status); + QFETCH(QMediaPlayer::Error, error); + QFETCH(int, errorCount); + QFETCH(bool, hasStreamFeature); + QFETCH(QString, backendMediaContentScheme); + QFETCH(bool, backendHasStream); + + if (hasStreamFeature) + mockProvider->setSupportedFeatures(QMediaServiceProviderHint::StreamPlayback); + + QMediaPlayer player; + + mockService->setState(QMediaPlayer::PlayingState, QMediaPlayer::NoMedia); + + QSignalSpy mediaSpy(&player, SIGNAL(currentMediaChanged(QMediaContent))); + QSignalSpy statusSpy(&player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus))); + QSignalSpy errorSpy(&player, SIGNAL(error(QMediaPlayer::Error))); + + player.setMedia(mediaContent); + + QTRY_COMPARE(player.mediaStatus(), status); + QCOMPARE(statusSpy.count(), 1); + QCOMPARE(qvariant_cast(statusSpy.last().value(0)), status); + + QCOMPARE(player.media(), mediaContent); + QCOMPARE(player.currentMedia(), mediaContent); + QCOMPARE(mediaSpy.count(), 1); + QCOMPARE(qvariant_cast(mediaSpy.last().value(0)), mediaContent); + + QCOMPARE(player.error(), error); + QCOMPARE(errorSpy.count(), errorCount); + if (errorCount > 0) { + QCOMPARE(qvariant_cast(errorSpy.last().value(0)), error); + QVERIFY(!player.errorString().isEmpty()); + } + + // Check the media actually passed to the backend + QCOMPARE(mockService->mockControl->media().canonicalUrl().scheme(), backendMediaContentScheme); + QCOMPARE(bool(mockService->mockControl->mediaStream()), backendHasStream); +} + QTEST_GUILESS_MAIN(tst_QMediaPlayer) #include "tst_qmediaplayer.moc" diff --git a/tests/auto/unit/qmultimedia_common/mockmediaplayercontrol.h b/tests/auto/unit/qmultimedia_common/mockmediaplayercontrol.h index 5b313fb95..5127498bc 100644 --- a/tests/auto/unit/qmultimedia_common/mockmediaplayercontrol.h +++ b/tests/auto/unit/qmultimedia_common/mockmediaplayercontrol.h @@ -91,11 +91,10 @@ public: { _stream = stream; _media = content; - if (_state != QMediaPlayer::StoppedState) { - _mediaStatus = _media.isNull() ? QMediaPlayer::NoMedia : QMediaPlayer::LoadingMedia; + _mediaStatus = _media.isNull() ? QMediaPlayer::NoMedia : QMediaPlayer::LoadingMedia; + if (_state != QMediaPlayer::StoppedState) emit stateChanged(_state = QMediaPlayer::StoppedState); - emit mediaStatusChanged(_mediaStatus); - } + emit mediaStatusChanged(_mediaStatus); emit mediaChanged(_media = content); } QIODevice *mediaStream() const { return _stream; } diff --git a/tests/auto/unit/qmultimedia_common/mockmediaserviceprovider.h b/tests/auto/unit/qmultimedia_common/mockmediaserviceprovider.h index 60ea672c9..89820c557 100644 --- a/tests/auto/unit/qmultimedia_common/mockmediaserviceprovider.h +++ b/tests/auto/unit/qmultimedia_common/mockmediaserviceprovider.h @@ -61,6 +61,16 @@ public: } } + QMediaServiceProviderHint::Features supportedFeatures(const QMediaService *) const + { + return features; + } + + void setSupportedFeatures(QMediaServiceProviderHint::Features f) + { + features = f; + } + QByteArray defaultDevice(const QByteArray &serviceType) const { if (serviceType == Q_MEDIASERVICE_CAMERA) @@ -97,6 +107,7 @@ public: QMediaService *service; bool deleteServiceOnRelease; + QMediaServiceProviderHint::Features features; }; #endif // MOCKMEDIASERVICEPROVIDER_H -- cgit v1.2.3