Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions test/browser/fake_events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Helper function used in browser tests to simulate HTML5 events
*/

function simulateKeyEvent(eventType, keyCode, code) {
var props = { keyCode, charCode: keyCode, view: window, bubbles: true, cancelable: true };
if (code) props['code'] = code;
var event = new KeyboardEvent(eventType, props);
return document.dispatchEvent(event);
}

function simulateKeyDown(keyCode, code = undefined) {
var doDefault = simulateKeyEvent('keydown', keyCode, code);
// As long as not handler called `preventDefault` we also send a keypress
// event.
if (doDefault) {
simulateKeyEvent('keypress', keyCode, code);
}
}

function simulateKeyUp(keyCode, code = undefined) {
simulateKeyEvent('keyup', keyCode, code);
}

function simulateMouseEvent(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);
}
}
14 changes: 7 additions & 7 deletions test/browser/test_sdl2_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ int main(int argc, char **argv) {

SDL_StartTextInput();

emscripten_run_script("keydown(38, 'ArrowUp'); keyup(38, 'ArrowUp')"); // up
emscripten_run_script("keydown(40, 'ArrowDown'); keyup(40, 'ArrowDown')"); // down
emscripten_run_script("keydown(37, 'ArrowLeft'); keyup(37, 'ArrowLeft');"); // left
emscripten_run_script("keydown(39, 'ArrowRight');keyup(39, 'ArrowRight');"); // right
emscripten_run_script("keydown(65, 'KeyA'); keyup(65, 'KeyA');"); // a
emscripten_run_script("keydown(66, 'KeyB'); keyup(66, 'KeyB');"); // b
emscripten_run_script("keydown(100, 'Numpad4'); keyup(100, 'Numpad4');"); // trigger the end
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_set_main_loop(pump_events, 3, 0);
return 99;
Expand Down
16 changes: 11 additions & 5 deletions test/browser/test_sdl2_mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,17 @@ int main() {
SDL_Rect rect = { 0, 0, 600, 450 };
SDL_RenderFillRect(renderer, &rect);

emscripten_run_script("simulateMouseEvent(10, 20, -1)"); // move from 0,0 to 10,20
emscripten_run_script("simulateMouseEvent(10, 20, 0)"); // click
emscripten_run_script("simulateMouseEvent(10, 20, 0)"); // click some more, but this one should be ignored through PeepEvent
emscripten_run_script("simulateMouseEvent(30, 70, -1)"); // move some more
emscripten_run_script("simulateMouseEvent(30, 70, 1)"); // trigger the end
#ifdef TEST_SDL_MOUSE_OFFSETS
int absolute = true;
#else
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

emscripten_set_main_loop(one, 0, 0);
}
6 changes: 3 additions & 3 deletions test/browser/test_sdl2_pumpevents.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ int main(int argc, char *argv[])
SDL_Window *window;
SDL_CreateWindow("window", 0, 0, 600, 450, 0);

emscripten_run_script("keydown(37, 'ArrowLeft');"); // left
emscripten_run_script("simulateKeyDown(37, 'ArrowLeft');"); // left
loop1();
emscripten_run_script("keydown(39, 'ArrowRight');"); // right
emscripten_run_script("simulateKeyDown(39, 'ArrowRight');"); // right
loop2();
emscripten_run_script("keydown(65, 'KeyA');"); // A
emscripten_run_script("simulateKeyDown(65, 'KeyA');"); // A
alphakey();
return 0;
}
4 changes: 2 additions & 2 deletions test/browser/test_sdl2_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ int main() {
SDL_CreateWindow("window", 0, 0, 600, 450, 0);
SDL_StartTextInput();

emscripten_run_script("simulateKeyEvent('a'.charCodeAt(0))"); // a
emscripten_run_script("simulateKeyEvent('A'.charCodeAt(0))"); // A
emscripten_run_script("simulateKeyDown('a'.charCodeAt(0))");
emscripten_run_script("simulateKeyDown('A'.charCodeAt(0))");

one();

Expand Down
16 changes: 11 additions & 5 deletions test/browser/test_sdl_mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@ int main() {
SDL_Rect rect = { 0, 0, 600, 450 };
SDL_FillRect(screen, &rect, 0x2244ffff);

emscripten_run_script("simulateMouseEvent(10, 20, -1)"); // move from 0,0 to 10,20
emscripten_run_script("simulateMouseEvent(10, 20, 0)"); // click
emscripten_run_script("simulateMouseEvent(10, 20, 0)"); // click some more, but this one should be ignored through PeepEvent
emscripten_run_script("simulateMouseEvent(30, 77, -1)"); // move some more
emscripten_run_script("simulateMouseEvent(30, 77, 1)"); // trigger the end
#ifdef TEST_SDL_MOUSE_OFFSETS
int absolute = true;
#else
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

emscripten_set_main_loop(one, 0, 0);
}
Expand Down
6 changes: 3 additions & 3 deletions test/browser/test_sdl_pumpevents.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(600, 400, 32, SDL_SWSURFACE);

emscripten_run_script("keydown(37);"); // left
emscripten_run_script("simulateKeyDown(37);"); // left
loop1();
emscripten_run_script("keydown(39);"); // right
emscripten_run_script("simulateKeyDown(39);"); // right
loop2();
emscripten_run_script("keydown(65);"); // A
emscripten_run_script("simulateKeyDown(65);"); // A
alphakey();
return 0;
}
6 changes: 3 additions & 3 deletions test/browser/test_sdl_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ int main() {
SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);
SDL_StartTextInput();

emscripten_run_script("simulateKeyEvent('a'.charCodeAt(0))"); // a
emscripten_run_script("simulateKeyEvent('A'.charCodeAt(0))"); // A
//
emscripten_run_script("simulateKeyDown('a'.charCodeAt(0))");
emscripten_run_script("simulateKeyDown('A'.charCodeAt(0))");

emscripten_exit_with_live_runtime();
}
9 changes: 2 additions & 7 deletions test/hello_world_gles_shell.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<hr>
<div id='output'></div>
<hr>
<script type='text/javascript' src='fake_events.js'></script>
<script type='text/javascript'>
/**
* TODO: Encapsulate this part in a reusable token such as
Expand All @@ -26,19 +27,13 @@
};

// Test code
function simulateKeyEvent(keyCode) {
var event = new KeyboardEvent("KeyboardEvent", {
code: keyCode
});
document.body.dispatchEvent(event);
}
async function reportResult(result) {
await fetch('http://localhost:8888/report_gl_result?' + result);
window.close();
}
function doTest() {
var firstImage = Module.canvas.toDataURL();
simulateKeyEvent(0x25 /* DOM_VK_LEFT */);
simulateKeyDown(0x25 /* DOM_VK_LEFT */);
setTimeout(function() {
var secondImage = Module.canvas.toDataURL();
reportResult(firstImage != secondImage);
Expand Down
Loading