File indexing completed on 2025-01-05 04:59:51

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Franck Arrecot <franck.arrecot@gmail.com>
0003  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 
0007 #include <testlib/qtest_zanshin.h>
0008 
0009 #include "akonadi/akonadicachingstorage.h"
0010 #include "akonadi/akonadicontextqueries.h"
0011 #include "akonadi/akonadiserializer.h"
0012 
0013 #include "testlib/akonadifakedata.h"
0014 #include "testlib/gencollection.h"
0015 #include "testlib/gentodo.h"
0016 #include "testlib/testhelpers.h"
0017 
0018 using namespace Testlib;
0019 
0020 class AkonadiContextQueriesTest : public QObject
0021 {
0022     Q_OBJECT
0023 private:
0024     Akonadi::StorageInterface::Ptr createCachingStorage(AkonadiFakeData &data, const Akonadi::Cache::Ptr &cache)
0025     {
0026         auto storage = Akonadi::StorageInterface::Ptr(data.createStorage());
0027         return Akonadi::StorageInterface::Ptr(new Akonadi::CachingStorage(cache, storage));
0028     }
0029 
0030 private slots:
0031     void shouldDealWithEmptyContextList()
0032     {
0033         // GIVEN
0034         AkonadiFakeData data;
0035 
0036         // WHEN
0037         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(Akonadi::StorageInterface::Ptr(data.createStorage()),
0038                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0039                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0040                                                                                    Akonadi::Cache::Ptr()));
0041         auto result = queries->findAll();
0042 
0043         // THEN
0044         QVERIFY(result->data().isEmpty());
0045         TestHelpers::waitForEmptyJobQueue();
0046         QVERIFY(result->data().isEmpty());
0047         QCOMPARE(result->data().size(), 0);
0048     }
0049 
0050     void shouldLookInAllReportedForAllContexts()
0051     {
0052         // GIVEN
0053         AkonadiFakeData data;
0054 
0055         // One top level collection
0056         data.createCollection(GenCollection().withId(42).withRootAsParent().withTaskContent());
0057 
0058         // Two contexts
0059         data.createItem(GenTodo().withParent(42).withId(42).withUid("ctx-42").withTitle(QStringLiteral("42")).asContext());
0060         data.createItem(GenTodo().withParent(42).withId(43).withUid("ctx-43").withTitle(QStringLiteral("43")).asContext());
0061 
0062         // WHEN
0063         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(Akonadi::StorageInterface::Ptr(data.createStorage()),
0064                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0065                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0066                                                                                    Akonadi::Cache::Ptr()));
0067         auto result = queries->findAll();
0068         result->data();
0069         result = queries->findAll(); // Should not cause any problem or wrong data
0070 
0071         // THEN
0072         QVERIFY(result->data().isEmpty());
0073         TestHelpers::waitForEmptyJobQueue();
0074 
0075         QCOMPARE(result->data().size(), 2);
0076         QCOMPARE(result->data().at(0)->name(), QStringLiteral("42"));
0077         QCOMPARE(result->data().at(1)->name(), QStringLiteral("43"));
0078     }
0079 
0080     void shouldIgnoreNonContexts()
0081     {
0082         // GIVEN
0083         AkonadiFakeData data;
0084 
0085         // One top level collection
0086         data.createCollection(GenCollection().withId(42).withRootAsParent().withTaskContent());
0087 
0088         // Two items, one of which not a context
0089         data.createItem(GenTodo().withParent(42).withId(42).withUid("ctx-42").withTitle(QStringLiteral("42")).asContext());
0090         data.createItem(GenTodo().withParent(42).withId(43).withUid("ctx-43").withTitle(QStringLiteral("43")));
0091 
0092         // WHEN
0093         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(Akonadi::StorageInterface::Ptr(data.createStorage()),
0094                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0095                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0096                                                                                    Akonadi::Cache::Ptr()));
0097         auto result = queries->findAll();
0098         result->data();
0099         result = queries->findAll(); // Should not cause any problem or wrong data
0100 
0101         // THEN
0102         QVERIFY(result->data().isEmpty());
0103         TestHelpers::waitForEmptyJobQueue();
0104 
0105         QCOMPARE(result->data().size(), 1);
0106         QCOMPARE(result->data().at(0)->name(), QStringLiteral("42"));
0107     }
0108 
0109     void shouldReactToContextAdded()
0110     {
0111         // GIVEN
0112         AkonadiFakeData data;
0113 
0114         // One top level collection
0115         data.createCollection(GenCollection().withId(42).withRootAsParent().withTaskContent());
0116 
0117         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(Akonadi::StorageInterface::Ptr(data.createStorage()),
0118                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0119                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0120                                                                                    Akonadi::Cache::Ptr()));
0121         auto result = queries->findAll();
0122         TestHelpers::waitForEmptyJobQueue();
0123         QVERIFY(result->data().isEmpty());
0124 
0125         // WHEN
0126         data.createItem(GenTodo().withParent(42).withId(42).withUid("ctx-42").withTitle(QStringLiteral("42")).asContext());
0127         data.createItem(GenTodo().withParent(42).withId(43).withUid("ctx-43").withTitle(QStringLiteral("43")).asContext());
0128 
0129         // THEN
0130         QCOMPARE(result->data().size(), 2);
0131         QCOMPARE(result->data().at(0)->name(), QStringLiteral("42"));
0132         QCOMPARE(result->data().at(1)->name(), QStringLiteral("43"));
0133     }
0134 
0135     void shouldReactToContextRemoved()
0136     {
0137         // GIVEN
0138         AkonadiFakeData data;
0139 
0140         // One top level collection
0141         data.createCollection(GenCollection().withId(42).withRootAsParent().withTaskContent());
0142 
0143         // Two contexts
0144         data.createItem(GenTodo().withParent(42).withId(42).withUid("ctx-42").withTitle(QStringLiteral("42")).asContext());
0145         data.createItem(GenTodo().withParent(42).withId(43).withUid("ctx-43").withTitle(QStringLiteral("43")).asContext());
0146 
0147         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(Akonadi::StorageInterface::Ptr(data.createStorage()),
0148                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0149                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0150                                                                                    Akonadi::Cache::Ptr()));
0151         auto result = queries->findAll();
0152         QVERIFY(result->data().isEmpty());
0153         TestHelpers::waitForEmptyJobQueue();
0154         QCOMPARE(result->data().size(), 2);
0155 
0156         // WHEN
0157         data.removeItem(Akonadi::Item(43));
0158 
0159         // THEN
0160         QCOMPARE(result->data().size(), 1);
0161         QCOMPARE(result->data().at(0)->name(), QStringLiteral("42"));
0162     }
0163 
0164     void shouldReactToContextChanges()
0165     {
0166         // GIVEN
0167         AkonadiFakeData data;
0168 
0169         // One top level collection
0170         data.createCollection(GenCollection().withId(42).withRootAsParent().withTaskContent());
0171 
0172         // Two contexts
0173         data.createItem(GenTodo().withParent(42).withId(42).withUid("ctx-42").withTitle(QStringLiteral("42")).asContext());
0174         data.createItem(GenTodo().withParent(42).withId(43).withUid("ctx-43").withTitle(QStringLiteral("43")).asContext());
0175 
0176         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(Akonadi::StorageInterface::Ptr(data.createStorage()),
0177                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0178                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0179                                                                                    Akonadi::Cache::Ptr()));
0180         auto result = queries->findAll();
0181         QVERIFY(result->data().isEmpty());
0182         TestHelpers::waitForEmptyJobQueue();
0183         QCOMPARE(result->data().size(), 2);
0184 
0185         // WHEN
0186         data.modifyItem(GenTodo(data.item(43)).withTitle(QStringLiteral("43bis")));
0187 
0188         // THEN
0189         QCOMPARE(result->data().size(), 2);
0190         QCOMPARE(result->data().at(0)->name(), QStringLiteral("42"));
0191         QCOMPARE(result->data().at(1)->name(), QStringLiteral("43bis"));
0192     }
0193 
0194     void shouldLookForAllContextTopLevelTasks()
0195     {
0196         // GIVEN
0197         AkonadiFakeData data;
0198 
0199         // One top level collection
0200         data.createCollection(GenCollection().withId(42).withRootAsParent().withTaskContent());
0201 
0202         // One context
0203         data.createItem(GenTodo().withParent(42).withId(40).withUid("ctx").withTitle(QStringLiteral("Context")).asContext());
0204 
0205         // Three tasks in the collection, two related to context, one not
0206         data.createItem(GenTodo().withParent(42).withId(42).withTitle(QStringLiteral("42")).withContexts({"ctx"}));
0207         data.createItem(GenTodo().withParent(42).withId(43).withTitle(QStringLiteral("43")));
0208         data.createItem(GenTodo().withParent(42).withId(44).withTitle(QStringLiteral("44")).withContexts({"ctx"}));
0209 
0210         // WHEN
0211         auto serializer = Akonadi::SerializerInterface::Ptr(new Akonadi::Serializer);
0212         auto cache = Akonadi::Cache::Ptr::create(serializer, Akonadi::MonitorInterface::Ptr(data.createMonitor()));
0213         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(createCachingStorage(data, cache),
0214                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0215                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0216                                                                                    cache));
0217 
0218         auto context = serializer->createContextFromItem(data.item(40));
0219         auto result = queries->findTopLevelTasks(context);
0220         result->data();
0221         result = queries->findTopLevelTasks(context); // Should not cause any problem or wrong data
0222 
0223         // THEN
0224         QVERIFY(result->data().isEmpty());
0225         TestHelpers::waitForEmptyJobQueue();
0226 
0227         QCOMPARE(result->data().size(), 2);
0228         QCOMPARE(result->data().at(0)->title(), QStringLiteral("42"));
0229         QCOMPARE(result->data().at(1)->title(), QStringLiteral("44"));
0230 
0231         // Should not change nothing
0232         result = queries->findTopLevelTasks(context);
0233         TestHelpers::waitForEmptyJobQueue();
0234 
0235         QCOMPARE(result->data().size(), 2);
0236         QCOMPARE(result->data().at(0)->title(), QStringLiteral("42"));
0237         QCOMPARE(result->data().at(1)->title(), QStringLiteral("44"));
0238     }
0239 
0240     void shouldNotListContextTasksTwiceIfTheyHaveAParentInTheSameContext()
0241     {
0242         // GIVEN
0243         AkonadiFakeData data;
0244 
0245         // One top level collection
0246         data.createCollection(GenCollection().withId(42).withRootAsParent().withTaskContent());
0247 
0248         // One context item
0249         data.createItem(GenTodo().withParent(42).withId(40).withUid("ctx").withTitle(QStringLiteral("Context")).asContext());
0250 
0251         // Five tasks in the collection, two related to context, three not, all forming an ancestry line
0252         data.createItem(GenTodo().withParent(42).withId(42).withTitle(QStringLiteral("42")).withUid("42"));
0253         data.createItem(GenTodo().withParent(42).withId(43).withTitle(QStringLiteral("43")).withUid("43").withParentUid("42").withContexts({"ctx"}));
0254         data.createItem(GenTodo().withParent(42).withId(44).withTitle(QStringLiteral("44")).withUid("44").withParentUid("43"));
0255         data.createItem(GenTodo().withParent(42).withId(45).withTitle(QStringLiteral("45")).withUid("45").withParentUid("44").withContexts({"ctx"}));
0256         data.createItem(GenTodo().withParent(42).withId(46).withTitle(QStringLiteral("46")).withUid("46").withParentUid("45"));
0257 
0258         // WHEN
0259         auto serializer = Akonadi::SerializerInterface::Ptr(new Akonadi::Serializer);
0260         auto cache = Akonadi::Cache::Ptr::create(serializer, Akonadi::MonitorInterface::Ptr(data.createMonitor()));
0261         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(createCachingStorage(data, cache),
0262                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0263                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0264                                                                                    cache));
0265 
0266         auto context = serializer->createContextFromItem(data.item(40));
0267         auto result = queries->findTopLevelTasks(context);
0268         result->data();
0269         result = queries->findTopLevelTasks(context); // Should not cause any problem or wrong data
0270 
0271         // THEN
0272         QVERIFY(result->data().isEmpty());
0273         TestHelpers::waitForEmptyJobQueue();
0274 
0275         QCOMPARE(result->data().size(), 1);
0276         QCOMPARE(result->data().at(0)->title(), QStringLiteral("43"));
0277 
0278         // Should not change anything
0279         result = queries->findTopLevelTasks(context);
0280         TestHelpers::waitForEmptyJobQueue();
0281 
0282         QCOMPARE(result->data().size(), 1);
0283         QCOMPARE(result->data().at(0)->title(), QStringLiteral("43"));
0284     }
0285 
0286     void shouldReactToItemAddsForTopLevelTask()
0287     {
0288         // GIVEN
0289         AkonadiFakeData data;
0290 
0291         // One top level collection
0292         data.createCollection(GenCollection().withId(42).withRootAsParent().withTaskContent());
0293 
0294         // One context item
0295         data.createItem(GenTodo().withParent(42).withId(40).withUid("ctx").withTitle(QStringLiteral("Context")).asContext());
0296 
0297         auto serializer = Akonadi::SerializerInterface::Ptr(new Akonadi::Serializer);
0298         auto cache = Akonadi::Cache::Ptr::create(serializer, Akonadi::MonitorInterface::Ptr(data.createMonitor()));
0299         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(createCachingStorage(data, cache),
0300                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0301                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0302                                                                                    cache));
0303 
0304         auto context = serializer->createContextFromItem(data.item(40));
0305         auto result = queries->findTopLevelTasks(context);
0306         TestHelpers::waitForEmptyJobQueue();
0307         QVERIFY(result->data().isEmpty());
0308 
0309         // WHEN
0310         data.createItem(GenTodo().withParent(42).withId(42).withTitle(QStringLiteral("42")).withContexts({"ctx"}));
0311 
0312         // THEN
0313         QCOMPARE(result->data().size(), 1);
0314         QCOMPARE(result->data().at(0)->title(), QStringLiteral("42"));
0315     }
0316 
0317     void shoudlReactToItemChangesForTopLevelTask()
0318     {
0319         // GIVEN
0320         AkonadiFakeData data;
0321 
0322         // One top level collection
0323         data.createCollection(GenCollection().withId(42).withRootAsParent().withTaskContent());
0324 
0325         // One context
0326         data.createItem(GenTodo().withParent(42).withId(40).withUid("ctx").withTitle(QStringLiteral("Context")).asContext());
0327 
0328         // One task related to the context
0329         data.createItem(GenTodo().withParent(42).withId(42).withTitle(QStringLiteral("42")).withContexts({"ctx"}));
0330 
0331         auto serializer = Akonadi::SerializerInterface::Ptr(new Akonadi::Serializer);
0332         auto cache = Akonadi::Cache::Ptr::create(serializer, Akonadi::MonitorInterface::Ptr(data.createMonitor()));
0333         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(createCachingStorage(data, cache),
0334                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0335                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0336                                                                                    cache));
0337 
0338         auto context = serializer->createContextFromItem(data.item(40));
0339         auto result = queries->findTopLevelTasks(context);
0340 
0341         bool replaceHandlerCalled = false;
0342         result->addPostReplaceHandler([&replaceHandlerCalled](const Domain::Task::Ptr &, int) {
0343                                           replaceHandlerCalled = true;
0344                                       });
0345         TestHelpers::waitForEmptyJobQueue();
0346         QCOMPARE(result->data().size(), 1);
0347 
0348         // WHEN
0349         data.modifyItem(GenTodo(data.item(42)).withTitle(QStringLiteral("42bis")));
0350 
0351         // THEN
0352         QCOMPARE(result->data().size(), 1);
0353         QCOMPARE(result->data().at(0)->title(), QStringLiteral("42bis"));
0354 
0355         QVERIFY(replaceHandlerCalled);
0356     }
0357 
0358     void shouldAddItemToCorrespondingResultWhenContextAddedToTopLevelTask()
0359     {
0360         // GIVEN
0361         AkonadiFakeData data;
0362 
0363         // One top level collection
0364         data.createCollection(GenCollection().withId(42).withRootAsParent().withTaskContent());
0365 
0366         // One context item
0367         data.createItem(GenTodo().withParent(42).withId(40).withUid("ctx").withTitle(QStringLiteral("Context")).asContext());
0368 
0369         // One task not related to the context
0370         data.createItem(GenTodo().withParent(42).withId(42).withTitle(QStringLiteral("42")));
0371 
0372         auto serializer = Akonadi::SerializerInterface::Ptr(new Akonadi::Serializer);
0373         auto cache = Akonadi::Cache::Ptr::create(serializer, Akonadi::MonitorInterface::Ptr(data.createMonitor()));
0374         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(createCachingStorage(data, cache),
0375                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0376                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0377                                                                                    cache));
0378 
0379         auto context = serializer->createContextFromItem(data.item(40));
0380         auto result = queries->findTopLevelTasks(context);
0381         TestHelpers::waitForEmptyJobQueue();
0382         QVERIFY(result->data().isEmpty());
0383 
0384         // WHEN
0385         data.modifyItem(GenTodo(data.item(42)).withContexts({"ctx"}));
0386 
0387         // THEN
0388         QCOMPARE(result->data().size(), 1);
0389         QCOMPARE(result->data().at(0)->title(), QStringLiteral("42"));
0390     }
0391 
0392     void shouldRemoveItemFromCorrespondingResultWhenContextRemovedFromTopLevelTask()
0393     {
0394         // GIVEN
0395         AkonadiFakeData data;
0396 
0397         // One top level collection
0398         data.createCollection(GenCollection().withId(42).withRootAsParent().withTaskContent());
0399 
0400         // One context item
0401         data.createItem(GenTodo().withParent(42).withId(40).withUid("ctx").withTitle(QStringLiteral("Context")).asContext());
0402 
0403         // One task related to the context
0404         data.createItem(GenTodo().withParent(42).withId(42).withTitle(QStringLiteral("42")).withContexts({"ctx"}));
0405 
0406         auto serializer = Akonadi::SerializerInterface::Ptr(new Akonadi::Serializer);
0407         auto cache = Akonadi::Cache::Ptr::create(serializer, Akonadi::MonitorInterface::Ptr(data.createMonitor()));
0408         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(createCachingStorage(data, cache),
0409                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0410                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0411                                                                                    cache));
0412 
0413         auto context = serializer->createContextFromItem(data.item(40));
0414         QVERIFY(context);
0415         auto result = queries->findTopLevelTasks(context);
0416         TestHelpers::waitForEmptyJobQueue();
0417         QCOMPARE(result->data().size(), 1);
0418 
0419         // WHEN
0420         data.modifyItem(GenTodo(data.item(42)).withContexts({}));
0421 
0422         // THEN
0423         QVERIFY(result->data().isEmpty());
0424     }
0425 
0426     void shouldMoveItemToCorrespondingResultWhenContextChangedOnTopLevelTask()
0427     {
0428         // GIVEN
0429         AkonadiFakeData data;
0430 
0431         // One top level collection
0432         data.createCollection(GenCollection().withId(42).withRootAsParent().withTaskContent());
0433 
0434         // Two context items
0435         data.createItem(GenTodo().withParent(42).withId(1).withUid("ctx-1").withTitle(QStringLiteral("Context 1")).asContext());
0436         data.createItem(GenTodo().withParent(42).withId(2).withUid("ctx-2").withTitle(QStringLiteral("Context 2")).asContext());
0437 
0438         // One task related to the first context
0439         data.createItem(GenTodo().withParent(42).withId(42).withTitle(QStringLiteral("42")).withContexts({"ctx-1"}));
0440 
0441         auto serializer = Akonadi::SerializerInterface::Ptr(new Akonadi::Serializer);
0442         auto cache = Akonadi::Cache::Ptr::create(serializer, Akonadi::MonitorInterface::Ptr(data.createMonitor()));
0443         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(createCachingStorage(data, cache),
0444                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0445                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0446                                                                                    cache));
0447 
0448         auto context1 = serializer->createContextFromItem(data.item(1));
0449         auto result1 = queries->findTopLevelTasks(context1);
0450         auto context2 = serializer->createContextFromItem(data.item(2));
0451         auto result2 = queries->findTopLevelTasks(context2);
0452         TestHelpers::waitForEmptyJobQueue();
0453         QCOMPARE(result1->data().size(), 1);
0454         QCOMPARE(result1->data().at(0)->title(), QStringLiteral("42"));
0455         QVERIFY(result2->data().isEmpty());
0456 
0457         // WHEN
0458         data.modifyItem(GenTodo(data.item(42)).withContexts({"ctx-2"}));
0459 
0460         // THEN
0461         QVERIFY(result1->data().isEmpty());
0462         QCOMPARE(result2->data().size(), 1);
0463         QCOMPARE(result2->data().at(0)->title(), QStringLiteral("42"));
0464     }
0465 
0466     void shoudlReactToItemRemovesForTopLevelTask()
0467     {
0468         // GIVEN
0469         AkonadiFakeData data;
0470 
0471         // One top level collection
0472         data.createCollection(GenCollection().withId(42).withRootAsParent().withTaskContent());
0473 
0474         // One context item
0475         data.createItem(GenTodo().withParent(42).withId(1).withUid("ctx-1").withTitle(QStringLiteral("Context 1")).asContext());
0476 
0477         // A task related to the context
0478         data.createItem(GenTodo().withId(42).withParent(42).withTitle(QStringLiteral("42")).withContexts({"ctx-1"}));
0479 
0480         auto serializer = Akonadi::SerializerInterface::Ptr(new Akonadi::Serializer);
0481         auto cache = Akonadi::Cache::Ptr::create(serializer, Akonadi::MonitorInterface::Ptr(data.createMonitor()));
0482         QScopedPointer<Domain::ContextQueries> queries(new Akonadi::ContextQueries(createCachingStorage(data, cache),
0483                                                                                    Akonadi::Serializer::Ptr(new Akonadi::Serializer),
0484                                                                                    Akonadi::MonitorInterface::Ptr(data.createMonitor()),
0485                                                                                    cache));
0486 
0487         auto context = serializer->createContextFromItem(data.item(1));
0488         QVERIFY(context);
0489         auto result = queries->findTopLevelTasks(context);
0490 
0491         bool removeHandlerCalled = false;
0492         result->addPostRemoveHandler([&removeHandlerCalled](const Domain::Task::Ptr &, int) {
0493                                           removeHandlerCalled = true;
0494                                       });
0495 
0496         TestHelpers::waitForEmptyJobQueue();
0497         QCOMPARE(result->data().size(), 1);
0498         QCOMPARE(result->data().at(0)->title(), QStringLiteral("42"));
0499 
0500         // WHEN
0501         data.removeItem(Akonadi::Item(42));
0502 
0503         // THEN
0504         QVERIFY(result->data().isEmpty());
0505         QVERIFY(removeHandlerCalled);
0506     }
0507 };
0508 
0509 ZANSHIN_TEST_MAIN(AkonadiContextQueriesTest)
0510 
0511 #include "akonadicontextqueriestest.moc"