2
* test_alarm.c -- Alarm test suite
4
* Copyright (C) 2007-2008 Johannes H. Jensen <joh@pseudoberries.com>
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.
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.
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.
21
* Johannes H. Jensen <joh@pseudoberries.com>
29
#define GCONF_DIR "/apps/alarm-clock/test"
43
* Called before each test case
46
alarm_fixture_setup (AlarmFixture *fix,
47
gconstpointer test_data)
49
fix->alarm = alarm_new (GCONF_DIR, 0);
51
g_assert (fix->alarm != NULL);
55
* TEST FIXTURE teardown
57
* Called after each test case
60
alarm_fixture_teardown (AlarmFixture *fix,
61
gconstpointer test_data)
63
g_assert (fix->alarm != NULL);
65
alarm_delete (fix->alarm);
66
g_object_unref (fix->alarm);
73
* TEST: Alarm properties
75
* 1. Will set properties and verify them
76
* 2. Will try to load properties from GConf and verify them
79
test_alarm_props (AlarmFixture *fix,
80
gconstpointer test_data)
82
Alarm *alarm = fix->alarm;
84
// g_object_set (alarm, "gconf-dir", "bar", NULL); // Shouldn't work
87
"type", ALARM_TYPE_TIMER,
91
"message", "Wakety zooom!",
92
"repeat", ALARM_REPEAT_MON | ALARM_REPEAT_WED,
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,
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);
118
g_object_unref (fix->alarm);
120
/* Load settings from GConf */
121
fix->alarm = alarm = alarm_new (GCONF_DIR, 0);
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);
146
test_alarm_type (AlarmFixture *fix,
147
gconstpointer test_data)
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");
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);
163
* TEST: AlarmNotifyType
166
test_alarm_notify_type (AlarmFixture *fix,
167
gconstpointer test_data)
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");
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);
186
test_alarm_list (AlarmFixture *fix,
187
gconstpointer test_data)
189
GList *list = NULL, *l;
191
Alarm *a, *a1, *a2, *a3;
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);
200
list = alarm_get_list (GCONF_DIR);
202
for (l = list; l; l = l->next, i++) {
218
g_assert_not_reached ();
223
g_assert_cmpint (a->id, ==, eq);
235
* Alarm signal handler
238
test_alarm_signal_alarm (Alarm *a, gchar *data)
240
//g_print ("ALARM on %p! Data: %s\n", a, data);
241
g_assert_cmpstr (data, ==, "the data");
247
* Error signal handler
250
test_alarm_signal_error (Alarm *a, GError *err, gchar *data)
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");
264
* TEST: Alarm signals
266
* 1. Test "alarm" signal
267
* 2. Test "error" signal
270
test_alarm_signals (AlarmFixture *fix,
271
gconstpointer test_data)
273
Alarm *alarm = fix->alarm;
277
"notify_type", ALARM_NOTIFY_COMMAND,
281
// Test alarm signals
282
g_signal_connect (alarm, "alarm",
283
G_CALLBACK (test_alarm_signal_alarm),
286
alarm_trigger (alarm);
288
g_assert_cmpint (state, ==, 1);
292
// Test error signals
293
g_signal_connect (alarm, "error", G_CALLBACK (test_alarm_signal_error),
296
alarm_error_trigger (alarm, 123, "Something bad happened");
298
g_assert_cmpint (state, ==, 2);
304
* Callback function for stopping the GMainLoop
307
stop_loop (GMainLoop *loop)
309
g_main_loop_quit (loop);
318
* 1. Test SOUND notification
319
* 2. Test COMMAND notification
322
test_alarm_notify (AlarmFixture *fix,
323
gconstpointer test_data)
325
Alarm *alarm = fix->alarm;
326
GMainLoop *loop = g_main_loop_new (g_main_context_default(), FALSE);
329
// Test SOUND notification
332
"notify_type", ALARM_NOTIFY_SOUND,
333
"sound_file", "file:///usr/share/sounds/question.wav",
334
"sound_repeat", FALSE,
337
alarm_trigger (alarm);
341
// Test COMMAND notification
344
"notify_type", ALARM_NOTIFY_COMMAND,
348
alarm_trigger (alarm);
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);
360
* Callback for alarm signal.
361
* Will set state to the current timestamp.
364
test_alarm_timers_alarm (Alarm *a, gchar *data)
366
g_debug ("test_alarm_timers_ALARM");
375
* 1. Test alarm CLOCK setting time to 5 seconds from now.
376
* 2. Test alarm TIMER setting time to 3 seconds.
379
test_alarm_timers (AlarmFixture *fix,
380
gconstpointer test_data)
382
Alarm *alarm = fix->alarm;
383
time_t now = time(NULL);
385
GMainLoop *loop = g_main_loop_new (g_main_context_default(), FALSE);
388
// Alarm signal handler
389
g_signal_connect (alarm, "alarm",
390
G_CALLBACK (test_alarm_timers_alarm),
394
// Test CLOCK 5sec from now
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);
403
"type", ALARM_TYPE_CLOCK,
404
"notify_type", ALARM_NOTIFY_COMMAND,
405
"command", "echo CMDALARM",
408
alarm_enable (alarm);
411
g_timeout_add (6000, stop_loop, loop); // Stop loop after 6 secs
412
g_main_loop_run (loop);
414
g_assert_cmpint (state - now, ==, 5);
422
"type", ALARM_TYPE_TIMER,
426
alarm_enable (alarm);
430
g_timeout_add (4000, stop_loop, loop); // Stop loop after 4 secs
431
g_main_loop_run (loop);
433
g_assert_cmpint (state - now, ==, 3);
442
test_alarm_repeat (AlarmFixture *fix,
443
gconstpointer test_data)
454
// Test single repeats to string and back again
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);
461
g_assert_cmpint (r, ==, rep);
466
// Test repeat to list
470
rep = ALARM_REPEAT_NONE;
471
list = alarm_repeat_to_list (rep);
472
g_assert (NULL == list);
474
rep = alarm_repeat_from_list (list);
475
g_assert_cmpint (rep, ==, ALARM_REPEAT_NONE);
478
rep = ALARM_REPEAT_MON | ALARM_REPEAT_WED | ALARM_REPEAT_FRI;
479
list = alarm_repeat_to_list (rep);
481
GString *s = g_string_new ("");
482
for (l = list; l; l = l->next) {
483
s = g_string_append (s, (gchar *)l->data);
485
g_assert_cmpstr (s->str, ==, "monwedfri");
486
g_string_free (s, TRUE);
489
r = alarm_repeat_from_list (list);
490
g_assert_cmpint (r, ==, rep);
492
// Should free data as well but we're lazy
500
test_alarm_bind (AlarmFixture *fix,
501
gconstpointer test_data)
503
Alarm *alarm = fix->alarm;
504
Alarm *alarm2 = alarm_new (GCONF_DIR, 1);
506
alarm_bind (alarm, "command", G_OBJECT (alarm2), "message");
508
g_object_set (alarm, "command", "wickid!", NULL);
510
g_assert_cmpstr (alarm->command, ==, alarm2->message);
512
alarm_delete (alarm2);
513
g_object_unref (alarm2);
521
int main (int argc, char **argv)
524
g_test_init(&argc, &argv, NULL);
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);
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);
539
return g_test_run ();