~elementary-os/elementaryos/os-patch-notify-osd-precise

« back to all changes in this revision

Viewing changes to tests/test-withlib.c

  • Committer: Sergey "Shnatsel" Davidoff
  • Date: 2012-06-18 21:08:31 UTC
  • Revision ID: shnatsel@gmail.com-20120618210831-g6k5y7vecjdylgic
Initial import, version 0.9.34-0ubuntu2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
**3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
 
3
**      10        20        30        40        50        60        70        80
 
4
**
 
5
** notify-osd
 
6
**
 
7
** test-withlib.c - libnotify based unit-tests
 
8
**
 
9
** Copyright 2009 Canonical Ltd.
 
10
**
 
11
** Authors:
 
12
**    Mirco "MacSlow" Mueller <mirco.mueller@canonical.com>
 
13
**    David Barth <david.barth@canonical.com>
 
14
**
 
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.
 
18
**
 
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.
 
23
**
 
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/>.
 
26
**
 
27
*******************************************************************************/
 
28
 
 
29
#include <stdlib.h>
 
30
#include <unistd.h>
 
31
#include <libnotify/notify.h>
 
32
#include "dbus.h"
 
33
#include "stack.h"
 
34
#include "config.h"
 
35
 
 
36
/* #define DBUS_NAME "org.freedesktop.Notifications" */
 
37
 
 
38
static
 
39
gboolean
 
40
stop_main_loop (GMainLoop *loop)
 
41
{
 
42
        g_main_loop_quit (loop);
 
43
 
 
44
        return FALSE;
 
45
}
 
46
 
 
47
static void
 
48
test_withlib_setup (gpointer fixture, gconstpointer user_data)
 
49
{
 
50
        notify_init ("libnotify");
 
51
}
 
52
 
 
53
static void
 
54
test_withlib_get_server_information (gpointer fixture, gconstpointer user_data)
 
55
{
 
56
        gchar *name = NULL, *vendor = NULL, *version = NULL, *specver = NULL;
 
57
        gboolean ret = FALSE;
 
58
 
 
59
        ret = notify_get_server_info (&name, &vendor, &version, &specver);
 
60
        
 
61
        g_assert (ret);
 
62
        g_assert (g_strrstr (name, "notify-osd"));
 
63
        g_assert (g_strrstr (specver, "1.1"));
 
64
}
 
65
 
 
66
static void
 
67
test_withlib_get_server_caps (gpointer fixture, gconstpointer user_data)
 
68
{
 
69
        GList *cap, *caps = NULL;
 
70
        gboolean test = FALSE;
 
71
 
 
72
        caps = notify_get_server_caps ();
 
73
 
 
74
        g_assert (caps);
 
75
 
 
76
        for (cap = g_list_first (caps);
 
77
             cap != NULL;
 
78
             cap = g_list_next (cap))
 
79
        {
 
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"))
 
84
                        test = TRUE;
 
85
        }
 
86
 
 
87
        g_assert (test);
 
88
}
 
89
 
 
90
static void
 
91
test_withlib_show_notification (gpointer fixture, gconstpointer user_data)
 
92
{
 
93
        NotifyNotification *n;
 
94
 
 
95
        n = notify_notification_new ("Test",
 
96
                                     "You should see a normal notification",
 
97
                                     "");
 
98
        notify_notification_show (n, NULL);
 
99
        sleep (3);
 
100
 
 
101
        g_object_unref(G_OBJECT(n));
 
102
}
 
103
 
 
104
static void
 
105
test_withlib_update_notification (gpointer fixture, gconstpointer user_data)
 
106
{
 
107
        NotifyNotification *n;
 
108
        gboolean res = FALSE;
 
109
 
 
110
        n = notify_notification_new ("Test",
 
111
                                     "New notification",
 
112
                                     "");
 
113
        res = notify_notification_show (n, NULL);
 
114
        g_assert (res);
 
115
        sleep (1);
 
116
 
 
117
        res = notify_notification_update (n, "Test (updated)",
 
118
                                    "The message body has been updated...",
 
119
                                    "");
 
120
        g_assert (res);
 
121
 
 
122
        res = notify_notification_show (n, NULL);
 
123
        g_assert (res);
 
124
        sleep (3);
 
125
 
 
126
        g_object_unref(G_OBJECT(n));
 
127
}
 
128
 
 
129
static void
 
130
test_withlib_pass_icon_data (gpointer fixture, gconstpointer user_data)
 
131
{
 
132
        NotifyNotification *n;
 
133
        GdkPixbuf* pixbuf;
 
134
        GMainLoop* loop;
 
135
 
 
136
        n = notify_notification_new ("Image Test",
 
137
                                     "You should see an image",
 
138
                                     "");
 
139
        g_print ("iconpath: %s\n", SRCDIR"/icons/avatar.png");
 
140
        pixbuf = gdk_pixbuf_new_from_file_at_scale (SRCDIR"/icons/avatar.png",
 
141
                                                    64, 64, TRUE, NULL);
 
142
        notify_notification_set_icon_from_pixbuf (n, pixbuf);
 
143
        notify_notification_show (n, NULL);
 
144
        
 
145
        loop = g_main_loop_new(NULL, FALSE);
 
146
        g_timeout_add (2000, (GSourceFunc) stop_main_loop, loop);
 
147
        g_main_loop_run (loop);
 
148
 
 
149
        g_object_unref(G_OBJECT(n));
 
150
}
 
151
 
 
152
static void
 
153
test_withlib_priority (gpointer fixture, gconstpointer user_data)
 
154
{
 
155
        NotifyNotification *n1, *n2, *n3, *n4;
 
156
        GMainLoop* loop;
 
157
 
 
158
        n1 = notify_notification_new ("Dummy Notification",
 
159
                                      "This is a test notification",
 
160
                                      "");
 
161
        notify_notification_show (n1, NULL);
 
162
        n2 = notify_notification_new ("Normal Notification",
 
163
                                      "You should see this *after* the urgent notification.",
 
164
                                      "");
 
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.",
 
169
                                      "");
 
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.",
 
175
                                      "");
 
176
        notify_notification_set_urgency (n4, NOTIFY_URGENCY_CRITICAL);
 
177
        notify_notification_show (n4, NULL);
 
178
        
 
179
        loop = g_main_loop_new(NULL, FALSE);
 
180
        g_timeout_add (8000, (GSourceFunc) stop_main_loop, loop);
 
181
        g_main_loop_run (loop);
 
182
 
 
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));
 
187
}
 
188
 
 
189
static GMainLoop* loop;
 
190
 
 
191
static char* test_action_callback_data = "Some string to pass to the action callback";
 
192
 
 
193
static void
 
194
callback (NotifyNotification *n,
 
195
          const char *action,
 
196
          void *user_data)
 
197
{
 
198
        g_assert (g_strcmp0 (action, "action") == 0);
 
199
        g_assert (user_data == test_action_callback_data);
 
200
        g_main_loop_quit (loop);
 
201
}
 
202
 
 
203
static void
 
204
test_withlib_actions (gpointer fixture, gconstpointer user_data)
 
205
{
 
206
        NotifyNotification *n1;
 
207
 
 
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.",
 
210
                                      "");
 
211
        notify_notification_add_action (n1,
 
212
                                        "action",
 
213
                                        "Action",
 
214
                                        (NotifyActionCallback)callback,
 
215
                                        test_action_callback_data,
 
216
                                        NULL);
 
217
        notify_notification_show (n1, NULL);
 
218
        
 
219
        loop = g_main_loop_new(NULL, FALSE);
 
220
        g_main_loop_run (loop);
 
221
 
 
222
        g_object_unref(G_OBJECT(n1));
 
223
}
 
224
 
 
225
static void
 
226
test_withlib_close_notification (gpointer fixture, gconstpointer user_data)
 
227
{
 
228
        NotifyNotification *n;
 
229
        GMainLoop* loop;
 
230
        gboolean res = FALSE;
 
231
 
 
232
        n = notify_notification_new ("Test Title",
 
233
                                     "This notification will be closed prematurely...",
 
234
                                     "");
 
235
        notify_notification_show (n, NULL);
 
236
        
 
237
        loop = g_main_loop_new(NULL, FALSE);
 
238
        g_timeout_add (1000, (GSourceFunc) stop_main_loop, loop);
 
239
        g_main_loop_run (loop);
 
240
 
 
241
        res = notify_notification_close (n, NULL);
 
242
        g_assert (res);
 
243
 
 
244
        g_timeout_add (2000, (GSourceFunc) stop_main_loop, loop);
 
245
        g_main_loop_run (loop);
 
246
 
 
247
        g_object_unref(G_OBJECT(n));
 
248
}
 
249
 
 
250
static void
 
251
test_withlib_append_hint (gpointer fixture, gconstpointer user_data)
 
252
{
 
253
        NotifyNotification *n;
 
254
        gboolean res = FALSE;
 
255
 
 
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);
 
261
        g_assert (res);
 
262
        sleep (1);
 
263
 
 
264
        /* append second part of body-text */
 
265
        res = notify_notification_update (n,
 
266
                                          " ",
 
267
                                          "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark. (first append)",
 
268
                                          NULL);
 
269
        notify_notification_set_hint_string (n, "append", "allowed");
 
270
        g_assert (res);
 
271
        res = notify_notification_show (n, NULL);
 
272
        g_assert (res);
 
273
        sleep (1);
 
274
 
 
275
        /* append third part of body-text */
 
276
        res = notify_notification_update (n,
 
277
                                          " ",
 
278
                                          "Съешь ещё этих мягких французских булок, да выпей чаю. (last append)",
 
279
                                          NULL);
 
280
        notify_notification_set_hint_string (n, "append", "allowed");
 
281
        g_assert (res);
 
282
        res = notify_notification_show (n, NULL);
 
283
        g_assert (res);
 
284
        sleep (1);
 
285
 
 
286
        g_object_unref(G_OBJECT(n));
 
287
}
 
288
 
 
289
static void
 
290
test_withlib_icon_only_hint (gpointer fixture, gconstpointer user_data)
 
291
{
 
292
        NotifyNotification *n;
 
293
        gboolean res = FALSE;
 
294
 
 
295
        /* init notification, supply first line of body-text */
 
296
        n = notify_notification_new (" ", /* needs this to be non-NULL */
 
297
                                     NULL,
 
298
                                     "notification-audio-play");
 
299
        notify_notification_set_hint_string (n, "icon-only", "allowed");
 
300
        res = notify_notification_show (n, NULL);
 
301
        g_assert (res);
 
302
        sleep (1);
 
303
 
 
304
        g_object_unref(G_OBJECT(n));
 
305
}
 
306
 
 
307
static void
 
308
test_withlib_swallow_markup (gpointer fixture, gconstpointer user_data)
 
309
{
 
310
        NotifyNotification *n;
 
311
        gboolean res = FALSE;
 
312
 
 
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 &gt; b &lt; c = 0",
 
315
                                     SRCDIR"/icons/avatar.png");
 
316
        res = notify_notification_show (n, NULL);
 
317
        g_assert (res);
 
318
        sleep (2);
 
319
 
 
320
        g_object_unref(G_OBJECT(n));
 
321
}
 
322
 
 
323
static void
 
324
test_withlib_throttle (gpointer fixture, gconstpointer user_data)
 
325
{
 
326
        NotifyNotification* n;
 
327
        gint                i;
 
328
        gboolean            res;
 
329
        gchar               buf[20];
 
330
        GError*             error = NULL;
 
331
 
 
332
        // see https://wiki.ubuntu.com/NotifyOSD#Flood%20prevention
 
333
        for (i = 0; i < MAX_STACK_SIZE + 10; i++)
 
334
        {
 
335
                // pretty-ify the output a bit
 
336
                if (i == 0)
 
337
                        g_print ("\n");
 
338
 
 
339
                // create dummy notification
 
340
                snprintf (buf, 19, "Test #%.2d", i);
 
341
                n = notify_notification_new (buf, buf, "");
 
342
 
 
343
                // inject it into the queue
 
344
                res = notify_notification_show (n, &error);
 
345
 
 
346
                // spit out error from notification-daemon
 
347
                if (!res && error)
 
348
                {
 
349
                        g_print ("Error \"%s\" while trying to show #%.2d\n",
 
350
                                 error->message,
 
351
                                 i);
 
352
                        g_error_free (error);
 
353
                        error = NULL;
 
354
                }
 
355
 
 
356
                // check if reaching limit causes error
 
357
                if (i > MAX_STACK_SIZE)
 
358
                        g_assert (!res);
 
359
 
 
360
                g_object_unref (n);
 
361
        }
 
362
}
 
363
 
 
364
GTestSuite *
 
365
test_withlib_create_test_suite (void)
 
366
{
 
367
        GTestSuite *ts = NULL;
 
368
 
 
369
        ts = g_test_create_suite ("libnotify");
 
370
 
 
371
        #define ADD_TEST(x) g_test_suite_add(ts, \
 
372
                g_test_create_case(#x, 0, NULL, test_withlib_setup, x, NULL) \
 
373
                )
 
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);
 
386
 
 
387
        return ts;
 
388
}