diff --git a/tests/Makefile b/tests/Makefile index e48c9aa..68f5540 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -20,7 +20,9 @@ X11_CFLAGS = $(shell $(PKG_CONFIG) --cflags x11) X11_LIBS = $(shell $(PKG_CONFIG) --libs x11) XIM_CFLAGS = $(shell $(PKG_CONFIG) --cflags xcb-imdkit xcb-aux xcb-xkb xkbcommon-x11) XIM_LIBS = $(shell $(PKG_CONFIG) --libs xcb-imdkit xcb-aux xcb-xkb xkbcommon-x11) -UNIT_LDLIBS = -lthread -lbio $(TEXT_LIBS) $(IBUS_LIBS) $(XIM_LIBS) +WL_CFLAGS = $(shell $(PKG_CONFIG) --cflags wayland-client xkbcommon) +WL_LIBS = $(shell $(PKG_CONFIG) --libs wayland-client xkbcommon) +UNIT_LDLIBS = -lthread -lbio $(TEXT_LIBS) $(IBUS_LIBS) $(XIM_LIBS) $(WL_LIBS) PROG = unit_test STRESS = stress_test @@ -33,10 +35,11 @@ LIVE = $(SMOKE) $(FAULT) TESTSRC = test_util.c str_test.c trie_test.c \ ko_test.c vi_test.c engine_test.c dict_test.c ipc_test.c \ popup_test.c font_test.c ibus_test.c server_test.c compose_test.c \ - xim_adapter_test.c + xim_adapter_test.c wl_adapter_test.c TESTOBJ = $(TESTSRC:.c=.o) +# imv2.c and vkv1.c are wayland-scanner's; the parent Makefile writes them. PARENTSRC = str.c trie.c dict.c ko.c vi.c ipc.c popup_layout.c \ - font.c compose.c + font.c compose.c imv2.c vkv1.c PARENTOBJ = $(PARENTSRC:%.c=unit_%.o) COMMONOBJ = $(TESTOBJ) $(PARENTOBJ) OBJS = unit_test.o stress_test.o $(COMMONOBJ) @@ -92,6 +95,9 @@ gtk_live_test: gtk_live_test.c $(LIVESRC) ../ipc.c ../ipc.h ../strans: $(MAKE) -C .. strans +../imv2.c ../imv2.h ../vkv1.c ../vkv1.h: + $(MAKE) -C .. $(@F) + xim_live_test: xim_live_test.c $(LIVESRC) ../ipc.c ../ipc.h $(HOSTCC) $(CPPFLAGS) -I.. $(X11_CFLAGS) $(HOST_CFLAGS) $(CFLAGS) \ $(LDFLAGS) -o $@ xim_live_test.c live.c ../ipc.c $(X11_LIBS) \ @@ -124,6 +130,8 @@ ibus_test.o: UNIT_CPPFLAGS += $(IBUS_CFLAGS) ibus_test.o: ../ibus.c xim_adapter_test.o: UNIT_CPPFLAGS += $(XIM_CFLAGS) xim_adapter_test.o: ../xim.c +wl_adapter_test.o unit_imv2.o unit_vkv1.o: UNIT_CPPFLAGS += $(WL_CFLAGS) +wl_adapter_test.o: ../wl.c ../imv2.h ../vkv1.h server_test.o: ../srv.c unit_font.o: UNIT_CPPFLAGS += $(TEXT_CFLAGS) unit_compose.o compose_test.o: UNIT_CPPFLAGS += $(IBUS_CFLAGS) diff --git a/tests/test.h b/tests/test.h index 8083c08..6567b3a 100644 --- a/tests/test.h +++ b/tests/test.h @@ -125,3 +125,5 @@ void xim_adapter_callback_cleanup(struct ct*); void xim_adapter_callback_transfer(struct ct*); void xim_adapter_callback_owner_loss(struct ct*); void xim_adapter_commit_encoding(struct ct*); +void wl_modifier_mask(struct ct*); +void wl_forwarded_keys(struct ct*); diff --git a/tests/unit_test.c b/tests/unit_test.c index 08cfd02..7c3ca9f 100644 --- a/tests/unit_test.c +++ b/tests/unit_test.c @@ -145,6 +145,8 @@ static const struct ct_test tests[] = { { "xim/callback-transfer", xim_adapter_callback_transfer }, { "xim/callback-owner-loss", xim_adapter_callback_owner_loss }, { "xim/commit-encoding", xim_adapter_commit_encoding }, + { "wl/modifier-mask", wl_modifier_mask }, + { "wl/forwarded-keys", wl_forwarded_keys }, }; #else static const struct ct_test tests[] = { diff --git a/tests/wl_adapter_test.c b/tests/wl_adapter_test.c new file mode 100644 index 0000000..275b37a --- /dev/null +++ b/tests/wl_adapter_test.c @@ -0,0 +1,106 @@ +#include +#include +#include "vkv1.h" + +/* The virtual keyboard is the only wire call the tested code makes. */ +static int nkeysent; +static void wirekey(struct zwp_virtual_keyboard_v1*, uint32_t, uint32_t, + uint32_t); + +#define zwp_virtual_keyboard_v1_key wirekey + +#include "wl.c" +#include "test.h" + +static void +wirekey(struct zwp_virtual_keyboard_v1 *vkb, uint32_t time, uint32_t code, + uint32_t state) +{ + USED(vkb); + USED(time); + USED(code); + USED(state); + nkeysent++; +} + +/* Only the modifiers are read from it, so nothing else need be here. */ +static char keymaptext[] = + "xkb_keymap {\n" + "xkb_keycodes { = 24; };\n" + "xkb_types { type \"ONE_LEVEL\" {\n" + " modifiers = none; map[none] = 1; level_name[1] = \"Any\"; }; };\n" + "xkb_compatibility { };\n" + "xkb_symbols { key { [ q ] }; };\n" + "};\n"; + +void +wl_modifier_mask(struct ct *t) +{ + struct xkb_context *ctx; + struct xkb_keymap *km; + xkb_mod_mask_t caps, ctrl, shift; + + kstate = nil; + CT_EQ_UINT(t, 0, modmask()); /* before any keymap arrives */ + ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + km = xkb_keymap_new_from_string(ctx, keymaptext, + XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS); + CT_CHECK(t, km != nil); + if(km != nil){ + kstate = xkb_state_new(km); + shift = (xkb_mod_mask_t)1 << + xkb_keymap_mod_get_index(km, XKB_MOD_NAME_SHIFT); + ctrl = (xkb_mod_mask_t)1 << + xkb_keymap_mod_get_index(km, XKB_MOD_NAME_CTRL); + caps = (xkb_mod_mask_t)1 << + xkb_keymap_mod_get_index(km, XKB_MOD_NAME_CAPS); + xkb_state_update_mask(kstate, shift, 0, 0, 0, 0, 0); + CT_EQ_UINT(t, Mshift, modmask()); + xkb_state_update_mask(kstate, shift|ctrl, 0, 0, 0, 0, 0); + CT_EQ_UINT(t, Mshift|Mctrl, modmask()); + /* Caps Lock is not Shift; a Korean key must not shift under it. */ + xkb_state_update_mask(kstate, 0, 0, caps, 0, 0, 0); + CT_EQ_UINT(t, 0, modmask()); + xkb_state_unref(kstate); + kstate = nil; + xkb_keymap_unref(km); + } + xkb_context_unref(ctx); +} + +void +wl_forwarded_keys(struct ct *t) +{ + haskeymap = 1; + memset(sent, 0, sizeof sent); + nkeysent = 0; + + forward(30, WL_KEYBOARD_KEY_STATE_PRESSED); + CT_CHECK(t, issent(30)); + forward(30, WL_KEYBOARD_KEY_STATE_RELEASED); + CT_CHECK(t, !issent(30)); + CT_EQ_INT(t, 2, nkeysent); + + /* A code the bitmap cannot hold is not passed on at all. */ + nkeysent = 0; + forward(Maxcode, WL_KEYBOARD_KEY_STATE_PRESSED); + CT_EQ_INT(t, 0, nkeysent); + + /* Whatever is still down is released, once each and no more. */ + forward(1, WL_KEYBOARD_KEY_STATE_PRESSED); + forward(200, WL_KEYBOARD_KEY_STATE_PRESSED); + nkeysent = 0; + releasekeys(); + CT_EQ_INT(t, 2, nkeysent); + CT_CHECK(t, !issent(1)); + CT_CHECK(t, !issent(200)); + releasekeys(); + CT_EQ_INT(t, 2, nkeysent); + + /* Before a keymap the virtual keyboard must not be touched. */ + haskeymap = 0; + nkeysent = 0; + forward(30, WL_KEYBOARD_KEY_STATE_PRESSED); + CT_EQ_INT(t, 0, nkeysent); + CT_CHECK(t, !issent(30)); +}