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

0001 /*
0002     SPDX-FileCopyrightText: 2019 David Faure <faure@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "aktest.h"
0008 
0009 #include "aggregatedfetchscope.h"
0010 #include "entities.h"
0011 #include "notificationmanager.h"
0012 #include "notificationsubscriber.h"
0013 
0014 #include <QObject>
0015 #include <QTest>
0016 
0017 using namespace Akonadi;
0018 using namespace Akonadi::Server;
0019 
0020 class TestableNotificationSubscriber : public NotificationSubscriber
0021 {
0022 public:
0023     explicit TestableNotificationSubscriber(NotificationManager *manager)
0024         : NotificationSubscriber(manager)
0025     {
0026         mSubscriber = "TestSubscriber";
0027     }
0028 
0029     using NotificationSubscriber::disconnectSubscriber;
0030     using NotificationSubscriber::modifySubscription;
0031     using NotificationSubscriber::registerSubscriber;
0032 };
0033 
0034 class NotificationManagerTest : public QObject
0035 {
0036     Q_OBJECT
0037 
0038 private Q_SLOTS:
0039     void testAggregatedFetchScope()
0040     {
0041         NotificationManager manager(AkThread::NoThread);
0042         QMetaObject::invokeMethod(&manager, "init", Qt::DirectConnection);
0043 
0044         // first subscriber, A
0045         TestableNotificationSubscriber subscriberA(&manager);
0046         Protocol::CreateSubscriptionCommand createCmd;
0047         createCmd.setSession("session1");
0048         subscriberA.registerSubscriber(createCmd);
0049         QVERIFY(!manager.tagFetchScope()->fetchIdOnly());
0050         QVERIFY(manager.tagFetchScope()->fetchAllAttributes()); // default is true
0051         QVERIFY(!manager.collectionFetchScope()->fetchIdOnly());
0052         QVERIFY(!manager.collectionFetchScope()->fetchStatistics());
0053 
0054         // set A's subscription settings
0055         Protocol::ModifySubscriptionCommand modifyCmd;
0056         {
0057             Protocol::TagFetchScope tagFetchScope;
0058             tagFetchScope.setFetchIdOnly(true);
0059             tagFetchScope.setFetchAllAttributes(false);
0060             modifyCmd.setTagFetchScope(tagFetchScope);
0061 
0062             Protocol::CollectionFetchScope collectionFetchScope;
0063             collectionFetchScope.setFetchIdOnly(true);
0064             collectionFetchScope.setIncludeStatistics(true);
0065             modifyCmd.setCollectionFetchScope(collectionFetchScope);
0066 
0067             Protocol::ItemFetchScope itemFetchScope;
0068             itemFetchScope.setFetch(Protocol::ItemFetchScope::FullPayload);
0069             itemFetchScope.setFetch(Protocol::ItemFetchScope::AllAttributes);
0070             itemFetchScope.setFetch(Protocol::ItemFetchScope::Size);
0071             itemFetchScope.setFetch(Protocol::ItemFetchScope::MTime);
0072             itemFetchScope.setFetch(Protocol::ItemFetchScope::RemoteRevision);
0073             itemFetchScope.setFetch(Protocol::ItemFetchScope::Flags);
0074             itemFetchScope.setFetch(Protocol::ItemFetchScope::RemoteID);
0075             itemFetchScope.setFetch(Protocol::ItemFetchScope::GID);
0076             itemFetchScope.setFetch(Protocol::ItemFetchScope::Tags);
0077             itemFetchScope.setFetch(Protocol::ItemFetchScope::Relations);
0078             itemFetchScope.setFetch(Protocol::ItemFetchScope::VirtReferences);
0079             modifyCmd.setItemFetchScope(itemFetchScope);
0080         }
0081         subscriberA.modifySubscription(modifyCmd);
0082         QVERIFY(manager.tagFetchScope()->fetchIdOnly());
0083         QVERIFY(!manager.tagFetchScope()->fetchAllAttributes());
0084         QVERIFY(manager.collectionFetchScope()->fetchIdOnly());
0085         QVERIFY(manager.collectionFetchScope()->fetchStatistics());
0086         QVERIFY(manager.itemFetchScope()->fullPayload());
0087         QVERIFY(manager.itemFetchScope()->allAttributes());
0088 
0089         // second subscriber, B
0090         TestableNotificationSubscriber subscriberB(&manager);
0091         subscriberB.registerSubscriber(createCmd);
0092         QVERIFY(!manager.tagFetchScope()->fetchIdOnly()); // A and B don't agree, so: false
0093         QVERIFY(manager.tagFetchScope()->fetchAllAttributes());
0094         QVERIFY(!manager.collectionFetchScope()->fetchIdOnly());
0095         QVERIFY(manager.collectionFetchScope()->fetchStatistics()); // at least one - so still true
0096         QVERIFY(manager.itemFetchScope()->fullPayload());
0097         QVERIFY(manager.itemFetchScope()->allAttributes());
0098         QVERIFY(manager.itemFetchScope()->fetchSize());
0099         QVERIFY(manager.itemFetchScope()->fetchMTime());
0100         QVERIFY(manager.itemFetchScope()->fetchRemoteRevision());
0101         QVERIFY(manager.itemFetchScope()->fetchFlags());
0102         QVERIFY(manager.itemFetchScope()->fetchRemoteId());
0103         QVERIFY(manager.itemFetchScope()->fetchGID());
0104         QVERIFY(manager.itemFetchScope()->fetchTags());
0105         QVERIFY(manager.itemFetchScope()->fetchRelations());
0106         QVERIFY(manager.itemFetchScope()->fetchVirtualReferences());
0107 
0108         // give it the same settings
0109         subscriberB.modifySubscription(modifyCmd);
0110         QVERIFY(manager.tagFetchScope()->fetchIdOnly()); // now they agree
0111         QVERIFY(!manager.tagFetchScope()->fetchAllAttributes());
0112         QVERIFY(manager.collectionFetchScope()->fetchIdOnly());
0113         QVERIFY(manager.collectionFetchScope()->fetchStatistics()); // no change for the "at least one" settings
0114 
0115         // revert B's settings, so we can check what happens when disconnecting
0116         modifyCmd.setTagFetchScope(Protocol::TagFetchScope());
0117         modifyCmd.setCollectionFetchScope(Protocol::CollectionFetchScope());
0118         subscriberB.modifySubscription(modifyCmd);
0119         QVERIFY(!manager.tagFetchScope()->fetchIdOnly());
0120         QVERIFY(manager.tagFetchScope()->fetchAllAttributes());
0121         QVERIFY(!manager.collectionFetchScope()->fetchIdOnly());
0122         QVERIFY(manager.collectionFetchScope()->fetchStatistics());
0123 
0124         // B goes away
0125         subscriberB.disconnectSubscriber();
0126         QVERIFY(manager.tagFetchScope()->fetchIdOnly()); // B cleaned up after itself, so A can have id-only again
0127         QVERIFY(!manager.tagFetchScope()->fetchAllAttributes());
0128         QVERIFY(manager.collectionFetchScope()->fetchIdOnly());
0129         QVERIFY(manager.collectionFetchScope()->fetchStatistics());
0130 
0131         // A goes away
0132         subscriberA.disconnectSubscriber();
0133         QVERIFY(!manager.collectionFetchScope()->fetchStatistics());
0134         QVERIFY(!manager.itemFetchScope()->fullPayload());
0135         QVERIFY(!manager.itemFetchScope()->allAttributes());
0136         QVERIFY(!manager.itemFetchScope()->fetchSize());
0137         QVERIFY(!manager.itemFetchScope()->fetchMTime());
0138         QVERIFY(!manager.itemFetchScope()->fetchRemoteRevision());
0139         QVERIFY(!manager.itemFetchScope()->fetchFlags());
0140         QVERIFY(!manager.itemFetchScope()->fetchRemoteId());
0141         QVERIFY(!manager.itemFetchScope()->fetchGID());
0142         QVERIFY(!manager.itemFetchScope()->fetchTags());
0143         QVERIFY(!manager.itemFetchScope()->fetchRelations());
0144         QVERIFY(!manager.itemFetchScope()->fetchVirtualReferences());
0145     }
0146 };
0147 
0148 AKTEST_MAIN(NotificationManagerTest)
0149 
0150 #include "notificationmanagertest.moc"