From ce230b071be0e3181241dbf5cb58415b53c6e091 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 7 Jan 2018 23:48:16 +0530 Subject: [PATCH] Add tests for SGR protocol encoding --- kitty/data-types.c | 2 ++ kitty/mouse.c | 20 ++++++++++++++++++++ kitty_tests/keys.py | 27 +++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/kitty/data-types.c b/kitty/data-types.c index d4199cdc3..bc27c0639 100644 --- a/kitty/data-types.c +++ b/kitty/data-types.c @@ -185,6 +185,7 @@ extern bool init_state(PyObject *module); extern bool init_keys(PyObject *module); extern bool init_graphics(PyObject *module); extern bool init_shaders(PyObject *module); +extern bool init_mouse(PyObject *module); #ifdef __APPLE__ extern int init_CoreText(PyObject *); extern bool init_cocoa(PyObject *module); @@ -217,6 +218,7 @@ PyInit_fast_data_types(void) { if (!init_keys(m)) return NULL; if (!init_graphics(m)) return NULL; if (!init_shaders(m)) return NULL; + if (!init_mouse(m)) return NULL; #ifdef __APPLE__ if (!init_CoreText(m)) return NULL; if (!init_cocoa(m)) return NULL; diff --git a/kitty/mouse.c b/kitty/mouse.c index 692887027..ef279f185 100644 --- a/kitty/mouse.c +++ b/kitty/mouse.c @@ -385,3 +385,23 @@ scroll_event(double UNUSED xoffset, double yoffset) { } } } + +static PyObject* +test_encode_mouse(PyObject *self UNUSED, PyObject *args) { + unsigned int x, y; + int mouse_tracking_protocol, button, action, mods; + if (!PyArg_ParseTuple(args, "IIiiii", &x, &y, &mouse_tracking_protocol, &button, &action, &mods)) return NULL; + int sz = encode_mouse_event_impl(x, y, mouse_tracking_protocol, button, action, mods); + return PyUnicode_FromStringAndSize(mouse_event_buf, sz); +} + +static PyMethodDef module_methods[] = { + METHODB(test_encode_mouse, METH_VARARGS), + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +bool +init_mouse(PyObject *module) { + if (PyModule_AddFunctions(module, module_methods) != 0) return false; + return true; +} diff --git a/kitty_tests/keys.py b/kitty_tests/keys.py index c45ca8cdf..9e1149805 100644 --- a/kitty_tests/keys.py +++ b/kitty_tests/keys.py @@ -78,3 +78,30 @@ class TestParser(BaseTest): keycode = getattr(defines, 'GLFW_KEY_' + key) base_key = smkx_key_map[keycode] km(modify_key_bytes(base_key, num).decode('ascii')[1:], key) + + def test_encode_mouse_event(self): + PRESS, RELEASE, DRAG, MOVE = range(4) + NORMAL_PROTOCOL, UTF8_PROTOCOL, SGR_PROTOCOL, URXVT_PROTOCOL = range(4) + L, M, R = defines.GLFW_MOUSE_BUTTON_LEFT, defines.GLFW_MOUSE_BUTTON_MIDDLE, defines.GLFW_MOUSE_BUTTON_RIGHT + protocol = SGR_PROTOCOL + + def enc(button=L, action=PRESS, mods=0, x=1, y=1): + return defines.test_encode_mouse(x, y, protocol, button, action, mods) + + self.ae(enc(), '<0;1;1M') + self.ae(enc(action=RELEASE), '<0;1;1m') + self.ae(enc(action=MOVE), '<35;1;1M') + self.ae(enc(action=DRAG), '<32;1;1M') + + self.ae(enc(R), '<2;1;1M') + self.ae(enc(R, action=RELEASE), '<2;1;1m') + self.ae(enc(R, action=DRAG), '<34;1;1M') + + self.ae(enc(M), '<1;1;1M') + self.ae(enc(M, action=RELEASE), '<1;1;1m') + self.ae(enc(M, action=DRAG), '<33;1;1M') + + self.ae(enc(x=1234, y=5678), '<0;1234;5678M') + self.ae(enc(mods=defines.GLFW_MOD_SHIFT), '<4;1;1M') + self.ae(enc(mods=defines.GLFW_MOD_ALT), '<8;1;1M') + self.ae(enc(mods=defines.GLFW_MOD_CONTROL), '<16;1;1M')