diff --git a/test/browser/fake_events.js b/test/browser/fake_events.js index 1dba0004a8b9b..59681e366a9a3 100644 --- a/test/browser/fake_events.js +++ b/test/browser/fake_events.js @@ -2,52 +2,59 @@ * Helper function used in browser tests to simulate HTML5 events */ -function simulateKeyEvent(eventType, keyCode, code, key) { +function simulateKeyEvent(eventType, keyCode, code, key, target) { var props = { keyCode, charCode: keyCode, view: window, bubbles: true, cancelable: true }; if (code) props['code'] = code; if (key) props['key'] = key; var event = new KeyboardEvent(eventType, props); - return document.dispatchEvent(event); + if (!target) target = document; + return target.dispatchEvent(event); } -function simulateKeyDown(keyCode, code = undefined, key = undefined) { - var doDefault = simulateKeyEvent('keydown', keyCode, code, key); +function simulateKeyDown(keyCode, code = undefined, key = undefined, target = undefined) { + var doDefault = simulateKeyEvent('keydown', keyCode, code, key, target); // As long as not handler called `preventDefault` we also send a keypress // event. if (doDefault) { - simulateKeyEvent('keypress', keyCode, code, key); + simulateKeyEvent('keypress', keyCode, code, key, target); } } -function simulateKeyUp(keyCode, code = undefined, key = undefined) { - simulateKeyEvent('keyup', keyCode, code, key); +function simulateKeyUp(keyCode, code = undefined, target = undefined) { + simulateKeyEvent('keyup', keyCode, code, target); } -function simulateMouseEvent(x, y, button, absolute) { +function simulateKeyDownUp(keyCode, code = undefined, target = undefined) { + simulateKeyDown(keyCode, code, target); + simulateKeyUp(keyCode, code, target); +} + +function simulateMouseEvent(eventType, x, y, button, absolute) { if (!absolute) { x += Module['canvas'].offsetLeft; y += Module['canvas'].offsetTop; } var event = document.createEvent("MouseEvents"); - if (button >= 0) { - var event1 = document.createEvent("MouseEvents"); - event1.initMouseEvent('mousedown', true, true, window, - 1, x, y, x, y, - 0, 0, 0, 0, - button, null); - Module['canvas'].dispatchEvent(event1); - var event2 = document.createEvent("MouseEvents"); - event2.initMouseEvent('mouseup', true, true, window, - 1, x, y, x, y, - 0, 0, 0, 0, - button, null); - Module['canvas'].dispatchEvent(event2); - } else { - var event1 = document.createEvent("MouseEvents"); - event1.initMouseEvent('mousemove', true, true, window, - 1, x, y, x, y, - 0, 0, 0, 0, - 0, null); - Module['canvas'].dispatchEvent(event1); - } + event.initMouseEvent(eventType, true, true, window, + 1, x, y, x, y, + 0, 0, 0, 0, + button, null); + Module['canvas'].dispatchEvent(event); +} + +function simulateMouseDown(x, y, button, absolute) { + simulateMouseEvent('mousedown', x, y, button, absolute); +} + +function simulateMouseUp(x, y, button, absolute) { + simulateMouseEvent('mouseup', x, y, button, absolute); +} + +function simulateMouseMove(x, y, absolute) { + simulateMouseEvent('mousemove', x, y, 0, absolute); +} + +function simulateMouseClick(x, y, button, absolute) { + simulateMouseDown(x, y, button, absolute); + simulateMouseUp(x, y, button, absolute); } diff --git a/test/canvas_focus.c b/test/browser/test_canvas_focus.c similarity index 74% rename from test/canvas_focus.c rename to test/browser/test_canvas_focus.c index 178ee65d0f699..d0a2133dbb034 100644 --- a/test/canvas_focus.c +++ b/test/browser/test_canvas_focus.c @@ -22,10 +22,8 @@ bool key_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userDat int main() { emscripten_set_keypress_callback("#canvas", 0, 1, key_callback); EM_ASM({ - var event = new KeyboardEvent("keypress", { 'keyCode': 38, 'charCode': 38, 'view': window, 'bubbles': true, 'cancelable': true }); - // Focus, then send an event, same as if the user clicked on it for focus. Module.canvas.focus(); - document.activeElement.dispatchEvent(event); + simulateKeyEvent("keypress", 38, undefined, undefined, /*target=*/document.activeElement); }); emscripten_exit_with_live_runtime(); __builtin_trap(); diff --git a/test/browser/test_glfw_events.c b/test/browser/test_glfw_events.c index 744973af54978..050a0879cdb5d 100644 --- a/test/browser/test_glfw_events.c +++ b/test/browser/test_glfw_events.c @@ -34,51 +34,51 @@ typedef struct { // Javascript event.button 0 = left, 1 = middle, 2 = right test_t g_tests[] = { - { "Module.injectMouseEvent(10.0, 10.0, 'mousedown', 0)", { 1, 10.0, 10.0, GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS, -1 } }, - { "Module.injectMouseEvent(10.0, 20.0, 'mouseup', 0)", { 1, 10.0, 20.0, GLFW_MOUSE_BUTTON_LEFT, GLFW_RELEASE, -1 } }, - { "Module.injectMouseEvent(10.0, 30.0, 'mousedown', 1)", { 1, 10.0, 30.0, GLFW_MOUSE_BUTTON_MIDDLE, GLFW_PRESS, -1 } }, - { "Module.injectMouseEvent(10.0, 40.0, 'mouseup', 1)", { 1, 10.0, 40.0, GLFW_MOUSE_BUTTON_MIDDLE, GLFW_RELEASE, -1 } }, - { "Module.injectMouseEvent(10.0, 30.0, 'mousedown', 2)", { 1, 10.0, 30.0, GLFW_MOUSE_BUTTON_RIGHT, GLFW_PRESS, -1 } }, - { "Module.injectMouseEvent(10.0, 40.0, 'mouseup', 2)", { 1, 10.0, 40.0, GLFW_MOUSE_BUTTON_RIGHT, GLFW_RELEASE, -1 } }, + { "simulateMouseDown(10.0, 10.0, 0)", { 1, 10.0, 10.0, GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS, -1 } }, + { "simulateMouseUp (10.0, 20.0, 0)", { 1, 10.0, 20.0, GLFW_MOUSE_BUTTON_LEFT, GLFW_RELEASE, -1 } }, + { "simulateMouseDown(10.0, 30.0, 1)", { 1, 10.0, 30.0, GLFW_MOUSE_BUTTON_MIDDLE, GLFW_PRESS, -1 } }, + { "simulateMouseUp (10.0, 40.0, 1)", { 1, 10.0, 40.0, GLFW_MOUSE_BUTTON_MIDDLE, GLFW_RELEASE, -1 } }, + { "simulateMouseDown(10.0, 30.0, 2)", { 1, 10.0, 30.0, GLFW_MOUSE_BUTTON_RIGHT, GLFW_PRESS, -1 } }, + { "simulateMouseUp (10.0, 40.0, 2)", { 1, 10.0, 40.0, GLFW_MOUSE_BUTTON_RIGHT, GLFW_RELEASE, -1 } }, //{ "Module.injectMouseEvent(10.0, 50.0, 'mousewheel', 0)", { 10.0, 50.0, -1, -1, -1 } }, //{ "Module.injectMouseEvent(10.0, 60.0, 'mousemove', 0)", { 10.0, 60.0, -1, -1, -1 } } - { "Module.injectKeyEvent('keydown', 8)", { 0, 0.0, 0.0, GLFW_KEY_BACKSPACE, GLFW_PRESS, -1 } }, - { "Module.injectKeyEvent('keyup', 8)", { 0, 0.0, 0.0, GLFW_KEY_BACKSPACE, GLFW_RELEASE, -1 } }, - { "Module.injectKeyEvent('keydown', 9)", { 0, 0.0, 0.0, GLFW_KEY_TAB, GLFW_PRESS, -1 } }, - { "Module.injectKeyEvent('keyup', 9)", { 0, 0.0, 0.0, GLFW_KEY_TAB, GLFW_RELEASE, -1 } }, - { "Module.injectKeyEvent('keydown', 112)", { 0, 0.0, 0.0, GLFW_KEY_F1, GLFW_PRESS, -1 } }, - { "Module.injectKeyEvent('keyup', 112)", { 0, 0.0, 0.0, GLFW_KEY_F1, GLFW_RELEASE, -1 } }, - { "Module.injectKeyEvent('keydown', 37)", { 0, 0.0, 0.0, GLFW_KEY_LEFT, GLFW_PRESS, -1 } }, - { "Module.injectKeyEvent('keyup', 37)", { 0, 0.0, 0.0, GLFW_KEY_LEFT, GLFW_RELEASE, -1 } }, - { "Module.injectKeyEvent('keydown', 39)", { 0, 0.0, 0.0, GLFW_KEY_RIGHT, GLFW_PRESS, -1 } }, - { "Module.injectKeyEvent('keyup', 39)", { 0, 0.0, 0.0, GLFW_KEY_RIGHT, GLFW_RELEASE, -1 } }, - { "Module.injectKeyEvent('keydown', 38)", { 0, 0.0, 0.0, GLFW_KEY_UP, GLFW_PRESS, -1 } }, - { "Module.injectKeyEvent('keyup', 38)", { 0, 0.0, 0.0, GLFW_KEY_UP, GLFW_RELEASE, -1 } }, - { "Module.injectKeyEvent('keydown', 40)", { 0, 0.0, 0.0, GLFW_KEY_DOWN, GLFW_PRESS, -1 } }, - { "Module.injectKeyEvent('keyup', 40)", { 0, 0.0, 0.0, GLFW_KEY_DOWN, GLFW_RELEASE, -1 } }, + { "simulateKeyDown(8)", { 0, 0.0, 0.0, GLFW_KEY_BACKSPACE, GLFW_PRESS, -1 } }, + { "simulateKeyUp (8)", { 0, 0.0, 0.0, GLFW_KEY_BACKSPACE, GLFW_RELEASE, -1 } }, + { "simulateKeyDown(9)", { 0, 0.0, 0.0, GLFW_KEY_TAB, GLFW_PRESS, -1 } }, + { "simulateKeyUp (9)", { 0, 0.0, 0.0, GLFW_KEY_TAB, GLFW_RELEASE, -1 } }, + { "simulateKeyDown(112)", { 0, 0.0, 0.0, GLFW_KEY_F1, GLFW_PRESS, -1 } }, + { "simulateKeyUp (112)", { 0, 0.0, 0.0, GLFW_KEY_F1, GLFW_RELEASE, -1 } }, + { "simulateKeyDown(37)", { 0, 0.0, 0.0, GLFW_KEY_LEFT, GLFW_PRESS, -1 } }, + { "simulateKeyUp (37)", { 0, 0.0, 0.0, GLFW_KEY_LEFT, GLFW_RELEASE, -1 } }, + { "simulateKeyDown(39)", { 0, 0.0, 0.0, GLFW_KEY_RIGHT, GLFW_PRESS, -1 } }, + { "simulateKeyUp (39)", { 0, 0.0, 0.0, GLFW_KEY_RIGHT, GLFW_RELEASE, -1 } }, + { "simulateKeyDown(38)", { 0, 0.0, 0.0, GLFW_KEY_UP, GLFW_PRESS, -1 } }, + { "simulateKeyUp (38)", { 0, 0.0, 0.0, GLFW_KEY_UP, GLFW_RELEASE, -1 } }, + { "simulateKeyDown(40)", { 0, 0.0, 0.0, GLFW_KEY_DOWN, GLFW_PRESS, -1 } }, + { "simulateKeyUp (40)", { 0, 0.0, 0.0, GLFW_KEY_DOWN, GLFW_RELEASE, -1 } }, #if USE_GLFW == 2 - { "Module.injectKeyEvent('keydown', 27)", { 0, 0.0, 0.0, GLFW_KEY_ESC, GLFW_PRESS, -1 } }, - { "Module.injectKeyEvent('keyup', 27)", { 0, 0.0, 0.0, GLFW_KEY_ESC, GLFW_RELEASE, -1 } }, + { "simulateKeyDown(27)", { 0, 0.0, 0.0, GLFW_KEY_ESC, GLFW_PRESS, -1 } }, + { "simulateKeyUp(27)", { 0, 0.0, 0.0, GLFW_KEY_ESC, GLFW_RELEASE, -1 } }, - { "Module.injectKeyEvent('keydown', 65)", { 0, 0.0, 0.0, 'A', GLFW_PRESS, -1, 'A' } }, - { "Module.injectKeyEvent('keypress', 65, {charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, 'A' } }, - { "Module.injectKeyEvent('keyup', 65)", { 0, 0.0, 0.0, 'A', GLFW_RELEASE, -1, 'A' } }, + { "simulateKeyDown(65)", { 0, 0.0, 0.0, 'A', GLFW_PRESS, -1, 'A' } }, + { "simulateKeyEvent('keypress', 65, {charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, 'A' } }, + { "simulateKeyUp(65)", { 0, 0.0, 0.0, 'A', GLFW_RELEASE, -1, 'A' } }, - { "Module.injectKeyEvent('keydown', 65, {ctrlKey: true})", { 0, 0.0, 0.0, 'A', GLFW_PRESS, -1, 'A' } }, - { "Module.injectKeyEvent('keypress', 65, {ctrlKey: true, charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, -1 } }, - { "Module.injectKeyEvent('keyup', 65, {ctrlKey: true})", { 0, 0.0, 0.0, 'A', GLFW_RELEASE, -1, 'A' } }, + { "simulateKeyDown(65, {ctrlKey: true})", { 0, 0.0, 0.0, 'A', GLFW_PRESS, -1, 'A' } }, + { "simulateKeyEvent('keypress', 65, {ctrlKey: true, charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, -1 } }, + { "simulateKeyUp(65, {ctrlKey: true})", { 0, 0.0, 0.0, 'A', GLFW_RELEASE, -1, 'A' } }, #else - { "Module.injectKeyEvent('keydown', 27)", { 0, 0.0, 0.0, GLFW_KEY_ESCAPE, GLFW_PRESS, -1 } }, - { "Module.injectKeyEvent('keyup', 27)", { 0, 0.0, 0.0, GLFW_KEY_ESCAPE, GLFW_RELEASE, -1 } }, + { "simulateKeyDown(27)", { 0, 0.0, 0.0, GLFW_KEY_ESCAPE, GLFW_PRESS, -1 } }, + { "simulateKeyUp(27)", { 0, 0.0, 0.0, GLFW_KEY_ESCAPE, GLFW_RELEASE, -1 } }, - { "Module.injectKeyEvent('keydown', 65)", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_PRESS, -1 } }, - { "Module.injectKeyEvent('keypress', 65, {charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, 'A' } }, - { "Module.injectKeyEvent('keyup', 65)", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_RELEASE, -1 } }, + { "simulateKeyDown(65)", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_PRESS, -1 } }, + { "simulateKeyEvent('keypress', 65, {charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, 'A' } }, + { "simulateKeyUp(65)", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_RELEASE, -1 } }, - { "Module.injectKeyEvent('keydown', 65, {ctrlKey: true})", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_PRESS, -1, 'A' } }, - { "Module.injectKeyEvent('keypress', 65, {ctrlKey: true, charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, -1 } }, - { "Module.injectKeyEvent('keyup', 65, {ctrlKey: true})", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_RELEASE, -1, 'A' } }, + { "simulateKeyDown(65, {ctrlKey: true})", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_PRESS, -1, 'A' } }, + { "simulateKeyEvent('keypress', 65, {ctrlKey: true, charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, -1 } }, + { "simulateKeyUp(65, {ctrlKey: true})", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_RELEASE, -1, 'A' } }, #endif }; @@ -166,40 +166,6 @@ static void on_error(int error, const char *msg) { int main() { unsigned int success = (1 << (sizeof(g_tests) / sizeof(test_t))) - 1; // (2^count)-1; - emscripten_run_script(MULTILINE( - Module.injectMouseEvent = function(x, y, event_, button) { - var canvas = Module['canvas']; - var event = new MouseEvent(event_, { - 'view': window, - 'bubbles': true, - 'cancelable': true, - 'screenX': canvas.offsetLeft + x, - 'screenY': canvas.offsetTop + y, - 'clientX': canvas.offsetLeft + x, - 'clientY': canvas.offsetTop + y, - 'button': button - }); - canvas.dispatchEvent(event); - - //var event = document.createEvent("MouseEvents"); - //var canvas = Module['canvas']; - //event.initMouseEvent(event_, true, true, window, 0, canvas.offsetLeft + x, canvas.offsetTop + y, canvas.offsetLeft + x, canvas.offsetTop + y, 0, 0, 0, 0, button, null); - //canvas.dispatchEvent(event); - }; - - Module.injectKeyEvent = function(type, keyCode, options) { - // KeyboardEvent constructor always returns 0 keyCode on Chrome, so use generic events - //var keyboardEvent = new KeyboardEvent(type, Object.assign({ keyCode: keyCode}, options)); - var keyboardEvent = document.createEventObject ? - document.createEventObject() : document.createEvent('Events'); - keyboardEvent.initEvent(type, true, true); - keyboardEvent.keyCode = keyCode; - keyboardEvent = Object.assign(keyboardEvent, options); - - canvas.dispatchEvent(keyboardEvent); - }; - )); - glfwInit(); #if USE_GLFW == 2 diff --git a/test/browser/test_sdl2_key.c b/test/browser/test_sdl2_key.c index 2db9199a47f67..247192db23dee 100644 --- a/test/browser/test_sdl2_key.c +++ b/test/browser/test_sdl2_key.c @@ -62,13 +62,13 @@ int main(int argc, char **argv) { SDL_StartTextInput(); - emscripten_run_script("simulateKeyDown(38, 'ArrowUp'); simulateKeyUp(38, 'ArrowUp')"); // up - emscripten_run_script("simulateKeyDown(40, 'ArrowDown'); simulateKeyUp(40, 'ArrowDown')"); // down - emscripten_run_script("simulateKeyDown(37, 'ArrowLeft'); simulateKeyUp(37, 'ArrowLeft');"); // left - emscripten_run_script("simulateKeyDown(39, 'ArrowRight');simulateKeyUp(39, 'ArrowRight');"); // right - emscripten_run_script("simulateKeyDown(65, 'KeyA'); simulateKeyUp(65, 'KeyA');"); // a - emscripten_run_script("simulateKeyDown(66, 'KeyB'); simulateKeyUp(66, 'KeyB');"); // b - emscripten_run_script("simulateKeyDown(100, 'Numpad4'); simulateKeyUp(100, 'Numpad4');"); // trigger the end + emscripten_run_script("simulateKeyDownUp(38, 'ArrowUp')"); + emscripten_run_script("simulateKeyDownUp(40, 'ArrowDown')"); + emscripten_run_script("simulateKeyDownUp(37, 'ArrowLeft')"); + emscripten_run_script("simulateKeyDownUp(39, 'ArrowRight')"); + emscripten_run_script("simulateKeyDownUp(65, 'KeyA')"); + emscripten_run_script("simulateKeyDownUp(66, 'KeyB')"); + emscripten_run_script("simulateKeyDownUp(100, 'Numpad4')"); // trigger the end emscripten_set_main_loop(pump_events, 3, 0); return 99; diff --git a/test/browser/test_sdl2_mouse.c b/test/browser/test_sdl2_mouse.c index 1473c300a6dbe..9d5e714a7bd78 100644 --- a/test/browser/test_sdl2_mouse.c +++ b/test/browser/test_sdl2_mouse.c @@ -90,11 +90,11 @@ int main() { int absolute = false; #endif - EM_ASM(simulateMouseEvent(10, 20, -1, $0), absolute); // move from 0,0 to 10,20 - EM_ASM(simulateMouseEvent(10, 20, 0, $0), absolute); // click - EM_ASM(simulateMouseEvent(10, 20, 0, $0), absolute); // click some more, but this one should be ignored through PeepEvent - EM_ASM(simulateMouseEvent(30, 70, -1, $0), absolute); // move some more - EM_ASM(simulateMouseEvent(30, 70, 1, $0), absolute); // trigger the end + EM_ASM(simulateMouseMove(10, 20, $0), absolute); // move from 0,0 to 10,20 + EM_ASM(simulateMouseClick(10, 20, 0, $0), absolute); // click + EM_ASM(simulateMouseClick(10, 20, 0, $0), absolute); // click some more, but this one should be ignored through PeepEvent + EM_ASM(simulateMouseMove(30, 70, $0), absolute); // move some more + EM_ASM(simulateMouseClick(30, 70, 1, $0), absolute); // trigger the end emscripten_set_main_loop(one, 0, 0); } diff --git a/test/browser/test_sdl_mouse.c b/test/browser/test_sdl_mouse.c index 8de4d65fc819a..3c9265780b816 100644 --- a/test/browser/test_sdl_mouse.c +++ b/test/browser/test_sdl_mouse.c @@ -76,11 +76,11 @@ int main() { int absolute = false; #endif - EM_ASM(simulateMouseEvent(10, 20, -1, $0), absolute); // move from 0,0 to 10,20 - EM_ASM(simulateMouseEvent(10, 20, 0, $0), absolute); // click - EM_ASM(simulateMouseEvent(10, 20, 0, $0), absolute); // click some more, but this one should be ignored through PeepEvent - EM_ASM(simulateMouseEvent(30, 77, -1, $0), absolute); // move some more - EM_ASM(simulateMouseEvent(30, 77, 1, $0), absolute); // trigger the end + EM_ASM(simulateMouseMove(10, 20, $0), absolute); // move from 0,0 to 10,20 + EM_ASM(simulateMouseClick(10, 20, 0, $0), absolute); // click + EM_ASM(simulateMouseClick(10, 20, 0, $0), absolute); // click some more, but this one should be ignored through PeepEvent + EM_ASM(simulateMouseMove(30, 77, $0), absolute); // move some more + EM_ASM(simulateMouseClick(30, 77, 1, $0), absolute); // trigger the end emscripten_set_main_loop(one, 0, 0); } diff --git a/test/test_browser.py b/test/test_browser.py index 4bc9e57e8f3e2..c40bbed422674 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -1004,7 +1004,7 @@ def post(): self.btest_exit('test_sdl_key_proxy.c', 223092870, args=['--proxy-to-worker', '--pre-js', 'pre.js', '-lSDL', '-lGL', '-sRUNTIME_DEBUG'], post_build=post) def test_canvas_focus(self): - self.btest_exit('canvas_focus.c') + self.btest_exit('test_canvas_focus.c', args=['--pre-js', test_file('browser/fake_events.js')]) def test_keydown_preventdefault_proxy(self): def post(): @@ -2858,7 +2858,7 @@ def test_glfw3(self, args): }) @requires_graphics_hardware def test_glfw_events(self, args): - self.btest_exit('test_glfw_events.c', args=args + ['-lglfw', '-lGL']) + self.btest_exit('test_glfw_events.c', args=args + ['-lglfw', '-lGL', '--pre-js', test_file('browser/fake_events.js')]) @requires_graphics_hardware def test_glfw3_hi_dpi_aware(self):