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

0001 /*
0002     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "collection.h"
0008 #include "collectionfetchjob.h"
0009 #include "control.h"
0010 #include "itemfetchjob.h"
0011 #include "itemfetchscope.h"
0012 #include "linkjob.h"
0013 #include "monitor.h"
0014 #include "qtest_akonadi.h"
0015 #include "searchcreatejob.h"
0016 #include "searchquery.h"
0017 #include "unlinkjob.h"
0018 
0019 #include <QObject>
0020 
0021 using namespace Akonadi;
0022 
0023 class LinkTest : public QObject
0024 {
0025     Q_OBJECT
0026 private Q_SLOTS:
0027     void initTestCase()
0028     {
0029         AkonadiTest::checkTestIsIsolated();
0030         Control::start();
0031     }
0032 
0033     void testLink()
0034     {
0035         auto create = new SearchCreateJob(QStringLiteral("linkTestFolder"), SearchQuery(), this);
0036         AKVERIFYEXEC(create);
0037 
0038         auto list = new CollectionFetchJob(Collection(1), CollectionFetchJob::Recursive, this);
0039         AKVERIFYEXEC(list);
0040         Collection col;
0041         const Collection::List fetchedCollections = list->collections();
0042         for (const Collection &c : fetchedCollections) {
0043             if (c.name() == QLatin1StringView("linkTestFolder")) {
0044                 col = c;
0045             }
0046         }
0047         QVERIFY(col.isValid());
0048 
0049         Item::List items;
0050         items << Item(3) << Item(4) << Item(6);
0051 
0052         // Force-retrieve payload from resource
0053         auto f = new ItemFetchJob(items, this);
0054         f->fetchScope().fetchFullPayload();
0055         AKVERIFYEXEC(f);
0056         const auto itemsLst = f->items();
0057         for (const Item &item : itemsLst) {
0058             QVERIFY(item.hasPayload<QByteArray>());
0059         }
0060 
0061         Monitor monitor;
0062         monitor.setCollectionMonitored(col);
0063         monitor.itemFetchScope().fetchFullPayload();
0064         AkonadiTest::akWaitForSignal(&monitor, &Monitor::monitorReady);
0065 
0066         qRegisterMetaType<Akonadi::Collection>();
0067         qRegisterMetaType<Akonadi::Item>();
0068         QSignalSpy lspy(&monitor, &Monitor::itemLinked);
0069         QSignalSpy uspy(&monitor, &Monitor::itemUnlinked);
0070         QVERIFY(lspy.isValid());
0071         QVERIFY(uspy.isValid());
0072 
0073         auto link = new LinkJob(col, items, this);
0074         AKVERIFYEXEC(link);
0075 
0076         QTRY_COMPARE(lspy.count(), 3);
0077         QTest::qWait(100);
0078         QVERIFY(uspy.isEmpty());
0079 
0080         QList<QVariant> arg = lspy.takeFirst();
0081         Item item = arg.at(0).value<Item>();
0082         QCOMPARE(item.mimeType(), QString::fromLatin1("application/octet-stream"));
0083         QVERIFY(item.hasPayload<QByteArray>());
0084 
0085         lspy.clear();
0086 
0087         auto fetch = new ItemFetchJob(col);
0088         AKVERIFYEXEC(fetch);
0089         QCOMPARE(fetch->items().count(), 3);
0090         const Item::List fetchedItems = fetch->items();
0091         for (const Item &fetchedItem : fetchedItems) {
0092             QVERIFY(items.contains(fetchedItem));
0093         }
0094 
0095         auto unlink = new UnlinkJob(col, items, this);
0096         AKVERIFYEXEC(unlink);
0097 
0098         QTRY_COMPARE(uspy.count(), 3);
0099         QTest::qWait(100);
0100         QVERIFY(lspy.isEmpty());
0101 
0102         fetch = new ItemFetchJob(col);
0103         AKVERIFYEXEC(fetch);
0104         QCOMPARE(fetch->items().count(), 0);
0105     }
0106 };
0107 
0108 QTEST_AKONADIMAIN(LinkTest)
0109 
0110 #include "linktest.moc"