diff options
Diffstat (limited to 'common/test/clientapitest.cpp')
-rw-r--r-- | common/test/clientapitest.cpp | 106 |
1 files changed, 103 insertions, 3 deletions
diff --git a/common/test/clientapitest.cpp b/common/test/clientapitest.cpp index 24b3fb9..789c656 100644 --- a/common/test/clientapitest.cpp +++ b/common/test/clientapitest.cpp | |||
@@ -3,6 +3,22 @@ | |||
3 | #include <functional> | 3 | #include <functional> |
4 | 4 | ||
5 | #include "../clientapi.h" | 5 | #include "../clientapi.h" |
6 | #include "../facade.h" | ||
7 | #include "../synclistresult.h" | ||
8 | |||
9 | class RevisionNotifier : public QObject | ||
10 | { | ||
11 | Q_OBJECT | ||
12 | public: | ||
13 | RevisionNotifier() : QObject() {}; | ||
14 | void notify(qint64 revision) | ||
15 | { | ||
16 | emit revisionChanged(revision); | ||
17 | } | ||
18 | |||
19 | Q_SIGNALS: | ||
20 | void revisionChanged(qint64); | ||
21 | }; | ||
6 | 22 | ||
7 | class DummyResourceFacade : public Akonadi2::StoreFacade<Akonadi2::ApplicationDomain::Event> | 23 | class DummyResourceFacade : public Akonadi2::StoreFacade<Akonadi2::ApplicationDomain::Event> |
8 | { | 24 | { |
@@ -11,18 +27,63 @@ public: | |||
11 | virtual Async::Job<void> create(const Akonadi2::ApplicationDomain::Event &domainObject){ return Async::null<void>(); }; | 27 | virtual Async::Job<void> create(const Akonadi2::ApplicationDomain::Event &domainObject){ return Async::null<void>(); }; |
12 | virtual Async::Job<void> modify(const Akonadi2::ApplicationDomain::Event &domainObject){ return Async::null<void>(); }; | 28 | virtual Async::Job<void> modify(const Akonadi2::ApplicationDomain::Event &domainObject){ return Async::null<void>(); }; |
13 | virtual Async::Job<void> remove(const Akonadi2::ApplicationDomain::Event &domainObject){ return Async::null<void>(); }; | 29 | virtual Async::Job<void> remove(const Akonadi2::ApplicationDomain::Event &domainObject){ return Async::null<void>(); }; |
14 | virtual Async::Job<void> load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback) | 30 | virtual Async::Job<qint64> load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback) |
15 | { | 31 | { |
16 | return Async::start<void>([this, resultCallback](Async::Future<void> &future) { | 32 | return Async::start<qint64>([this, resultCallback](Async::Future<qint64> &future) { |
17 | qDebug() << "load called"; | 33 | qDebug() << "load called"; |
18 | for(const auto &result : results) { | 34 | for(const auto &result : results) { |
19 | resultCallback(result); | 35 | resultCallback(result); |
20 | } | 36 | } |
37 | future.setValue(0); | ||
21 | future.setFinished(); | 38 | future.setFinished(); |
22 | }); | 39 | }); |
23 | } | 40 | } |
24 | 41 | ||
42 | Async::Job<void> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > &resultProvider) | ||
43 | { | ||
44 | auto runner = QSharedPointer<QueryRunner>::create(query); | ||
45 | //The runner only lives as long as the resultProvider | ||
46 | resultProvider->setQueryRunner(runner); | ||
47 | runner->setQuery([this, resultProvider, query](qint64 oldRevision, qint64 newRevision) -> Async::Job<qint64> { | ||
48 | qDebug() << "Creating query for revisions: " << oldRevision << newRevision; | ||
49 | return Async::start<qint64>([this, resultProvider, query](Async::Future<qint64> &future) { | ||
50 | //TODO only emit changes and don't replace everything | ||
51 | resultProvider->clear(); | ||
52 | //rerun query | ||
53 | std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> addCallback = std::bind(&Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr>::add, resultProvider, std::placeholders::_1); | ||
54 | load(query, addCallback).then<void, qint64>([resultProvider, &future](qint64 queriedRevision) { | ||
55 | //TODO set revision in result provider? | ||
56 | //TODO update all existing results with new revision | ||
57 | resultProvider->complete(); | ||
58 | future.setValue(queriedRevision); | ||
59 | future.setFinished(); | ||
60 | }).exec(); | ||
61 | }); | ||
62 | }); | ||
63 | |||
64 | //Ensure the notification is emitted in the right thread | ||
65 | //Otherwise we get crashes as we call revisionChanged from the test. | ||
66 | if (!notifier) { | ||
67 | notifier.reset(new RevisionNotifier); | ||
68 | } | ||
69 | |||
70 | //TODO somehow disconnect as resultNotifier is destroyed. Otherwise we keep the runner alive forever. | ||
71 | if (query.liveQuery) { | ||
72 | QObject::connect(notifier.data(), &RevisionNotifier::revisionChanged, [runner](qint64 newRevision) { | ||
73 | runner->revisionChanged(newRevision); | ||
74 | }); | ||
75 | } | ||
76 | |||
77 | return Async::start<void>([runner](Async::Future<void> &future) { | ||
78 | runner->run().then<void>([&future]() { | ||
79 | //TODO if not live query, destroy runner. | ||
80 | future.setFinished(); | ||
81 | }).exec(); | ||
82 | }); | ||
83 | } | ||
84 | |||
25 | QList<Akonadi2::ApplicationDomain::Event::Ptr> results; | 85 | QList<Akonadi2::ApplicationDomain::Event::Ptr> results; |
86 | QSharedPointer<RevisionNotifier> notifier; | ||
26 | }; | 87 | }; |
27 | 88 | ||
28 | class ClientAPITest : public QObject | 89 | class ClientAPITest : public QObject |
@@ -30,21 +91,60 @@ class ClientAPITest : public QObject | |||
30 | Q_OBJECT | 91 | Q_OBJECT |
31 | private Q_SLOTS: | 92 | private Q_SLOTS: |
32 | 93 | ||
94 | void initTestCase() | ||
95 | { | ||
96 | Akonadi2::FacadeFactory::instance().resetFactory(); | ||
97 | } | ||
98 | |||
33 | void testLoad() | 99 | void testLoad() |
34 | { | 100 | { |
35 | DummyResourceFacade facade; | 101 | DummyResourceFacade facade; |
36 | facade.results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>()); | 102 | facade.results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>()); |
37 | 103 | ||
38 | Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>("dummyresource", [facade](){ return new DummyResourceFacade(facade); }); | 104 | Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>("dummyresource", |
105 | [&facade](bool &externallyManaged) { | ||
106 | externallyManaged = true; | ||
107 | return &facade; | ||
108 | } | ||
109 | ); | ||
39 | 110 | ||
40 | Akonadi2::Query query; | 111 | Akonadi2::Query query; |
41 | query.resources << "dummyresource"; | 112 | query.resources << "dummyresource"; |
113 | query.liveQuery = false; | ||
42 | 114 | ||
43 | async::SyncListResult<Akonadi2::ApplicationDomain::Event::Ptr> result(Akonadi2::Store::load<Akonadi2::ApplicationDomain::Event>(query)); | 115 | async::SyncListResult<Akonadi2::ApplicationDomain::Event::Ptr> result(Akonadi2::Store::load<Akonadi2::ApplicationDomain::Event>(query)); |
44 | result.exec(); | 116 | result.exec(); |
45 | QCOMPARE(result.size(), 1); | 117 | QCOMPARE(result.size(), 1); |
46 | } | 118 | } |
47 | 119 | ||
120 | void testLiveQuery() | ||
121 | { | ||
122 | DummyResourceFacade facade; | ||
123 | facade.results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>()); | ||
124 | |||
125 | Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>("dummyresource", | ||
126 | [&facade](bool &externallManage){ | ||
127 | externallManage = true; | ||
128 | return &facade; | ||
129 | } | ||
130 | ); | ||
131 | |||
132 | Akonadi2::Query query; | ||
133 | query.resources << "dummyresource"; | ||
134 | query.liveQuery = true; | ||
135 | |||
136 | async::SyncListResult<Akonadi2::ApplicationDomain::Event::Ptr> result(Akonadi2::Store::load<Akonadi2::ApplicationDomain::Event>(query)); | ||
137 | result.exec(); | ||
138 | QCOMPARE(result.size(), 1); | ||
139 | |||
140 | //Enter a second result | ||
141 | facade.results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id2", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>()); | ||
142 | qWarning() << &facade; | ||
143 | QVERIFY(facade.notifier); | ||
144 | facade.notifier->revisionChanged(2); | ||
145 | QTRY_COMPARE(result.size(), 2); | ||
146 | } | ||
147 | |||
48 | }; | 148 | }; |
49 | 149 | ||
50 | QTEST_MAIN(ClientAPITest) | 150 | QTEST_MAIN(ClientAPITest) |