~macslow/notify-osd/mouse-movement-monitor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*******************************************************************************
**3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
**      10        20        30        40        50        60        70        80
**
** notify-osd
**
** test-withlib.c - libnotify based unit-tests
**
** Copyright 2009 Canonical Ltd.
**
** Authors:
**    Mirco "MacSlow" Mueller <mirco.mueller@canonical.com>
**    David Barth <david.barth@canonical.com>
**
** This program is free software: you can redistribute it and/or modify it
** under the terms of the GNU General Public License version 3, as published
** by the Free Software Foundation.
**
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranties of
** MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License along
** with this program.  If not, see <http://www.gnu.org/licenses/>.
**
*******************************************************************************/

#include <stdlib.h>
#include <unistd.h>
#include <libnotify/notify.h>
#include "dbus.h"
#include "config.h"

/* #define DBUS_NAME "org.freedesktop.Notifications" */

static
gboolean
stop_main_loop (GMainLoop *loop)
{
	g_main_loop_quit (loop);

	return FALSE;
}

static void
test_withlib_setup (void)
{
	notify_init ("libnotify");
}

static void
test_withlib_get_server_information (void)
{
	gchar *name = NULL, *vendor = NULL, *version = NULL, *specver = NULL;
	gboolean ret = FALSE;

	ret = notify_get_server_info (&name, &vendor, &version, &specver);
	
	g_assert (ret);
	g_assert (g_strrstr (name, "notify-osd"));
	g_assert (g_strrstr (specver, "0.10"));
}

static void
test_withlib_get_server_caps (void)
{
	GList *cap, *caps = NULL;
	gboolean test = FALSE;

	caps = notify_get_server_caps ();

	g_assert (caps);

	for (cap = g_list_first (caps);
	     cap != NULL;
	     cap = g_list_next (cap))
	{
		if (!g_strcmp0 (cap->data, "x-canonical-private-synchronous") ||
		    !g_strcmp0 (cap->data, "x-canonical-append") ||
		    !g_strcmp0 (cap->data, "x-canonical-private-icon-only") ||
		    !g_strcmp0 (cap->data, "x-canonical-truncation"))
			test = TRUE;
	}

	g_assert (test);
}

static void
test_withlib_show_notification (void)
{
        NotifyNotification *n;

	n = notify_notification_new ("Test",
				     "You should see a normal notification",
				     "", NULL);
	notify_notification_show (n, NULL);
	sleep (3);

	g_object_unref(G_OBJECT(n));
}

static void
test_withlib_update_notification (void)
{
        NotifyNotification *n;
	gboolean res = FALSE;

	n = notify_notification_new ("Test",
				     "New notification",
				     "", NULL);
	res = notify_notification_show (n, NULL);
	g_assert (res);
	sleep (1);

	res = notify_notification_update (n, "Test (updated)",
				    "The message body has been updated...",
				    "");
	g_assert (res);

	res = notify_notification_show (n, NULL);
	g_assert (res);
	sleep (3);

	g_object_unref(G_OBJECT(n));
}

static void
test_withlib_pass_icon_data (void)
{
        NotifyNotification *n;
	GdkPixbuf* pixbuf;
        GMainLoop* loop;

	n = notify_notification_new ("Image Test",
				     "You should see an image",
				     "", NULL);
	g_print ("iconpath: %s\n", SRCDIR"/icons/avatar.png");
	pixbuf = gdk_pixbuf_new_from_file_at_scale (SRCDIR"/icons/avatar.png",
						    64, 64, TRUE, NULL);
	notify_notification_set_icon_from_pixbuf (n, pixbuf);
	notify_notification_show (n, NULL);
	
	loop = g_main_loop_new(NULL, FALSE);
        g_timeout_add (2000, (GSourceFunc) stop_main_loop, loop);
        g_main_loop_run (loop);

	g_object_unref(G_OBJECT(n));
}

static void
test_withlib_priority (void)
{
        NotifyNotification *n1, *n2, *n3, *n4;
        GMainLoop* loop;

	n1 = notify_notification_new ("Dummy Notification",
				      "This is a test notification",
				      "", NULL);
	notify_notification_show (n1, NULL);
	n2 = notify_notification_new ("Normal Notification",
				      "You should see this *after* the urgent notification.",
				      "", NULL);
	notify_notification_set_urgency (n2, NOTIFY_URGENCY_LOW);
	notify_notification_show (n2, NULL);
	n3 = notify_notification_new ("Synchronous Notification",
				      "You should immediately see this notification.",
				      "", NULL);
	notify_notification_set_hint_string (n3, "synchronous", "test");
	notify_notification_set_urgency (n3, NOTIFY_URGENCY_NORMAL);
	notify_notification_show (n3, NULL);
	n4 = notify_notification_new ("Urgent Notification",
				      "You should see a dialog box, and after, a normal notification.",
				      "", NULL);
	notify_notification_set_urgency (n4, NOTIFY_URGENCY_CRITICAL);
	notify_notification_show (n4, NULL);
	
	loop = g_main_loop_new(NULL, FALSE);
        g_timeout_add (8000, (GSourceFunc) stop_main_loop, loop);
        g_main_loop_run (loop);

	g_object_unref(G_OBJECT(n1));
	g_object_unref(G_OBJECT(n2));
	g_object_unref(G_OBJECT(n3));
	g_object_unref(G_OBJECT(n4));
}

static GMainLoop* loop;

static char* test_action_callback_data = "Some string to pass to the action callback";

static void
callback (NotifyNotification *n,
	  const char *action,
	  void *user_data)
{
	g_assert (g_strcmp0 (action, "action") == 0);
	g_assert (user_data == test_action_callback_data);
	g_main_loop_quit (loop);
}

static void
test_withlib_actions (void)
{
	NotifyNotification *n1;

	n1 = notify_notification_new ("Notification with an action",
				      "You should see that in a dialog box. Click the 'Action' button for the test to succeed.",
				      "", NULL);
	notify_notification_add_action (n1,
					"action",
					"Action",
					(NotifyActionCallback)callback,
					test_action_callback_data,
					NULL);
	notify_notification_show (n1, NULL);
	
	loop = g_main_loop_new(NULL, FALSE);
        g_main_loop_run (loop);

	g_object_unref(G_OBJECT(n1));
}

static void
test_withlib_close_notification (void)
{
        NotifyNotification *n;
        GMainLoop* loop;
	gboolean res = FALSE;

	n = notify_notification_new ("Test Title",
				     "This notification will be closed prematurely...",
				     "", NULL);
	notify_notification_show (n, NULL);
	
	loop = g_main_loop_new(NULL, FALSE);
        g_timeout_add (1000, (GSourceFunc) stop_main_loop, loop);
        g_main_loop_run (loop);

	res = notify_notification_close (n, NULL);
	g_assert (res);

        g_timeout_add (2000, (GSourceFunc) stop_main_loop, loop);
        g_main_loop_run (loop);

	g_object_unref(G_OBJECT(n));
}

static void
test_withlib_append_hint (void)
{
        NotifyNotification *n;
	gboolean res = FALSE;

	/* init notification, supply first line of body-text */
	n = notify_notification_new ("Test (append-hint)",
				     "The quick brown fox jumps over the lazy dog.",
				     SRCDIR"/icons/avatar.png",
				     NULL);
	res = notify_notification_show (n, NULL);
	g_assert (res);
	sleep (1);

	/* append second part of body-text */
	res = notify_notification_update (n,
					  " ",
					  "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark. (first append)",
					  NULL);
	notify_notification_set_hint_string (n, "append", "allowed");
	g_assert (res);
	res = notify_notification_show (n, NULL);
	g_assert (res);
 	sleep (1);

	/* append third part of body-text */
	res = notify_notification_update (n,
					  " ",
					  "Съешь ещё этих мягких французских булок, да выпей чаю. (last append)",
					  NULL);
	notify_notification_set_hint_string (n, "append", "allowed");
	g_assert (res);
	res = notify_notification_show (n, NULL);
	g_assert (res);
 	sleep (1);

	g_object_unref(G_OBJECT(n));
}

static void
test_withlib_icon_only_hint (void)
{
        NotifyNotification *n;
	gboolean res = FALSE;

	/* init notification, supply first line of body-text */
	n = notify_notification_new (" ", /* needs this to be non-NULL */
				     NULL,
				     "notification-audio-play",
				     NULL);
	notify_notification_set_hint_string (n, "icon-only", "allowed");
	res = notify_notification_show (n, NULL);
	g_assert (res);
	sleep (1);

	g_object_unref(G_OBJECT(n));
}

static void
test_withlib_swallow_markup (void)
{
        NotifyNotification *n;
	gboolean res = FALSE;

	n = notify_notification_new ("Swallow markup test",
				     "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",
				     SRCDIR"/icons/avatar.png",
				     NULL);
	res = notify_notification_show (n, NULL);
	g_assert (res);
	sleep (2);

	g_object_unref(G_OBJECT(n));
}

GTestSuite *
test_withlib_create_test_suite (void)
{
	GTestSuite *ts = NULL;

	ts = g_test_create_suite ("libnotify");

	#define ADD_TEST(x) g_test_suite_add(ts, \
		g_test_create_case(#x, 0, NULL, test_withlib_setup, x, NULL) \
		)
	ADD_TEST(test_withlib_get_server_information);
	ADD_TEST(test_withlib_get_server_caps);
	ADD_TEST(test_withlib_show_notification);
	ADD_TEST(test_withlib_update_notification);
	ADD_TEST(test_withlib_pass_icon_data);
	ADD_TEST(test_withlib_close_notification);
	ADD_TEST(test_withlib_priority);
	ADD_TEST(test_withlib_append_hint);
	ADD_TEST(test_withlib_icon_only_hint);
	ADD_TEST(test_withlib_swallow_markup);
	ADD_TEST(test_withlib_actions);

	return ts;
}