]> Pileus Git - ~andy/gtk/blob - gtk/tests/accel.c
d99ebb0298edd7de13ccc894821a0f190517aecf
[~andy/gtk] / gtk / tests / accel.c
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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include <gtk/gtk.h>
20 #include <locale.h>
21
22 static void
23 test_one_accel (const char *accel,
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
32   accel_key = 0;
33   gtk_accelerator_parse_with_keycode (accel,
34                                       &accel_key,
35                                       &keycodes,
36                                       &mods);
37
38   if (has_keysym)
39     {
40       guint accel_key_2;
41       GdkModifierType mods_2;
42
43       gtk_accelerator_parse (accel,
44                              &accel_key_2,
45                              &mods_2);
46       g_assert (accel_key == accel_key_2);
47       g_assert (mods == mods_2);
48     }
49
50   if (has_keysym)
51     g_assert (accel_key != 0);
52   g_assert (keycodes);
53   g_assert (keycodes[0] != 0);
54
55   label = gtk_accelerator_get_label_with_keycode (NULL,
56                                                   accel_key,
57                                                   *keycodes,
58                                                   mods);
59
60   g_assert_cmpstr (label, ==, exp_label);
61
62   name = gtk_accelerator_name_with_keycode (NULL,
63                                             accel_key,
64                                             *keycodes,
65                                             mods);
66   g_assert_cmpstr (name, ==, accel);
67
68   g_free (keycodes);
69   g_free (label);
70   g_free (name);
71 }
72
73 static void
74 accel1 (void)
75 {
76   test_one_accel ("0xb3", "0xb3", FALSE);
77 }
78
79 static void
80 accel2 (void)
81 {
82   test_one_accel ("<Primary><Alt>z", "Ctrl+Alt+Z", TRUE);
83 }
84
85 static void
86 accel3 (void)
87 {
88   test_one_accel ("KP_7", "7", TRUE);
89 }
90
91 static void
92 accel4 (void)
93 {
94   test_one_accel ("<Primary>KP_7", "Ctrl+7", TRUE);
95 }
96
97 static void
98 accel5 (void)
99 {
100   test_one_accel ("<Shift>exclam", "Shift+!", TRUE);
101 }
102
103 static void
104 keysyms (void)
105 {
106   g_assert (gdk_keyval_from_name ("KP_7") == GDK_KEY_KP_7);
107 }
108
109 int
110 main (int   argc,
111       char *argv[])
112 {
113   setlocale (LC_ALL, "en_GB.UTF-8");
114
115   gtk_test_init (&argc, &argv);
116
117   g_test_add_func ("/keysyms", keysyms);
118
119   g_test_add_func ("/accel1", accel1);
120   g_test_add_func ("/accel2", accel2);
121   g_test_add_func ("/accel3", accel3);
122   g_test_add_func ("/accel4", accel4);
123   g_test_add_func ("/accel5", accel5);
124   return g_test_run();
125 }