]> Pileus Git - ~andy/gtk/blob - gtk/tests/accel.c
tests: Check "without keycode" code path too
[~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 accel (void)
75 {
76   test_one_accel ("0xb3", "0xb3", FALSE);
77   test_one_accel ("<Primary><Alt>z", "Ctrl+Alt+Z", TRUE);
78 }
79
80 int
81 main (int   argc,
82       char *argv[])
83 {
84   setlocale (LC_ALL, "en_GB.UTF-8");
85
86   gtk_test_init (&argc, &argv);
87   g_test_add_func ("/accel", accel);
88   return g_test_run();
89 }