Add a test for the results of key sequences This test verifies the core purpose of this library, which is to translate the user's keypresses into keysyms according to the keymap and the XKB specification. The tests emulate a series of key presses, and checks that the resulting keysyms are what we expect. Several of the tests currently fail, and plenty more should be added and maybe split up. It also currently uses an RMLVO keymap, which comes from the xkeyboard-config data set, and whose behaviour may change in the future. So it should probably be changed to use several files of our own, but it's OK for now. Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
diff --git a/Makefile.am b/Makefile.am
index a5d34f5..a8ec13d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -124,7 +124,8 @@ TESTS = \
test/context \
test/rules-file \
test/dump \
- test/stringcomp
+ test/stringcomp \
+ test/keyseq
TESTS_LDADD = libxkbcommon-priv.la
test_xkey_LDADD = $(TESTS_LDADD)
@@ -138,6 +139,7 @@ test_rules_file_CFLAGS = $(AM_CFLAGS) -Wno-declaration-after-statement
test_rules_file_LDADD = $(TESTS_LDADD)
test_dump_LDADD = $(TESTS_LDADD)
test_stringcomp_LDADD = $(TESTS_LDADD)
+test_keyseq_LDADD = $(TESTS_LDADD)
check_PROGRAMS = $(TESTS)
diff --git a/test/.gitignore b/test/.gitignore
index 13df4aa..0879e57 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -10,3 +10,4 @@ context
rules-file
stringcomp
dump
+keyseq
diff --git a/test/keyseq.c b/test/keyseq.c
new file mode 100644
index 0000000..87e4262
--- /dev/null
+++ b/test/keyseq.c
@@ -0,0 +1,277 @@
+/*
+ * Copyright © 2012 Ran Benita <ran234@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <assert.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#include <linux/input.h>
+#include <X11/keysym.h>
+
+#include "xkbcommon/xkbcommon.h"
+
+enum {
+ DOWN,
+ UP,
+ BOTH,
+ NEXT,
+ FINISH,
+};
+
+#define EVDEV_OFFSET 8
+
+/*
+ * Test a sequence of keysyms, resulting from a sequence of key presses,
+ * against the keysyms they're supposed to generate.
+ *
+ * - Each test runs with a clean state.
+ * - Each line in the test is made up of:
+ * + A keycode, given as a KEY_* from linux/input.h.
+ * + A direction - DOWN for press, UP for release, BOTH for
+ * immediate press + release.
+ * + A sequence of keysyms that should result from this keypress.
+ *
+ * The vararg format is:
+ * <KEY_*> <DOWN | UP | BOTH> <XK_* (zero or more)> <NEXT | FINISH>
+ *
+ * See below for examples.
+ */
+static int
+test_key_seq(struct xkb_keymap *keymap, ...)
+{
+ struct xkb_state *state;
+
+ va_list ap;
+ xkb_keycode_t keycode;
+ int op;
+ xkb_keysym_t keysym;
+
+ const xkb_keysym_t *syms;
+ unsigned int nsyms, i;
+ char ksbuf[16];
+
+ state = xkb_state_new(keymap);
+ assert(state);
+
+ va_start(ap, keymap);
+
+ for (;;) {
+ keycode = va_arg(ap, int) + EVDEV_OFFSET;
+ op = va_arg(ap, int);
+
+ nsyms = xkb_key_get_syms(state, keycode, &syms);
+ fprintf(stderr, "got %d syms for key 0x%x: [", nsyms, keycode);
+
+ if (op == DOWN || op == BOTH)
+ xkb_state_update_key(state, keycode, XKB_KEY_DOWN);
+ if (op == UP || op == BOTH)
+ xkb_state_update_key(state, keycode, XKB_KEY_UP);
+
+ for (i = 0; i < nsyms; i++) {
+ keysym = va_arg(ap, int);
+ xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
+ fprintf(stderr, "%s%s", (i != 0) ? ", " : "", ksbuf);
+
+ if (keysym == FINISH || keysym == NEXT) {
+ xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
+ fprintf(stderr, "Did not expect keysym: %s.\n", ksbuf);
+ goto fail;
+ }
+
+ if (keysym != syms[i]) {
+ xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
+ fprintf(stderr, "Expected keysym: %s. ", ksbuf);;
+ xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
+ fprintf(stderr, "Got keysym: %s.\n", ksbuf);;
+ goto fail;
+ }
+ }
+
+ fprintf(stderr, "]\n");
+
+ keysym = va_arg(ap, int);
+ if (keysym == NEXT)
+ continue;
+ if (keysym == FINISH)
+ break;
+
+ xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
+ fprintf(stderr, "Expected keysym: %s. Didn't get it.\n", ksbuf);
+ goto fail;
+ }
+
+ va_end(ap);
+ xkb_state_unref(state);
+ return 1;
+
+fail:
+ va_end(ap);
+ xkb_state_unref(state);
+ return 1;
+}
+
+int
+main(void)
+{
+ struct xkb_context *ctx;
+ struct xkb_keymap *keymap;
+ const struct xkb_rule_names names = {
+ .rules = "evdev",
+ .model = "evdev",
+ .layout = "us,il",
+ .variant = "",
+ .options = "grp:menu_toggle",
+ };
+ char *XXX_remove_me_str;
+
+ ctx = xkb_context_new(0);
+ assert(ctx);
+ keymap = xkb_map_new_from_names(ctx, &names, 0);
+ assert(keymap);
+
+ XXX_remove_me_str = xkb_map_get_as_string(keymap);
+ fprintf(stderr, "keymap is:\n%s\n\n", XXX_remove_me_str);
+
+ assert(test_key_seq(keymap,
+ KEY_H, BOTH, XK_h, NEXT,
+ KEY_E, BOTH, XK_e, NEXT,
+ KEY_L, BOTH, XK_l, NEXT,
+ KEY_L, BOTH, XK_l, NEXT,
+ KEY_O, BOTH, XK_o, FINISH));
+
+ assert(test_key_seq(keymap,
+ KEY_H, BOTH, XK_h, NEXT,
+ KEY_LEFTSHIFT, DOWN, XK_Shift_L, NEXT,
+ KEY_E, BOTH, XK_E, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_LEFTSHIFT, UP, XK_Shift_L, NEXT,
+ KEY_L, BOTH, XK_l, NEXT,
+ KEY_O, BOTH, XK_o, FINISH));
+
+ /* Base modifier cleared on key release... */
+ assert(test_key_seq(keymap,
+ KEY_H, BOTH, XK_h, NEXT,
+ KEY_LEFTSHIFT, DOWN, XK_Shift_L, NEXT,
+ KEY_E, BOTH, XK_E, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_LEFTSHIFT, DOWN, XK_Shift_L, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_O, BOTH, XK_O, FINISH));
+
+ /* ... But only by the keycode that set it. */
+ assert(test_key_seq(keymap,
+ KEY_H, BOTH, XK_h, NEXT,
+ KEY_LEFTSHIFT, DOWN, XK_Shift_L, NEXT,
+ KEY_E, BOTH, XK_E, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_RIGHTSHIFT, UP, XK_Shift_R, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_O, BOTH, XK_O, FINISH));
+
+ /*
+ * A base modifier should only be cleared when no other key affecting
+ * the modifier is down.
+ */
+ assert(test_key_seq(keymap,
+ KEY_H, BOTH, XK_h, NEXT,
+ KEY_LEFTSHIFT, DOWN, XK_Shift_L, NEXT,
+ KEY_E, BOTH, XK_E, NEXT,
+ KEY_RIGHTSHIFT, DOWN, XK_Shift_R, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_RIGHTSHIFT, UP, XK_Shift_R, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_LEFTSHIFT, UP, XK_Shift_L, NEXT,
+ KEY_O, BOTH, XK_o, FINISH));
+
+ /* Group switching / locking. */
+ assert(test_key_seq(keymap,
+ KEY_H, BOTH, XK_h, NEXT,
+ KEY_E, BOTH, XK_e, NEXT,
+ KEY_COMPOSE, BOTH, XK_ISO_Next_Group, NEXT,
+ KEY_K, BOTH, XK_hebrew_lamed, NEXT,
+ KEY_F, BOTH, XK_hebrew_kaph, NEXT,
+ KEY_COMPOSE, BOTH, XK_ISO_Next_Group, NEXT,
+ KEY_O, BOTH, XK_o, FINISH));
+
+ /* Locked modifiers. */
+ assert(test_key_seq(keymap,
+ KEY_CAPSLOCK, BOTH, XK_Caps_Lock, NEXT,
+ KEY_H, BOTH, XK_H, NEXT,
+ KEY_E, BOTH, XK_E, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_O, BOTH, XK_O, FINISH));
+
+ assert(test_key_seq(keymap,
+ KEY_H, BOTH, XK_h, NEXT,
+ KEY_E, BOTH, XK_e, NEXT,
+ KEY_CAPSLOCK, BOTH, XK_Caps_Lock, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_CAPSLOCK, BOTH, XK_Caps_Lock, NEXT,
+ KEY_O, BOTH, XK_o, FINISH));
+
+ assert(test_key_seq(keymap,
+ KEY_H, BOTH, XK_h, NEXT,
+ KEY_CAPSLOCK, DOWN, XK_Caps_Lock, NEXT,
+ KEY_E, BOTH, XK_E, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_CAPSLOCK, UP, XK_Caps_Lock, NEXT,
+ KEY_O, BOTH, XK_O, FINISH));
+
+ assert(test_key_seq(keymap,
+ KEY_H, BOTH, XK_h, NEXT,
+ KEY_E, BOTH, XK_e, NEXT,
+ KEY_CAPSLOCK, UP, XK_Caps_Lock, NEXT,
+ KEY_L, BOTH, XK_l, NEXT,
+ KEY_L, BOTH, XK_l, NEXT,
+ KEY_O, BOTH, XK_o, FINISH));
+
+ /*
+ * A key release affecting a locked modifier should clear it
+ * regardless of the key press.
+ */
+ assert(test_key_seq(keymap,
+ KEY_H, BOTH, XK_h, NEXT,
+ KEY_CAPSLOCK, DOWN, XK_Caps_Lock, NEXT,
+ KEY_E, BOTH, XK_E, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_CAPSLOCK, UP, XK_Caps_Lock, NEXT,
+ KEY_L, BOTH, XK_L, NEXT,
+ KEY_CAPSLOCK, UP, XK_Caps_Lock, NEXT,
+ KEY_O, BOTH, XK_o, FINISH));
+
+ /* Simple Num Lock sanity check. */
+ assert(test_key_seq(keymap,
+ KEY_KP1, BOTH, XK_KP_End, NEXT,
+ KEY_NUMLOCK, BOTH, XK_Num_Lock, NEXT,
+ KEY_KP1, BOTH, XK_KP_1, NEXT,
+ KEY_KP2, BOTH, XK_KP_2, NEXT,
+ KEY_NUMLOCK, BOTH, XK_Num_Lock, NEXT,
+ KEY_KP2, BOTH, XK_KP_Down, FINISH));
+
+ xkb_map_unref(keymap);
+ xkb_context_unref(ctx);
+ return 0;
+}