Rename the parsing error signal to parsingError.

Refactor the error handling code a little.

Change-Id: I717b3aaacb24660b3f26769f19ac718b73106473
Reviewed-on: http://codereview.qt-project.org/6401
Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: derick hawcroft <derick.hawcroft@nokia.com>
This commit is contained in:
Michael Goddard
2011-10-11 16:38:34 +10:00
committed by Qt by Nokia
parent 8943111428
commit 0da05239d2
5 changed files with 40 additions and 32 deletions

View File

@@ -351,7 +351,7 @@ void QSample::load()
connect(m_stream, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(decoderError()));
m_waveDecoder = new QWaveDecoder(m_stream);
connect(m_waveDecoder, SIGNAL(formatKnown()), SLOT(decoderReady()));
connect(m_waveDecoder, SIGNAL(invalidFormat()), SLOT(decoderError()));
connect(m_waveDecoder, SIGNAL(parsingError()), SLOT(decoderError()));
connect(m_waveDecoder, SIGNAL(readyRead()), SLOT(readSample()));
}

View File

@@ -105,6 +105,13 @@ qint64 QWaveDecoder::writeData(const char *data, qint64 len)
return -1;
}
void QWaveDecoder::parsingFailed()
{
Q_ASSERT(source);
source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData()));
emit parsingError();
}
void QWaveDecoder::handleData()
{
// As a special "state", if we have junk to skip, we do
@@ -112,9 +119,13 @@ void QWaveDecoder::handleData()
discardBytes(junkToSkip); // this also updates junkToSkip
// If we couldn't skip all the junk, return
if (junkToSkip > 0)
if (junkToSkip > 0) {
// We might have run out
if (source->atEnd())
parsingFailed();
return;
}
}
if (state == QWaveDecoder::InitialState) {
if (source->bytesAvailable() < qint64(sizeof(RIFFHeader)))
@@ -124,11 +135,9 @@ void QWaveDecoder::handleData()
source->read(reinterpret_cast<char *>(&riff), sizeof(RIFFHeader));
// RIFF = little endian RIFF, RIFX = big endian RIFF
if (((qstrncmp(riff.descriptor.id, "RIFF", 4) != 0) && (qstrncmp(riff.descriptor.id, "RIFX", 4) != 0)) ||
qstrncmp(riff.type, "WAVE", 4) != 0) {
source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData()));
emit invalidFormat();
if (((qstrncmp(riff.descriptor.id, "RIFF", 4) != 0) && (qstrncmp(riff.descriptor.id, "RIFX", 4) != 0))
|| qstrncmp(riff.type, "WAVE", 4) != 0) {
parsingFailed();
return;
} else {
state = QWaveDecoder::WaitingForFormatState;
@@ -160,9 +169,7 @@ void QWaveDecoder::handleData()
if (wave.audioFormat != 0 && wave.audioFormat != 1) {
// 32bit wave files have format == 0xFFFE (WAVE_FORMAT_EXTENSIBLE).
// but don't support them at the moment.
source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData()));
emit invalidFormat();
parsingFailed();
return;
} else {
format.setCodec(QLatin1String("audio/pcm"));
@@ -209,11 +216,9 @@ void QWaveDecoder::handleData()
}
}
// If we hit the end without finding data, it's a parsing error
if (source->atEnd()) {
source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData()));
emit invalidFormat();
return;
parsingFailed();
}
}

View File

@@ -82,7 +82,7 @@ public:
Q_SIGNALS:
void formatKnown();
void invalidFormat();
void parsingError();
private Q_SLOTS:
void handleData();
@@ -94,6 +94,7 @@ private:
bool enoughDataAvailable();
bool findChunk(const char *chunkId);
void discardBytes(qint64 numBytes);
void parsingFailed();
enum State {
InitialState,

View File

@@ -0,0 +1 @@
1

View File

@@ -48,7 +48,7 @@
#define QTRY_COMPARE(__expr, __expected) \
do { \
const int __step = 50; \
const int __timeout = 10000; \
const int __timeout = 1000; \
if (!(__expr)) { \
QTest::qWait(0); \
} \
@@ -82,8 +82,8 @@ public slots:
private slots:
void integrity_data();
void integrity();
void file_data();
void file();
void readAllAtOnce();
void readPerByte();
@@ -105,7 +105,7 @@ void tst_QWaveDecoder::cleanupTestCase()
{
}
void tst_QWaveDecoder::integrity_data()
void tst_QWaveDecoder::file_data()
{
QTest::addColumn<QString>("file");
QTest::addColumn<tst_QWaveDecoder::Corruption>("corruption");
@@ -115,6 +115,7 @@ void tst_QWaveDecoder::integrity_data()
QTest::addColumn<QAudioFormat::Endian>("byteorder");
QTest::newRow("File is empty") << QString("empty.wav") << tst_QWaveDecoder::NotAWav << -1 << -1 << -1 << QAudioFormat::LittleEndian;
QTest::newRow("File is one byte") << QString("onebyte.wav") << tst_QWaveDecoder::NotAWav << -1 << -1 << -1 << QAudioFormat::LittleEndian;
QTest::newRow("File is not a wav(text)") << QString("notawav.wav") << tst_QWaveDecoder::NotAWav << -1 << -1 << -1 << QAudioFormat::LittleEndian;
QTest::newRow("Wav file has no sample data") << QString("nosampledata.wav") << tst_QWaveDecoder::NoSampleData << -1 << -1 << -1 << QAudioFormat::LittleEndian;
QTest::newRow("corrupt fmt chunk descriptor") << QString("corrupt_fmtdesc_1_16_8000.le.wav") << tst_QWaveDecoder::FormatDescriptor << -1 << -1 << -1 << QAudioFormat::LittleEndian;
@@ -138,7 +139,7 @@ void tst_QWaveDecoder::integrity_data()
QTest::newRow("File isawav_2_32_44100_be.wav") << QString("isawav_2_32_44100_be.wav") << tst_QWaveDecoder::FormatDescriptor << 2 << 32 << 44100 << QAudioFormat::BigEndian;
}
void tst_QWaveDecoder::integrity()
void tst_QWaveDecoder::file()
{
QFETCH(QString, file);
QFETCH(tst_QWaveDecoder::Corruption, corruption);
@@ -147,7 +148,6 @@ void tst_QWaveDecoder::integrity()
QFETCH(int, samplerate);
QFETCH(QAudioFormat::Endian, byteorder);
QFile stream;
stream.setFileName(QString("data/") + file);
stream.open(QIODevice::ReadOnly);
@@ -156,31 +156,32 @@ void tst_QWaveDecoder::integrity()
QWaveDecoder waveDecoder(&stream);
QSignalSpy validFormatSpy(&waveDecoder, SIGNAL(formatKnown()));
QSignalSpy invalidFormatSpy(&waveDecoder, SIGNAL(invalidFormat()));
QSignalSpy parsingErrorSpy(&waveDecoder, SIGNAL(parsingError()));
if (corruption == NotAWav) {
QTRY_COMPARE(validFormatSpy.count(), 0);
QTRY_COMPARE(invalidFormatSpy.count(), 0);
QSKIP("Not all failures detected correctly yet", SkipSingle);
QTRY_COMPARE(parsingErrorSpy.count(), 1);
QCOMPARE(validFormatSpy.count(), 0);
} else if (corruption == NoSampleData) {
QTRY_COMPARE(validFormatSpy.count(), 1);
QTRY_COMPARE(invalidFormatSpy.count(), 0);
QCOMPARE(parsingErrorSpy.count(), 0);
QVERIFY(waveDecoder.audioFormat().isValid());
QVERIFY(waveDecoder.size() == 0);
QVERIFY(waveDecoder.duration() == 0);
} else if (corruption == FormatDescriptor) {
QTRY_COMPARE(invalidFormatSpy.count(), 1);
QTRY_COMPARE(validFormatSpy.count(), 0);
QTRY_COMPARE(parsingErrorSpy.count(), 1);
QCOMPARE(validFormatSpy.count(), 0);
} else if (corruption == FormatString) {
QTRY_COMPARE(invalidFormatSpy.count(), 1);
QTRY_COMPARE(validFormatSpy.count(), 0);
QTRY_COMPARE(parsingErrorSpy.count(), 1);
QCOMPARE(validFormatSpy.count(), 0);
QVERIFY(!waveDecoder.audioFormat().isValid());
} else if (corruption == DataDescriptor) {
QTRY_COMPARE(validFormatSpy.count(), 0);
QTRY_COMPARE(invalidFormatSpy.count(), 1);
QTRY_COMPARE(parsingErrorSpy.count(), 1);
QCOMPARE(validFormatSpy.count(), 0);
QVERIFY(waveDecoder.size() == 0);
} else if (corruption == None) {
QTRY_COMPARE(validFormatSpy.count(), 1);
QTRY_COMPARE(invalidFormatSpy.count(), 0);
QCOMPARE(parsingErrorSpy.count(), 0);
QVERIFY(waveDecoder.audioFormat().isValid());
QVERIFY(waveDecoder.size() > 0);
QVERIFY(waveDecoder.duration() == 250);