diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/qmimedatabase-cache.pro b/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/qmimedatabase-cache.pro index d0dbf077c8..d0b834824a 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/qmimedatabase-cache.pro +++ b/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/qmimedatabase-cache.pro @@ -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 diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/tst_qmimedatabase-cache.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/tst_qmimedatabase-cache.cpp index 7f78312582..2113672821 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/tst_qmimedatabase-cache.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/tst_qmimedatabase-cache.cpp @@ -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); diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/qmimedatabase-xml.pro b/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/qmimedatabase-xml.pro index 6d7cbc6016..4add6c2bcd 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/qmimedatabase-xml.pro +++ b/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/qmimedatabase-xml.pro @@ -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 diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/tst_qmimedatabase-xml.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/tst_qmimedatabase-xml.cpp index e24b1b1249..c4a1dab697 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/tst_qmimedatabase-xml.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/tst_qmimedatabase-xml.cpp @@ -33,7 +33,7 @@ #include "../tst_qmimedatabase.h" -void tst_QMimeDatabase::init() +void tst_QMimeDatabase::initTestCaseInternal() { qputenv("QT_NO_MIME_CACHE", "1"); } diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp index 99ebf77a3d..a4028ed70e 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -37,6 +37,8 @@ #include #include +#include +#include #include #include #include @@ -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()); diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h index 26def8f290..2e927aa1bc 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h @@ -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