Debian Codegraph @

tests/online-accounts-ui/tst_notification/tst_notification.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <[email protected]>
5 *
6 * This file is part of lomiri-online-accounts-ui
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 3, as published
10 * by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranties of
14 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "notification.h"
22
23 #include <QDebug>
24 #include <QList>
25 #include <QMap>
26 #include <QPair>
27 #include <QSignalSpy>
28 #include <QTest>
29 #include <QVariantMap>
30 #include <libnotify/notification.h>
31 #include <libnotify/notify.h>
32
33 using namespace OnlineAccountsUi;
34
35 /* Mocking libnotify: */
36
37 static char *appName = NULL;
38
39 gboolean notify_init(const char *app_name)
40 {
41 appName = g_strdup(app_name);
42 return true;
43 }
44
45 gboolean notify_is_initted()
46 {
47 return appName != NULL;
48 }
49
50 typedef QPair<QByteArray,QByteArray> ActionPair;
51
52 struct MockNotification {
53 QString summary;
54 QString body;
55 QList<ActionPair> actions;
56 QVariantMap hints;
57 bool visible;
58
59 MockNotification();
60 ~MockNotification();
61 bool invokeAction(const QByteArray &action);
62 void close();
63
64 struct CallbackData {
65 NotifyActionCallback callback;
66 gpointer userData;
67 };
68
69 QMap<QByteArray,CallbackData> callbacks;
70
71 /* "closed" signal connection */
72 void (*closedCallback)(gpointer, gpointer);
73 gpointer closedUserData;
74 };
75
76 static QSet<MockNotification*> notifications;
77
78 MockNotification::MockNotification():
79 visible(false)
80 {
81 notifications.insert(this);
82 }
83
84 MockNotification::~MockNotification()
85 {
86 notifications.remove(this);
87 }
88
89 bool MockNotification::invokeAction(const QByteArray &action)
90 {
91 if (!callbacks.contains(action)) return false;
92 CallbackData &callbackData = callbacks[action];
93 callbackData.callback(reinterpret_cast<NotifyNotification*>(this),
94 (char *)action.data(), callbackData.userData);
95 return true;
96 }
97
98 void MockNotification::close()
99 {
100 if (!closedCallback) return;
101
102 closedCallback(closedUserData, this);
103 }
104
105 gulong g_signal_connect_data(gpointer instance,
106 const gchar *detailed_signal,
107 GCallback c_handler,
108 gpointer data,
109 GClosureNotify destroy_data,
110 GConnectFlags connect_flags)
111 {
112 Q_UNUSED(destroy_data);
113 Q_UNUSED(connect_flags);
114
115 MockNotification *mock = reinterpret_cast<MockNotification*>(instance);
116 if (qstrcmp(detailed_signal, "closed") == 0) {
117 mock->closedCallback = (void (*)(gpointer,gpointer))c_handler;
118 mock->closedUserData = data;
119 } else {
120 qWarning() << "Unmocked signal" << detailed_signal;
121 return 0;
122 }
123
124 return 1;
125 }
126
127 void g_object_unref(gpointer object)
128 {
129 MockNotification *mock = reinterpret_cast<MockNotification*>(object);
130 delete mock;
131 }
132
133 NotifyNotification *
134 notify_notification_new(const char *summary,
135 const char *body,
136 const char *icon)
137 {
138 Q_UNUSED(icon);
139 MockNotification *notification = new MockNotification;
140 notification->summary = QString::fromUtf8(summary);
141 notification->body = QString::fromUtf8(body);
142 return reinterpret_cast<NotifyNotification*>(notification);
143 }
144
145 gboolean
146 notify_notification_show(NotifyNotification *notification,
147 GError **error)
148 {
149 Q_UNUSED(error);
150 MockNotification *mock =
151 reinterpret_cast<MockNotification*>(notification);
152 mock->visible = true;
153 return true;
154 }
155
156 void notify_notification_add_action(NotifyNotification *notification,
157 const char *action,
158 const char *label,
159 NotifyActionCallback callback,
160 gpointer user_data,
161 GFreeFunc free_func)
162 {
163 Q_UNUSED(free_func);
164 MockNotification *mock =
165 reinterpret_cast<MockNotification*>(notification);
166 mock->actions.append(ActionPair(action, label));
167 MockNotification::CallbackData &callbackData = mock->callbacks[action];
168 callbackData.callback = callback;
169 callbackData.userData = user_data;
170 }
171
172 void notify_notification_set_hint(NotifyNotification *notification,
173 const char *key,
174 GVariant *value)
175 {
176 MockNotification *mock =
177 reinterpret_cast<MockNotification*>(notification);
178 QVariant variant;
179 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
180 variant = bool(g_variant_get_boolean(value));
181 } else {
182 /* Add support for any needed types */
183 qWarning() << "Unsupported variant type";
184 }
185 mock->hints.insert(QString::fromUtf8(key), variant);
186 }
187
188 /* End of mock code */
189
190 class NotificationTest: public QObject
191 {
192 Q_OBJECT
193
194 public:
195 NotificationTest();
196
197 private Q_SLOTS:
198 void testInitialization();
199 void testDestruction();
200 void testContents();
201 void testSnapDecision();
202 void testVisibility();
203 void testClosing();
204 void testActions();
205 };
206
207 NotificationTest::NotificationTest():
208 QObject(0)
209 {
210 }
211
212 void NotificationTest::testInitialization()
213 {
214 Notification first("summary", "body");
215 QVERIFY(notify_is_initted());
216 }
217
218 void NotificationTest::testDestruction()
219 {
220 QVERIFY(notifications.isEmpty());
221
222 {
223 Notification one("one", "One");
224 QCOMPARE(notifications.count(), 1);
225
226 {
227 Notification two("two", "Two");
228 QCOMPARE(notifications.count(), 2);
229 }
230 QCOMPARE(notifications.count(), 1);
231 }
232
233 QCOMPARE(notifications.count(), 0);
234 }
235
236 void NotificationTest::testContents()
237 {
238 Notification first("Summary", "Body");
239 MockNotification *mock = *(notifications.begin());
240 QCOMPARE(mock->summary, QString("Summary"));
241 QCOMPARE(mock->body, QString("Body"));
242 QVERIFY(mock->hints.isEmpty());
243 }
244
245 void NotificationTest::testSnapDecision()
246 {
247 Notification first("Summary", "Body");
248 MockNotification *mock = *(notifications.begin());
249 QVERIFY(mock->hints.isEmpty());
250
251 first.setSnapDecision(true);
252 QCOMPARE(mock->hints["x-lomiri-snap-decisions"].toBool(), true);
253
254 first.setSnapDecision(false);
255 QCOMPARE(mock->hints["x-lomiri-snap-decisions"].toBool(), false);
256 }
257
258 void NotificationTest::testVisibility()
259 {
260 Notification first("Summary", "Body");
261 MockNotification *mock = *(notifications.begin());
262 QVERIFY(!mock->visible);
263
264 first.show();
265 QVERIFY(mock->visible);
266 }
267
268 void NotificationTest::testClosing()
269 {
270 Notification notification("Something", "Here");
271 MockNotification *mock = *(notifications.begin());
272
273 QSignalSpy closed(&notification, SIGNAL(closed()));
274 mock->close();
275 QCOMPARE(closed.count(), 1);
276 }
277
278 void NotificationTest::testActions()
279 {
280 Notification notification("Something", "Here");
281 MockNotification *mock = *(notifications.begin());
282
283 QSignalSpy actionInvoked(&notification,
284 SIGNAL(actionInvoked(const QString &)));
285 notification.addAction("SayHello", "Say Hello");
286 notification.addAction("ok", "OK");
287 notification.addAction("cancel", "Cancel");
288
289 QCOMPARE(actionInvoked.count(), 0);
290
291 mock->invokeAction("ok");
292 QCOMPARE(actionInvoked.count(), 1);
293 QCOMPARE(actionInvoked.at(0).at(0).toString(), QString("ok"));
294 actionInvoked.clear();
295
296 mock->invokeAction("cancel");
297 QCOMPARE(actionInvoked.count(), 1);
298 QCOMPARE(actionInvoked.at(0).at(0).toString(), QString("cancel"));
299 actionInvoked.clear();
300
301 mock->invokeAction("SayHello");
302 QCOMPARE(actionInvoked.count(), 1);
303 QCOMPARE(actionInvoked.at(0).at(0).toString(), QString("SayHello"));
304 }
305
306 QTEST_MAIN(NotificationTest);
307
308 #include "tst_notification.moc"