File indexing completed on 2024-11-10 04:40:08

0001 /*
0002     SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "attributefactorytest.h"
0008 #include "collectionpathresolver.h"
0009 #include "testattribute.h"
0010 
0011 #include "attributefactory.h"
0012 #include "collection.h"
0013 #include "itemcreatejob.h"
0014 #include "itemfetchjob.h"
0015 #include "itemfetchscope.h"
0016 #include "qtest_akonadi.h"
0017 #include "resourceselectjob_p.h"
0018 
0019 using namespace Akonadi;
0020 
0021 QTEST_AKONADIMAIN(AttributeFactoryTest)
0022 
0023 static Collection res1;
0024 
0025 void AttributeFactoryTest::initTestCase()
0026 {
0027     AkonadiTest::checkTestIsIsolated();
0028     auto resolver = new CollectionPathResolver(QStringLiteral("res1"), this);
0029     AKVERIFYEXEC(resolver);
0030     res1 = Collection(resolver->collection());
0031 }
0032 
0033 void AttributeFactoryTest::testUnknownAttribute()
0034 {
0035     // The attribute is currently not registered.
0036     Item item;
0037     item.setMimeType(QStringLiteral("text/directory"));
0038     item.setPayload<QByteArray>("payload");
0039     auto ta = new TestAttribute;
0040     {
0041         auto created = AttributeFactory::createAttribute(ta->type()); // DefaultAttribute
0042         QVERIFY(created != nullptr);
0043         delete created;
0044     }
0045     ta->data = "lalala";
0046     item.addAttribute(ta);
0047     auto cjob = new ItemCreateJob(item, res1);
0048     AKVERIFYEXEC(cjob);
0049     int id = cjob->item().id();
0050     item = Item(id);
0051     auto fjob = new ItemFetchJob(item);
0052     fjob->fetchScope().fetchFullPayload();
0053     fjob->fetchScope().fetchAllAttributes();
0054     AKVERIFYEXEC(fjob);
0055     QCOMPARE(fjob->items().count(), 1);
0056     item = fjob->items().first();
0057     QVERIFY(item.hasAttribute<TestAttribute>()); // has DefaultAttribute
0058     ta = item.attribute<TestAttribute>();
0059     QVERIFY(!ta); // but can't cast it to TestAttribute
0060 }
0061 
0062 void AttributeFactoryTest::testRegisteredAttribute()
0063 {
0064     AttributeFactory::registerAttribute<TestAttribute>();
0065 
0066     Item item;
0067     item.setMimeType(QStringLiteral("text/directory"));
0068     item.setPayload<QByteArray>("payload");
0069     auto ta = new TestAttribute;
0070     {
0071         auto created = AttributeFactory::createAttribute(ta->type());
0072         QVERIFY(created != nullptr);
0073         delete created;
0074     }
0075     ta->data = "lalala";
0076     item.addAttribute(ta);
0077     auto cjob = new ItemCreateJob(item, res1);
0078     AKVERIFYEXEC(cjob);
0079     int id = cjob->item().id();
0080     item = Item(id);
0081     auto fjob = new ItemFetchJob(item);
0082     fjob->fetchScope().fetchFullPayload();
0083     fjob->fetchScope().fetchAllAttributes();
0084     AKVERIFYEXEC(fjob);
0085     QCOMPARE(fjob->items().count(), 1);
0086     item = fjob->items().first();
0087     QVERIFY(item.hasAttribute<TestAttribute>());
0088     ta = item.attribute<TestAttribute>();
0089     QVERIFY(ta);
0090     QCOMPARE(ta->data, QByteArray("lalala"));
0091 }
0092 
0093 #include "moc_attributefactorytest.cpp"