1 /* accel.c - test accel parsing
2  * Copyright (C) 2011 Bastien Nocera <hadess@hadess.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include <gtk/gtk.h>
18 #include <locale.h>
19 
20 static void
test_one_accel(const char * accel,GdkModifierType exp_mods,guint exp_key,const char * exp_label,gboolean has_keysym)21 test_one_accel (const char      *accel,
22                 GdkModifierType  exp_mods,
23                 guint            exp_key,
24 		const char      *exp_label,
25 		gboolean         has_keysym)
26 {
27   guint accel_key;
28   GdkModifierType mods;
29   guint *keycodes;
30   char *label, *name;
31   gboolean ret;
32 
33   accel_key = 0;
34   ret = gtk_accelerator_parse_with_keycode (accel,
35                                             gdk_display_get_default (),
36                                             &accel_key,
37                                             &keycodes,
38                                             &mods);
39   g_assert_true (ret);
40 
41   if (has_keysym)
42     {
43       guint accel_key_2;
44       GdkModifierType mods_2;
45 
46       ret = gtk_accelerator_parse (accel, &accel_key_2, &mods_2);
47       g_assert_true (ret);
48       g_assert_true (accel_key == accel_key_2);
49       g_assert_true (mods == mods_2);
50     }
51 
52   if (has_keysym)
53     g_assert_true (accel_key == exp_key);
54   g_assert_true (mods == exp_mods);
55   g_assert_nonnull (keycodes);
56   g_assert_true (keycodes[0] != 0);
57 
58   label = gtk_accelerator_get_label_with_keycode (NULL,
59 						  accel_key,
60 						  *keycodes,
61 						  mods);
62 
63   g_assert_cmpstr (label, ==, exp_label);
64 
65   name = gtk_accelerator_name_with_keycode (NULL,
66 					    accel_key,
67 					    *keycodes,
68 					    mods);
69   g_assert_cmpstr (name, ==, accel);
70 
71   g_free (keycodes);
72   g_free (label);
73   g_free (name);
74 }
75 
76 static void
accel1(void)77 accel1 (void)
78 {
79   test_one_accel ("0xb3", 0, 0xb3, "0xb3", FALSE);
80 }
81 
82 static void
accel2(void)83 accel2 (void)
84 {
85   test_one_accel ("<Control><Alt>z", GDK_CONTROL_MASK|GDK_ALT_MASK, GDK_KEY_z, "Ctrl+Alt+Z", TRUE);
86 }
87 
88 static void
accel3(void)89 accel3 (void)
90 {
91   test_one_accel ("KP_7", 0, GDK_KEY_KP_7, "KP 7", TRUE);
92 }
93 
94 static void
accel4(void)95 accel4 (void)
96 {
97   test_one_accel ("<Control>KP_7", GDK_CONTROL_MASK, GDK_KEY_KP_7, "Ctrl+KP 7", TRUE);
98 }
99 
100 static void
accel5(void)101 accel5 (void)
102 {
103   test_one_accel ("<Shift>exclam", GDK_SHIFT_MASK, GDK_KEY_exclam, "Shift+!", TRUE);
104 }
105 
106 static void
accel6(void)107 accel6 (void)
108 {
109   test_one_accel ("<Hyper>x", GDK_HYPER_MASK, GDK_KEY_x, "Hyper+X", TRUE);
110 }
111 
112 static void
accel7(void)113 accel7 (void)
114 {
115   test_one_accel ("<Super>x", GDK_SUPER_MASK, GDK_KEY_x, "Super+X", TRUE);
116 }
117 
118 static void
accel8(void)119 accel8 (void)
120 {
121   test_one_accel ("<Meta>x", GDK_META_MASK, GDK_KEY_x, "Meta+X", TRUE);
122 }
123 
124 static void
keysyms(void)125 keysyms (void)
126 {
127   g_assert_cmpuint (gdk_keyval_from_name ("KP_7"), ==, GDK_KEY_KP_7);
128 }
129 
130 int
main(int argc,char * argv[])131 main (int   argc,
132       char *argv[])
133 {
134   setlocale (LC_ALL, "en_GB.UTF-8");
135 
136   gtk_test_init (&argc, &argv);
137 
138   g_test_add_func ("/keysyms", keysyms);
139 
140   g_test_add_func ("/accel1", accel1);
141   g_test_add_func ("/accel2", accel2);
142   g_test_add_func ("/accel3", accel3);
143   g_test_add_func ("/accel4", accel4);
144   g_test_add_func ("/accel5", accel5);
145   g_test_add_func ("/accel6", accel6);
146   g_test_add_func ("/accel7", accel7);
147   g_test_add_func ("/accel8", accel8);
148 
149   return g_test_run();
150 }
151