~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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
////////////////////////////////////////////////////////////////////////////////
//3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
//      10        20        30        40        50        60        70        80
//
// notify-osd
//
// test-notification.c - implements unit-tests for exercising API of abstract
//                       notification object
//
// Copyright 2009 Canonical Ltd.
//
// Authors:
//    Mirco "MacSlow" Mueller <mirco.mueller@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/>.
//
////////////////////////////////////////////////////////////////////////////////

#define _XOPEN_SOURCE 500 // needed for usleep() from unistd.h
#include <unistd.h>

#include "notification.h"

static void
test_notification_new (void)
{
	Notification* n = NULL;
	GTimeVal*     timestamp;

	// create new object
	n = notification_new ();

	// test validity of main notification object
	g_assert (n);

	// test validity of initialized notification object
	g_assert_cmpint (notification_get_id (n), ==, -1);
	g_assert (!notification_get_title (n));
	g_assert (!notification_get_body (n));
	g_assert_cmpint (notification_get_value (n), ==, -2);
	g_assert (!notification_get_icon_themename (n));
	g_assert (!notification_get_icon_filename (n));
	g_assert (!notification_get_icon_pixbuf (n));
	g_assert_cmpint (notification_get_onscreen_time (n), ==, 0);
	g_assert (!notification_get_sender_name (n));
	g_assert_cmpint (notification_get_sender_pid (n), ==, 0);
	timestamp = notification_get_timestamp (n);
	g_assert_cmpint (timestamp->tv_sec, ==, 0);
	g_assert_cmpint (timestamp->tv_usec, ==, 0);
	g_assert_cmpint (notification_get_urgency (n), ==, URGENCY_NONE);

	// clean up
	g_object_unref (n);
	n = NULL;
}

static void
test_notification_destroy (void)
{
	Notification* n = NULL;

	// create new object
	n = notification_new ();

	// test validity of main notification object
	g_assert (n != NULL);

	// clean up
	g_object_unref (n);
	n = NULL;
}

static void
test_notification_setget_id (void)
{
	Notification* n = NULL;

	// create new object
	n = notification_new ();

	// if nothing has been set yet it should return -1
	g_assert_cmpint (notification_get_id (n), ==, -1);

	// a negative id should not be stored
	notification_set_id (n, -3);
	g_assert_cmpint (notification_get_id (n), >=, -1);

	// smallest possible id is 0
	notification_set_id (n, 0);
	g_assert_cmpint (notification_get_id (n), ==, 0);

	// largest possible id is G_MAXINT
	notification_set_id (n, G_MAXINT);
	g_assert_cmpint (notification_get_id (n), ==, G_MAXINT);

	// clean up
	g_object_unref (n);
	n = NULL;
}

static void
test_notification_setget_title (void)
{
	Notification* n = NULL;

	// create new object
	n = notification_new ();

	// if no title has been set yet it should return NULL
	g_assert (notification_get_title (n) == NULL);

	// set an initial title-text and verify it
	notification_set_title (n, "Some title text");
	g_assert_cmpstr (notification_get_title (n), ==, "Some title text");

	// set a new title-text and verify it
	notification_set_title (n, "The new summary");
	g_assert_cmpstr (notification_get_title (n), !=, "Some title text");
	g_assert_cmpstr (notification_get_title (n), ==, "The new summary");

	// clean up
	g_object_unref (n);
	n = NULL;
}

static void
test_notification_setget_body (void)
{
	Notification* n = NULL;

	// create new object
	n = notification_new ();

	// if no body has been set yet it should return NULL
	g_assert (notification_get_body (n) == NULL);

	// set an initial body-text and verify it
	notification_set_body (n, "Example body text");
	g_assert_cmpstr (notification_get_body (n), ==, "Example body text");

	// set a new body-text and verify it
	notification_set_body (n, "Some new body text");
	g_assert_cmpstr (notification_get_body (n), !=, "Example body text");
	g_assert_cmpstr (notification_get_body (n), ==, "Some new body text");

	// clean up
	g_object_unref (n);
	n = NULL;
}

static void
test_notification_setget_value (void)
{
	Notification* n = NULL;

	// create new object
	n = notification_new ();

	// if no value has been set yet it should return 0
	g_assert_cmpint (notification_get_value (n), ==, -2);

	// set an initial value and verify it
	notification_set_value (n, 25);
	g_assert_cmpint (notification_get_value (n), ==, 25);

	// set a new value and verify it
	notification_set_value (n, 45);
	g_assert_cmpint (notification_get_value (n), !=, 25);
	g_assert_cmpint (notification_get_value (n), ==, 45);

	// test allowed range
	notification_set_value (n, NOTIFICATION_VALUE_MAX_ALLOWED + 1);
	g_assert_cmpint (notification_get_value (n),
			 ==,
			 NOTIFICATION_VALUE_MAX_ALLOWED);
	notification_set_value (n, NOTIFICATION_VALUE_MIN_ALLOWED - 2);
	g_assert_cmpint (notification_get_value (n),
			 ==,
			 NOTIFICATION_VALUE_MIN_ALLOWED);

	// clean up
	g_object_unref (n);
	n = NULL;
}

static void
test_notification_setget_icon_themename (void)
{
	Notification* n = NULL;

	// create new object
	n = notification_new ();

	// if no icon-themename has been set yet it should return NULL
	g_assert (notification_get_icon_themename (n) == NULL);

	// set an initial icon-themename and verify it
	notification_set_icon_themename (n, "notification-message-im");
	g_assert_cmpstr (notification_get_icon_themename (n),
			 ==,
			 "notification-message-im");

	// set a new icon-themename and verify it
	notification_set_icon_themename (n, "notification-device-usb");
	g_assert_cmpstr (notification_get_icon_themename (n),
			 !=,
			 "notification-message-im");
	g_assert_cmpstr (notification_get_icon_themename (n),
			 ==,
			 "notification-device-usb");

	// clean up
	g_object_unref (n);
	n = NULL;
}

static void
test_notification_setget_icon_filename (void)
{
	Notification* n = NULL;

	// create new object
	n = notification_new ();

	// if no icon-filename has been set yet it should return NULL
	g_assert (notification_get_icon_filename (n) == NULL);

	// set an initial icon-filename and verify it
	notification_set_icon_filename (n, "/usr/share/icon/photo.png");
	g_assert_cmpstr (notification_get_icon_filename (n),
			 ==,
			 "/usr/share/icon/photo.png");

	// set a new icon-filename and verify it
	notification_set_icon_filename (n, "/tmp/drawing.svg");
	g_assert_cmpstr (notification_get_icon_filename (n),
			 !=,
			 "/usr/share/icon/photo.png");
	g_assert_cmpstr (notification_get_icon_filename (n),
			 ==,
			 "/tmp/drawing.svg");

	// passing an invalid/NULL pointer should not change the stored
	// icon-filename 
	notification_set_icon_filename (n, NULL);
	g_assert_cmpstr (notification_get_icon_filename (n),
			 ==,
			 "/tmp/drawing.svg");

	// pass an empty (but not NULL) strings
	notification_set_icon_filename (n, "");
	g_assert_cmpstr (notification_get_icon_filename (n),
			 ==,
			 "");
	notification_set_icon_filename (n, "\0");
	g_assert_cmpstr (notification_get_icon_filename (n),
			 ==,
			 "\0");

	// clean up
	g_object_unref (n);
	n = NULL;
}

static void
test_notification_setget_icon_pixbuf (void)
{
	Notification* n      = NULL;
	GdkPixbuf*    pixbuf = NULL;

	// create new object
	n = notification_new ();

	// if no icon-pixbuf has been set yet it should return NULL
	g_assert (notification_get_icon_pixbuf (n) == NULL);

	// create pixbuf for testing
	pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 100, 100);

	// set an initial icon-pixbuf and verify it
	g_assert (pixbuf);
	notification_set_icon_pixbuf (n, pixbuf);
	g_assert (notification_get_icon_pixbuf (n) != NULL);

	// passing an invalid/NULL pointer should not change the stored
	// icon-pixbuf
	notification_set_icon_pixbuf (n, NULL);
	g_assert (notification_get_icon_pixbuf (n) != NULL);

	// clean up
	g_object_unref (n);
	n = NULL;

	// more clean up
	g_object_unref (pixbuf);
	pixbuf = NULL;
}

static void
test_notification_setget_onscreen_time (void)
{
	Notification* n = NULL;

	// create new object
	n = notification_new ();

	// if no onscreen-time has been set yet it should return 0
	g_assert_cmpint (notification_get_onscreen_time (n), ==, 0);

	// setting a negative onscreen-time should fail
	notification_set_onscreen_time (n, -1);
	g_assert_cmpint (notification_get_onscreen_time (n), ==, 0);

	// set an positive onscreen-time and verify it
	notification_set_onscreen_time (n, 1000);
	g_assert_cmpint (notification_get_onscreen_time (n), ==, 1000);

	// set a new onscreen-time and verify it
	notification_set_onscreen_time (n, 5000);
	g_assert_cmpint (notification_get_onscreen_time (n), !=, 1000);
	g_assert_cmpint (notification_get_onscreen_time (n), ==, 5000);

	// setting a new onscreen-time smaller than the currently stored one
	// should fail
	notification_set_onscreen_time (n, 4000);
	g_assert_cmpint (notification_get_onscreen_time (n), ==, 5000);

	// clean up
	g_object_unref (n);
	n = NULL;
}

static void
test_notification_setget_sender_name (void)
{
	Notification* n = NULL;

	// create new object
	n = notification_new ();

	// if no sender-name has been set yet it should return NULL
	g_assert (notification_get_sender_name (n) == NULL);

	// set an initial sender-name and verify it
	notification_set_sender_name (n, "evolution");
	g_assert_cmpstr (notification_get_sender_name (n), ==, "evolution");

	// set a new sender-name and verify it
	notification_set_sender_name (n, "pidgin");
	g_assert_cmpstr (notification_get_sender_name (n), !=, "evolution");
	g_assert_cmpstr (notification_get_sender_name (n), ==, "pidgin");

	// passing an invalid/NULL pointer should not change the stored
	// sender-name 
	notification_set_sender_name (n, NULL);
	g_assert_cmpstr (notification_get_sender_name (n), ==, "pidgin");

	// pass an empty (but not NULL) strings
	notification_set_sender_name (n, "");
	g_assert_cmpstr (notification_get_sender_name (n), ==, "");
	notification_set_sender_name (n, "\0");
	g_assert_cmpstr (notification_get_sender_name (n), ==, "\0");

	// clean up
	g_object_unref (n);
	n = NULL;
}

static void
test_notification_setget_sender_pid (void)
{
	Notification* n = NULL;

	// create new object
	n = notification_new ();

	// if nothing has been set yet it should return 0
	g_assert_cmpint (notification_get_sender_pid (n), ==, 0);

	// a negative pid makes no sense and should therefore not be stored
	notification_set_sender_pid (n, -1);
	g_assert_cmpint (notification_get_sender_pid (n), ==, 0);

	// smallest possible pid is 1
	notification_set_sender_pid (n, 1);
	g_assert_cmpint (notification_get_sender_pid (n), ==, 1);

	// largest possible id is G_MAXINT
	notification_set_sender_pid (n, G_MAXINT);
	g_assert_cmpint (notification_get_sender_pid (n), ==, G_MAXINT);

	// a pid of 0 would mean something before the init process is sending us
	// a notification, leave the stored pid untouched
	notification_set_sender_pid (n, 0);
	g_assert_cmpint (notification_get_sender_pid (n), ==, G_MAXINT);

	// clean up
	g_object_unref (n);
	n = NULL;
}

static void
test_notification_setget_timestamp (void)
{
	Notification* n     = NULL;
	GTimeVal*       tvptr = NULL;
	GTimeVal        tv_old;
	GTimeVal        tv_new;

	// create new object
	n = notification_new ();

	// if no reception-time has been set yet it should return 0/0
	tvptr = notification_get_timestamp (n);
	g_assert_cmpint (tvptr->tv_sec, ==, 0);
	g_assert_cmpint (tvptr->tv_usec, ==, 0);

	// ehm... well, get current time
	g_get_current_time (&tv_old);

	// store current time as reception-time and verify it
	notification_set_timestamp (n, &tv_old);
	tvptr = notification_get_timestamp (n);
	g_assert_cmpint (tvptr->tv_sec, ==, tv_old.tv_sec);
	g_assert_cmpint (tvptr->tv_usec, ==, tv_old.tv_usec);

	// wait at least two seconds
	sleep (2);

	// get current time
	g_get_current_time (&tv_new);

	// trying to store an older timestamp over a newer one should fail
	// second-granularity
	notification_set_timestamp (n, &tv_new);
	notification_set_timestamp (n, &tv_old);
	tvptr = notification_get_timestamp (n);
	g_assert_cmpint (tvptr->tv_sec, !=, tv_old.tv_sec);
	g_assert_cmpint (tvptr->tv_sec, ==, tv_new.tv_sec);

	// get current time
	g_get_current_time (&tv_old);
	notification_set_timestamp (n, &tv_old);

	// wait some micro-seconds
	usleep (10000);

	// get current time
	g_get_current_time (&tv_new);

	// trying to store an older timestamp over a newer one should fail
	// microsecond-granularity
	notification_set_timestamp (n, &tv_new);
	notification_set_timestamp (n, &tv_old);
	tvptr = notification_get_timestamp (n);
	g_assert_cmpint (tvptr->tv_usec, !=, tv_old.tv_usec);
	g_assert_cmpint (tvptr->tv_usec, ==, tv_new.tv_usec);

	// clean up
	g_object_unref (n);
	n = NULL;
}

static void
test_notification_setget_urgency (void)
{
	Notification* n = NULL;

	// create new object
	n = notification_new ();

	// if no urgency has been set yet it should return urgency-none
	g_assert_cmpint (notification_get_urgency (n), ==, URGENCY_NONE);

	// test all three urgency-levels
	notification_set_urgency (n, URGENCY_HIGH);
	g_assert_cmpint (notification_get_urgency (n), ==, URGENCY_HIGH);
	notification_set_urgency (n, URGENCY_LOW);
	g_assert_cmpint (notification_get_urgency (n), ==, URGENCY_LOW);
	notification_set_urgency (n, URGENCY_NORMAL);
	g_assert_cmpint (notification_get_urgency (n), ==, URGENCY_NORMAL);

	// test non-urgency levels, last valid urgency should be returned
	notification_set_urgency (n, 5);
	g_assert_cmpint (notification_get_urgency (n), ==, URGENCY_NORMAL);
	notification_set_urgency (n, 23);
	g_assert_cmpint (notification_get_urgency (n), ==, URGENCY_NORMAL);
	notification_set_urgency (n, -2);
	g_assert_cmpint (notification_get_urgency (n), ==, URGENCY_NORMAL);

	// clean up
	g_object_unref (n);
	n = NULL;
}

GTestSuite *
test_notification_create_test_suite (void)
{
	GTestSuite *ts = NULL;
	GTestCase  *tc = NULL;

	ts = g_test_create_suite ("notification");
	tc = g_test_create_case ("can create",
				 0,
				 NULL,
				 NULL,
				 test_notification_new,
				 NULL);
	g_test_suite_add (ts, tc);

	g_test_suite_add (
		ts,
		g_test_create_case (
			"can destroy",
			0,
			NULL,
			NULL,
			test_notification_destroy,
			NULL));

	g_test_suite_add (
		ts,
		g_test_create_case (
			"can set|get id",
			0,
			NULL,
			NULL,
			test_notification_setget_id,
			NULL));

	g_test_suite_add (
		ts,
		g_test_create_case (
			"can set|get title",
			0,
			NULL,
			NULL,
			test_notification_setget_title,
			NULL));

	g_test_suite_add (
		ts,
		g_test_create_case (
			"can set|get body",
			0,
			NULL,
			NULL,
			test_notification_setget_body,
			NULL));

	g_test_suite_add (
		ts,
		g_test_create_case (
			"can set|get value",
			0,
			NULL,
			NULL,
			test_notification_setget_value,
			NULL));

	g_test_suite_add (
		ts,
		g_test_create_case (
			"can set|get icon-themename",
			0,
			NULL,
			NULL,
			test_notification_setget_icon_themename,
			NULL));

	g_test_suite_add (
		ts,
		g_test_create_case (
			"can set|get icon-filename",
			0,
			NULL,
			NULL,
			test_notification_setget_icon_filename,
			NULL));

	g_test_suite_add (
		ts,
		g_test_create_case (
			"can set|get icon-pixbuf",
			0,
			NULL,
			NULL,
			test_notification_setget_icon_pixbuf,
			NULL));

	g_test_suite_add (
		ts,
		g_test_create_case (
			"can set|get onscreen-time",
			0,
			NULL,
			NULL,
			test_notification_setget_onscreen_time,
			NULL));

	g_test_suite_add (
		ts,
		g_test_create_case (
			"can set|get sender-name",
			0,
			NULL,
			NULL,
			test_notification_setget_sender_name,
			NULL));

	g_test_suite_add (
		ts,
		g_test_create_case (
			"can set|get sender-pid",
			0,
			NULL,
			NULL,
			test_notification_setget_sender_pid,
			NULL));

	g_test_suite_add (
		ts,
		g_test_create_case (
			"can set|get timestamp",
			0,
			NULL,
			NULL,
			test_notification_setget_timestamp,
			NULL));

	g_test_suite_add (
		ts,
		g_test_create_case (
			"can set|get urgency",
			0,
			NULL,
			NULL,
			test_notification_setget_urgency,
			NULL));

	return ts;
}