2 * Copyright (C) 2012 Red Hat, Inc12 Red Hat, Inc
3 * Authors: Matthias Clasen
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 #include <gdk/gdkkeysyms.h>
21 #include "../gtkkeyhash.h"
22 #include "../gtkprivate.h"
27 counting_destroy (gpointer data)
39 hash = _gtk_key_hash_new (gdk_keymap_get_default (), counting_destroy);
41 keys = _gtk_key_hash_lookup (hash, 0, 0, 0, 0);
42 g_assert (keys == NULL);
44 _gtk_key_hash_add_entry (hash, 1, 0, NULL);
45 _gtk_key_hash_add_entry (hash, 1, 1, NULL);
46 _gtk_key_hash_add_entry (hash, 2, 0, NULL);
47 _gtk_key_hash_add_entry (hash, 3, 0, NULL);
48 _gtk_key_hash_add_entry (hash, 4, 0, NULL);
50 _gtk_key_hash_free (hash);
52 g_assert_cmpint (count, ==, 5);
60 GdkModifierType modifiers;
64 test_lookup (GtkKeyHash *hash,
66 GdkModifierType modifiers,
78 gdk_keymap_get_entries_for_keyval (gdk_keymap_get_default (), keyval, &keys, &n_keys);
82 res = _gtk_key_hash_lookup (hash, keys[0].keycode, modifiers, mask, keys[0].group);
85 g_assert_cmpint (g_slist_length (res), ==, n_results);
87 va_start (ap, n_results);
88 for (i = 0, l = res; i < n_results; i++, l = l->next)
91 g_assert_cmpint (d, ==, GPOINTER_TO_INT (l->data));
99 add_entries (GtkKeyHash *hash,
104 for (i = 0; entries[i].keyval; i++)
105 _gtk_key_hash_add_entry (hash, entries[i].keyval, entries[i].modifiers, GINT_TO_POINTER (i+1));
108 #define DEFAULT_MASK (GDK_CONTROL_MASK \
119 static Entry entries[] = {
120 { GDK_KEY_a, GDK_CONTROL_MASK },
121 { GDK_KEY_a, GDK_CONTROL_MASK|GDK_SHIFT_MASK } ,
122 { GDK_KEY_b, GDK_MOD1_MASK|GDK_CONTROL_MASK },
127 hash = _gtk_key_hash_new (gdk_keymap_get_default (), NULL);
128 add_entries (hash, entries);
130 test_lookup (hash, GDK_KEY_a, GDK_CONTROL_MASK, DEFAULT_MASK, 4, 1, 1, 2, 2);
131 test_lookup (hash, GDK_KEY_A, GDK_CONTROL_MASK, DEFAULT_MASK, 4, 1, 1, 2, 2);
132 test_lookup (hash, GDK_KEY_a, GDK_MOD1_MASK, DEFAULT_MASK, 0);
133 test_lookup (hash, GDK_KEY_F10, 0, DEFAULT_MASK, 4, 4, 4, 4, 4);
134 test_lookup (hash, GDK_KEY_F10, GDK_SHIFT_MASK, DEFAULT_MASK, 4, 4, 4, 4, 4);
136 _gtk_key_hash_free (hash);
140 hyper_equals_super (void)
142 GdkModifierType mods1, mods2;
144 mods1 = GDK_HYPER_MASK;
145 gdk_keymap_map_virtual_modifiers (gdk_keymap_get_default (), &mods1);
146 mods1 = mods1 & ~GDK_HYPER_MASK;
147 mods2 = GDK_SUPER_MASK;
148 gdk_keymap_map_virtual_modifiers (gdk_keymap_get_default (), &mods2);
149 mods2 = mods2 & ~GDK_SUPER_MASK;
151 return mods1 == mods2;
158 static Entry entries[] = {
159 { GDK_KEY_a, GDK_SUPER_MASK },
160 { GDK_KEY_b, GDK_HYPER_MASK } ,
161 { GDK_KEY_c, GDK_META_MASK },
162 { GDK_KEY_d, GDK_SUPER_MASK|GDK_HYPER_MASK },
166 hash = _gtk_key_hash_new (gdk_keymap_get_default (), NULL);
167 add_entries (hash, entries);
169 test_lookup (hash, GDK_KEY_a, GDK_SUPER_MASK, DEFAULT_MASK, 2, 1, 1);
170 test_lookup (hash, GDK_KEY_a, GDK_HYPER_MASK, DEFAULT_MASK, 0);
171 test_lookup (hash, GDK_KEY_b, GDK_HYPER_MASK, DEFAULT_MASK, 2, 2, 2);
172 test_lookup (hash, GDK_KEY_c, GDK_META_MASK, DEFAULT_MASK, 2, 3, 3);
173 if (hyper_equals_super ())
175 GdkModifierType mods;
177 /* test that colocated virtual modifiers don't count twice */
178 test_lookup (hash, GDK_KEY_d, GDK_SUPER_MASK, DEFAULT_MASK, 0);
179 test_lookup (hash, GDK_KEY_d, GDK_HYPER_MASK, DEFAULT_MASK, 0);
181 mods = GDK_HYPER_MASK;
182 gdk_keymap_map_virtual_modifiers (gdk_keymap_get_default (), &mods);
183 test_lookup (hash, GDK_KEY_d, mods, DEFAULT_MASK, 0);
186 _gtk_key_hash_free (hash);
191 main (int argc, char **argv)
193 /* initialize test program */
194 gtk_test_init (&argc, &argv);
196 g_test_add_func ("/keyhash/basic", test_basic);
198 /* FIXME: need to make these independent of xkb configuration */
199 g_test_add_func ("/keyhash/match", test_match);
200 g_test_add_func ("/keyhash/virtual", test_virtual);