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>
38 #include <sys/types.h>
52 * Test a sequence of keysyms, resulting from a sequence of key presses,
53 * against the keysyms they're supposed to generate.
55 * - Each test runs with a clean state.
56 * - Each line in the test is made up of:
57 * + A keycode, given as a KEY_* from linux/input.h.
58 * + A direction - DOWN for press, UP for release, BOTH for
59 * immediate press + release, REPEAT to just get the syms.
60 * + A sequence of keysyms that should result from this keypress.
62 * The vararg format is:
63 * <KEY_*> <DOWN | UP | BOTH> <XKB_KEY_* (zero or more)> <NEXT | FINISH>
65 * See below for examples.
68 test_key_seq_va(struct xkb_keymap *keymap, va_list ap)
70 struct xkb_state *state;
76 const xkb_keysym_t *syms;
78 unsigned int nsyms, i;
81 fprintf(stderr, "----\n");
83 state = xkb_state_new(keymap);
87 kc = va_arg(ap, int) + EVDEV_OFFSET;
90 nsyms = xkb_state_key_get_syms(state, kc, &syms);
92 sym = xkb_state_key_get_one_sym(state, kc);
96 fprintf(stderr, "got %u syms for keycode %u: [", nsyms, kc);
98 if (op == DOWN || op == BOTH)
99 xkb_state_update_key(state, kc, XKB_KEY_DOWN);
100 if (op == UP || op == BOTH)
101 xkb_state_update_key(state, kc, XKB_KEY_UP);
103 for (i = 0; i < nsyms; i++) {
104 keysym = va_arg(ap, int);
105 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
106 fprintf(stderr, "%s%s", (i != 0) ? ", " : "", ksbuf);
108 if (keysym == FINISH || keysym == NEXT) {
109 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
110 fprintf(stderr, "Did not expect keysym: %s.\n", ksbuf);
114 if (keysym != syms[i]) {
115 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
116 fprintf(stderr, "Expected keysym: %s. ", ksbuf);;
117 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
118 fprintf(stderr, "Got keysym: %s.\n", ksbuf);;
124 keysym = va_arg(ap, int);
125 if (keysym != XKB_KEY_NoSymbol) {
126 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
127 fprintf(stderr, "Expected %s, but got no keysyms.\n", ksbuf);
132 fprintf(stderr, "]\n");
134 keysym = va_arg(ap, int);
137 if (keysym == FINISH)
140 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
141 fprintf(stderr, "Expected keysym: %s. Didn't get it.\n", ksbuf);
145 xkb_state_unref(state);
149 xkb_state_unref(state);
154 test_key_seq(struct xkb_keymap *keymap, ...)
159 va_start(ap, keymap);
160 ret = test_key_seq_va(keymap, ap);
167 test_makedir(const char *parent, const char *path)
172 dirname = asprintf_safe("%s/%s", parent, path);
175 err = _mkdir(dirname);
177 err = mkdir(dirname, 0777);
185 test_maketempdir(const char *template)
188 const char *basetmp = getenv("TMP");
189 if (basetmp == NULL) {
190 basetmp = getenv("TEMP");
192 if (basetmp == NULL) {
193 basetmp = getenv("top_builddir");
195 assert(basetmp != NULL);
196 char *tmpdir = asprintf_safe("%s/%s", basetmp, template);
197 assert(tmpdir != NULL);
198 char *tmp = _mktemp(tmpdir);
199 assert(tmp == tmpdir);
200 int ret = _mkdir(tmp);
204 char *tmpdir = asprintf_safe("/tmp/%s", template);
205 assert(tmpdir != NULL);
206 char *tmp = mkdtemp(tmpdir);
207 assert(tmp == tmpdir);
213 test_get_path(const char *path_rel)
218 srcdir = getenv("top_srcdir");
222 if (path_rel[0] == '/')
223 return strdup(path_rel);
225 path = asprintf_safe("%s/test/data%s%s", srcdir,
226 path_rel[0] ? "/" : "", path_rel);
228 fprintf(stderr, "Failed to allocate path for %s\n", path_rel);
235 test_read_file(const char *path_rel)
238 char *ret, *tmp, *path;
239 int fd, count, remaining;
241 path = test_get_path(path_rel);
245 fd = open(path, O_RDONLY);
250 if (fstat(fd, &info) != 0) {
255 ret = malloc(info.st_size + 1);
261 remaining = info.st_size;
263 while ((count = read(fd, tmp, remaining)) > 0) {
267 ret[info.st_size] = '\0';
270 if (remaining != 0) {
279 test_get_context(enum test_context_flags test_flags)
281 enum xkb_context_flags ctx_flags;
282 struct xkb_context *ctx;
285 ctx_flags = XKB_CONTEXT_NO_DEFAULT_INCLUDES;
286 if (test_flags & CONTEXT_ALLOW_ENVIRONMENT_NAMES) {
287 unsetenv("XKB_DEFAULT_RULES");
288 unsetenv("XKB_DEFAULT_MODEL");
289 unsetenv("XKB_DEFAULT_LAYOUT");
290 unsetenv("XKB_DEFAULT_VARIANT");
291 unsetenv("XKB_DEFAULT_OPTIONS");
294 ctx_flags |= XKB_CONTEXT_NO_ENVIRONMENT_NAMES;
297 ctx = xkb_context_new(ctx_flags);
301 path = test_get_path("");
303 xkb_context_unref(ctx);
307 xkb_context_include_path_append(ctx, path);
314 test_compile_file(struct xkb_context *context, const char *path_rel)
316 struct xkb_keymap *keymap;
320 path = test_get_path(path_rel);
324 file = fopen(path, "rb");
326 fprintf(stderr, "Failed to open path: %s\n", path);
330 assert(file != NULL);
332 keymap = xkb_keymap_new_from_file(context, file,
333 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
337 fprintf(stderr, "Failed to compile path: %s\n", path);
342 fprintf(stderr, "Successfully compiled path: %s\n", path);
349 test_compile_string(struct xkb_context *context, const char *string)
351 struct xkb_keymap *keymap;
353 keymap = xkb_keymap_new_from_string(context, string,
354 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
356 fprintf(stderr, "Failed to compile string\n");
364 test_compile_buffer(struct xkb_context *context, const char *buf, size_t len)
366 struct xkb_keymap *keymap;
368 keymap = xkb_keymap_new_from_buffer(context, buf, len,
369 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
371 fprintf(stderr, "Failed to compile keymap from memory buffer\n");
379 test_compile_rules(struct xkb_context *context, const char *rules,
380 const char *model, const char *layout,
381 const char *variant, const char *options)
383 struct xkb_keymap *keymap;
384 struct xkb_rule_names rmlvo = {
385 .rules = isempty(rules) ? NULL : rules,
386 .model = isempty(model) ? NULL : model,
387 .layout = isempty(layout) ? NULL : layout,
388 .variant = isempty(variant) ? NULL : variant,
389 .options = isempty(options) ? NULL : options
392 if (!rules && !model && !layout && !variant && !options)
393 keymap = xkb_keymap_new_from_names(context, NULL, 0);
395 keymap = xkb_keymap_new_from_names(context, &rmlvo, 0);
399 "Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
400 rules, model, layout, variant, options);