2 * Copyright © 2009 Dan Nicholson <dbn.lists@gmail.com>
3 * Copyright © 2012 Intel Corporation
4 * Copyright © 2012 Ran Benita <ran234@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Except as contained in this notice, the names of the authors or their
24 * institutions shall not be used in advertising or otherwise to promote the
25 * sale, use or other dealings in this Software without prior written
26 * authorization from the authors.
28 * Author: Dan Nicholson <dbn.lists@gmail.com>
29 * Daniel Stone <daniel@fooishbar.org>
30 * Ran Benita <ran234@gmail.com>
37 #include <sys/types.h>
51 * Test a sequence of keysyms, resulting from a sequence of key presses,
52 * against the keysyms they're supposed to generate.
54 * - Each test runs with a clean state.
55 * - Each line in the test is made up of:
56 * + A keycode, given as a KEY_* from linux/input.h.
57 * + A direction - DOWN for press, UP for release, BOTH for
58 * immediate press + release, REPEAT to just get the syms.
59 * + A sequence of keysyms that should result from this keypress.
61 * The vararg format is:
62 * <KEY_*> <DOWN | UP | BOTH> <XKB_KEY_* (zero or more)> <NEXT | FINISH>
64 * See below for examples.
67 test_key_seq_va(struct xkb_keymap *keymap, va_list ap)
69 struct xkb_state *state;
75 const xkb_keysym_t *syms;
77 unsigned int nsyms, i;
80 fprintf(stderr, "----\n");
82 state = xkb_state_new(keymap);
86 kc = va_arg(ap, int) + EVDEV_OFFSET;
89 nsyms = xkb_state_key_get_syms(state, kc, &syms);
91 sym = xkb_state_key_get_one_sym(state, kc);
95 fprintf(stderr, "got %u syms for keycode %u: [", nsyms, kc);
97 if (op == DOWN || op == BOTH)
98 xkb_state_update_key(state, kc, XKB_KEY_DOWN);
99 if (op == UP || op == BOTH)
100 xkb_state_update_key(state, kc, XKB_KEY_UP);
102 for (i = 0; i < nsyms; i++) {
103 keysym = va_arg(ap, int);
104 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
105 fprintf(stderr, "%s%s", (i != 0) ? ", " : "", ksbuf);
107 if (keysym == FINISH || keysym == NEXT) {
108 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
109 fprintf(stderr, "Did not expect keysym: %s.\n", ksbuf);
113 if (keysym != syms[i]) {
114 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
115 fprintf(stderr, "Expected keysym: %s. ", ksbuf);;
116 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
117 fprintf(stderr, "Got keysym: %s.\n", ksbuf);;
123 keysym = va_arg(ap, int);
124 if (keysym != XKB_KEY_NoSymbol) {
125 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
126 fprintf(stderr, "Expected %s, but got no keysyms.\n", ksbuf);
131 fprintf(stderr, "]\n");
133 keysym = va_arg(ap, int);
136 if (keysym == FINISH)
139 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
140 fprintf(stderr, "Expected keysym: %s. Didn't get it.\n", ksbuf);
144 xkb_state_unref(state);
148 xkb_state_unref(state);
153 test_key_seq(struct xkb_keymap *keymap, ...)
158 va_start(ap, keymap);
159 ret = test_key_seq_va(keymap, ap);
166 test_get_path(const char *path_rel)
171 srcdir = getenv("top_srcdir");
175 if (path_rel[0] == '/')
176 return strdup(path_rel);
178 path = asprintf_safe("%s/test/data%s%s", srcdir,
179 path_rel[0] ? "/" : "", path_rel);
181 fprintf(stderr, "Failed to allocate path for %s\n", path_rel);
188 test_read_file(const char *path_rel)
191 char *ret, *tmp, *path;
192 int fd, count, remaining;
194 path = test_get_path(path_rel);
198 fd = open(path, O_RDONLY);
203 if (fstat(fd, &info) != 0) {
208 ret = malloc(info.st_size + 1);
214 remaining = info.st_size;
216 while ((count = read(fd, tmp, remaining))) {
220 ret[info.st_size] = '\0';
223 if (remaining != 0) {
232 test_get_context(enum test_context_flags test_flags)
234 enum xkb_context_flags ctx_flags;
235 struct xkb_context *ctx;
238 ctx_flags = XKB_CONTEXT_NO_DEFAULT_INCLUDES;
239 if (test_flags & CONTEXT_ALLOW_ENVIRONMENT_NAMES) {
240 unsetenv("XKB_DEFAULT_RULES");
241 unsetenv("XKB_DEFAULT_MODEL");
242 unsetenv("XKB_DEFAULT_LAYOUT");
243 unsetenv("XKB_DEFAULT_VARIANT");
244 unsetenv("XKB_DEFAULT_OPTIONS");
247 ctx_flags |= XKB_CONTEXT_NO_ENVIRONMENT_NAMES;
250 ctx = xkb_context_new(ctx_flags);
254 path = test_get_path("");
256 xkb_context_unref(ctx);
260 xkb_context_include_path_append(ctx, path);
267 test_compile_file(struct xkb_context *context, const char *path_rel)
269 struct xkb_keymap *keymap;
273 path = test_get_path(path_rel);
277 file = fopen(path, "rb");
279 fprintf(stderr, "Failed to open path: %s\n", path);
283 assert(file != NULL);
285 keymap = xkb_keymap_new_from_file(context, file,
286 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
290 fprintf(stderr, "Failed to compile path: %s\n", path);
295 fprintf(stderr, "Successfully compiled path: %s\n", path);
302 test_compile_string(struct xkb_context *context, const char *string)
304 struct xkb_keymap *keymap;
306 keymap = xkb_keymap_new_from_string(context, string,
307 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
309 fprintf(stderr, "Failed to compile string\n");
317 test_compile_buffer(struct xkb_context *context, const char *buf, size_t len)
319 struct xkb_keymap *keymap;
321 keymap = xkb_keymap_new_from_buffer(context, buf, len,
322 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
324 fprintf(stderr, "Failed to compile keymap from memory buffer\n");
332 test_compile_rules(struct xkb_context *context, const char *rules,
333 const char *model, const char *layout,
334 const char *variant, const char *options)
336 struct xkb_keymap *keymap;
337 struct xkb_rule_names rmlvo = {
338 .rules = isempty(rules) ? NULL : rules,
339 .model = isempty(model) ? NULL : model,
340 .layout = isempty(layout) ? NULL : layout,
341 .variant = isempty(variant) ? NULL : variant,
342 .options = isempty(options) ? NULL : options
345 if (!rules && !model && !layout && !variant && !options)
346 keymap = xkb_keymap_new_from_names(context, NULL, 0);
348 keymap = xkb_keymap_new_from_names(context, &rmlvo, 0);
352 "Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
353 rules, model, layout, variant, options);