summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElvis Angelaccio <[email protected]>2015-10-20 19:49:27 +0200
committerElvis Angelaccio <[email protected]>2015-10-20 19:54:24 +0200
commit11c94a48e076702dae200c216f7edfe891bf9409 (patch)
treedede6364ea01c26f05cf5a3a74fa0275bb0638c7
parent35a101bdf9d6c5b548ba251be9c81583a03491ca (diff)
Introduce unit test for mimetypes detection
The static function determineMimeType() defined in archive_kerfuffle.cpp is a good candidate to be unit tested. To do so, we need to make it visible from the outside. Differential Revision: D431
-rw-r--r--kerfuffle/archive_kerfuffle.cpp13
-rw-r--r--kerfuffle/archive_kerfuffle.h7
-rw-r--r--kerfuffle/tests/CMakeLists.txt1
-rw-r--r--kerfuffle/tests/data/zip_with_wrong_extension.rarbin0 -> 168 bytes
-rw-r--r--kerfuffle/tests/mimetypetest.cpp63
5 files changed, 77 insertions, 7 deletions
diff --git a/kerfuffle/archive_kerfuffle.cpp b/kerfuffle/archive_kerfuffle.cpp
index 326c664..65c3a38 100644
--- a/kerfuffle/archive_kerfuffle.cpp
+++ b/kerfuffle/archive_kerfuffle.cpp
@@ -39,14 +39,16 @@
#include <KPluginLoader>
#include <KMimeTypeTrader>
-#include <KServiceTypeTrader>
-static bool comparePlugins(const KService::Ptr &p1, const KService::Ptr &p2)
+namespace Kerfuffle
+{
+
+bool Archive::comparePlugins(const KService::Ptr &p1, const KService::Ptr &p2)
{
return (p1->property(QLatin1String( "X-KDE-Priority" )).toInt()) > (p2->property(QLatin1String( "X-KDE-Priority" )).toInt());
}
-static QString determineMimeType(const QString& filename)
+QString Archive::determineMimeType(const QString& filename)
{
QMimeDatabase db;
QMimeType mimeFromExtension = db.mimeTypeForFile(filename, QMimeDatabase::MatchExtension);
@@ -79,7 +81,7 @@ static QString determineMimeType(const QString& filename)
return mimeFromContent.name();
}
-static KService::List findPluginOffers(const QString& filename, const QString& fixedMimeType)
+KService::List Archive::findPluginOffers(const QString& filename, const QString& fixedMimeType)
{
KService::List offers;
@@ -99,9 +101,6 @@ static KService::List findPluginOffers(const QString& filename, const QString& f
return offers;
}
-namespace Kerfuffle
-{
-
QDebug operator<<(QDebug d, const fileRootNodePair &pair)
{
d.nospace() << "fileRootNodePair(" << pair.file << "," << pair.rootNode << ")";
diff --git a/kerfuffle/archive_kerfuffle.h b/kerfuffle/archive_kerfuffle.h
index f55fa18..2cb015d 100644
--- a/kerfuffle/archive_kerfuffle.h
+++ b/kerfuffle/archive_kerfuffle.h
@@ -37,6 +37,8 @@
#include <QVariant>
#include <QDebug>
+#include <KServiceTypeTrader>
+
class KJob;
namespace Kerfuffle
@@ -130,6 +132,11 @@ class KERFUFFLE_EXPORT Archive : public QObject
Q_OBJECT
public:
+
+ static bool comparePlugins(const KService::Ptr &p1, const KService::Ptr &p2);
+ static QString determineMimeType(const QString& filename);
+ static KService::List findPluginOffers(const QString& filename, const QString& fixedMimeType);
+
static Archive *create(const QString &fileName, QObject *parent = 0);
static Archive *create(const QString &fileName, const QString &fixedMimeType, QObject *parent = 0);
~Archive();
diff --git a/kerfuffle/tests/CMakeLists.txt b/kerfuffle/tests/CMakeLists.txt
index 0456e81..fccd896 100644
--- a/kerfuffle/tests/CMakeLists.txt
+++ b/kerfuffle/tests/CMakeLists.txt
@@ -20,4 +20,5 @@ endmacro(KERFUFFLE_UNIT_TESTS)
KERFUFFLE_UNIT_TESTS(
archivetest
jobstest
+ mimetypetest
)
diff --git a/kerfuffle/tests/data/zip_with_wrong_extension.rar b/kerfuffle/tests/data/zip_with_wrong_extension.rar
new file mode 100644
index 0000000..5bc97e6
--- /dev/null
+++ b/kerfuffle/tests/data/zip_with_wrong_extension.rar
Binary files differ
diff --git a/kerfuffle/tests/mimetypetest.cpp b/kerfuffle/tests/mimetypetest.cpp
new file mode 100644
index 0000000..7f6c212
--- /dev/null
+++ b/kerfuffle/tests/mimetypetest.cpp
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2015 Elvis Angelaccio <[email protected]>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "kerfuffle/archive_kerfuffle.h"
+
+#include <QTest>
+
+using namespace Kerfuffle;
+
+class MimeTypeTest : public QObject
+{
+ Q_OBJECT
+
+private Q_SLOTS:
+
+ void testEmptyFilename();
+ void testTarDetection();
+ void testWrongZipExtension();
+
+};
+
+QTEST_GUILESS_MAIN(MimeTypeTest)
+
+void MimeTypeTest::testEmptyFilename()
+{
+ QCOMPARE(Archive::determineMimeType(QString()), QStringLiteral("application/octet-stream"));
+}
+
+void MimeTypeTest::testTarDetection()
+{
+ const QString testFile = QFINDTESTDATA("data/simplearchive.tar.gz");
+ QCOMPARE(Archive::determineMimeType(testFile), QStringLiteral("application/x-compressed-tar"));
+}
+
+void MimeTypeTest::testWrongZipExtension()
+{
+ const QString testFile = QFINDTESTDATA("data/zip_with_wrong_extension.rar");
+ QCOMPARE(Archive::determineMimeType(testFile), QStringLiteral("application/zip"));
+}
+
+#include "mimetypetest.moc"