QMimeDatabase: Run more tests on non-XDG/shared-mime-info platforms

On Windows and OS X, where QStandardPaths does not use XDG_DATA_DIRS/
_HOME and shared-mime-info is not installed, the tests that require
additional shared mime info xml files were never run.
Mend that by using QStandardPaths' test mode instead of setting
XDG_DATA_HOME.

Change-Id: I53b75c293c41c4dac63986dcb88972c2b54d5428
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Eike Ziller 2015-07-22 16:10:29 +02:00 committed by David Faure
parent 7f0f6380b1
commit 3dcabd8c64
6 changed files with 50 additions and 25 deletions

View File

@ -11,3 +11,5 @@ RESOURCES += ../testdata.qrc
*-g++*:QMAKE_CXXFLAGS += -W -Wall -Wextra -Wshadow -Wno-long-long -Wnon-virtual-dtor
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0
unix:!mac:!qnx: DEFINES += USE_XDG_DATA_DIRS

View File

@ -39,7 +39,7 @@
#include "../tst_qmimedatabase.cpp"
void tst_QMimeDatabase::init()
void tst_QMimeDatabase::initTestCaseInternal()
{
#ifdef QT_NO_PROCESS
QSKIP("No qprocess support", SkipAll);

View File

@ -12,3 +12,5 @@ RESOURCES += ../testdata.qrc
*-g++*:QMAKE_CXXFLAGS += -W -Wall -Wextra -Wshadow -Wno-long-long -Wnon-virtual-dtor
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0
unix:!mac:!qnx: DEFINES += USE_XDG_DATA_DIRS

View File

@ -33,7 +33,7 @@
#include "../tst_qmimedatabase.h"
void tst_QMimeDatabase::init()
void tst_QMimeDatabase::initTestCaseInternal()
{
qputenv("QT_NO_MIME_CACHE", "1");
}

View File

@ -37,6 +37,8 @@
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QStandardPaths>
#include <QtCore/QTemporaryDir>
#include <QtCore/QTextStream>
#include <QFutureSynchronizer>
#include <QtConcurrent/QtConcurrentRun>
@ -116,29 +118,30 @@ tst_QMimeDatabase::tst_QMimeDatabase()
void tst_QMimeDatabase::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
m_localMimeDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/mime";
if (QDir(m_localMimeDir).exists()) {
QVERIFY2(QDir(m_localMimeDir).removeRecursively(), qPrintable(m_localMimeDir + ": " + qt_error_string()));
}
QString errorMessage;
#ifdef USE_XDG_DATA_DIRS
// Create a temporary "global" XDG data dir for later use
// It will initially contain a copy of freedesktop.org.xml
QVERIFY(m_temporaryDir.isValid());
// Create a "global" and a "local" XDG data dir, right here.
// The local dir will be empty initially, while the global dir will contain a copy of freedesktop.org.xml
const QDir here = QDir(m_temporaryDir.path());
m_globalXdgDir = m_temporaryDir.path() + QStringLiteral("/global");
m_localXdgDir = m_temporaryDir.path() + QStringLiteral("/local");
const QString globalPackageDir = m_globalXdgDir + QStringLiteral("/mime/packages");
QVERIFY(here.mkpath(globalPackageDir) && here.mkpath(m_localXdgDir));
QVERIFY(here.mkpath(globalPackageDir));
qputenv("XDG_DATA_DIRS", QFile::encodeName(m_globalXdgDir));
qputenv("XDG_DATA_HOME", QFile::encodeName(m_localXdgDir));
qDebug() << "\nLocal XDG_DATA_HOME: " << m_localXdgDir
<< "\nGlobal XDG_DATA_DIRS: " << m_globalXdgDir;
qDebug() << "\nGlobal XDG_DATA_DIRS: " << m_globalXdgDir;
const QString freeDesktopXml = QStringLiteral("freedesktop.org.xml");
const QString xmlFileName = QLatin1String(RESOURCE_PREFIX) + freeDesktopXml;
const QString xmlTargetFileName = globalPackageDir + QLatin1Char('/') + freeDesktopXml;
QString errorMessage;
QVERIFY2(copyResourceFile(xmlFileName, xmlTargetFileName, &errorMessage), qPrintable(errorMessage));
#endif
m_testSuite = QFINDTESTDATA("testfiles");
if (m_testSuite.isEmpty())
@ -150,7 +153,19 @@ void tst_QMimeDatabase::initTestCase()
m_qmlAgainFileName = QLatin1String(RESOURCE_PREFIX) + qmlAgainFileName;
QVERIFY2(QFile::exists(m_qmlAgainFileName), qPrintable(errorMessage.arg(qmlAgainFileName)));
init();
initTestCaseInternal();
m_isUsingCacheProvider = !qEnvironmentVariableIsSet("QT_NO_MIME_CACHE");
}
void tst_QMimeDatabase::init()
{
// clean up local data from previous runs
QDir(m_localMimeDir).removeRecursively();
}
void tst_QMimeDatabase::cleanupTestCase()
{
QDir(m_localMimeDir).removeRecursively();
}
void tst_QMimeDatabase::mimeTypeForName()
@ -847,6 +862,10 @@ QT_END_NAMESPACE
void tst_QMimeDatabase::installNewGlobalMimeType()
{
#if !defined(USE_XDG_DATA_DIRS)
QSKIP("This test requires XDG_DATA_DIRS");
#endif
#ifdef QT_NO_PROCESS
QSKIP("This test requires QProcess support");
#else
@ -868,7 +887,7 @@ void tst_QMimeDatabase::installNewGlobalMimeType()
QString errorMessage;
QVERIFY2(copyResourceFile(m_yastMimeTypes, destFile, &errorMessage), qPrintable(errorMessage));
QVERIFY2(copyResourceFile(m_qmlAgainFileName, destQmlFile, &errorMessage), qPrintable(errorMessage));
if (!waitAndRunUpdateMimeDatabase(mimeDir))
if (m_isUsingCacheProvider && !waitAndRunUpdateMimeDatabase(mimeDir))
QSKIP("shared-mime-info not found, skipping mime.cache test");
QCOMPARE(db.mimeTypeForFile(QLatin1String("foo.ymu"), QMimeDatabase::MatchExtension).name(),
@ -887,7 +906,7 @@ void tst_QMimeDatabase::installNewGlobalMimeType()
// Now test removing it again
QFile::remove(destFile);
QFile::remove(destQmlFile);
if (!waitAndRunUpdateMimeDatabase(mimeDir))
if (m_isUsingCacheProvider && !waitAndRunUpdateMimeDatabase(mimeDir))
QSKIP("shared-mime-info not found, skipping mime.cache test");
QCOMPARE(db.mimeTypeForFile(QLatin1String("foo.ymu"), QMimeDatabase::MatchExtension).name(),
QString::fromLatin1("application/octet-stream"));
@ -905,8 +924,7 @@ void tst_QMimeDatabase::installNewLocalMimeType()
QMimeDatabase db;
QVERIFY(!db.mimeTypeForName(QLatin1String("text/x-suse-ymp")).isValid());
const QString mimeDir = m_localXdgDir + QLatin1String("/mime");
const QString destDir = mimeDir + QLatin1String("/packages/");
const QString destDir = m_localMimeDir + QLatin1String("/packages/");
QDir().mkpath(destDir);
const QString destFile = destDir + QLatin1String(yastFileName);
QFile::remove(destFile);
@ -915,9 +933,9 @@ void tst_QMimeDatabase::installNewLocalMimeType()
QString errorMessage;
QVERIFY2(copyResourceFile(m_yastMimeTypes, destFile, &errorMessage), qPrintable(errorMessage));
QVERIFY2(copyResourceFile(m_qmlAgainFileName, destQmlFile, &errorMessage), qPrintable(errorMessage));
if (!runUpdateMimeDatabase(mimeDir)) {
if (m_isUsingCacheProvider && !runUpdateMimeDatabase(m_localMimeDir)) {
const QString skipWarning = QStringLiteral("shared-mime-info not found, skipping mime.cache test (")
+ QDir::toNativeSeparators(mimeDir) + QLatin1Char(')');
+ QDir::toNativeSeparators(m_localMimeDir) + QLatin1Char(')');
QSKIP(qPrintable(skipWarning));
}
@ -937,14 +955,14 @@ void tst_QMimeDatabase::installNewLocalMimeType()
// Now test removing the local mimetypes again (note, this leaves a mostly-empty mime.cache file)
QFile::remove(destFile);
QFile::remove(destQmlFile);
if (!waitAndRunUpdateMimeDatabase(mimeDir))
if (m_isUsingCacheProvider && !waitAndRunUpdateMimeDatabase(m_localMimeDir))
QSKIP("shared-mime-info not found, skipping mime.cache test");
QCOMPARE(db.mimeTypeForFile(QLatin1String("foo.ymu"), QMimeDatabase::MatchExtension).name(),
QString::fromLatin1("application/octet-stream"));
QVERIFY(!db.mimeTypeForName(QLatin1String("text/x-suse-ymp")).isValid());
// And now the user goes wild and uses rm -rf
QFile::remove(mimeDir + QString::fromLatin1("/mime.cache"));
QFile::remove(m_localMimeDir + QString::fromLatin1("/mime.cache"));
QCOMPARE(db.mimeTypeForFile(QLatin1String("foo.ymu"), QMimeDatabase::MatchExtension).name(),
QString::fromLatin1("application/octet-stream"));
QVERIFY(!db.mimeTypeForName(QLatin1String("text/x-suse-ymp")).isValid());

View File

@ -46,6 +46,8 @@ public:
private slots:
void initTestCase();
void init();
void cleanupTestCase();
void mimeTypeForName();
void mimeTypeForFileName_data();
@ -86,14 +88,15 @@ private slots:
void installNewLocalMimeType();
private:
void init(); // test-specific
void initTestCaseInternal(); // test-specific
QString m_globalXdgDir;
QString m_localXdgDir;
QString m_localMimeDir;
QString m_yastMimeTypes;
QString m_qmlAgainFileName;
QTemporaryDir m_temporaryDir;
QString m_testSuite;
bool m_isUsingCacheProvider;
};
#endif // TST_QMIMEDATABASE_H