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

0001 /*
0002     SPDX-FileCopyrightText: 2019 David Faure <faure@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QObject>
0008 
0009 #include "aggregatedfetchscope.h"
0010 #include "shared/aktest.h"
0011 
0012 #include <QTest>
0013 
0014 using namespace Akonadi;
0015 using namespace Akonadi::Server;
0016 
0017 class AggregatedFetchScopeTest : public QObject
0018 {
0019     Q_OBJECT
0020 
0021 private Q_SLOTS:
0022 
0023     void testTagApply()
0024     {
0025         AggregatedTagFetchScope scope;
0026 
0027         // first subscriber, A
0028         scope.addSubscriber();
0029         Protocol::TagFetchScope oldTagScope;
0030         Protocol::TagFetchScope tagScopeA;
0031         QSet<QByteArray> attrs = {"FOO"};
0032         tagScopeA.setAttributes(attrs);
0033         tagScopeA.setFetchIdOnly(true);
0034         tagScopeA.setFetchAllAttributes(false);
0035         scope.apply(oldTagScope, tagScopeA);
0036         QCOMPARE(scope.attributes(), attrs);
0037         QVERIFY(scope.fetchIdOnly());
0038         QVERIFY(!scope.fetchAllAttributes());
0039 
0040         // second subscriber, B
0041         Protocol::TagFetchScope tagScopeB = tagScopeA;
0042         tagScopeB.setFetchIdOnly(false);
0043         tagScopeB.setFetchAllAttributes(true);
0044         scope.addSubscriber();
0045         scope.apply(oldTagScope, tagScopeB);
0046         QCOMPARE(scope.attributes(), attrs);
0047         QVERIFY(!scope.fetchIdOnly());
0048         QVERIFY(scope.fetchAllAttributes());
0049 
0050         // then B goes away
0051         scope.apply(tagScopeB, oldTagScope);
0052         scope.removeSubscriber();
0053         QCOMPARE(scope.attributes(), attrs);
0054         QVERIFY(scope.fetchIdOnly());
0055         QVERIFY(!scope.fetchAllAttributes());
0056 
0057         // A goes away
0058         scope.apply(tagScopeA, oldTagScope);
0059         scope.removeSubscriber();
0060         QCOMPARE(scope.attributes(), QSet<QByteArray>());
0061     }
0062 
0063     void testCollectionApply()
0064     {
0065         AggregatedCollectionFetchScope scope;
0066 
0067         // first subscriber, A
0068         scope.addSubscriber();
0069         Protocol::CollectionFetchScope oldCollectionScope;
0070         Protocol::CollectionFetchScope collectionScopeA;
0071         QSet<QByteArray> attrs = {"FOO"};
0072         collectionScopeA.setAttributes(attrs);
0073         collectionScopeA.setFetchIdOnly(true);
0074         scope.apply(oldCollectionScope, collectionScopeA);
0075         QCOMPARE(scope.attributes(), attrs);
0076         QVERIFY(scope.fetchIdOnly());
0077 
0078         // second subscriber, B
0079         Protocol::CollectionFetchScope collectionScopeB = collectionScopeA;
0080         collectionScopeB.setFetchIdOnly(false);
0081         scope.addSubscriber();
0082         scope.apply(oldCollectionScope, collectionScopeB);
0083         QCOMPARE(scope.attributes(), attrs);
0084         QVERIFY(!scope.fetchIdOnly());
0085 
0086         // then B goes away
0087         scope.apply(collectionScopeB, oldCollectionScope);
0088         scope.removeSubscriber();
0089         QCOMPARE(scope.attributes(), attrs);
0090         QVERIFY(scope.fetchIdOnly());
0091 
0092         // A goes away
0093         scope.apply(collectionScopeA, oldCollectionScope);
0094         scope.removeSubscriber();
0095         QCOMPARE(scope.attributes(), QSet<QByteArray>());
0096     }
0097 
0098     void testItemApply()
0099     {
0100         AggregatedItemFetchScope scope;
0101         QCOMPARE(scope.ancestorDepth(), Protocol::ItemFetchScope::NoAncestor);
0102 
0103         // first subscriber, A
0104         scope.addSubscriber();
0105         Protocol::ItemFetchScope oldItemScope;
0106         Protocol::ItemFetchScope itemScopeA;
0107         QList<QByteArray> parts = {"FOO"};
0108         QSet<QByteArray> partsSet = {"FOO"};
0109         itemScopeA.setRequestedParts(parts);
0110         itemScopeA.setAncestorDepth(Protocol::ItemFetchScope::ParentAncestor);
0111         itemScopeA.setFetch(Protocol::ItemFetchScope::CacheOnly);
0112         itemScopeA.setFetch(Protocol::ItemFetchScope::IgnoreErrors);
0113         scope.apply(oldItemScope, itemScopeA);
0114         QCOMPARE(scope.requestedParts(), partsSet);
0115         QCOMPARE(scope.ancestorDepth(), Protocol::ItemFetchScope::ParentAncestor);
0116         QVERIFY(scope.cacheOnly());
0117         QVERIFY(scope.ignoreErrors());
0118 
0119         // second subscriber, B
0120         Protocol::ItemFetchScope itemScopeB = itemScopeA;
0121         itemScopeB.setAncestorDepth(Protocol::ItemFetchScope::AllAncestors);
0122         scope.addSubscriber();
0123         QVERIFY(!scope.cacheOnly()); // they don't agree so: false
0124         QVERIFY(!scope.ignoreErrors());
0125         scope.apply(oldItemScope, itemScopeB);
0126         QCOMPARE(scope.requestedParts(), partsSet);
0127         QCOMPARE(scope.ancestorDepth(), Protocol::ItemFetchScope::AllAncestors);
0128 
0129         // subscriber C with ParentAncestor - but that won't make change it
0130         Protocol::ItemFetchScope itemScopeC = itemScopeA;
0131         scope.addSubscriber();
0132         scope.apply(oldItemScope, itemScopeC);
0133         QCOMPARE(scope.requestedParts(), partsSet);
0134         QCOMPARE(scope.ancestorDepth(), Protocol::ItemFetchScope::AllAncestors); // no change
0135 
0136         // then C goes away
0137         scope.apply(itemScopeC, oldItemScope);
0138         scope.removeSubscriber();
0139         QCOMPARE(scope.requestedParts(), partsSet);
0140         QCOMPARE(scope.ancestorDepth(), Protocol::ItemFetchScope::AllAncestors);
0141 
0142         // then B goes away
0143         scope.apply(itemScopeB, oldItemScope);
0144         scope.removeSubscriber();
0145         QCOMPARE(scope.requestedParts(), partsSet);
0146         QCOMPARE(scope.ancestorDepth(), Protocol::ItemFetchScope::ParentAncestor);
0147 
0148         // A goes away
0149         scope.apply(itemScopeA, oldItemScope);
0150         scope.removeSubscriber();
0151         QCOMPARE(scope.requestedParts(), QSet<QByteArray>());
0152         QCOMPARE(scope.ancestorDepth(), Protocol::ItemFetchScope::NoAncestor);
0153     }
0154 };
0155 
0156 QTEST_MAIN(AggregatedFetchScopeTest)
0157 
0158 #include "aggregatedfetchscopetest.moc"