summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp')
-rw-r--r--tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp131
1 files changed, 124 insertions, 7 deletions
diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp
index fae26f4971..ce0d9c8047 100644
--- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp
+++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp
@@ -6,6 +6,8 @@
#include "qstandardpaths.h"
#ifdef Q_OS_UNIX
+#include <dirent.h>
+#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif
@@ -25,6 +27,8 @@
#include <QProcess>
#endif
+using namespace Qt::StringLiterals;
+
static const char *const additionalMimeFiles[] = {
"yast2-metapackage-handler-mimetypes.xml",
"qml-again.xml",
@@ -245,6 +249,7 @@ void tst_QMimeDatabase::mimeTypeForFileName_data()
// fdo bug 15436, needs shared-mime-info >= 0.40 (and this tests the globs2-parsing code).
QTest::newRow("glob that ends with *, also matches *.pdf. *.pdf has higher weight") << "README.pdf" << "application/pdf";
QTest::newRow("directory") << "/" << "inode/directory";
+ QTest::newRow("resource-directory") << ":/files/" << "inode/directory";
QTest::newRow("doesn't exist, no extension") << "IDontExist" << "application/octet-stream";
QTest::newRow("doesn't exist but has known extension") << "IDontExist.txt" << "text/plain";
QTest::newRow("empty") << "" << "application/octet-stream";
@@ -277,7 +282,7 @@ void tst_QMimeDatabase::mimeTypeForFileName()
QVERIFY(mimes.isEmpty());
} else {
QVERIFY2(!mimes.isEmpty(), msgMimeTypeForFileNameFailed(mimes, expectedMimeType).constData());
- QVERIFY2(mimes.count() == 1, msgMimeTypeForFileNameFailed(mimes, expectedMimeType).constData());
+ QVERIFY2(mimes.size() == 1, msgMimeTypeForFileNameFailed(mimes, expectedMimeType).constData());
QCOMPARE(mimes.first().name(), expectedMimeType);
}
}
@@ -328,7 +333,7 @@ void tst_QMimeDatabase::inheritance()
const QMimeType directory = db.mimeTypeForName(QString::fromLatin1("inode/directory"));
QVERIFY(directory.isValid());
- QCOMPARE(directory.parentMimeTypes().count(), 0);
+ QCOMPARE(directory.parentMimeTypes().size(), 0);
QVERIFY(!directory.inherits(QLatin1String("application/octet-stream")));
// Check that text/x-patch knows that it inherits from text/plain (it says so explicitly)
@@ -349,7 +354,7 @@ void tst_QMimeDatabase::inheritance()
const QStringList shellParents = shellscript.parentMimeTypes();
QVERIFY(shellParents.contains(QLatin1String("text/plain")));
QVERIFY(shellParents.contains(QLatin1String("application/x-executable")));
- QCOMPARE(shellParents.count(), 2); // only the above two
+ QCOMPARE(shellParents.size(), 2); // only the above two
const QStringList allShellAncestors = shellscript.allAncestors();
QVERIFY(allShellAncestors.contains(QLatin1String("text/plain")));
QVERIFY(allShellAncestors.contains(QLatin1String("application/x-executable")));
@@ -497,6 +502,42 @@ void tst_QMimeDatabase::mimeTypeForFileWithContent()
QCOMPARE(mime.name(), QString::fromLatin1("application/smil+xml"));
}
+ // Test what happens with Qt resources (file engines in general)
+ {
+ QFile rccFile(":/files/test.txt");
+
+ mime = db.mimeTypeForFile(rccFile.fileName());
+ QCOMPARE(mime.name(), "text/plain"_L1);
+
+ QVERIFY(rccFile.open(QIODevice::ReadOnly));
+ mime = db.mimeTypeForData(&rccFile);
+ QCOMPARE(mime.name(), "text/x-qml"_L1);
+ QVERIFY(rccFile.isOpen());
+
+ mime = db.mimeTypeForFile(rccFile.fileName(), QMimeDatabase::MatchContent);
+ QCOMPARE(mime.name(), "text/x-qml"_L1);
+ }
+
+ // Directories
+ {
+ mime = db.mimeTypeForFile("/");
+ QCOMPARE(mime.name(), "inode/directory"_L1);
+
+ QString dirName = QDir::tempPath();
+ if (!dirName.endsWith(u'/'))
+ dirName += u'/';
+ mime = db.mimeTypeForFile(dirName);
+ QCOMPARE(mime.name(), "inode/directory"_L1);
+
+ while (dirName.endsWith(u'/'))
+ dirName.chop(1);
+ mime = db.mimeTypeForFile(dirName);
+ QCOMPARE(mime.name(), "inode/directory"_L1);
+
+ mime = db.mimeTypeForFile(":/files");
+ QCOMPARE(mime.name(), "inode/directory"_L1);
+ }
+
// Test what happens with an incorrect path
mime = db.mimeTypeForFile(QString::fromLatin1("file:///etc/passwd" /* incorrect code, use a path instead */));
QVERIFY(mime.isDefault());
@@ -584,6 +625,82 @@ void tst_QMimeDatabase::mimeTypeForFileAndContent()
QCOMPARE(buffer.pos(), qint64(0));
}
+#ifdef Q_OS_UNIX
+void tst_QMimeDatabase::mimeTypeForUnixSpecials_data()
+{
+ QTest::addColumn<QString>("name");
+ QTest::addColumn<QString>("expected");
+
+ static const char * const mimeTypes[] = {
+ "inode/blockdevice",
+ "inode/chardevice",
+ "inode/fifo",
+ "inode/socket",
+ };
+ enum SpecialType {
+ FoundBlock = 0,
+ FoundChar = 1,
+ FoundFifo = 2,
+ FoundSocket = 3,
+ };
+ uint found = 0;
+ auto nothingfound = []() {
+ QSKIP("No special Unix inode types found!");
+ };
+
+ // on a standard Linux system (systemd), /dev/log is a symlink to a socket
+ // and /dev/initctl is a symlink to a FIFO
+ int devfd = open("/dev", O_RDONLY);
+ DIR *devdir = fdopendir(devfd); // takes ownership
+ if (!devdir)
+ return nothingfound();
+
+ while (struct dirent *ent = readdir(devdir)) {
+ struct stat statbuf;
+ if (fstatat(devfd, ent->d_name, &statbuf, 0) < 0)
+ continue;
+
+ SpecialType type;
+ if (S_ISBLK(statbuf.st_mode)) {
+ type = FoundBlock;
+ } else if (S_ISCHR(statbuf.st_mode)) {
+ type = FoundChar;
+ } else if (S_ISFIFO(statbuf.st_mode)) {
+ type = FoundFifo;
+ } else if (S_ISSOCK(statbuf.st_mode)) {
+ type = FoundSocket;
+ } else {
+ if (!S_ISREG(statbuf.st_mode) && !S_ISDIR(statbuf.st_mode))
+ qWarning("Could not tell what file type '%s' is: %#o'",
+ ent->d_name, statbuf.st_mode);
+ continue;
+ }
+
+ if (found & (1U << type))
+ continue; // we've already seen such a type
+
+ const char *mimeType = mimeTypes[type];
+ QTest::addRow("%s", mimeType)
+ << u"/dev/"_s + QFile::decodeName(ent->d_name) << mimeType;
+ found |= (1U << type);
+ }
+ closedir(devdir);
+
+ if (!found)
+ nothingfound();
+}
+
+void tst_QMimeDatabase::mimeTypeForUnixSpecials()
+{
+ QFETCH(QString, name);
+ QFETCH(QString, expected);
+
+ qInfo() << "Testing that" << name << "is" << expected;
+ QMimeDatabase db;
+ QCOMPARE(db.mimeTypeForFile(name).name(), expected);
+}
+#endif
+
void tst_QMimeDatabase::allMimeTypes()
{
QMimeDatabase db;
@@ -591,7 +708,7 @@ void tst_QMimeDatabase::allMimeTypes()
QVERIFY(!lst.isEmpty());
// Hardcoding this is the only way to check both providers find the same number of mimetypes.
- QCOMPARE(lst.count(), 851);
+ QCOMPARE(lst.size(), 851);
foreach (const QMimeType &mime, lst) {
const QString name = mime.name();
@@ -741,7 +858,7 @@ void tst_QMimeDatabase::findByFileName()
//qDebug() << Q_FUNC_INFO << "mimeTypeForFile() returned" << resultMimeTypeName;
const bool failed = resultMimeTypeName != mimeTypeName;
- const bool shouldFail = (xFail.length() >= 1 && xFail.at(0) == QLatin1Char('x'));
+ const bool shouldFail = (xFail.size() >= 1 && xFail.at(0) == QLatin1Char('x'));
if (shouldFail != failed) {
// Results are ambiguous when multiple MIME types have the same glob
// -> accept the current result if the found MIME type actually
@@ -788,7 +905,7 @@ void tst_QMimeDatabase::findByData()
QByteArray data = f.read(16384);
const QString resultMimeTypeName = database.mimeTypeForData(data).name();
- if (xFail.length() >= 2 && xFail.at(1) == QLatin1Char('x')) {
+ if (xFail.size() >= 2 && xFail.at(1) == QLatin1Char('x')) {
// Expected to fail
QVERIFY2(resultMimeTypeName != mimeTypeName, qPrintable(resultMimeTypeName));
} else {
@@ -819,7 +936,7 @@ void tst_QMimeDatabase::findByFile()
QMimeDatabase database;
const QString resultMimeTypeName = database.mimeTypeForFile(filePath).name();
//qDebug() << Q_FUNC_INFO << filePath << "->" << resultMimeTypeName;
- if (xFail.length() >= 3 && xFail.at(2) == QLatin1Char('x')) {
+ if (xFail.size() >= 3 && xFail.at(2) == QLatin1Char('x')) {
// Expected to fail
QVERIFY2(resultMimeTypeName != mimeTypeName, qPrintable(resultMimeTypeName));
} else {