~ubuntu-branches/ubuntu/oneiric/alarm-clock-applet/oneiric

« back to all changes in this revision

Viewing changes to src/tests/test_alarm.c

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-05-30 23:24:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090530232427-88on1j2ily4ajxdz
Tags: upstream-0.2.6
ImportĀ upstreamĀ versionĀ 0.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * test_alarm.c -- Alarm test suite
 
3
 * 
 
4
 * Copyright (C) 2007-2008 Johannes H. Jensen <joh@pseudoberries.com>
 
5
 * 
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 * 
 
20
 * Authors:
 
21
 *              Johannes H. Jensen <joh@pseudoberries.com>
 
22
 */
 
23
 
 
24
#include "alarm.h"
 
25
#include <time.h>
 
26
#include <glib.h>
 
27
#include <string.h>
 
28
 
 
29
#define GCONF_DIR "/apps/alarm-clock/test"
 
30
 
 
31
/* Fixture */
 
32
typedef struct {
 
33
        Alarm *alarm;
 
34
} AlarmFixture;
 
35
 
 
36
static int state = 0;
 
37
 
 
38
 
 
39
 
 
40
/*
 
41
 * TEST FIXTURE setup
 
42
 * 
 
43
 * Called before each test case
 
44
 */
 
45
static void
 
46
alarm_fixture_setup (AlarmFixture *fix,
 
47
                                         gconstpointer test_data)
 
48
{
 
49
        fix->alarm = alarm_new (GCONF_DIR, 0);
 
50
        
 
51
        g_assert (fix->alarm != NULL);
 
52
}
 
53
 
 
54
/*
 
55
 * TEST FIXTURE teardown
 
56
 * 
 
57
 * Called after each test case
 
58
 */
 
59
static void
 
60
alarm_fixture_teardown (AlarmFixture *fix,
 
61
                                                gconstpointer test_data)
 
62
{
 
63
        g_assert (fix->alarm != NULL);
 
64
        
 
65
        alarm_delete (fix->alarm);
 
66
        g_object_unref (fix->alarm);
 
67
}
 
68
                                                                                  
 
69
 
 
70
 
 
71
 
 
72
/*
 
73
 * TEST: Alarm properties
 
74
 * 
 
75
 * 1. Will set properties and verify them
 
76
 * 2. Will try to load properties from GConf and verify them
 
77
 */
 
78
static void
 
79
test_alarm_props (AlarmFixture *fix,
 
80
                                  gconstpointer test_data)
 
81
{
 
82
        Alarm *alarm = fix->alarm;
 
83
        
 
84
//      g_object_set (alarm, "gconf-dir", "bar", NULL);         // Shouldn't work
 
85
        g_object_set (alarm, 
 
86
                                  "id", 0,
 
87
                                  "type", ALARM_TYPE_TIMER,
 
88
                                  "time", 1234,
 
89
                                  "timestamp", 5678,
 
90
                                  "active", FALSE,
 
91
                                  "message", "Wakety zooom!",
 
92
                                  "repeat", ALARM_REPEAT_MON | ALARM_REPEAT_WED,
 
93
                                  "snooze", 12,
 
94
                                  "notify_type", ALARM_NOTIFY_COMMAND,
 
95
                                  "sound_file", "file:///foo/bar",
 
96
                                  "sound_repeat", FALSE, 
 
97
                                  "command", "wiggle-your-toe --arg",
 
98
                                  "notify_bubble", TRUE, 
 
99
                                  NULL);
 
100
        
 
101
        /* Verify properties */
 
102
        g_assert_cmpstr (alarm->gconf_dir, ==, GCONF_DIR);
 
103
        g_assert_cmpint (alarm->id, ==, 0);
 
104
        g_assert_cmpint (alarm->type, ==, ALARM_TYPE_TIMER);
 
105
        g_assert_cmpint (alarm->time, ==, 1234);
 
106
        g_assert_cmpint (alarm->timestamp, ==, 5678);
 
107
        g_assert_cmpint (alarm->active, ==, FALSE);
 
108
        g_assert_cmpstr (alarm->message, ==, "Wakety zooom!");
 
109
        g_assert_cmpint (alarm->repeat, ==, ALARM_REPEAT_MON | ALARM_REPEAT_WED);
 
110
        g_assert_cmpint (alarm->snooze, ==, 12);
 
111
        g_assert_cmpint (alarm->notify_type, ==, ALARM_NOTIFY_COMMAND);
 
112
        g_assert_cmpstr (alarm->sound_file, ==, "file:///foo/bar");
 
113
        g_assert_cmpint (alarm->sound_loop, ==, FALSE);
 
114
        g_assert_cmpstr (alarm->command, ==, "wiggle-your-toe --arg");
 
115
        g_assert_cmpint (alarm->notify_bubble, ==, TRUE);
 
116
        
 
117
        /* Unref alarm */
 
118
        g_object_unref (fix->alarm);
 
119
        
 
120
        /* Load settings from GConf */
 
121
        fix->alarm = alarm = alarm_new (GCONF_DIR, 0);
 
122
        
 
123
        /* Verify properties */
 
124
        g_assert_cmpstr (alarm->gconf_dir, ==, GCONF_DIR);
 
125
        g_assert_cmpint (alarm->id, ==, 0);
 
126
        g_assert_cmpint (alarm->type, ==, ALARM_TYPE_TIMER);
 
127
        g_assert_cmpint (alarm->time, ==, 1234);
 
128
        g_assert_cmpint (alarm->timestamp, ==, 5678);
 
129
        g_assert_cmpint (alarm->active, ==, FALSE);
 
130
        g_assert_cmpstr (alarm->message, ==, "Wakety zooom!");
 
131
        g_assert_cmpint (alarm->repeat, ==, ALARM_REPEAT_MON | ALARM_REPEAT_WED);
 
132
        g_assert_cmpint (alarm->snooze, ==, 12);
 
133
        g_assert_cmpint (alarm->notify_type, ==, ALARM_NOTIFY_COMMAND);
 
134
        g_assert_cmpstr (alarm->sound_file, ==, "file:///foo/bar");
 
135
        g_assert_cmpint (alarm->sound_loop, ==, FALSE);
 
136
        g_assert_cmpstr (alarm->command, ==, "wiggle-your-toe --arg");
 
137
        g_assert_cmpint (alarm->notify_bubble, ==, TRUE);
 
138
}
 
139
 
 
140
 
 
141
 
 
142
/*
 
143
 * TEST: AlarmType
 
144
 */
 
145
static void 
 
146
test_alarm_type (AlarmFixture *fix,
 
147
                                 gconstpointer test_data)
 
148
{
 
149
        // alarm_type_to_string ()
 
150
        g_assert        (alarm_type_to_string (ALARM_TYPE_INVALID) ==  NULL);
 
151
        g_assert_cmpstr (alarm_type_to_string (ALARM_TYPE_CLOCK),  ==, "clock");
 
152
        g_assert_cmpstr (alarm_type_to_string (ALARM_TYPE_TIMER),  ==, "timer");
 
153
        
 
154
        // alarm_type_from_string ()
 
155
        g_assert_cmpint (alarm_type_from_string ("invalid"), ==, ALARM_TYPE_INVALID);
 
156
        g_assert_cmpint (alarm_type_from_string ("clock"),   ==, ALARM_TYPE_CLOCK);
 
157
        g_assert_cmpint (alarm_type_from_string ("timer"),   ==, ALARM_TYPE_TIMER);
 
158
}
 
159
 
 
160
 
 
161
 
 
162
/*
 
163
 * TEST: AlarmNotifyType
 
164
 */
 
165
static void 
 
166
test_alarm_notify_type (AlarmFixture *fix,
 
167
                                                gconstpointer test_data)
 
168
{
 
169
        // alarm_notify_type_to_string ()
 
170
        g_assert                (alarm_notify_type_to_string (ALARM_NOTIFY_INVALID)  ==  NULL);
 
171
        g_assert_cmpstr (alarm_notify_type_to_string (ALARM_NOTIFY_SOUND),   ==, "sound");
 
172
        g_assert_cmpstr (alarm_notify_type_to_string (ALARM_NOTIFY_COMMAND), ==, "command");
 
173
        
 
174
        // alarm_notify_type_from_string ()
 
175
        g_assert_cmpint (alarm_notify_type_from_string ("invalid"), ==, ALARM_NOTIFY_INVALID);
 
176
        g_assert_cmpint (alarm_notify_type_from_string ("sound"),   ==, ALARM_NOTIFY_SOUND);
 
177
        g_assert_cmpint (alarm_notify_type_from_string ("command"), ==, ALARM_NOTIFY_COMMAND);
 
178
}
 
179
 
 
180
 
 
181
 
 
182
/*
 
183
 * TEST: Alarm list
 
184
 */
 
185
static void
 
186
test_alarm_list (AlarmFixture *fix,
 
187
                                 gconstpointer test_data)
 
188
{
 
189
        GList *list = NULL, *l;
 
190
        guint i = 0;
 
191
        Alarm *a, *a1, *a2, *a3;
 
192
        
 
193
        // Initialize with some dummy alarms
 
194
        a = alarm_new (GCONF_DIR, 3);   g_object_unref (a);
 
195
        a = alarm_new (GCONF_DIR, 5);   g_object_unref (a);
 
196
        a = alarm_new (GCONF_DIR, 8);   g_object_unref (a);
 
197
        a = alarm_new (GCONF_DIR, 123); g_object_unref (a);
 
198
        
 
199
        // Get list
 
200
        list = alarm_get_list (GCONF_DIR);
 
201
        
 
202
        for (l = list; l; l = l->next, i++) {
 
203
                int eq;
 
204
                switch (i) {
 
205
                case 0:
 
206
                        eq = 3;
 
207
                        break;
 
208
                case 1:
 
209
                        eq = 5;
 
210
                        break;
 
211
                case 2:
 
212
                        eq = 8;
 
213
                        break;
 
214
                case 3:
 
215
                        eq = 123;
 
216
                        break;
 
217
                default:
 
218
                        g_assert_not_reached ();
 
219
                        break;
 
220
                }
 
221
                
 
222
                a = ALARM (l->data);
 
223
                g_assert_cmpint (a->id, ==, eq);
 
224
                
 
225
                alarm_delete (a);
 
226
                g_object_unref (a);
 
227
        }
 
228
        
 
229
        g_list_free (list);
 
230
}
 
231
 
 
232
 
 
233
 
 
234
/*
 
235
 * Alarm signal handler
 
236
 */
 
237
static void
 
238
test_alarm_signal_alarm (Alarm *a, gchar *data)
 
239
{
 
240
        //g_print ("ALARM on %p! Data: %s\n", a, data);
 
241
        g_assert_cmpstr (data, ==, "the data");
 
242
        
 
243
        state = 1;
 
244
}
 
245
 
 
246
/*
 
247
 * Error signal handler
 
248
 */
 
249
static void
 
250
test_alarm_signal_error (Alarm *a, GError *err, gchar *data)
 
251
{
 
252
        //g_print ("ERROR on %p! Message: %s, Code: %d, Data: %s", a, err->message, err->code, data);
 
253
        g_assert_cmpint (a->id, ==, 0);
 
254
        g_assert_cmpstr (err->message, ==, "Something bad happened");
 
255
        g_assert_cmpint (err->code, ==, 123);
 
256
        g_assert_cmpstr (data, ==, "the error data");
 
257
        
 
258
        state = 2;
 
259
}
 
260
 
 
261
 
 
262
 
 
263
/*
 
264
 * TEST: Alarm signals
 
265
 * 
 
266
 * 1. Test "alarm" signal
 
267
 * 2. Test "error" signal
 
268
 */
 
269
static void
 
270
test_alarm_signals (AlarmFixture *fix,
 
271
                                        gconstpointer test_data)
 
272
{
 
273
        Alarm *alarm = fix->alarm;
 
274
        state = 0;
 
275
        
 
276
        g_object_set (alarm, 
 
277
                                  "notify_type", ALARM_NOTIFY_COMMAND, 
 
278
                                  "command", "echo",
 
279
                                  NULL);
 
280
        
 
281
        // Test alarm signals
 
282
        g_signal_connect (alarm, "alarm",
 
283
                                          G_CALLBACK (test_alarm_signal_alarm),
 
284
                                          "the data");
 
285
        
 
286
        alarm_trigger (alarm);
 
287
        
 
288
        g_assert_cmpint (state, ==, 1);
 
289
        
 
290
        
 
291
        
 
292
        // Test error signals
 
293
        g_signal_connect (alarm, "error", G_CALLBACK (test_alarm_signal_error),
 
294
                                          "the error data");
 
295
        
 
296
        alarm_error_trigger (alarm, 123, "Something bad happened");
 
297
        
 
298
        g_assert_cmpint (state, ==, 2);
 
299
}
 
300
 
 
301
 
 
302
 
 
303
/*
 
304
 * Callback function for stopping the GMainLoop
 
305
 */
 
306
static gboolean
 
307
stop_loop (GMainLoop *loop)
 
308
{
 
309
        g_main_loop_quit (loop);
 
310
        return FALSE;
 
311
}
 
312
 
 
313
 
 
314
 
 
315
/*
 
316
 * TEST: Notify types
 
317
 * 
 
318
 * 1. Test SOUND notification
 
319
 * 2. Test COMMAND notification
 
320
 */
 
321
static void
 
322
test_alarm_notify (AlarmFixture *fix,
 
323
                                        gconstpointer test_data)
 
324
{
 
325
        Alarm *alarm = fix->alarm;
 
326
        GMainLoop *loop = g_main_loop_new (g_main_context_default(), FALSE);
 
327
        
 
328
        //
 
329
        // Test SOUND notification
 
330
        //
 
331
        g_object_set (alarm,
 
332
                                  "notify_type", ALARM_NOTIFY_SOUND,
 
333
                                  "sound_file", "file:///usr/share/sounds/question.wav",
 
334
                                  "sound_repeat", FALSE,
 
335
                                  NULL);
 
336
        
 
337
        alarm_trigger (alarm);
 
338
        
 
339
        
 
340
        //
 
341
        // Test COMMAND notification
 
342
        //
 
343
        g_object_set (alarm,
 
344
                                  "notify_type", ALARM_NOTIFY_COMMAND,
 
345
                                  "command", "ls -a",
 
346
                                  NULL);
 
347
        
 
348
        alarm_trigger (alarm);
 
349
        
 
350
        // We need to run the main loop for a couple of seconds
 
351
        // so the player error/state callbacks get the alarm
 
352
        // instance while it's still alive.
 
353
        g_timeout_add (2000, stop_loop, loop);  // Stop loop after 2 secs
 
354
        g_main_loop_run (loop);
 
355
}
 
356
 
 
357
 
 
358
 
 
359
/*
 
360
 * Callback for alarm signal. 
 
361
 * Will set state to the current timestamp.
 
362
 */
 
363
static void
 
364
test_alarm_timers_alarm (Alarm *a, gchar *data)
 
365
{
 
366
        g_debug ("test_alarm_timers_ALARM");
 
367
        state = time (NULL);
 
368
}
 
369
 
 
370
 
 
371
 
 
372
/*
 
373
 * TEST: Alarm timers
 
374
 * 
 
375
 * 1. Test alarm CLOCK setting time to 5 seconds from now.
 
376
 * 2. Test alarm TIMER setting time to 3 seconds.
 
377
 */
 
378
static void
 
379
test_alarm_timers (AlarmFixture *fix,
 
380
                                   gconstpointer test_data)
 
381
{
 
382
        Alarm *alarm    = fix->alarm;
 
383
        time_t now              = time(NULL);
 
384
        struct tm *tm;
 
385
        GMainLoop *loop = g_main_loop_new (g_main_context_default(), FALSE);
 
386
        
 
387
        
 
388
        // Alarm signal handler
 
389
        g_signal_connect (alarm, "alarm",
 
390
                                          G_CALLBACK (test_alarm_timers_alarm),
 
391
                                          "the data");
 
392
        
 
393
        // 
 
394
        // Test CLOCK 5sec from now
 
395
        //
 
396
        
 
397
        now = time (NULL);
 
398
        tm = localtime (&now);
 
399
        //g_debug ("TEST SET %d => %d:%d:%d", now, tm->tm_hour, tm->tm_min, tm->tm_sec + 5);
 
400
        alarm_set_time (alarm, tm->tm_hour, tm->tm_min, tm->tm_sec + 5);
 
401
        
 
402
        g_object_set (alarm,
 
403
                                  "type", ALARM_TYPE_CLOCK,
 
404
                                  "notify_type", ALARM_NOTIFY_COMMAND,
 
405
                                  "command", "echo CMDALARM",
 
406
                                  NULL);
 
407
        
 
408
        alarm_enable (alarm);
 
409
        
 
410
        state = 0;
 
411
        g_timeout_add (6000, stop_loop, loop);  // Stop loop after 6 secs
 
412
        g_main_loop_run (loop);
 
413
        
 
414
        g_assert_cmpint (state - now, ==, 5);
 
415
        
 
416
        
 
417
        
 
418
        // 
 
419
        // Test TIMER 3sec
 
420
        //
 
421
        g_object_set (alarm, 
 
422
                                  "type", ALARM_TYPE_TIMER,
 
423
                                  "time", 3,
 
424
                                  NULL);
 
425
        
 
426
        alarm_enable (alarm);
 
427
        
 
428
        state = 0;
 
429
        now = time (NULL);
 
430
        g_timeout_add (4000, stop_loop, loop);  // Stop loop after 4 secs
 
431
        g_main_loop_run (loop);
 
432
        
 
433
        g_assert_cmpint (state - now, ==, 3);
 
434
}
 
435
 
 
436
 
 
437
 
 
438
/*
 
439
 * TEST: AlarmRepeat
 
440
 */
 
441
static void
 
442
test_alarm_repeat (AlarmFixture *fix,
 
443
                                   gconstpointer test_data)
 
444
{
 
445
        AlarmRepeat r, rep;
 
446
        const gchar *str;
 
447
        gint i, wnow, wday;
 
448
        GSList *list, *l;
 
449
        struct tm *tm;
 
450
        time_t now;
 
451
        time (&now);
 
452
        
 
453
        //
 
454
        // Test single repeats to string and back again
 
455
        //
 
456
        g_print ("SINGLE:\n");
 
457
        for (r = ALARM_REPEAT_SUN, i = 1; r <= ALARM_REPEAT_SAT; r = 1 << ++i) {
 
458
                str = alarm_repeat_to_string (r);
 
459
                rep = alarm_repeat_from_string (str);
 
460
                
 
461
                g_assert_cmpint (r, ==, rep);
 
462
        }
 
463
        
 
464
        
 
465
        // 
 
466
        // Test repeat to list
 
467
        //
 
468
        
 
469
        // NONE
 
470
        rep = ALARM_REPEAT_NONE;
 
471
        list = alarm_repeat_to_list (rep);
 
472
        g_assert (NULL == list);
 
473
        
 
474
        rep = alarm_repeat_from_list (list);
 
475
        g_assert_cmpint (rep, ==, ALARM_REPEAT_NONE);
 
476
        
 
477
        // MULTIPLE
 
478
        rep = ALARM_REPEAT_MON | ALARM_REPEAT_WED | ALARM_REPEAT_FRI;
 
479
        list = alarm_repeat_to_list (rep);
 
480
        
 
481
        GString *s = g_string_new ("");
 
482
        for (l = list; l; l = l->next) {
 
483
                s = g_string_append (s, (gchar *)l->data);
 
484
        }
 
485
        g_assert_cmpstr (s->str, ==, "monwedfri");
 
486
        g_string_free (s, TRUE);
 
487
        
 
488
        
 
489
        r = alarm_repeat_from_list (list);
 
490
        g_assert_cmpint (r, ==, rep);
 
491
        
 
492
        // Should free data as well but we're lazy
 
493
        g_slist_free (list);
 
494
}
 
495
 
 
496
/*
 
497
 * TEST: alarm_bind
 
498
 */
 
499
static void
 
500
test_alarm_bind (AlarmFixture *fix,
 
501
                                 gconstpointer test_data)
 
502
{
 
503
        Alarm *alarm  = fix->alarm;
 
504
        Alarm *alarm2 = alarm_new (GCONF_DIR, 1);
 
505
        
 
506
        alarm_bind (alarm, "command", G_OBJECT (alarm2), "message");
 
507
        
 
508
        g_object_set (alarm, "command", "wickid!", NULL);
 
509
        
 
510
        g_assert_cmpstr (alarm->command, ==, alarm2->message);
 
511
        
 
512
        alarm_delete (alarm2);
 
513
        g_object_unref (alarm2);
 
514
}
 
515
 
 
516
 
 
517
 
 
518
/*
 
519
 * Run them tests!
 
520
 */
 
521
int main (int argc, char **argv)
 
522
{
 
523
        g_type_init();
 
524
        g_test_init(&argc, &argv, NULL);
 
525
        
 
526
        // Alarm primitives
 
527
        g_test_add ("/alarm/type",                AlarmFixture, 0, NULL, test_alarm_type, NULL);
 
528
        g_test_add ("/alarm/nofity_type", AlarmFixture, 0, NULL, test_alarm_notify_type, NULL);
 
529
        g_test_add ("/alarm/list",                AlarmFixture, 0, NULL, test_alarm_list, NULL);
 
530
        g_test_add ("/alarm/repeat",      AlarmFixture, 0, NULL, test_alarm_repeat, NULL);
 
531
        
 
532
        // Full system tests
 
533
        g_test_add ("/alarm/props",  AlarmFixture, 0, alarm_fixture_setup, test_alarm_props, alarm_fixture_teardown);
 
534
        g_test_add ("/alarm/signal", AlarmFixture, 0, alarm_fixture_setup, test_alarm_signals, alarm_fixture_teardown);
 
535
        g_test_add ("/alarm/notify", AlarmFixture, 0, alarm_fixture_setup, test_alarm_notify, alarm_fixture_teardown);
 
536
        g_test_add ("/alarm/bind",       AlarmFixture, 0, alarm_fixture_setup, test_alarm_bind, alarm_fixture_teardown);
 
537
        g_test_add ("/alarm/timers", AlarmFixture, 0, alarm_fixture_setup, test_alarm_timers, alarm_fixture_teardown);
 
538
        
 
539
        return g_test_run ();
 
540
}