File indexing completed on 2024-11-10 04:40:14
0001 /* 0002 SPDX-FileCopyrightText: 2009 Kevin Krammer <kevin.krammer@gmx.at> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "mimetypecheckertest.h" 0008 #include "testattribute.h" 0009 0010 #include "collection.h" 0011 #include "item.h" 0012 0013 #include <KRandom> 0014 0015 #include <QMimeDatabase> 0016 0017 #include <QTest> 0018 0019 #include <utility> 0020 0021 QTEST_MAIN(MimeTypeCheckerTest) 0022 0023 using namespace Akonadi; 0024 0025 MimeTypeCheckerTest::MimeTypeCheckerTest(QObject *parent) 0026 : QObject(parent) 0027 { 0028 mCalendarSubTypes << QStringLiteral("application/x-vnd.akonadi.calendar.event") << QStringLiteral("application/x-vnd.akonadi.calendar.todo"); 0029 } 0030 0031 void MimeTypeCheckerTest::initTestCase() 0032 { 0033 QVERIFY(QMimeDatabase().mimeTypeForName(QLatin1StringView("application/x-vnd.akonadi.calendar.event")).isValid()); 0034 0035 MimeTypeChecker emptyChecker; 0036 MimeTypeChecker calendarChecker; 0037 MimeTypeChecker subTypeChecker; 0038 MimeTypeChecker aliasChecker; 0039 0040 // for testing reset through assignments 0041 const QLatin1StringView textPlain = QLatin1StringView("text/plain"); 0042 mEmptyChecker.addWantedMimeType(textPlain); 0043 QVERIFY(!mEmptyChecker.wantedMimeTypes().isEmpty()); 0044 QVERIFY(mEmptyChecker.hasWantedMimeTypes()); 0045 0046 const QLatin1StringView textCalendar = QLatin1StringView("text/calendar"); 0047 calendarChecker.addWantedMimeType(textCalendar); 0048 QCOMPARE(calendarChecker.wantedMimeTypes().count(), 1); 0049 0050 subTypeChecker.setWantedMimeTypes(mCalendarSubTypes); 0051 QCOMPARE(subTypeChecker.wantedMimeTypes().count(), 2); 0052 0053 const QLatin1StringView textVCard = QLatin1StringView("text/directory"); 0054 aliasChecker.addWantedMimeType(textVCard); 0055 QCOMPARE(aliasChecker.wantedMimeTypes().count(), 1); 0056 0057 // test assignment works correctly 0058 mEmptyChecker = emptyChecker; 0059 mCalendarChecker = calendarChecker; 0060 mSubTypeChecker = subTypeChecker; 0061 mAliasChecker = aliasChecker; 0062 0063 QVERIFY(mEmptyChecker.wantedMimeTypes().isEmpty()); 0064 QVERIFY(!mEmptyChecker.hasWantedMimeTypes()); 0065 0066 QCOMPARE(mCalendarChecker.wantedMimeTypes().count(), 1); 0067 QCOMPARE(mCalendarChecker.wantedMimeTypes(), QStringList() << textCalendar); 0068 0069 QCOMPARE(mSubTypeChecker.wantedMimeTypes().count(), 2); 0070 const auto calendarSubTypes = QSet<QString>(mCalendarSubTypes.begin(), mCalendarSubTypes.end()); 0071 const auto wantedMimeTypes = mSubTypeChecker.wantedMimeTypes(); 0072 const auto wantedSubTypes = QSet<QString>(wantedMimeTypes.begin(), wantedMimeTypes.end()); 0073 QCOMPARE(wantedSubTypes, calendarSubTypes); 0074 0075 QCOMPARE(mAliasChecker.wantedMimeTypes().count(), 1); 0076 QCOMPARE(mAliasChecker.wantedMimeTypes(), QStringList() << textVCard); 0077 } 0078 0079 void MimeTypeCheckerTest::testCollectionCheck() 0080 { 0081 Collection invalidCollection; 0082 Collection emptyCollection(1); 0083 Collection calendarCollection(2); 0084 Collection eventCollection(3); 0085 Collection journalCollection(4); 0086 Collection vcardCollection(5); 0087 Collection aliasCollection(6); 0088 0089 const QLatin1StringView textCalendar = QLatin1StringView("text/calendar"); 0090 calendarCollection.setContentMimeTypes(QStringList() << textCalendar); 0091 const QLatin1StringView akonadiEvent = QLatin1StringView("application/x-vnd.akonadi.calendar.event"); 0092 eventCollection.setContentMimeTypes(QStringList() << akonadiEvent); 0093 journalCollection.setContentMimeTypes(QStringList() << QStringLiteral("application/x-vnd.akonadi.calendar.journal")); 0094 const QLatin1StringView textDirectory = QLatin1StringView("text/directory"); 0095 vcardCollection.setContentMimeTypes(QStringList() << textDirectory); 0096 aliasCollection.setContentMimeTypes(QStringList() << QStringLiteral("text/x-vcard")); 0097 0098 Collection::List voidCollections; 0099 voidCollections << invalidCollection << emptyCollection; 0100 0101 Collection::List subTypeCollections; 0102 subTypeCollections << eventCollection << journalCollection; 0103 0104 Collection::List calendarCollections = subTypeCollections; 0105 calendarCollections << calendarCollection; 0106 0107 Collection::List contactCollections; 0108 contactCollections << vcardCollection << aliasCollection; 0109 0110 //// empty checker fails for all 0111 Collection::List collections = voidCollections + calendarCollections + contactCollections; 0112 for (const Collection &collection : std::as_const(collections)) { 0113 QVERIFY(!mEmptyChecker.isWantedCollection(collection)); 0114 QVERIFY(!MimeTypeChecker::isWantedCollection(collection, QString())); 0115 } 0116 0117 //// calendar checker fails for void and contact collections 0118 collections = voidCollections + contactCollections; 0119 for (const Collection &collection : std::as_const(collections)) { 0120 QVERIFY(!mCalendarChecker.isWantedCollection(collection)); 0121 QVERIFY(!MimeTypeChecker::isWantedCollection(collection, textCalendar)); 0122 } 0123 0124 // but accepts all calendar collections 0125 collections = calendarCollections; 0126 for (const Collection &collection : std::as_const(collections)) { 0127 QVERIFY(mCalendarChecker.isWantedCollection(collection)); 0128 QVERIFY(MimeTypeChecker::isWantedCollection(collection, textCalendar)); 0129 } 0130 0131 //// sub type checker fails for all but the event collection 0132 collections = voidCollections + calendarCollections + contactCollections; 0133 collections.removeAll(eventCollection); 0134 for (const Collection &collection : std::as_const(collections)) { 0135 QVERIFY(!mSubTypeChecker.isWantedCollection(collection)); 0136 QVERIFY(!MimeTypeChecker::isWantedCollection(collection, akonadiEvent)); 0137 } 0138 0139 // but accepts the event collection 0140 collections = Collection::List() << eventCollection; 0141 for (const Collection &collection : std::as_const(collections)) { 0142 QVERIFY(mSubTypeChecker.isWantedCollection(collection)); 0143 QVERIFY(MimeTypeChecker::isWantedCollection(collection, akonadiEvent)); 0144 } 0145 0146 //// alias checker fails for void and calendar collections 0147 collections = voidCollections + calendarCollections; 0148 for (const Collection &collection : std::as_const(collections)) { 0149 QVERIFY(!mAliasChecker.isWantedCollection(collection)); 0150 QVERIFY(!MimeTypeChecker::isWantedCollection(collection, textDirectory)); 0151 } 0152 0153 // but accepts all contact collections 0154 collections = contactCollections; 0155 for (const Collection &collection : std::as_const(collections)) { 0156 QVERIFY(mAliasChecker.isWantedCollection(collection)); 0157 QVERIFY(MimeTypeChecker::isWantedCollection(collection, textDirectory)); 0158 } 0159 } 0160 0161 void MimeTypeCheckerTest::testItemCheck() 0162 { 0163 Item invalidItem; 0164 Item emptyItem(1); 0165 Item calendarItem(2); 0166 Item eventItem(3); 0167 Item journalItem(4); 0168 Item vcardItem(5); 0169 Item aliasItem(6); 0170 0171 const QLatin1StringView textCalendar = QLatin1StringView("text/calendar"); 0172 calendarItem.setMimeType(textCalendar); 0173 const QLatin1StringView akonadiEvent = QLatin1StringView("application/x-vnd.akonadi.calendar.event"); 0174 eventItem.setMimeType(akonadiEvent); 0175 journalItem.setMimeType(QStringLiteral("application/x-vnd.akonadi.calendar.journal")); 0176 const QLatin1StringView textDirectory = QLatin1StringView("text/directory"); 0177 vcardItem.setMimeType(textDirectory); 0178 aliasItem.setMimeType(QStringLiteral("text/x-vcard")); 0179 0180 Item::List voidItems; 0181 voidItems << invalidItem << emptyItem; 0182 0183 Item::List subTypeItems; 0184 subTypeItems << eventItem << journalItem; 0185 0186 Item::List calendarItems = subTypeItems; 0187 calendarItems << calendarItem; 0188 0189 Item::List contactItems; 0190 contactItems << vcardItem << aliasItem; 0191 0192 //// empty checker fails for all 0193 Item::List items = voidItems + calendarItems + contactItems; 0194 for (const Item &item : std::as_const(items)) { 0195 QVERIFY(!mEmptyChecker.isWantedItem(item)); 0196 QVERIFY(!MimeTypeChecker::isWantedItem(item, QString())); 0197 } 0198 0199 //// calendar checker fails for void and contact items 0200 items = voidItems + contactItems; 0201 for (const Item &item : std::as_const(items)) { 0202 QVERIFY(!mCalendarChecker.isWantedItem(item)); 0203 QVERIFY(!MimeTypeChecker::isWantedItem(item, textCalendar)); 0204 } 0205 0206 // but accepts all calendar items 0207 items = calendarItems; 0208 for (const Item &item : std::as_const(items)) { 0209 QVERIFY(mCalendarChecker.isWantedItem(item)); 0210 QVERIFY(MimeTypeChecker::isWantedItem(item, textCalendar)); 0211 } 0212 0213 //// sub type checker fails for all but the event item 0214 items = voidItems + calendarItems + contactItems; 0215 items.removeAll(eventItem); 0216 for (const Item &item : std::as_const(items)) { 0217 QVERIFY(!mSubTypeChecker.isWantedItem(item)); 0218 QVERIFY(!MimeTypeChecker::isWantedItem(item, akonadiEvent)); 0219 } 0220 0221 // but accepts the event item 0222 items = Item::List() << eventItem; 0223 for (const Item &item : std::as_const(items)) { 0224 QVERIFY(mSubTypeChecker.isWantedItem(item)); 0225 QVERIFY(MimeTypeChecker::isWantedItem(item, akonadiEvent)); 0226 } 0227 0228 //// alias checker fails for void and calendar items 0229 items = voidItems + calendarItems; 0230 for (const Item &item : std::as_const(items)) { 0231 QVERIFY(!mAliasChecker.isWantedItem(item)); 0232 QVERIFY(!MimeTypeChecker::isWantedItem(item, textDirectory)); 0233 } 0234 0235 // but accepts all contact items 0236 items = contactItems; 0237 for (const Item &item : std::as_const(items)) { 0238 QVERIFY(mAliasChecker.isWantedItem(item)); 0239 QVERIFY(MimeTypeChecker::isWantedItem(item, textDirectory)); 0240 } 0241 } 0242 0243 void MimeTypeCheckerTest::testStringMatchEquivalent() 0244 { 0245 // check that a random and thus not installed MIME type 0246 // can still be checked just like with direct string comparison 0247 0248 const QLatin1StringView installedMimeType("text/plain"); 0249 const QString randomMimeType = QLatin1StringView("application/x-vnd.test.") + KRandom::randomString(10); 0250 0251 MimeTypeChecker installedTypeChecker; 0252 installedTypeChecker.addWantedMimeType(installedMimeType); 0253 0254 MimeTypeChecker randomTypeChecker; 0255 randomTypeChecker.addWantedMimeType(randomMimeType); 0256 0257 Item item1(1); 0258 item1.setMimeType(installedMimeType); 0259 Item item2(2); 0260 item2.setMimeType(randomMimeType); 0261 0262 Collection collection1(1); 0263 collection1.setContentMimeTypes(QStringList() << installedMimeType); 0264 Collection collection2(2); 0265 collection2.setContentMimeTypes(QStringList() << randomMimeType); 0266 Collection collection3(3); 0267 collection3.setContentMimeTypes(QStringList() << installedMimeType << randomMimeType); 0268 0269 QVERIFY(installedTypeChecker.isWantedItem(item1)); 0270 QVERIFY(!randomTypeChecker.isWantedItem(item1)); 0271 QVERIFY(MimeTypeChecker::isWantedItem(item1, installedMimeType)); 0272 QVERIFY(!MimeTypeChecker::isWantedItem(item1, randomMimeType)); 0273 0274 QVERIFY(!installedTypeChecker.isWantedItem(item2)); 0275 QVERIFY(randomTypeChecker.isWantedItem(item2)); 0276 QVERIFY(!MimeTypeChecker::isWantedItem(item2, installedMimeType)); 0277 QVERIFY(MimeTypeChecker::isWantedItem(item2, randomMimeType)); 0278 0279 QVERIFY(installedTypeChecker.isWantedCollection(collection1)); 0280 QVERIFY(!randomTypeChecker.isWantedCollection(collection1)); 0281 QVERIFY(MimeTypeChecker::isWantedCollection(collection1, installedMimeType)); 0282 QVERIFY(!MimeTypeChecker::isWantedCollection(collection1, randomMimeType)); 0283 0284 QVERIFY(!installedTypeChecker.isWantedCollection(collection2)); 0285 QVERIFY(randomTypeChecker.isWantedCollection(collection2)); 0286 QVERIFY(!MimeTypeChecker::isWantedCollection(collection2, installedMimeType)); 0287 QVERIFY(MimeTypeChecker::isWantedCollection(collection2, randomMimeType)); 0288 0289 QVERIFY(installedTypeChecker.isWantedCollection(collection3)); 0290 QVERIFY(randomTypeChecker.isWantedCollection(collection3)); 0291 QVERIFY(MimeTypeChecker::isWantedCollection(collection3, installedMimeType)); 0292 QVERIFY(MimeTypeChecker::isWantedCollection(collection3, randomMimeType)); 0293 } 0294 0295 #include "moc_mimetypecheckertest.cpp"