annotate data/fileio/test/EncodingTest.h @ 1345:c0cdacc47f4e 3.0-integration

Add filename encoding tests
author Chris Cannam
date Fri, 06 Jan 2017 12:11:08 +0000
parents
children 75ad55315db4
rev   line source
Chris@1345 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1345 2
Chris@1345 3 /*
Chris@1345 4 Sonic Visualiser
Chris@1345 5 An audio file viewer and annotation editor.
Chris@1345 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1345 7
Chris@1345 8 This program is free software; you can redistribute it and/or
Chris@1345 9 modify it under the terms of the GNU General Public License as
Chris@1345 10 published by the Free Software Foundation; either version 2 of the
Chris@1345 11 License, or (at your option) any later version. See the file
Chris@1345 12 COPYING included with this distribution for more information.
Chris@1345 13 */
Chris@1345 14
Chris@1345 15 #ifndef TEST_AUDIO_ENCODINGS_H
Chris@1345 16 #define TEST_AUDIO_ENCODINGS_H
Chris@1345 17
Chris@1345 18 // Quick tests for filename encodings and encoding of ID3 data. Not a
Chris@1345 19 // test of audio codecs.
Chris@1345 20
Chris@1345 21 #include "../AudioFileReaderFactory.h"
Chris@1345 22 #include "../AudioFileReader.h"
Chris@1345 23
Chris@1345 24 #include <cmath>
Chris@1345 25
Chris@1345 26 #include <QObject>
Chris@1345 27 #include <QtTest>
Chris@1345 28 #include <QDir>
Chris@1345 29
Chris@1345 30 #include <iostream>
Chris@1345 31
Chris@1345 32 using namespace std;
Chris@1345 33
Chris@1345 34 static QString encodingDir = "svcore/data/fileio/test/encodings";
Chris@1345 35
Chris@1345 36 static const char *mapping[][2] = {
Chris@1345 37 { u8"id3v2-iso-8859-1", u8"Café de Paris" },
Chris@1345 38 { u8"id3v2-ucs-2", u8"Café de 重庆" },
Chris@1345 39 { u8"Tëmple of Spörks", u8"Tëmple of Spörks" },
Chris@1345 40 { u8"スポークの寺院", u8"スポークの寺院" }
Chris@1345 41 };
Chris@1345 42 static const int mappingCount = 4;
Chris@1345 43
Chris@1345 44 class EncodingTest : public QObject
Chris@1345 45 {
Chris@1345 46 Q_OBJECT
Chris@1345 47
Chris@1345 48 const char *strOf(QString s) {
Chris@1345 49 return strdup(s.toLocal8Bit().data());
Chris@1345 50 }
Chris@1345 51
Chris@1345 52 private slots:
Chris@1345 53 void init()
Chris@1345 54 {
Chris@1345 55 if (!QDir(encodingDir).exists()) {
Chris@1345 56 cerr << "ERROR: Audio encoding file directory \"" << encodingDir << "\" does not exist" << endl;
Chris@1345 57 QVERIFY2(QDir(encodingDir).exists(), "Audio encoding file directory not found");
Chris@1345 58 }
Chris@1345 59 }
Chris@1345 60
Chris@1345 61 void read_data()
Chris@1345 62 {
Chris@1345 63 QTest::addColumn<QString>("audiofile");
Chris@1345 64 QStringList files = QDir(encodingDir).entryList(QDir::Files);
Chris@1345 65 foreach (QString filename, files) {
Chris@1345 66 QTest::newRow(strOf(filename)) << filename;
Chris@1345 67 }
Chris@1345 68 }
Chris@1345 69
Chris@1345 70 void read()
Chris@1345 71 {
Chris@1345 72 QFETCH(QString, audiofile);
Chris@1345 73
Chris@1345 74 AudioFileReaderFactory::Parameters params;
Chris@1345 75 AudioFileReader *reader =
Chris@1345 76 AudioFileReaderFactory::createReader
Chris@1345 77 (encodingDir + "/" + audiofile, params);
Chris@1345 78
Chris@1345 79 QVERIFY(reader != nullptr);
Chris@1345 80
Chris@1345 81 QStringList fileAndExt = audiofile.split(".");
Chris@1345 82 QString file = fileAndExt[0];
Chris@1345 83 QString extension = fileAndExt[1];
Chris@1345 84
Chris@1345 85 if (extension == "mp3") {
Chris@1345 86
Chris@1345 87 QString title = reader->getTitle();
Chris@1345 88 QVERIFY(title != QString());
Chris@1345 89
Chris@1345 90 bool found = false;
Chris@1345 91 for (int m = 0; m < mappingCount; ++m) {
Chris@1345 92 if (file == QString::fromUtf8(mapping[m][0])) {
Chris@1345 93 found = true;
Chris@1345 94 QCOMPARE(title, QString::fromUtf8(mapping[m][1]));
Chris@1345 95 break;
Chris@1345 96 }
Chris@1345 97 }
Chris@1345 98
Chris@1345 99 if (!found) {
Chris@1345 100 cerr << "Failed to find filename \""
Chris@1345 101 << file << "\" in title mapping array" << endl;
Chris@1345 102 QVERIFY(found);
Chris@1345 103 }
Chris@1345 104 }
Chris@1345 105 }
Chris@1345 106 };
Chris@1345 107
Chris@1345 108 #endif