From c4e782df9dc88ab531dad47a44bc48058e7e628c Mon Sep 17 00:00:00 2001 From: Doris Verria Date: Sun, 13 Jun 2021 17:01:05 +0200 Subject: Add test case for invalid source file/device to audiodecoder test Check that the appropriate errors are set on the decoder when media sources can not be resolved. Change-Id: Ie9bcc1298bf561a4ee4a90e4d6d74869541a1c6e Reviewed-by: Volker Hilsheimer Reviewed-by: Lars Knoll --- .../tst_qaudiodecoderbackend.cpp | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/tests/auto/integration/qaudiodecoderbackend/tst_qaudiodecoderbackend.cpp b/tests/auto/integration/qaudiodecoderbackend/tst_qaudiodecoderbackend.cpp index b3ccb0a10..885aa34e0 100644 --- a/tests/auto/integration/qaudiodecoderbackend/tst_qaudiodecoderbackend.cpp +++ b/tests/auto/integration/qaudiodecoderbackend/tst_qaudiodecoderbackend.cpp @@ -35,6 +35,7 @@ #define TEST_FILE_NAME "testdata/test.wav" #define TEST_UNSUPPORTED_FILE_NAME "testdata/test-unsupported.avi" #define TEST_CORRUPTED_FILE_NAME "testdata/test-corrupted.wav" +#define TEST_INVALID_SOURCE "invalid" QT_USE_NAMESPACE @@ -57,6 +58,7 @@ private slots: void fileTest(); void unsupportedFileTest(); void corruptedFileTest(); + void invalidSource(); void deviceTest(); private: @@ -425,6 +427,98 @@ void tst_QAudioDecoderBackend::corruptedFileTest() QVERIFY(!d.bufferAvailable()); } +void tst_QAudioDecoderBackend::invalidSource() +{ + QAudioDecoder d; + if (d.error() == QAudioDecoder::NotSupportedError) + QSKIP("There is no audio decoding support on this platform."); + QAudioBuffer buffer; + + QVERIFY(d.state() == QAudioDecoder::StoppedState); + QVERIFY(d.bufferAvailable() == false); + QCOMPARE(d.source(), QUrl()); + QVERIFY(d.audioFormat() == QAudioFormat()); + + // Test invalid file source + QFileInfo fileInfo(TEST_INVALID_SOURCE); + QUrl url = QUrl::fromLocalFile(fileInfo.absoluteFilePath()); + d.setSource(url); + QVERIFY(d.state() == QAudioDecoder::StoppedState); + QVERIFY(!d.bufferAvailable()); + QCOMPARE(d.source(), url); + + QSignalSpy readySpy(&d, SIGNAL(bufferReady())); + QSignalSpy bufferChangedSpy(&d, SIGNAL(bufferAvailableChanged(bool))); + QSignalSpy errorSpy(&d, SIGNAL(error(QAudioDecoder::Error))); + QSignalSpy stateSpy(&d, SIGNAL(stateChanged(QAudioDecoder::State))); + QSignalSpy durationSpy(&d, SIGNAL(durationChanged(qint64))); + QSignalSpy finishedSpy(&d, SIGNAL(finished())); + QSignalSpy positionSpy(&d, SIGNAL(positionChanged(qint64))); + + d.start(); + QTRY_VERIFY(d.state() == QAudioDecoder::StoppedState); + QVERIFY(!d.bufferAvailable()); + QCOMPARE(d.audioFormat(), QAudioFormat()); + QCOMPARE(d.duration(), qint64(-1)); + QCOMPARE(d.position(), qint64(-1)); + + // Check the error code. + QTRY_VERIFY(!errorSpy.isEmpty()); + + // Have to use qvariant_cast, toInt will return 0 because unrecognized type; + QAudioDecoder::Error errorCode = qvariant_cast(errorSpy.takeLast().at(0)); + QCOMPARE(errorCode, QAudioDecoder::ResourceError); + QCOMPARE(d.error(), QAudioDecoder::ResourceError); + + // Check all other spies. + QVERIFY(readySpy.isEmpty()); + QVERIFY(bufferChangedSpy.isEmpty()); + QVERIFY(stateSpy.isEmpty()); + QVERIFY(finishedSpy.isEmpty()); + QVERIFY(positionSpy.isEmpty()); + QVERIFY(durationSpy.isEmpty()); + + errorSpy.clear(); + + d.stop(); + QTRY_COMPARE(d.state(), QAudioDecoder::StoppedState); + QCOMPARE(d.duration(), qint64(-1)); + QVERIFY(!d.bufferAvailable()); + + QFile file; + file.setFileName(TEST_INVALID_SOURCE); + file.open(QIODevice::ReadOnly); + d.setSourceDevice(&file); + + d.start(); + QTRY_VERIFY(d.state() == QAudioDecoder::StoppedState); + QVERIFY(!d.bufferAvailable()); + QCOMPARE(d.audioFormat(), QAudioFormat()); + QCOMPARE(d.duration(), qint64(-1)); + QCOMPARE(d.position(), qint64(-1)); + + // Check the error code. + QTRY_VERIFY(!errorSpy.isEmpty()); + errorCode = qvariant_cast(errorSpy.takeLast().at(0)); + QCOMPARE(errorCode, QAudioDecoder::AccessDeniedError); + QCOMPARE(d.error(), QAudioDecoder::AccessDeniedError); + + // Check all other spies. + QVERIFY(readySpy.isEmpty()); + QVERIFY(bufferChangedSpy.isEmpty()); + QVERIFY(stateSpy.isEmpty()); + QVERIFY(finishedSpy.isEmpty()); + QVERIFY(positionSpy.isEmpty()); + QVERIFY(durationSpy.isEmpty()); + + errorSpy.clear(); + + d.stop(); + QTRY_COMPARE(d.state(), QAudioDecoder::StoppedState); + QCOMPARE(d.duration(), qint64(-1)); + QVERIFY(!d.bufferAvailable()); +} + void tst_QAudioDecoderBackend::deviceTest() { if (!isWavSupported()) -- cgit v1.2.3