Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wismill committed Sep 27, 2024
1 parent aab6425 commit 6adac2d
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
30 changes: 30 additions & 0 deletions test/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,33 @@ test_compile_rules(struct xkb_context *context, const char *rules,

return keymap;
}

struct xkb_keymap *
test_compile_rules_with_flags(struct xkb_context *context, const char *rules,
const char *model, const char *layout,
const char *variant, const char *options,
enum xkb_keymap_compile_flags flags)
{
struct xkb_keymap *keymap;
struct xkb_rule_names rmlvo = {
.rules = isempty(rules) ? NULL : rules,
.model = isempty(model) ? NULL : model,
.layout = isempty(layout) ? NULL : layout,
.variant = isempty(variant) ? NULL : variant,
.options = isempty(options) ? NULL : options
};

if (!rules && !model && !layout && !variant && !options)
keymap = xkb_keymap_new_from_names(context, NULL, flags);
else
keymap = xkb_keymap_new_from_names(context, &rmlvo, flags);

if (!keymap) {
fprintf(stderr,
"Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
rules, model, layout, variant, options);
return NULL;
}

return keymap;
}
8 changes: 7 additions & 1 deletion test/data/symbols/awesome
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ xkb_symbols "awesome" {
key <AC01> { [ a, A, Return, Return ] };
key <AC02> { [ s, S, Left] };
key <AC03> { [ d, D, Down] };
key <AC04> { [ f, F, Righ] };
key <AC04> { [ f, F, Right] };
key <AC05> { [ g, G, BackSpace, BackSpace ] };

key <AB05> { [ b, B, Delete, Delete ] };

key <LCTL> {
groupsRedirect=0,
symbols[1] = [ {Control_L, ISO_First_Group } ],
actions[1] = [ {SetMods(modifiers=Control), SetGroup(group=-4) } ]
};
};
76 changes: 76 additions & 0 deletions test/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <stdio.h>
#include <stdlib.h>

#include "evdev-scancodes.h"
#include "test.h"
#include "keymap.h"

Expand Down Expand Up @@ -230,6 +231,80 @@ test_multiple_keysyms_per_level(void)
xkb_context_unref(context);
}

static void
test_multiple_actions_per_level(void)
{
struct xkb_context *context = test_get_context(0);
struct xkb_keymap *keymap;
xkb_keycode_t kc;
int keysyms_count;
const xkb_layout_index_t first_layout = 0;
const xkb_keysym_t *keysyms;
xkb_mod_index_t ctrl;
xkb_layout_index_t layout;
xkb_mod_mask_t base_mods;

assert(context);

keymap = test_compile_rules_with_flags(
context, "evdev", "pc104", "awesome,cz", NULL, "grp:menu_toggle",
XKB_KEYMAP_COMPILE_RANGE_REDIRECT_TO_0);
assert(keymap);

ctrl = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL);

kc = xkb_keymap_key_by_name(keymap, "LCTL");
keysyms_count = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 0, &keysyms);
assert(keysyms_count == 2);
assert(keysyms[0] == XKB_KEY_Control_L);
assert(keysyms[1] == XKB_KEY_ISO_First_Group);

struct xkb_state *state = xkb_state_new(keymap);
assert(state);
layout = xkb_state_key_get_layout(state, KEY_LEFTCTRL + EVDEV_OFFSET);
assert(layout == 0);
xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
base_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_DEPRESSED);
assert(base_mods == (1U << ctrl));
layout = xkb_state_key_get_layout(state, XKB_KEY_2 + EVDEV_OFFSET);
assert(layout == 0);
xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
base_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_DEPRESSED);
assert(base_mods == 0);
layout = xkb_state_key_get_layout(state, XKB_KEY_2 + EVDEV_OFFSET);
assert(layout == 0);
xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_UP);
layout = xkb_state_key_get_layout(state, XKB_KEY_2 + EVDEV_OFFSET);
assert(layout == 1);
xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
base_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_DEPRESSED);
assert(base_mods == (1U << ctrl));
layout = xkb_state_key_get_layout(state, XKB_KEY_2 + EVDEV_OFFSET);
assert(layout == 0);
xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
base_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_DEPRESSED);
assert(base_mods == 0);
layout = xkb_state_key_get_layout(state, XKB_KEY_2 + EVDEV_OFFSET);
assert(layout == 1);
xkb_state_unref(state);

assert(test_key_seq(keymap,
KEY_2, BOTH, XKB_KEY_2, NEXT,
KEY_LEFTCTRL, DOWN, XKB_KEY_Control_L, XKB_KEY_ISO_First_Group, NEXT,
KEY_2, BOTH, XKB_KEY_2, NEXT,
KEY_LEFTCTRL, UP, XKB_KEY_Control_L, XKB_KEY_ISO_First_Group, NEXT,
KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
KEY_2, BOTH, XKB_KEY_ecaron, NEXT,
KEY_LEFTCTRL, DOWN, XKB_KEY_Control_L, XKB_KEY_ISO_First_Group, NEXT,
KEY_2, BOTH, XKB_KEY_2, NEXT,
KEY_LEFTCTRL, UP, XKB_KEY_Control_L, XKB_KEY_ISO_First_Group, NEXT,
KEY_2, BOTH, XKB_KEY_ecaron, FINISH));

xkb_keymap_unref(keymap);
xkb_context_unref(context);
}

int
main(void)
{
Expand All @@ -239,6 +314,7 @@ main(void)
test_keymap();
test_numeric_keysyms();
test_multiple_keysyms_per_level();
test_multiple_actions_per_level();

return 0;
}
5 changes: 5 additions & 0 deletions test/stringcomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ main(int argc, char *argv[])
/* Now test that the dump of the dump is equal to the dump! */
dump2 = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
assert(dump2);
/* FIXME remove debug */
if (!streq(dump, dump2)) {
fprintf(stderr, "*** dump (%lu) ***\n%s\n", strlen(dump), dump);
fprintf(stderr, "*** dump2 (%lu) ***\n%s\n", strlen(dump2), dump2);
}
assert(streq(dump, dump2));

/* Test response to invalid formats and flags. */
Expand Down
5 changes: 5 additions & 0 deletions test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ test_compile_rules(struct xkb_context *context, const char *rules,
const char *model, const char *layout, const char *variant,
const char *options);

struct xkb_keymap *
test_compile_rules_with_flags(struct xkb_context *context, const char *rules,
const char *model, const char *layout,
const char *variant, const char *options,
enum xkb_keymap_compile_flags flags);

#ifdef _WIN32
#define setenv(varname, value, overwrite) _putenv_s((varname), (value))
Expand Down

0 comments on commit 6adac2d

Please sign in to comment.