File indexing completed on 2024-11-10 04:40:20
0001 /* 0002 SPDX-FileCopyrightText: 2016 Daniel Vrátil <dvratil@kde.org> 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 ItemMoveHandlerTest : public QObject 0023 { 0024 Q_OBJECT 0025 0026 FakeAkonadiServer mAkonadi; 0027 0028 public: 0029 ItemMoveHandlerTest() 0030 { 0031 mAkonadi.init(); 0032 } 0033 0034 Protocol::FetchItemsResponse fetchResponse(quint64 id, const QString &rid, const QString &rrev, const QString &mt) 0035 { 0036 Protocol::FetchItemsResponse item; 0037 item.setId(id); 0038 item.setRemoteId(rid); 0039 item.setRemoteRevision(rrev); 0040 item.setMimeType(mt); 0041 return item; 0042 } 0043 0044 private Q_SLOTS: 0045 void testMove_data() 0046 { 0047 const Collection srcCol = Collection::retrieveByName(QStringLiteral("Collection B")); 0048 const Collection destCol = Collection::retrieveByName(QStringLiteral("Collection A")); 0049 0050 QTest::addColumn<TestScenario::List>("scenarios"); 0051 QTest::addColumn<Protocol::ChangeNotificationList>("expectedNotifications"); 0052 QTest::addColumn<QVariant>("newValue"); 0053 0054 auto notificationTemplate = Protocol::ItemChangeNotificationPtr::create(); 0055 notificationTemplate->setOperation(Protocol::ItemChangeNotification::Move); 0056 notificationTemplate->setResource("akonadi_fake_resource_0"); 0057 notificationTemplate->setDestinationResource("akonadi_fake_resource_0"); 0058 notificationTemplate->setSessionId(FakeAkonadiServer::instanceName().toLatin1()); 0059 notificationTemplate->setParentCollection(srcCol.id()); 0060 notificationTemplate->setParentDestCollection(destCol.id()); 0061 0062 { 0063 auto cmd = Protocol::MoveItemsCommandPtr::create(1, destCol.id()); 0064 0065 TestScenario::List scenarios; 0066 scenarios << FakeAkonadiServer::loginScenario() << TestScenario::create(5, TestScenario::ClientCmd, cmd) 0067 << TestScenario::create(5, TestScenario::ServerCmd, Protocol::MoveItemsResponsePtr::create()); 0068 0069 auto notification = Protocol::ItemChangeNotificationPtr::create(*notificationTemplate); 0070 notification->setItems({fetchResponse(1, QStringLiteral("A"), QString(), QStringLiteral("application/octet-stream"))}); 0071 0072 QTest::newRow("move item") << scenarios << Protocol::ChangeNotificationList{notification} << QVariant::fromValue(destCol.id()); 0073 } 0074 0075 { 0076 auto cmd = Protocol::MoveItemsCommandPtr::create(QList<qint64>{2, 3}, destCol.id()); 0077 0078 TestScenario::List scenarios; 0079 scenarios << FakeAkonadiServer::loginScenario() << TestScenario::create(5, TestScenario::ClientCmd, cmd) 0080 << TestScenario::create(5, TestScenario::ServerCmd, Protocol::MoveItemsResponsePtr::create()); 0081 0082 auto notification = Protocol::ItemChangeNotificationPtr::create(*notificationTemplate); 0083 notification->setItems({fetchResponse(3, QStringLiteral("C"), QString(), QStringLiteral("application/octet-stream")), 0084 fetchResponse(2, QStringLiteral("B"), QString(), QStringLiteral("application/octet-stream"))}); 0085 0086 QTest::newRow("move items") << scenarios << Protocol::ChangeNotificationList{notification} << QVariant::fromValue(destCol.id()); 0087 } 0088 } 0089 0090 void testMove() 0091 { 0092 QFETCH(TestScenario::List, scenarios); 0093 QFETCH(Protocol::ChangeNotificationList, expectedNotifications); 0094 QFETCH(QVariant, newValue); 0095 0096 mAkonadi.setScenarios(scenarios); 0097 mAkonadi.runTest(); 0098 0099 auto notificationSpy = mAkonadi.notificationSpy(); 0100 if (expectedNotifications.isEmpty()) { 0101 QVERIFY(notificationSpy->isEmpty() || notificationSpy->takeFirst().first().value<Protocol::ChangeNotificationList>().isEmpty()); 0102 return; 0103 } 0104 QTRY_COMPARE(notificationSpy->count(), 1); 0105 // Only one notify call 0106 QCOMPARE(notificationSpy->first().count(), 1); 0107 const auto receivedNotifications = notificationSpy->first().first().value<Protocol::ChangeNotificationList>(); 0108 QCOMPARE(receivedNotifications.size(), expectedNotifications.count()); 0109 0110 for (int i = 0; i < expectedNotifications.size(); i++) { 0111 QCOMPARE(*receivedNotifications.at(i).staticCast<Protocol::ItemChangeNotification>(), 0112 *expectedNotifications.at(i).staticCast<Protocol::ItemChangeNotification>()); 0113 const auto notification = receivedNotifications.at(i).staticCast<Protocol::ItemChangeNotification>(); 0114 QCOMPARE(notification->parentDestCollection(), newValue.toInt()); 0115 0116 const auto ntfItems = notification->items(); 0117 for (const auto &ntfItem : ntfItems) { 0118 const PimItem item = PimItem::retrieveById(ntfItem.id()); 0119 QCOMPARE(item.collectionId(), newValue.toInt()); 0120 } 0121 } 0122 } 0123 }; 0124 0125 AKTEST_FAKESERVER_MAIN(ItemMoveHandlerTest) 0126 0127 #include "itemmovehandlertest.moc"