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>
36 #include <sys/types.h>
42 * Test a sequence of keysyms, resulting from a sequence of key presses,
43 * against the keysyms they're supposed to generate.
45 * - Each test runs with a clean state.
46 * - Each line in the test is made up of:
47 * + A keycode, given as a KEY_* from linux/input.h.
48 * + A direction - DOWN for press, UP for release, BOTH for
49 * immediate press + release, REPEAT to just get the syms.
50 * + A sequence of keysyms that should result from this keypress.
52 * The vararg format is:
53 * <KEY_*> <DOWN | UP | BOTH> <XKB_KEY_* (zero or more)> <NEXT | FINISH>
55 * See below for examples.
58 test_key_seq(struct xkb_keymap *keymap, ...)
60 struct xkb_state *state;
67 const xkb_keysym_t *syms;
68 unsigned int nsyms, i;
71 fprintf(stderr, "----\n");
73 state = xkb_state_new(keymap);
79 kc = va_arg(ap, int) + EVDEV_OFFSET;
82 nsyms = xkb_state_key_get_syms(state, kc, &syms);
83 fprintf(stderr, "got %d syms for key 0x%x: [", nsyms, kc);
85 if (op == DOWN || op == BOTH)
86 xkb_state_update_key(state, kc, XKB_KEY_DOWN);
87 if (op == UP || op == BOTH)
88 xkb_state_update_key(state, kc, XKB_KEY_UP);
90 for (i = 0; i < nsyms; i++) {
91 keysym = va_arg(ap, int);
92 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
93 fprintf(stderr, "%s%s", (i != 0) ? ", " : "", ksbuf);
95 if (keysym == FINISH || keysym == NEXT) {
96 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
97 fprintf(stderr, "Did not expect keysym: %s.\n", ksbuf);
101 if (keysym != syms[i]) {
102 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
103 fprintf(stderr, "Expected keysym: %s. ", ksbuf);;
104 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
105 fprintf(stderr, "Got keysym: %s.\n", ksbuf);;
110 fprintf(stderr, "]\n");
112 keysym = va_arg(ap, int);
115 if (keysym == FINISH)
118 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
119 fprintf(stderr, "Expected keysym: %s. Didn't get it.\n", ksbuf);
124 xkb_state_unref(state);
129 xkb_state_unref(state);
134 test_get_path(const char *path_rel)
136 static char path[PATH_MAX];
137 const char *srcdir = getenv("srcdir");
139 snprintf(path, PATH_MAX - 1,
140 "%s/test/data/%s", srcdir ? srcdir : ".",
141 path_rel ? path_rel : "");
147 test_read_file(const char *path_rel)
151 int fd, count, remaining;
153 fd = open(test_get_path(path_rel), O_RDONLY);
157 if (fstat(fd, &info) != 0) {
162 ret = malloc(info.st_size + 1);
168 remaining = info.st_size;
170 while ((count = read(fd, tmp, remaining))) {
174 ret[info.st_size] = '\0';
177 if (remaining != 0) {
186 test_get_context(enum test_context_flags test_flags)
188 struct xkb_context *ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
193 xkb_context_include_path_append(ctx, test_get_path(""));
199 test_compile_file(struct xkb_context *context, const char *path_rel)
201 struct xkb_keymap *keymap;
203 const char *path = test_get_path(path_rel);
205 file = fopen(path, "r");
207 fprintf(stderr, "Failed to open path: %s\n", path);
210 assert(file != NULL);
212 keymap = xkb_keymap_new_from_file(context, file,
213 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
217 fprintf(stderr, "Failed to compile path: %s\n", path);
221 fprintf(stderr, "Successfully compiled path: %s\n", path);
227 test_compile_string(struct xkb_context *context, const char *string)
229 struct xkb_keymap *keymap;
231 keymap = xkb_keymap_new_from_string(context, string,
232 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
234 fprintf(stderr, "Failed to compile string\n");
242 test_compile_rules(struct xkb_context *context, const char *rules,
243 const char *model, const char *layout,
244 const char *variant, const char *options)
246 struct xkb_keymap *keymap;
247 struct xkb_rule_names rmlvo = {
255 keymap = xkb_keymap_new_from_names(context, &rmlvo, 0);
258 "Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
259 rules, model, layout, variant, options);