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

0001 /*
0002     SPDX-FileCopyrightText: 2009 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "agentinstance.h"
0008 #include "agentmanager.h"
0009 #include "collection.h"
0010 #include "collectionfetchjob.h"
0011 #include "collectionmodifyjob.h"
0012 #include "item.h"
0013 #include "itemcopyjob.h"
0014 #include "itemfetchjob.h"
0015 #include "itemfetchscope.h"
0016 #include "qtest_akonadi.h"
0017 
0018 #include <QHash>
0019 
0020 using namespace Akonadi;
0021 
0022 class CacheTest : public QObject
0023 {
0024     Q_OBJECT
0025 private:
0026     void enableAgent(const QString &id, bool enable)
0027     {
0028         auto instance = AgentManager::self()->instance(id);
0029         QVERIFY(instance.isValid());
0030 
0031         instance.setIsOnline(enable);
0032         QTRY_COMPARE(Akonadi::AgentManager::self()->instance(id).isOnline(), enable);
0033     }
0034 
0035 private Q_SLOTS:
0036     void initTestCase()
0037     {
0038         AkonadiTest::checkTestIsIsolated();
0039     }
0040     void testRetrievalErrorBurst() // caused rare server crashes with old item retrieval code
0041     {
0042         Collection col(AkonadiTest::collectionIdFromPath(QStringLiteral("res1/foo")));
0043         QVERIFY(col.isValid());
0044 
0045         enableAgent(QStringLiteral("akonadi_knut_resource_0"), false);
0046 
0047         auto fetch = new ItemFetchJob(col, this);
0048         fetch->fetchScope().fetchFullPayload(true);
0049         QVERIFY(!fetch->exec());
0050     }
0051 
0052     void testResourceRetrievalOnFetch_data()
0053     {
0054         QTest::addColumn<Item>("item");
0055         QTest::addColumn<bool>("resourceEnabled");
0056 
0057         QTest::newRow("resource online") << Item(1) << true;
0058         QTest::newRow("resource offline") << Item(2) << false;
0059     }
0060 
0061     void testResourceRetrievalOnFetch()
0062     {
0063         QFETCH(Item, item);
0064         QFETCH(bool, resourceEnabled);
0065 
0066         auto fetch = new ItemFetchJob(item, this);
0067         fetch->fetchScope().fetchFullPayload();
0068         fetch->fetchScope().setCacheOnly(true);
0069         AKVERIFYEXEC(fetch);
0070         QCOMPARE(fetch->items().count(), 1);
0071         item = fetch->items().first();
0072         QVERIFY(item.isValid());
0073         QVERIFY(!item.hasPayload());
0074 
0075         enableAgent(QStringLiteral("akonadi_knut_resource_0"), resourceEnabled);
0076 
0077         fetch = new ItemFetchJob(item, this);
0078         fetch->fetchScope().fetchFullPayload();
0079         QCOMPARE(fetch->exec(), resourceEnabled);
0080         if (resourceEnabled) {
0081             QCOMPARE(fetch->items().count(), 1);
0082             item = fetch->items().first();
0083             QVERIFY(item.isValid());
0084             QVERIFY(item.hasPayload());
0085             QVERIFY(item.revision() > 0); // was changed by the resource delivering the payload
0086         }
0087 
0088         fetch = new ItemFetchJob(item, this);
0089         fetch->fetchScope().fetchFullPayload();
0090         fetch->fetchScope().setCacheOnly(true);
0091         AKVERIFYEXEC(fetch);
0092         QCOMPARE(fetch->items().count(), 1);
0093         item = fetch->items().first();
0094         QVERIFY(item.isValid());
0095         QCOMPARE(item.hasPayload(), resourceEnabled);
0096     }
0097 
0098     void testResourceRetrievalOnCopy_data()
0099     {
0100         QTest::addColumn<Item>("item");
0101         QTest::addColumn<bool>("resourceEnabled");
0102 
0103         QTest::newRow("online") << Item(3) << true;
0104         QTest::newRow("offline") << Item(4) << false;
0105     }
0106 
0107     void testResourceRetrievalOnCopy()
0108     {
0109         QFETCH(Item, item);
0110         QFETCH(bool, resourceEnabled);
0111 
0112         auto fetch = new ItemFetchJob(item, this);
0113         fetch->fetchScope().fetchFullPayload();
0114         fetch->fetchScope().setCacheOnly(true);
0115         AKVERIFYEXEC(fetch);
0116         QCOMPARE(fetch->items().count(), 1);
0117         item = fetch->items().first();
0118         QVERIFY(item.isValid());
0119         QVERIFY(!item.hasPayload());
0120 
0121         enableAgent(QStringLiteral("akonadi_knut_resource_0"), resourceEnabled);
0122 
0123         Collection dest(AkonadiTest::collectionIdFromPath(QStringLiteral("res3")));
0124         QVERIFY(dest.isValid());
0125 
0126         auto copy = new ItemCopyJob(item, dest, this);
0127         QCOMPARE(copy->exec(), resourceEnabled);
0128 
0129         fetch = new ItemFetchJob(item, this);
0130         fetch->fetchScope().fetchFullPayload();
0131         fetch->fetchScope().setCacheOnly(true);
0132         AKVERIFYEXEC(fetch);
0133         QCOMPARE(fetch->items().count(), 1);
0134         item = fetch->items().first();
0135         QVERIFY(item.isValid());
0136         QCOMPARE(item.hasPayload(), resourceEnabled);
0137     }
0138 };
0139 
0140 QTEST_AKONADIMAIN(CacheTest)
0141 
0142 #include "cachetest.moc"