1
/*******************************************************************************
2
**3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
3
** 10 20 30 40 50 60 70 80
7
** test-withlib.c - libnotify based unit-tests
9
** Copyright 2009 Canonical Ltd.
12
** Mirco "MacSlow" Mueller <mirco.mueller@canonical.com>
13
** David Barth <david.barth@canonical.com>
15
** This program is free software: you can redistribute it and/or modify it
16
** under the terms of the GNU General Public License version 3, as published
17
** by the Free Software Foundation.
19
** This program is distributed in the hope that it will be useful, but
20
** WITHOUT ANY WARRANTY; without even the implied warranties of
21
** MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
22
** PURPOSE. See the GNU General Public License for more details.
24
** You should have received a copy of the GNU General Public License along
25
** with this program. If not, see <http://www.gnu.org/licenses/>.
27
*******************************************************************************/
31
#include <libnotify/notify.h>
36
/* #define DBUS_NAME "org.freedesktop.Notifications" */
40
stop_main_loop (GMainLoop *loop)
42
g_main_loop_quit (loop);
48
test_withlib_setup (gpointer fixture, gconstpointer user_data)
50
notify_init ("libnotify");
54
test_withlib_get_server_information (gpointer fixture, gconstpointer user_data)
56
gchar *name = NULL, *vendor = NULL, *version = NULL, *specver = NULL;
59
ret = notify_get_server_info (&name, &vendor, &version, &specver);
62
g_assert (g_strrstr (name, "notify-osd"));
63
g_assert (g_strrstr (specver, "1.1"));
67
test_withlib_get_server_caps (gpointer fixture, gconstpointer user_data)
69
GList *cap, *caps = NULL;
70
gboolean test = FALSE;
72
caps = notify_get_server_caps ();
76
for (cap = g_list_first (caps);
78
cap = g_list_next (cap))
80
if (!g_strcmp0 (cap->data, "x-canonical-private-synchronous") ||
81
!g_strcmp0 (cap->data, "x-canonical-append") ||
82
!g_strcmp0 (cap->data, "x-canonical-private-icon-only") ||
83
!g_strcmp0 (cap->data, "x-canonical-truncation"))
91
test_withlib_show_notification (gpointer fixture, gconstpointer user_data)
93
NotifyNotification *n;
95
n = notify_notification_new ("Test",
96
"You should see a normal notification",
98
notify_notification_show (n, NULL);
101
g_object_unref(G_OBJECT(n));
105
test_withlib_update_notification (gpointer fixture, gconstpointer user_data)
107
NotifyNotification *n;
108
gboolean res = FALSE;
110
n = notify_notification_new ("Test",
113
res = notify_notification_show (n, NULL);
117
res = notify_notification_update (n, "Test (updated)",
118
"The message body has been updated...",
122
res = notify_notification_show (n, NULL);
126
g_object_unref(G_OBJECT(n));
130
test_withlib_pass_icon_data (gpointer fixture, gconstpointer user_data)
132
NotifyNotification *n;
136
n = notify_notification_new ("Image Test",
137
"You should see an image",
139
g_print ("iconpath: %s\n", SRCDIR"/icons/avatar.png");
140
pixbuf = gdk_pixbuf_new_from_file_at_scale (SRCDIR"/icons/avatar.png",
142
notify_notification_set_icon_from_pixbuf (n, pixbuf);
143
notify_notification_show (n, NULL);
145
loop = g_main_loop_new(NULL, FALSE);
146
g_timeout_add (2000, (GSourceFunc) stop_main_loop, loop);
147
g_main_loop_run (loop);
149
g_object_unref(G_OBJECT(n));
153
test_withlib_priority (gpointer fixture, gconstpointer user_data)
155
NotifyNotification *n1, *n2, *n3, *n4;
158
n1 = notify_notification_new ("Dummy Notification",
159
"This is a test notification",
161
notify_notification_show (n1, NULL);
162
n2 = notify_notification_new ("Normal Notification",
163
"You should see this *after* the urgent notification.",
165
notify_notification_set_urgency (n2, NOTIFY_URGENCY_LOW);
166
notify_notification_show (n2, NULL);
167
n3 = notify_notification_new ("Synchronous Notification",
168
"You should immediately see this notification.",
170
notify_notification_set_hint_string (n3, "synchronous", "test");
171
notify_notification_set_urgency (n3, NOTIFY_URGENCY_NORMAL);
172
notify_notification_show (n3, NULL);
173
n4 = notify_notification_new ("Urgent Notification",
174
"You should see a dialog box, and after, a normal notification.",
176
notify_notification_set_urgency (n4, NOTIFY_URGENCY_CRITICAL);
177
notify_notification_show (n4, NULL);
179
loop = g_main_loop_new(NULL, FALSE);
180
g_timeout_add (8000, (GSourceFunc) stop_main_loop, loop);
181
g_main_loop_run (loop);
183
g_object_unref(G_OBJECT(n1));
184
g_object_unref(G_OBJECT(n2));
185
g_object_unref(G_OBJECT(n3));
186
g_object_unref(G_OBJECT(n4));
189
static GMainLoop* loop;
191
static char* test_action_callback_data = "Some string to pass to the action callback";
194
callback (NotifyNotification *n,
198
g_assert (g_strcmp0 (action, "action") == 0);
199
g_assert (user_data == test_action_callback_data);
200
g_main_loop_quit (loop);
204
test_withlib_actions (gpointer fixture, gconstpointer user_data)
206
NotifyNotification *n1;
208
n1 = notify_notification_new ("Notification with an action",
209
"You should see that in a dialog box. Click the 'Action' button for the test to succeed.",
211
notify_notification_add_action (n1,
214
(NotifyActionCallback)callback,
215
test_action_callback_data,
217
notify_notification_show (n1, NULL);
219
loop = g_main_loop_new(NULL, FALSE);
220
g_main_loop_run (loop);
222
g_object_unref(G_OBJECT(n1));
226
test_withlib_close_notification (gpointer fixture, gconstpointer user_data)
228
NotifyNotification *n;
230
gboolean res = FALSE;
232
n = notify_notification_new ("Test Title",
233
"This notification will be closed prematurely...",
235
notify_notification_show (n, NULL);
237
loop = g_main_loop_new(NULL, FALSE);
238
g_timeout_add (1000, (GSourceFunc) stop_main_loop, loop);
239
g_main_loop_run (loop);
241
res = notify_notification_close (n, NULL);
244
g_timeout_add (2000, (GSourceFunc) stop_main_loop, loop);
245
g_main_loop_run (loop);
247
g_object_unref(G_OBJECT(n));
251
test_withlib_append_hint (gpointer fixture, gconstpointer user_data)
253
NotifyNotification *n;
254
gboolean res = FALSE;
256
/* init notification, supply first line of body-text */
257
n = notify_notification_new ("Test (append-hint)",
258
"The quick brown fox jumps over the lazy dog.",
259
SRCDIR"/icons/avatar.png");
260
res = notify_notification_show (n, NULL);
264
/* append second part of body-text */
265
res = notify_notification_update (n,
267
"Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark. (first append)",
269
notify_notification_set_hint_string (n, "append", "allowed");
271
res = notify_notification_show (n, NULL);
275
/* append third part of body-text */
276
res = notify_notification_update (n,
278
"Съешь ещё этих мягких французских булок, да выпей чаю. (last append)",
280
notify_notification_set_hint_string (n, "append", "allowed");
282
res = notify_notification_show (n, NULL);
286
g_object_unref(G_OBJECT(n));
290
test_withlib_icon_only_hint (gpointer fixture, gconstpointer user_data)
292
NotifyNotification *n;
293
gboolean res = FALSE;
295
/* init notification, supply first line of body-text */
296
n = notify_notification_new (" ", /* needs this to be non-NULL */
298
"notification-audio-play");
299
notify_notification_set_hint_string (n, "icon-only", "allowed");
300
res = notify_notification_show (n, NULL);
304
g_object_unref(G_OBJECT(n));
308
test_withlib_swallow_markup (gpointer fixture, gconstpointer user_data)
310
NotifyNotification *n;
311
gboolean res = FALSE;
313
n = notify_notification_new ("Swallow markup test",
314
"This text is hopefully neither <b>bold</b>, <i>italic</i> nor <u>underlined</u>.\n\nA little math-notation:\n\n\ta > b < c = 0",
315
SRCDIR"/icons/avatar.png");
316
res = notify_notification_show (n, NULL);
320
g_object_unref(G_OBJECT(n));
324
test_withlib_throttle (gpointer fixture, gconstpointer user_data)
326
NotifyNotification* n;
330
GError* error = NULL;
332
// see https://wiki.ubuntu.com/NotifyOSD#Flood%20prevention
333
for (i = 0; i < MAX_STACK_SIZE + 10; i++)
335
// pretty-ify the output a bit
339
// create dummy notification
340
snprintf (buf, 19, "Test #%.2d", i);
341
n = notify_notification_new (buf, buf, "");
343
// inject it into the queue
344
res = notify_notification_show (n, &error);
346
// spit out error from notification-daemon
349
g_print ("Error \"%s\" while trying to show #%.2d\n",
352
g_error_free (error);
356
// check if reaching limit causes error
357
if (i > MAX_STACK_SIZE)
365
test_withlib_create_test_suite (void)
367
GTestSuite *ts = NULL;
369
ts = g_test_create_suite ("libnotify");
371
#define ADD_TEST(x) g_test_suite_add(ts, \
372
g_test_create_case(#x, 0, NULL, test_withlib_setup, x, NULL) \
374
ADD_TEST(test_withlib_get_server_information);
375
ADD_TEST(test_withlib_get_server_caps);
376
ADD_TEST(test_withlib_show_notification);
377
ADD_TEST(test_withlib_update_notification);
378
ADD_TEST(test_withlib_pass_icon_data);
379
ADD_TEST(test_withlib_close_notification);
380
ADD_TEST(test_withlib_priority);
381
ADD_TEST(test_withlib_append_hint);
382
ADD_TEST(test_withlib_icon_only_hint);
383
ADD_TEST(test_withlib_swallow_markup);
384
ADD_TEST(test_withlib_actions);
385
ADD_TEST(test_withlib_throttle);