diff options
Diffstat (limited to 'tests/auto/tools/rcc/tst_rcc.cpp')
-rw-r--r-- | tests/auto/tools/rcc/tst_rcc.cpp | 212 |
1 files changed, 203 insertions, 9 deletions
diff --git a/tests/auto/tools/rcc/tst_rcc.cpp b/tests/auto/tools/rcc/tst_rcc.cpp index 0124b580a8..dbf5cebd9d 100644 --- a/tests/auto/tools/rcc/tst_rcc.cpp +++ b/tests/auto/tools/rcc/tst_rcc.cpp @@ -1,5 +1,6 @@ /**************************************************************************** ** +** Copyright (C) 2012 Giuseppe D'Angelo <dangelog@gmail.com> ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/ ** @@ -39,28 +40,34 @@ ** ****************************************************************************/ +#include <QtTest/QtTest> +#include <QtCore/QString> #include <QtCore/QCoreApplication> #include <QtCore/QByteArray> -#include <QtCore/QDebug> #include <QtCore/QDir> #include <QtCore/QFile> #include <QtCore/QProcess> -#include <QtCore/QTimer> - -#include <QtTest/QtTest> +#include <QtCore/QDirIterator> +#include <QtCore/QMap> +#include <QtCore/QList> +#include <QtCore/QResource> +#include <QtCore/QLocale> +typedef QMap<QString, QString> QStringMap; +Q_DECLARE_METATYPE(QStringMap) class tst_rcc : public QObject { Q_OBJECT -public: - tst_rcc() {} private slots: void rcc_data(); void rcc(); -}; + void binary_data(); + void binary(); + void cleanupTestCase(); +}; QString findExpectedFile(const QString &base) { @@ -95,14 +102,16 @@ static QString doCompare(const QStringList &actual, const QStringList &expected) return ba; } - void tst_rcc::rcc_data() { QTest::addColumn<QString>("directory"); QTest::addColumn<QString>("qrcfile"); QTest::addColumn<QString>("expected"); - QTest::newRow("images") << SRCDIR "data" << "images.qrc" << "images.expected"; + QString dataPath = QFINDTESTDATA("data/images/"); + if (dataPath.isEmpty()) + QFAIL("data path not found"); + QTest::newRow("images") << dataPath << "images.qrc" << "images.expected"; } void tst_rcc::rcc() @@ -157,6 +166,191 @@ void tst_rcc::rcc() +static void createRccBinaryData(const QString &baseDir, const QString &qrcFileName, const QString &rccFileName) +{ + QString currentDir = QDir::currentPath(); + QDir::setCurrent(baseDir); + + QProcess rccProcess; + rccProcess.start("rcc", QStringList() << "-binary" << "-o" << rccFileName << qrcFileName); + bool ok = rccProcess.waitForFinished(); + if (!ok) { + QString errorString = QString::fromLatin1("Could not start rcc (is it in PATH?): %1").arg(rccProcess.errorString()); + QFAIL(qPrintable(errorString)); + } + + QByteArray output = rccProcess.readAllStandardOutput(); + if (!output.isEmpty()) { + QString errorMessage = QString::fromLatin1("rcc stdout: %1").arg(QString::fromLocal8Bit(output)); + QWARN(qPrintable(errorMessage)); + } + + output = rccProcess.readAllStandardError(); + if (!output.isEmpty()) { + QString errorMessage = QString::fromLatin1("rcc stderr: %1").arg(QString::fromLocal8Bit(output)); + QWARN(qPrintable(errorMessage)); + } + + QDir::setCurrent(currentDir); +} + +static QStringList readLinesFromFile(const QString &fileName) +{ + QFile file(fileName); + + bool ok = file.open(QIODevice::ReadOnly | QIODevice::Text); + if (!ok) + QWARN(qPrintable(QString::fromLatin1("Could not open testdata file %1: %2").arg(fileName, file.errorString()))); + + QStringList lines = QString::fromUtf8(file.readAll()).split(QLatin1Char('\n'), QString::SkipEmptyParts); + return lines; +} + +static QStringMap readExpectedFiles(const QString &fileName) +{ + QStringMap expectedFiles; + + QStringList lines = readLinesFromFile(fileName); + foreach (const QString &line, lines) { + QString resourceFileName = line.section(QLatin1Char(' '), 0, 0, QString::SectionSkipEmpty); + QString actualFileName = line.section(QLatin1Char(' '), 1, 1, QString::SectionSkipEmpty); + expectedFiles[resourceFileName] = actualFileName; + } + + return expectedFiles; +} + +/* + The following test looks for all *.qrc files under data/binary/. For each + .qrc file found, these files are processed (assuming the file found is + called "base.qrc"): + + - base.qrc : processed by rcc; creates base.rcc + - base.locale : (optional) list of locales to test, one per line + - base.expected : list of pairs (file path in resource, path to real file), + one per line; the pair separated by a whitespace; the paths to real files + relative to data/binary/ (for testing the C locale) + - base.localeName.expected : for each localeName in the base.locale file, + as the above .expected file +*/ + +void tst_rcc::binary_data() +{ + QTest::addColumn<QString>("resourceFile"); + QTest::addColumn<QLocale>("locale"); + QTest::addColumn<QString>("baseDirectory"); + QTest::addColumn<QStringMap>("expectedFiles"); + + QString dataPath = QFINDTESTDATA("data/binary/"); + if (dataPath.isEmpty()) + QFAIL("data path not found"); + + QDirIterator iter(dataPath, QStringList() << QLatin1String("*.qrc")); + while (iter.hasNext()) + { + iter.next(); + QFileInfo qrcFileInfo = iter.fileInfo(); + QString absoluteBaseName = QFileInfo(qrcFileInfo.absolutePath(), qrcFileInfo.baseName()).absoluteFilePath(); + QString rccFileName = absoluteBaseName + QLatin1String(".rcc"); + createRccBinaryData(dataPath, qrcFileInfo.absoluteFilePath(), rccFileName); + + QString localeFileName = absoluteBaseName + QLatin1String(".locale"); + QFile localeFile(localeFileName); + if (localeFile.exists()) { + QStringList locales = readLinesFromFile(localeFileName); + foreach (const QString &locale, locales) { + QString expectedFileName = QString::fromLatin1("%1.%2.%3").arg(absoluteBaseName, locale, QLatin1String("expected")); + QStringMap expectedFiles = readExpectedFiles(expectedFileName); + QTest::newRow(qPrintable(qrcFileInfo.baseName() + QLatin1Char('_') + locale)) << rccFileName + << QLocale(locale) + << dataPath + << expectedFiles; + } + } + + // always test for the C locale as well + QString expectedFileName = absoluteBaseName + QLatin1String(".expected"); + QStringMap expectedFiles = readExpectedFiles(expectedFileName); + QTest::newRow(qPrintable(qrcFileInfo.baseName() + QLatin1String("_C"))) << rccFileName + << QLocale::c() + << dataPath + << expectedFiles; + } +} + +void tst_rcc::binary() +{ + QFETCH(QString, baseDirectory); + QFETCH(QString, resourceFile); + QFETCH(QLocale, locale); + QFETCH(QStringMap, expectedFiles); + + const QString rootPrefix = QLatin1String("/test_root/"); + const QString resourceRootPrefix = QLatin1Char(':') + rootPrefix; + + QLocale oldDefaultLocale; + QLocale::setDefault(locale); + QVERIFY(QFile::exists(resourceFile)); + QVERIFY(QResource::registerResource(resourceFile, rootPrefix)); + + { // need to destroy the iterators on the resource, in order to be able to unregister it + + // read all the files inside the resources + QDirIterator iter(resourceRootPrefix, QDir::Files, QDirIterator::Subdirectories); + QList<QString> filesFound; + while (iter.hasNext()) + filesFound << iter.next(); + + // add the test root prefix to the expected file names + QList<QString> expectedFileNames = expectedFiles.keys(); + for (QList<QString>::iterator i = expectedFileNames.begin(); i < expectedFileNames.end(); ++i) { + // poor man's canonicalPath, which doesn't work with resources + if ((*i).startsWith(QLatin1Char('/'))) + (*i).remove(0, 1); + *i = resourceRootPrefix + *i; + } + + // check that we have all (and only) the expected files + qSort(filesFound); + qSort(expectedFileNames); + QCOMPARE(filesFound, expectedFileNames); + + // now actually check the file contents + QDir directory(baseDirectory); + for (QStringMap::const_iterator i = expectedFiles.constBegin(); i != expectedFiles.constEnd(); ++i) { + QString resourceFileName = i.key(); + QString actualFileName = i.value(); + + QFile resourceFile(resourceRootPrefix + resourceFileName); + QVERIFY(resourceFile.open(QIODevice::ReadOnly)); + QByteArray resourceData = resourceFile.readAll(); + resourceFile.close(); + + QFile actualFile(QFileInfo(directory, actualFileName).absoluteFilePath()); + QVERIFY(actualFile.open(QIODevice::ReadOnly)); + QByteArray actualData = actualFile.readAll(); + actualFile.close(); + QCOMPARE(resourceData, actualData); + } + + } + + QVERIFY(QResource::unregisterResource(resourceFile, rootPrefix)); + QLocale::setDefault(oldDefaultLocale); +} + + +void tst_rcc::cleanupTestCase() +{ + QString dataPath = QFINDTESTDATA("data/binary/"); + if (dataPath.isEmpty()) + return; + QDir dataDir(dataPath); + QFileInfoList entries = dataDir.entryInfoList(QStringList() << QLatin1String("*.rcc")); + foreach (const QFileInfo &entry, entries) + QFile::remove(entry.absoluteFilePath()); +} + QTEST_APPLESS_MAIN(tst_rcc) #include "tst_rcc.moc" |