~kamstrup/gtkhotkey/trunk

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
/*
 * This file is part of GtkHotkey.
 * Copyright Mikkel Kamstrup Erlandsen, March, 2008
 *
 *   GtkHotkey is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   GtkHotkey is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with GtkHotkey.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#include <glib.h>
#include <glib/gstdio.h>
#include <glib-object.h>
#include <glib/gtestutils.h>

#include "src/gtk-hotkey-registry.c"
#include "src/gtk-hotkey-key-file-registry.c"

/* Shortcut to defining a test */
#define TEST(func) g_test_add ("/registry/"#func, Fixture, NULL, (void (*) (Fixture*, gconstpointer))setup, test_##func, teardown);

#define TEST_APP "gtk-hotkey-test"
#define TEST_KEY "gtk-hotkey-test-key"
#define TEST_SIGN "<Alt>F3"
#define TEST_SIGN_ALT "<Control><Shift>F3"

/* Test fixture passed to all tests */
typedef struct {
	GtkHotkeyRegistry	*registry;
	gboolean			was_updated_emitted;
	gboolean			was_deleted_emitted;
	gulong				updated_handle;
	gulong				deleted_handle;
} Fixture;

void
hotkey_updated_cb (GtkHotkeyRegistry	*storage,
                   GtkHotkey			*info,
                   gpointer				*data);

void
hotkey_updated_cb (GtkHotkeyRegistry	*storage,
                   GtkHotkey			*info,
                   gpointer				*data)
{
	Fixture *fix;
	
	g_return_if_fail (GTK_HOTKEY_IS_REGISTRY(storage));
	g_return_if_fail (GTK_HOTKEY_IS_HOTKEY(info));
	g_return_if_fail (data != NULL);
	
	fix = (Fixture*) data;
	fix->was_updated_emitted = TRUE;
}

static void
hotkey_deleted_cb (GtkHotkeyRegistry	*storage,
				   GtkHotkey	*info,
				   gpointer			*data)
{
	Fixture *fix;
	
	g_return_if_fail (GTK_HOTKEY_IS_REGISTRY(storage));
	g_return_if_fail (GTK_HOTKEY_IS_HOTKEY(info));
	g_return_if_fail (data != NULL);
	
	fix = (Fixture*) data;
	fix->was_deleted_emitted = TRUE;
}

static void
setup (Fixture		 *fix,
	   gconstpointer test_data)
{
	/* Use an alternate place for storing hotkey info */
	gchar *tmpdir = g_strconcat (g_get_tmp_dir(), "gtkhotkey-test", NULL);
	g_setenv ("XDG_CONFIG_DIR", tmpdir, TRUE);
	g_remove (tmpdir);
	g_free (tmpdir);
  
	fix->registry = gtk_hotkey_registry_get_default ();
	fix->was_updated_emitted = FALSE;
	fix->was_deleted_emitted = FALSE;
	
	g_assert (GTK_HOTKEY_IS_REGISTRY(fix->registry));
	
	fix->updated_handle = g_signal_connect (fix->registry, "hotkey-updated",
										   G_CALLBACK(hotkey_updated_cb), fix);
	fix->deleted_handle = g_signal_connect (fix->registry, "hotkey-deleted",
											G_CALLBACK(hotkey_deleted_cb), fix);
}

static void
teardown (Fixture		*fix,
		  gconstpointer test_data)
{
	g_signal_handler_disconnect (fix->registry, fix->updated_handle);
	g_signal_handler_disconnect (fix->registry, fix->deleted_handle);
	
	g_object_unref (fix->registry);
}

static void
test_emit_updated (Fixture		*fix,
				  gconstpointer	test_data)
{
	GtkHotkey   *hotkey;
	
	hotkey = gtk_hotkey_registry_get_hotkey (fix->registry,
	                                         TEST_APP, TEST_KEY,
	                                         TEST_SIGN, NULL);
	g_assert (GTK_HOTKEY_IS_HOTKEY (hotkey));
  
	gtk_hotkey_registry_hotkey_updated (fix->registry, hotkey);	
	if (!fix->was_updated_emitted)
		g_critical ("'hotkey-updated' was not emitted by "
					"gtk_hotkey_registry_hotkey_updated()");

	g_object_unref (hotkey);
}

static void
test_emit_deleted (Fixture		*fix,
				  gconstpointer	test_data)
{
	GtkHotkey   *hotkey;
	
	hotkey = gtk_hotkey_registry_get_hotkey (fix->registry,
	                                         TEST_APP, TEST_KEY,
	                                         TEST_SIGN, NULL);
	g_assert (GTK_HOTKEY_IS_HOTKEY (hotkey));
  
	gtk_hotkey_registry_hotkey_deleted (fix->registry, hotkey);	
	if (!fix->was_deleted_emitted)
		g_critical ("'hotkey-deleted' was not emitted by "
					"gtk_hotkey_registry_hotkey_deleted()");

	g_object_unref (hotkey);
}

/**
 * Test that has_hotkey() returns false on bogus input
 */
static void
test_has_non_existing_hotkey	(Fixture		*fix,
								 gconstpointer	test_data)
{
	g_assert (!gtk_hotkey_registry_has_hotkey (fix->registry,
											  "foobar",
											  "blah"));
	g_assert (!fix->was_updated_emitted && !fix->was_deleted_emitted);
}

static void
test_lookup_non_existing_hotkey	(Fixture		*fix,
								 gconstpointer	test_data)
{
	/* Without GError */
	g_assert (gtk_hotkey_registry_lookup_hotkey (fix->registry,
                                                 "foobar",
                                                 "blah") == NULL);
	g_assert (!fix->was_updated_emitted && !fix->was_deleted_emitted);
}

static void
test_delete_twice	(Fixture		*fix,
	                  gconstpointer	test_data)
{
	GtkHotkey   *hotkey;

	/* Without GError */
	hotkey = gtk_hotkey_registry_get_hotkey (fix->registry,
	                                         TEST_APP, TEST_KEY,
	                                         TEST_SIGN, NULL);
	
	g_assert (gtk_hotkey_delete (hotkey, NULL));
	g_assert (fix->was_deleted_emitted);

	/* Second time around should be a no-op */
	fix->was_deleted_emitted = FALSE;
	g_assert (!gtk_hotkey_delete (hotkey, NULL));
	g_assert (!fix->was_deleted_emitted);

	g_object_unref (hotkey);
	
	/* With error */
	GError *error = NULL;
	hotkey = gtk_hotkey_registry_get_hotkey (fix->registry,
	                                         TEST_APP, TEST_KEY,
	                                         TEST_SIGN, NULL);	
	g_assert (gtk_hotkey_delete (hotkey, &error));
	g_assert (fix->was_deleted_emitted);
	g_assert (error == NULL);

	/* Second time around should be a no-op */
	fix->was_deleted_emitted = FALSE;
	g_assert (!gtk_hotkey_delete (hotkey, &error));
	g_assert (!fix->was_deleted_emitted);	
	g_assert (error != NULL);
	// FIXME: Check error code and domain

	g_error_free (error);
	g_object_unref (hotkey);
}

static void
test_save_load_single	(Fixture		*fix,
						 gconstpointer	test_data)
{
	GtkHotkey   *hotkey, *fookey;
	GError			*error;
	
	g_assert (!gtk_hotkey_registry_has_hotkey (fix->registry,
											  TEST_APP,
											  TEST_KEY));

	hotkey = gtk_hotkey_registry_get_hotkey (fix->registry,
	                                         TEST_APP, TEST_KEY,
	                                         TEST_SIGN, NULL);

	/* Registry was updated (hotkey created), but nothing was deleted */
	g_assert (fix->was_updated_emitted && !fix->was_deleted_emitted);
	
	error = NULL;
	gtk_hotkey_set_signature (hotkey, TEST_SIGN_ALT, &error);
	if (error)
		g_critical ("Failed to update hotkey: %s", error->message);
	
	g_assert (fix->was_updated_emitted);
	
	g_assert (gtk_hotkey_registry_has_hotkey (fix->registry,
											 TEST_APP,
											 TEST_KEY));

	/* Lookup the hotkey again, and assert that the signature has been
     * changed from the default */
	error = NULL;
	fookey = gtk_hotkey_registry_get_hotkey (fix->registry,
	                                         TEST_APP, TEST_KEY,
	                                         TEST_SIGN, NULL);
	
	if (error)
		g_critical ("Failed to read hotkey: %s", error->message);
	
	g_assert_cmpstr (TEST_APP, == , gtk_hotkey_get_application_id(fookey));
	g_assert_cmpstr (TEST_KEY, == , gtk_hotkey_get_key_id(fookey));
	g_assert_cmpstr (TEST_SIGN_ALT, == , gtk_hotkey_get_signature(fookey));
	
	error = NULL;
	gtk_hotkey_delete (hotkey, &error);
	
	if (error)
		g_critical ("Failed to delete hotkey: %s", error->message);
	
	g_assert (fix->was_deleted_emitted);
	g_assert (!gtk_hotkey_registry_has_hotkey (fix->registry,
											  TEST_APP,
											  TEST_KEY));
	
	g_object_unref (hotkey);
	g_object_unref (fookey);
}

static void
test_hotkey_equals (Fixture		*fix,
					gconstpointer	test_data)
{
	GtkHotkey *k1, *k2;
	
	k1 = gtk_hotkey_registry_get_hotkey (fix->registry,
	                                         TEST_APP, TEST_KEY,
	                                         TEST_SIGN, NULL);
	k2 = gtk_hotkey_registry_get_hotkey (fix->registry,
	                                         TEST_APP, TEST_KEY,
	                                         TEST_SIGN, NULL);
	g_assert (k1 == k2);
	
	/* FIXME: Check with GAppInfo set too */
	
	g_object_unref (k1);
	g_object_unref (k2);
}

static void
test_get_application_hotkeys (Fixture		*fix,
							  gconstpointer	test_data)
{
	GtkHotkey   *hotkey, *fookey;
	GList			*hotkeys;
	GError			*error;
	
	hotkey = gtk_hotkey_registry_get_hotkey (fix->registry,
	                                         TEST_APP, TEST_KEY"1",
	                                         TEST_SIGN, NULL);
	fookey = gtk_hotkey_registry_get_hotkey (fix->registry,
	                                         TEST_APP, TEST_KEY"2",
	                                         TEST_SIGN_ALT, NULL);
	
	/* Retrieve all hotkeys */
	error = NULL;
	hotkeys = gtk_hotkey_registry_get_application_hotkeys (fix->registry,
	                                                       TEST_APP,
	                                                       &error);
	if (error) g_critical ("Failed to get application hotkeys : %s",
						   error->message);
	
	g_assert_cmpint (g_list_length (hotkeys), == , 2);
	g_list_foreach (hotkeys, (GFunc)g_object_unref, NULL);
	g_list_free (hotkeys);
	
	/* Delete the first of the hotkeys */
	error = NULL;
	gtk_hotkey_delete (hotkey, &error);
	
	if (error) g_critical ("Failed to delete first hotkey: %s", error->message);
	
	/* Assert that we have exactly one hotkey and that it is the right one */
	error = NULL;
	hotkeys = gtk_hotkey_registry_get_application_hotkeys (fix->registry, TEST_APP,
														 &error);
	if (error) g_critical ("Failed to get application hotkeys : %s",
						   error->message);
	
	g_assert_cmpint (g_list_length (hotkeys), == , 1);
	g_assert (gtk_hotkey_equals (fookey, GTK_HOTKEY(hotkeys->data), FALSE));
	
	g_list_foreach (hotkeys, (GFunc)g_object_unref, NULL);
	g_list_free (hotkeys);
	
	/* Clean up */
	/* Delete the first of the hotkeys */
	gtk_hotkey_delete (fookey, NULL);
	g_assert (!gtk_hotkey_registry_has_hotkey (fix->registry,
											  TEST_APP, TEST_KEY"2"));
	
	g_object_unref (hotkey);
	g_object_unref (fookey);
}

static void
test_get_all_hotkeys (Fixture		*fix,
					  gconstpointer	test_data)
{
	GtkHotkey   *hotkey, *fotkey, *botkey;
	GList			*hotkeys;
	GError			*error;
		
	hotkey = gtk_hotkey_registry_get_hotkey (fix->registry,
	                                         TEST_APP"1", TEST_KEY"1",
	                                         TEST_SIGN, NULL);
	fotkey = gtk_hotkey_registry_get_hotkey (fix->registry,
	                                         TEST_APP"1", TEST_KEY"2",
	                                         TEST_SIGN, NULL);
	botkey = gtk_hotkey_registry_get_hotkey (fix->registry,
	                                         TEST_APP"2", TEST_KEY"1",
	                                         TEST_SIGN, NULL);
	
	/* Retrieve all hotkeys */
	error = NULL;
	hotkeys = gtk_hotkey_registry_get_all_hotkeys (fix->registry);
	
	g_assert_cmpint (g_list_length (hotkeys), == , 3);
	g_list_foreach (hotkeys, (GFunc)g_object_unref, NULL);
	g_list_free (hotkeys);
	
	/* Delete the first of the hotkeys */
	error = NULL;
	gtk_hotkey_delete (hotkey, &error);	
	if (error) g_critical ("Failed to delete first hotkey: %s", error->message);
	
	/* Assert that we have two hotkeys left */
	error = NULL;
	hotkeys = gtk_hotkey_registry_get_all_hotkeys (fix->registry);
	
	g_assert_cmpint (g_list_length (hotkeys), == , 2);
	g_list_foreach (hotkeys, (GFunc)g_object_unref, NULL);
	g_list_free (hotkeys);
	
	/* Clean up */
	gtk_hotkey_delete (fotkey, NULL);
	g_assert (!gtk_hotkey_registry_has_hotkey (fix->registry,
											  TEST_APP"1", TEST_KEY"2"));
	
	gtk_hotkey_delete (botkey, NULL);
	g_assert (!gtk_hotkey_registry_has_hotkey (fix->registry,
											  TEST_APP"2", TEST_KEY"1"));
	
	g_object_unref (hotkey);
	g_object_unref (fotkey);
	g_object_unref (botkey);
	
}

int
main (int   argc,
      char *argv[])
{
	g_type_init ();
	g_test_init (&argc, &argv, NULL);

	g_test_bug_base ("http://bugzilla.gnome.org/");
	TEST (emit_updated);
	TEST (emit_deleted);
	TEST (has_non_existing_hotkey);
	TEST (lookup_non_existing_hotkey);
	TEST (save_load_single);
	TEST (hotkey_equals);
	TEST (get_application_hotkeys);
	TEST (get_all_hotkeys);
	
	
  return g_test_run();
}