diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-03-24 22:15:18 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-03-24 22:15:18 +0100 |
commit | 9ea268a6d0f4054c31b2729ecd6cfcc9d07a2d6a (patch) | |
tree | 41fef7daac9e5ae64d61452a3f38c82243d29fdd /tests/notificationtest.cpp | |
parent | 84d70933c0cd0987d5fee5a78f413fec82bb1288 (diff) | |
download | sink-9ea268a6d0f4054c31b2729ecd6cfcc9d07a2d6a.tar.gz sink-9ea268a6d0f4054c31b2729ecd6cfcc9d07a2d6a.zip |
Implemented notification support in the model.
This will allow us to fold things like progress and sync status directly
into the model. Usecases are mail download progress and folder sync
progress.
Ideally we would also solve the resource/account state through this.
Diffstat (limited to 'tests/notificationtest.cpp')
-rw-r--r-- | tests/notificationtest.cpp | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/tests/notificationtest.cpp b/tests/notificationtest.cpp new file mode 100644 index 0000000..9433586 --- /dev/null +++ b/tests/notificationtest.cpp | |||
@@ -0,0 +1,124 @@ | |||
1 | #include <QtTest> | ||
2 | |||
3 | #include <QString> | ||
4 | #include <QSignalSpy> | ||
5 | |||
6 | #include "store.h" | ||
7 | #include "resourceconfig.h" | ||
8 | #include "resourcecontrol.h" | ||
9 | #include "modelresult.h" | ||
10 | #include "log.h" | ||
11 | #include "test.h" | ||
12 | #include "testutils.h" | ||
13 | #include "notifier.h" | ||
14 | #include "notification.h" | ||
15 | |||
16 | using namespace Sink; | ||
17 | using namespace Sink::ApplicationDomain; | ||
18 | |||
19 | /** | ||
20 | * Test of complete system using the dummy resource. | ||
21 | * | ||
22 | * This test requires the dummy resource installed. | ||
23 | */ | ||
24 | class NotificationTest : public QObject | ||
25 | { | ||
26 | Q_OBJECT | ||
27 | |||
28 | private slots: | ||
29 | void initTestCase() | ||
30 | { | ||
31 | Sink::Test::initTest(); | ||
32 | ResourceConfig::addResource("sink.dummy.instance1", "sink.dummy"); | ||
33 | } | ||
34 | |||
35 | void cleanup() | ||
36 | { | ||
37 | VERIFYEXEC(Sink::Store::removeDataFromDisk(QByteArray("sink.dummy.instance1"))); | ||
38 | } | ||
39 | |||
40 | void testSyncNotifications() | ||
41 | { | ||
42 | auto query = Query().resourceFilter("sink.dummy.instance1"); | ||
43 | query.setType<ApplicationDomain::Mail>(); | ||
44 | query.filter("id1"); | ||
45 | query.filter("id2"); | ||
46 | |||
47 | QList<Sink::Notification> statusNotifications; | ||
48 | QList<Sink::Notification> infoNotifications; | ||
49 | Sink::Notifier notifier("sink.dummy.instance1"); | ||
50 | notifier.registerHandler([&] (const Sink::Notification &n){ | ||
51 | SinkLogCtx(Sink::Log::Context{"dummyresourcetest"}) << "Received notification " << n; | ||
52 | if (n.type == Notification::Status) { | ||
53 | statusNotifications << n; | ||
54 | } | ||
55 | if (n.type == Notification::Info) { | ||
56 | infoNotifications << n; | ||
57 | } | ||
58 | }); | ||
59 | |||
60 | // Ensure all local data is processed | ||
61 | VERIFYEXEC(Sink::Store::synchronize(query)); | ||
62 | VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "sink.dummy.instance1")); | ||
63 | |||
64 | //FIXME it can happen that we get a changereplay notification pair first. | ||
65 | QTRY_COMPARE(statusNotifications.size(), 5); | ||
66 | //Sync | ||
67 | QCOMPARE(statusNotifications.at(0).code, static_cast<int>(ApplicationDomain::Status::ConnectedStatus)); | ||
68 | QCOMPARE(statusNotifications.at(1).code, static_cast<int>(Sink::ApplicationDomain::Status::BusyStatus)); | ||
69 | QCOMPARE(statusNotifications.at(2).code, static_cast<int>(Sink::ApplicationDomain::Status::ConnectedStatus)); | ||
70 | //Changereplay | ||
71 | QCOMPARE(statusNotifications.at(3).code, static_cast<int>(Sink::ApplicationDomain::Status::BusyStatus)); | ||
72 | QCOMPARE(statusNotifications.at(4).code, static_cast<int>(Sink::ApplicationDomain::Status::ConnectedStatus)); | ||
73 | |||
74 | QTRY_COMPARE(infoNotifications.size(), 2); | ||
75 | QCOMPARE(infoNotifications.at(0).code, static_cast<int>(ApplicationDomain::SyncStatus::SyncInProgress)); | ||
76 | QCOMPARE(infoNotifications.at(0).entities, QList<QByteArray>{} << "id1" << "id2"); | ||
77 | QCOMPARE(infoNotifications.at(1).code, static_cast<int>(ApplicationDomain::SyncStatus::SyncSuccess)); | ||
78 | QCOMPARE(infoNotifications.at(1).entities, QList<QByteArray>{} << "id1" << "id2"); | ||
79 | |||
80 | QCOMPARE(infoNotifications.at(1).code, static_cast<int>(ApplicationDomain::SyncStatus::SyncSuccess)); | ||
81 | } | ||
82 | |||
83 | void testModelNotifications() | ||
84 | { | ||
85 | auto query = Query().resourceFilter("sink.dummy.instance1"); | ||
86 | query.setType<ApplicationDomain::Mail>(); | ||
87 | query.setFlags(Query::LiveQuery | Query::UpdateStatus); | ||
88 | |||
89 | VERIFYEXEC(Sink::Store::synchronize(query)); | ||
90 | VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "sink.dummy.instance1")); | ||
91 | |||
92 | auto model = Sink::Store::loadModel<Sink::ApplicationDomain::Mail>(query); | ||
93 | QTRY_VERIFY(model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()); | ||
94 | QVERIFY(model->rowCount() >= 1); | ||
95 | |||
96 | QSignalSpy changedSpy(model.data(), &QAbstractItemModel::dataChanged); | ||
97 | auto mail = model->index(0, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).value<Mail::Ptr>(); | ||
98 | auto newQuery = query; | ||
99 | newQuery.filter(mail->identifier()); | ||
100 | |||
101 | QList<int> status; | ||
102 | QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [&] (const QModelIndex &begin, const QModelIndex &end, const QVector<int> &roles) { | ||
103 | QVERIFY(begin.row() == end.row()); | ||
104 | if (begin.row() == 0) { | ||
105 | status << model->data(begin, Store::StatusRole).value<int>(); | ||
106 | // qWarning() << "New status: " << status.last() << roles; | ||
107 | } | ||
108 | }); | ||
109 | |||
110 | //This will trigger a modification of all previous items as well. | ||
111 | VERIFYEXEC(Sink::Store::synchronize(newQuery)); | ||
112 | VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "sink.dummy.instance1")); | ||
113 | |||
114 | QCOMPARE(status.size(), 3); | ||
115 | //Sync progress of item | ||
116 | QCOMPARE(status.at(0), static_cast<int>(ApplicationDomain::SyncStatus::SyncInProgress)); | ||
117 | QCOMPARE(status.at(1), static_cast<int>(ApplicationDomain::SyncStatus::SyncSuccess)); | ||
118 | //Modification triggered during sync | ||
119 | QCOMPARE(status.at(2), static_cast<int>(ApplicationDomain::SyncStatus::SyncSuccess)); | ||
120 | } | ||
121 | }; | ||
122 | |||
123 | QTEST_MAIN(NotificationTest) | ||
124 | #include "notificationtest.moc" | ||