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 |
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 (accelerator: accel, |
35 | display: gdk_display_get_default (), |
36 | accelerator_key: &accel_key, |
37 | accelerator_codes: &keycodes, |
38 | accelerator_mods: &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 (accelerator: accel, accelerator_key: &accel_key_2, accelerator_mods: &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 | accelerator_key: accel_key, |
60 | keycode: *keycodes, |
61 | accelerator_mods: mods); |
62 | |
63 | g_assert_cmpstr (label, ==, exp_label); |
64 | |
65 | name = gtk_accelerator_name_with_keycode (NULL, |
66 | accelerator_key: accel_key, |
67 | keycode: *keycodes, |
68 | accelerator_mods: mods); |
69 | g_assert_cmpstr (name, ==, accel); |
70 | |
71 | g_free (mem: keycodes); |
72 | g_free (mem: label); |
73 | g_free (mem: name); |
74 | } |
75 | |
76 | static void |
77 | accel1 (void) |
78 | { |
79 | test_one_accel (accel: "0xb3" , exp_mods: 0, exp_key: 0xb3, exp_label: "0xb3" , FALSE); |
80 | } |
81 | |
82 | static void |
83 | accel2 (void) |
84 | { |
85 | test_one_accel (accel: "<Control><Alt>z" , exp_mods: GDK_CONTROL_MASK|GDK_ALT_MASK, GDK_KEY_z, exp_label: "Ctrl+Alt+Z" , TRUE); |
86 | } |
87 | |
88 | static void |
89 | accel3 (void) |
90 | { |
91 | test_one_accel (accel: "KP_7" , exp_mods: 0, GDK_KEY_KP_7, exp_label: "KP 7" , TRUE); |
92 | } |
93 | |
94 | static void |
95 | accel4 (void) |
96 | { |
97 | test_one_accel (accel: "<Control>KP_7" , exp_mods: GDK_CONTROL_MASK, GDK_KEY_KP_7, exp_label: "Ctrl+KP 7" , TRUE); |
98 | } |
99 | |
100 | static void |
101 | accel5 (void) |
102 | { |
103 | test_one_accel (accel: "<Shift>exclam" , exp_mods: GDK_SHIFT_MASK, GDK_KEY_exclam, exp_label: "Shift+!" , TRUE); |
104 | } |
105 | |
106 | static void |
107 | accel6 (void) |
108 | { |
109 | test_one_accel (accel: "<Hyper>x" , exp_mods: GDK_HYPER_MASK, GDK_KEY_x, exp_label: "Hyper+X" , TRUE); |
110 | } |
111 | |
112 | static void |
113 | accel7 (void) |
114 | { |
115 | test_one_accel (accel: "<Super>x" , exp_mods: GDK_SUPER_MASK, GDK_KEY_x, exp_label: "Super+X" , TRUE); |
116 | } |
117 | |
118 | static void |
119 | accel8 (void) |
120 | { |
121 | test_one_accel (accel: "<Meta>x" , exp_mods: GDK_META_MASK, GDK_KEY_x, exp_label: "Meta+X" , TRUE); |
122 | } |
123 | |
124 | static void |
125 | keysyms (void) |
126 | { |
127 | g_assert_cmpuint (gdk_keyval_from_name ("KP_7" ), ==, GDK_KEY_KP_7); |
128 | } |
129 | |
130 | int |
131 | main (int argc, |
132 | char *argv[]) |
133 | { |
134 | setlocale (LC_ALL, locale: "en_GB.UTF-8" ); |
135 | |
136 | gtk_test_init (argcp: &argc, argvp: &argv); |
137 | |
138 | g_test_add_func (testpath: "/keysyms" , test_func: keysyms); |
139 | |
140 | g_test_add_func (testpath: "/accel1" , test_func: accel1); |
141 | g_test_add_func (testpath: "/accel2" , test_func: accel2); |
142 | g_test_add_func (testpath: "/accel3" , test_func: accel3); |
143 | g_test_add_func (testpath: "/accel4" , test_func: accel4); |
144 | g_test_add_func (testpath: "/accel5" , test_func: accel5); |
145 | g_test_add_func (testpath: "/accel6" , test_func: accel6); |
146 | g_test_add_func (testpath: "/accel7" , test_func: accel7); |
147 | g_test_add_func (testpath: "/accel8" , test_func: accel8); |
148 | |
149 | return g_test_run(); |
150 | } |
151 | |