diff options
Diffstat (limited to 'tests/notificationtest.cpp')
-rw-r--r-- | tests/notificationtest.cpp | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/tests/notificationtest.cpp b/tests/notificationtest.cpp new file mode 100644 index 0000000..a34d325 --- /dev/null +++ b/tests/notificationtest.cpp | |||
@@ -0,0 +1,132 @@ | |||
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("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 | if (n.id == "changereplay") { | ||
54 | //We filter all changereplay notifications. | ||
55 | //Not the best way but otherwise the test becomes unstable and we currently | ||
56 | //only have the id to detect changereplay notifications. | ||
57 | return; | ||
58 | } | ||
59 | statusNotifications << n; | ||
60 | } | ||
61 | if (n.type == Notification::Info) { | ||
62 | infoNotifications << n; | ||
63 | } | ||
64 | }); | ||
65 | |||
66 | // Ensure all local data is processed | ||
67 | VERIFYEXEC(Sink::Store::synchronize(query)); | ||
68 | VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "sink.dummy.instance1")); | ||
69 | |||
70 | QVERIFY(statusNotifications.size() <= 3); | ||
71 | QTRY_COMPARE(statusNotifications.size(), 3); | ||
72 | //Sync | ||
73 | QCOMPARE(statusNotifications.at(0).code, static_cast<int>(ApplicationDomain::Status::ConnectedStatus)); | ||
74 | QCOMPARE(statusNotifications.at(1).code, static_cast<int>(ApplicationDomain::Status::BusyStatus)); | ||
75 | QCOMPARE(statusNotifications.at(2).code, static_cast<int>(ApplicationDomain::Status::ConnectedStatus)); | ||
76 | //Changereplay | ||
77 | // It can happen that we get a changereplay notification pair first and then a second one at the end, | ||
78 | // we therefore currently filter all changereplay notifications (see above). | ||
79 | // QCOMPARE(statusNotifications.at(3).code, static_cast<int>(Sink::ApplicationDomain::Status::BusyStatus)); | ||
80 | // QCOMPARE(statusNotifications.at(4).code, static_cast<int>(Sink::ApplicationDomain::Status::ConnectedStatus)); | ||
81 | |||
82 | QTRY_COMPARE(infoNotifications.size(), 2); | ||
83 | QCOMPARE(infoNotifications.at(0).code, static_cast<int>(ApplicationDomain::SyncStatus::SyncInProgress)); | ||
84 | QCOMPARE(infoNotifications.at(0).entities, QList<QByteArray>{} << "id1" << "id2"); | ||
85 | QCOMPARE(infoNotifications.at(1).code, static_cast<int>(ApplicationDomain::SyncStatus::SyncSuccess)); | ||
86 | QCOMPARE(infoNotifications.at(1).entities, QList<QByteArray>{} << "id1" << "id2"); | ||
87 | |||
88 | QCOMPARE(infoNotifications.at(1).code, static_cast<int>(ApplicationDomain::SyncStatus::SyncSuccess)); | ||
89 | } | ||
90 | |||
91 | void testModelNotifications() | ||
92 | { | ||
93 | auto query = Query().resourceFilter("sink.dummy.instance1"); | ||
94 | query.setType<ApplicationDomain::Mail>(); | ||
95 | query.setFlags(Query::LiveQuery | Query::UpdateStatus); | ||
96 | |||
97 | VERIFYEXEC(Sink::Store::synchronize(query)); | ||
98 | VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "sink.dummy.instance1")); | ||
99 | |||
100 | auto model = Sink::Store::loadModel<Sink::ApplicationDomain::Mail>(query); | ||
101 | QTRY_VERIFY(model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()); | ||
102 | QVERIFY(model->rowCount() >= 1); | ||
103 | |||
104 | QSignalSpy changedSpy(model.data(), &QAbstractItemModel::dataChanged); | ||
105 | auto mail = model->index(0, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).value<Mail::Ptr>(); | ||
106 | auto newQuery = query; | ||
107 | newQuery.filter(mail->identifier()); | ||
108 | |||
109 | QList<int> status; | ||
110 | QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [&] (const QModelIndex &begin, const QModelIndex &end, const QVector<int> &roles) { | ||
111 | QVERIFY(begin.row() == end.row()); | ||
112 | if (begin.row() == 0) { | ||
113 | status << model->data(begin, Store::StatusRole).value<int>(); | ||
114 | // qWarning() << "New status: " << status.last() << roles; | ||
115 | } | ||
116 | }); | ||
117 | |||
118 | //This will trigger a modification of all previous items as well. | ||
119 | VERIFYEXEC(Sink::Store::synchronize(newQuery)); | ||
120 | VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "sink.dummy.instance1")); | ||
121 | |||
122 | QCOMPARE(status.size(), 3); | ||
123 | //Sync progress of item | ||
124 | QCOMPARE(status.at(0), static_cast<int>(ApplicationDomain::SyncStatus::SyncInProgress)); | ||
125 | QCOMPARE(status.at(1), static_cast<int>(ApplicationDomain::SyncStatus::SyncSuccess)); | ||
126 | //Modification triggered during sync | ||
127 | QCOMPARE(status.at(2), static_cast<int>(ApplicationDomain::SyncStatus::SyncSuccess)); | ||
128 | } | ||
129 | }; | ||
130 | |||
131 | QTEST_MAIN(NotificationTest) | ||
132 | #include "notificationtest.moc" | ||