File indexing completed on 2024-11-10 04:40:18
0001 /* 0002 SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 #include <QObject> 0007 0008 #include "storage/entity.h" 0009 0010 #include "aktest.h" 0011 #include "entities.h" 0012 #include "fakeakonadiserver.h" 0013 0014 #include "private/imapset_p.h" 0015 #include "private/scope_p.h" 0016 0017 #include <QTest> 0018 0019 using namespace Akonadi; 0020 using namespace Akonadi::Server; 0021 0022 class CollectionModifyHandlerTest : public QObject 0023 { 0024 Q_OBJECT 0025 0026 FakeAkonadiServer mAkonadi; 0027 0028 public: 0029 CollectionModifyHandlerTest() 0030 { 0031 mAkonadi.init(); 0032 } 0033 0034 private Q_SLOTS: 0035 void testModify_data() 0036 { 0037 QTest::addColumn<TestScenario::List>("scenarios"); 0038 QTest::addColumn<Protocol::ChangeNotificationList>("expectedNotifications"); 0039 QTest::addColumn<QVariant>("newValue"); 0040 0041 auto notificationTemplate = Protocol::CollectionChangeNotificationPtr::create(); 0042 notificationTemplate->setOperation(Protocol::CollectionChangeNotification::Modify); 0043 notificationTemplate->setParentCollection(4); 0044 notificationTemplate->setResource("akonadi_fake_resource_0"); 0045 notificationTemplate->setSessionId(FakeAkonadiServer::instanceName().toLatin1()); 0046 0047 auto collectionTemplate = Protocol::FetchCollectionsResponsePtr::create(); 0048 collectionTemplate->setId(5); 0049 collectionTemplate->setRemoteId(QStringLiteral("ColD")); 0050 collectionTemplate->setRemoteRevision(QStringLiteral("")); 0051 collectionTemplate->setName(QStringLiteral("New Name")); 0052 collectionTemplate->setParentId(4); 0053 collectionTemplate->setResource(QStringLiteral("akonadi_fake_resource_0")); 0054 0055 { 0056 auto cmd = Protocol::ModifyCollectionCommandPtr::create(5); 0057 cmd->setName(QStringLiteral("New Name")); 0058 0059 TestScenario::List scenarios; 0060 scenarios << FakeAkonadiServer::loginScenario() << TestScenario::create(5, TestScenario::ClientCmd, cmd) 0061 << TestScenario::create(5, TestScenario::ServerCmd, Protocol::ModifyCollectionResponsePtr::create()); 0062 0063 auto notification = Protocol::CollectionChangeNotificationPtr::create(*notificationTemplate); 0064 notification->setChangedParts(QSet<QByteArray>() << "NAME"); 0065 notification->setCollection(*collectionTemplate); 0066 QTest::newRow("modify collection") << scenarios << Protocol::ChangeNotificationList{notification} 0067 << QVariant::fromValue(QStringLiteral("New Name")); 0068 } 0069 { 0070 auto cmd = Protocol::ModifyCollectionCommandPtr::create(5); 0071 cmd->setEnabled(false); 0072 0073 TestScenario::List scenarios; 0074 scenarios << FakeAkonadiServer::loginScenario() << TestScenario::create(5, TestScenario::ClientCmd, cmd) 0075 << TestScenario::create(5, TestScenario::ServerCmd, Protocol::ModifyCollectionResponsePtr::create()); 0076 0077 Protocol::FetchCollectionsResponse collection(*collectionTemplate); 0078 collection.setEnabled(false); 0079 auto notification = Protocol::CollectionChangeNotificationPtr::create(*notificationTemplate); 0080 notification->setChangedParts(QSet<QByteArray>() << "ENABLED"); 0081 notification->setCollection(collection); 0082 auto unsubscribeNotification = Protocol::CollectionChangeNotificationPtr::create(*notificationTemplate); 0083 unsubscribeNotification->setOperation(Protocol::CollectionChangeNotification::Unsubscribe); 0084 unsubscribeNotification->setCollection(std::move(collection)); 0085 0086 QTest::newRow("disable collection") << scenarios << Protocol::ChangeNotificationList{notification, unsubscribeNotification} 0087 << QVariant::fromValue(false); 0088 } 0089 { 0090 auto cmd = Protocol::ModifyCollectionCommandPtr::create(5); 0091 cmd->setEnabled(true); 0092 0093 TestScenario::List scenarios; 0094 scenarios << FakeAkonadiServer::loginScenario() << TestScenario::create(5, TestScenario::ClientCmd, cmd) 0095 << TestScenario::create(5, TestScenario::ServerCmd, Protocol::ModifyCollectionResponsePtr::create()); 0096 0097 auto notification = Protocol::CollectionChangeNotificationPtr::create(*notificationTemplate); 0098 notification->setChangedParts(QSet<QByteArray>() << "ENABLED"); 0099 notification->setCollection(*collectionTemplate); 0100 auto subscribeNotification = Protocol::CollectionChangeNotificationPtr::create(*notificationTemplate); 0101 subscribeNotification->setOperation(Protocol::CollectionChangeNotification::Subscribe); 0102 subscribeNotification->setCollection(*collectionTemplate); 0103 0104 QTest::newRow("enable collection") << scenarios << Protocol::ChangeNotificationList{notification, subscribeNotification} 0105 << QVariant::fromValue(true); 0106 } 0107 { 0108 auto cmd = Protocol::ModifyCollectionCommandPtr::create(5); 0109 cmd->setEnabled(false); 0110 cmd->setSyncPref(Tristate::True); 0111 cmd->setDisplayPref(Tristate::True); 0112 cmd->setIndexPref(Tristate::True); 0113 0114 TestScenario::List scenarios; 0115 scenarios << FakeAkonadiServer::loginScenario() << TestScenario::create(5, TestScenario::ClientCmd, cmd) 0116 << TestScenario::create(5, TestScenario::ServerCmd, Protocol::ModifyCollectionResponsePtr::create()); 0117 0118 Protocol::FetchCollectionsResponse collection(*collectionTemplate); 0119 collection.setEnabled(false); 0120 collection.setSyncPref(Tristate::True); 0121 collection.setDisplayPref(Tristate::True); 0122 collection.setIndexPref(Tristate::True); 0123 auto notification = Protocol::CollectionChangeNotificationPtr::create(*notificationTemplate); 0124 notification->setChangedParts(QSet<QByteArray>() << "ENABLED" 0125 << "SYNC" 0126 << "DISPLAY" 0127 << "INDEX"); 0128 notification->setCollection(collection); 0129 auto unsubscribeNotification = Protocol::CollectionChangeNotificationPtr::create(*notificationTemplate); 0130 unsubscribeNotification->setOperation(Protocol::CollectionChangeNotification::Unsubscribe); 0131 unsubscribeNotification->setCollection(std::move(collection)); 0132 0133 QTest::newRow("local override enable") << scenarios << Protocol::ChangeNotificationList{notification, unsubscribeNotification} 0134 << QVariant::fromValue(true); 0135 } 0136 } 0137 0138 void testModify() 0139 { 0140 QFETCH(TestScenario::List, scenarios); 0141 QFETCH(Protocol::ChangeNotificationList, expectedNotifications); 0142 QFETCH(QVariant, newValue); 0143 0144 mAkonadi.setScenarios(scenarios); 0145 mAkonadi.runTest(); 0146 0147 auto notificationSpy = mAkonadi.notificationSpy(); 0148 if (expectedNotifications.isEmpty()) { 0149 QVERIFY(notificationSpy->isEmpty() || notificationSpy->takeFirst().first().value<Protocol::ChangeNotificationList>().isEmpty()); 0150 return; 0151 } 0152 QTRY_COMPARE(notificationSpy->count(), 1); 0153 // Only one notify call 0154 QCOMPARE(notificationSpy->first().count(), 1); 0155 const auto receivedNotifications = notificationSpy->first().first().value<Protocol::ChangeNotificationList>(); 0156 QCOMPARE(receivedNotifications.size(), expectedNotifications.count()); 0157 0158 for (int i = 0; i < expectedNotifications.size(); i++) { 0159 const auto recvNtf = receivedNotifications.at(i).staticCast<Protocol::CollectionChangeNotification>(); 0160 const auto expNtf = expectedNotifications.at(i).staticCast<Protocol::CollectionChangeNotification>(); 0161 if (*recvNtf != *expNtf) { 0162 qDebug() << "Actual: " << Protocol::debugString(recvNtf); 0163 qDebug() << "Expected:" << Protocol::debugString(expNtf); 0164 } 0165 QCOMPARE(*recvNtf, *expNtf); 0166 const auto notification = receivedNotifications.at(i).staticCast<Protocol::CollectionChangeNotification>(); 0167 if (notification->changedParts().contains("NAME")) { 0168 Collection col = Collection::retrieveById(notification->collection().id()); 0169 QCOMPARE(col.name(), newValue.toString()); 0170 } 0171 if (!notification->changedParts().intersects({"ENABLED", "SYNC", "DISPLAY", "INDEX"})) { 0172 Collection col = Collection::retrieveById(notification->collection().id()); 0173 const bool sync = col.syncPref() == Collection::Undefined ? col.enabled() : col.syncPref() == Collection::True; 0174 QCOMPARE(sync, newValue.toBool()); 0175 const bool display = col.displayPref() == Collection::Undefined ? col.enabled() : col.displayPref() == Collection::True; 0176 QCOMPARE(display, newValue.toBool()); 0177 const bool index = col.indexPref() == Collection::Undefined ? col.enabled() : col.indexPref() == Collection::True; 0178 QCOMPARE(index, newValue.toBool()); 0179 } 0180 } 0181 } 0182 }; 0183 0184 AKTEST_FAKESERVER_MAIN(CollectionModifyHandlerTest) 0185 0186 #include "collectionmodifyhandlertest.moc"