Path: blob/main/test/browser/test_glfw_events.c
4150 views
/*1* Copyright 2016 The Emscripten Authors. All rights reserved.2* Emscripten is available under two separate licenses, the MIT license and the3* University of Illinois/NCSA Open Source License. Both these licenses can be4* found in the LICENSE file.5*/67#if USE_GLFW == 28#include <GL/glfw.h>9#else10#include <GLFW/glfw3.h>11#endif12#include <stdio.h>13#include <emscripten.h>1415#define MULTILINE(...) #__VA_ARGS__16#define WIDTH 64017#define HEIGHT 4801819// Setup tests20typedef struct {21int mouse;22double x, y;23int button;24int action;25int modify;26int character;27} test_args_t;2829typedef struct {30char cmd[80];31test_args_t args;32} test_t;3334// Javascript event.button 0 = left, 1 = middle, 2 = right35test_t g_tests[] = {36{ "simulateMouseDown(10.0, 10.0, 0)", { 1, 10.0, 10.0, GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS, -1 } },37{ "simulateMouseUp (10.0, 20.0, 0)", { 1, 10.0, 20.0, GLFW_MOUSE_BUTTON_LEFT, GLFW_RELEASE, -1 } },38{ "simulateMouseDown(10.0, 30.0, 1)", { 1, 10.0, 30.0, GLFW_MOUSE_BUTTON_MIDDLE, GLFW_PRESS, -1 } },39{ "simulateMouseUp (10.0, 40.0, 1)", { 1, 10.0, 40.0, GLFW_MOUSE_BUTTON_MIDDLE, GLFW_RELEASE, -1 } },40{ "simulateMouseDown(10.0, 30.0, 2)", { 1, 10.0, 30.0, GLFW_MOUSE_BUTTON_RIGHT, GLFW_PRESS, -1 } },41{ "simulateMouseUp (10.0, 40.0, 2)", { 1, 10.0, 40.0, GLFW_MOUSE_BUTTON_RIGHT, GLFW_RELEASE, -1 } },42//{ "Module.injectMouseEvent(10.0, 50.0, 'mousewheel', 0)", { 10.0, 50.0, -1, -1, -1 } },43//{ "Module.injectMouseEvent(10.0, 60.0, 'mousemove', 0)", { 10.0, 60.0, -1, -1, -1 } }4445{ "simulateKeyDown(8)", { 0, 0.0, 0.0, GLFW_KEY_BACKSPACE, GLFW_PRESS, -1 } },46{ "simulateKeyUp (8)", { 0, 0.0, 0.0, GLFW_KEY_BACKSPACE, GLFW_RELEASE, -1 } },47{ "simulateKeyDown(9)", { 0, 0.0, 0.0, GLFW_KEY_TAB, GLFW_PRESS, -1 } },48{ "simulateKeyUp (9)", { 0, 0.0, 0.0, GLFW_KEY_TAB, GLFW_RELEASE, -1 } },49{ "simulateKeyDown(112)", { 0, 0.0, 0.0, GLFW_KEY_F1, GLFW_PRESS, -1 } },50{ "simulateKeyUp (112)", { 0, 0.0, 0.0, GLFW_KEY_F1, GLFW_RELEASE, -1 } },51{ "simulateKeyDown(37)", { 0, 0.0, 0.0, GLFW_KEY_LEFT, GLFW_PRESS, -1 } },52{ "simulateKeyUp (37)", { 0, 0.0, 0.0, GLFW_KEY_LEFT, GLFW_RELEASE, -1 } },53{ "simulateKeyDown(39)", { 0, 0.0, 0.0, GLFW_KEY_RIGHT, GLFW_PRESS, -1 } },54{ "simulateKeyUp (39)", { 0, 0.0, 0.0, GLFW_KEY_RIGHT, GLFW_RELEASE, -1 } },55{ "simulateKeyDown(38)", { 0, 0.0, 0.0, GLFW_KEY_UP, GLFW_PRESS, -1 } },56{ "simulateKeyUp (38)", { 0, 0.0, 0.0, GLFW_KEY_UP, GLFW_RELEASE, -1 } },57{ "simulateKeyDown(40)", { 0, 0.0, 0.0, GLFW_KEY_DOWN, GLFW_PRESS, -1 } },58{ "simulateKeyUp (40)", { 0, 0.0, 0.0, GLFW_KEY_DOWN, GLFW_RELEASE, -1 } },59#if USE_GLFW == 260{ "simulateKeyDown(27)", { 0, 0.0, 0.0, GLFW_KEY_ESC, GLFW_PRESS, -1 } },61{ "simulateKeyUp(27)", { 0, 0.0, 0.0, GLFW_KEY_ESC, GLFW_RELEASE, -1 } },6263{ "simulateKeyDown(65)", { 0, 0.0, 0.0, 'A', GLFW_PRESS, -1, 'A' } },64{ "simulateKeyEvent('keypress', 65, {charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, 'A' } },65{ "simulateKeyUp(65)", { 0, 0.0, 0.0, 'A', GLFW_RELEASE, -1, 'A' } },6667{ "simulateKeyDown(65, {ctrlKey: true})", { 0, 0.0, 0.0, 'A', GLFW_PRESS, -1, 'A' } },68{ "simulateKeyEvent('keypress', 65, {ctrlKey: true, charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, -1 } },69{ "simulateKeyUp(65, {ctrlKey: true})", { 0, 0.0, 0.0, 'A', GLFW_RELEASE, -1, 'A' } },70#else71{ "simulateKeyDown(27)", { 0, 0.0, 0.0, GLFW_KEY_ESCAPE, GLFW_PRESS, -1 } },72{ "simulateKeyUp(27)", { 0, 0.0, 0.0, GLFW_KEY_ESCAPE, GLFW_RELEASE, -1 } },7374{ "simulateKeyDown(65)", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_PRESS, -1 } },75{ "simulateKeyEvent('keypress', 65, {charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, 'A' } },76{ "simulateKeyUp(65)", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_RELEASE, -1 } },7778{ "simulateKeyDown(65, {ctrlKey: true})", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_PRESS, -1, 'A' } },79{ "simulateKeyEvent('keypress', 65, {ctrlKey: true, charCode: 65})", { 0, 0.0, 0.0, -1, -1, -1, -1 } },80{ "simulateKeyUp(65, {ctrlKey: true})", { 0, 0.0, 0.0, GLFW_KEY_A, GLFW_RELEASE, -1, 'A' } },81#endif82};8384static unsigned int g_test_actual = 0;85static unsigned int g_test_count = sizeof(g_tests) / sizeof(test_t);86static unsigned int g_state = 0;8788#if USE_GLFW == 289static void on_mouse_button_callback(int button, int action) {90#else91static void on_mouse_button_callback(GLFWwindow* window, int button, int action, int modify) {92#endif93test_args_t args = g_tests[g_test_actual].args;94if (args.button == button && args.action == action)95{96g_state |= 1 << g_test_actual;97}98else99{100printf("Test %d: FAIL\n", g_test_actual);101}102}103104#if USE_GLFW == 2105static void on_mouse_move(int x, int y) {106#else107static void on_mouse_move(GLFWwindow* window, double x, double y) {108#endif109test_args_t args = g_tests[g_test_actual].args;110if (args.x == x && args.y == y)111{112g_state |= 1 << g_test_actual;113}114else115{116printf("Test %d: FAIL\n", g_test_actual);117}118}119120#if USE_GLFW == 2121static void on_key_callback(int key, int action) {122#else123static void on_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {124#endif125test_args_t args = g_tests[g_test_actual].args;126if (args.button == key && args.action == action)127{128g_state |= 1 << g_test_actual;129}130else131{132printf("Test %d: FAIL\n", g_test_actual);133}134}135136#if USE_GLFW == 2137static void on_char_callback(int character, int action) {138#else139static void on_char_callback(GLFWwindow* window, unsigned int character) {140#endif141test_args_t args = g_tests[g_test_actual].args;142if (args.character != -1 && args.character == character) {143g_state |= 1 << g_test_actual;144} else {145printf("Test %d: FAIL\n", g_test_actual);146}147148}149150#if USE_GLFW == 3151static void on_mouse_wheel(GLFWwindow* window, double x, double y) {152test_args_t args = g_tests[g_test_actual].args;153if (args.x == x && args.y == y) {154g_state |= 1 << g_test_actual;155} else {156printf("Test %d: FAIL\n", g_test_actual);157}158}159160static void on_error(int error, const char *msg) {161printf("%d: %s\n", error, msg);162}163#endif164165int main() {166unsigned int success = (1 << (sizeof(g_tests) / sizeof(test_t))) - 1; // (2^count)-1;167168glfwInit();169170#if USE_GLFW == 2171glfwOpenWindow(WIDTH, HEIGHT, 5, 6, 5, 0, 0, 0, GLFW_WINDOW); // != GL_TRUE)172173glfwSetMousePosCallback(on_mouse_move);174glfwSetCharCallback(on_char_callback);175#else176glfwSetErrorCallback(on_error);177printf("%s\n", glfwGetVersionString());178179GLFWwindow * _mainWindow = NULL;180glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);181_mainWindow = glfwCreateWindow(WIDTH, HEIGHT, "glfw3_events", NULL, NULL);182glfwMakeContextCurrent(_mainWindow);183184glfwSetCursorPosCallback(_mainWindow, on_mouse_move);185glfwSetScrollCallback(_mainWindow, on_mouse_wheel);186glfwSetCharCallback(_mainWindow, on_char_callback);187#endif188189for (int p = 0; p < 2; ++p) { // 2 passes, with and without callbacks.190printf("Running Test pass %d\n", p);191192#if USE_GLFW == 2193glfwSetMouseButtonCallback(p == 0 ? NULL : on_mouse_button_callback);194glfwSetKeyCallback(p == 0 ? NULL : on_key_callback);195#else196glfwSetMouseButtonCallback(_mainWindow, p == 0 ? NULL : on_mouse_button_callback);197glfwSetKeyCallback(_mainWindow, p == 0 ? NULL : on_key_callback);198#endif199g_state = p == 0 ? success : 0;200201for (int i = 0; i < g_test_count; ++i) {202g_test_actual = i;203test_t test = g_tests[g_test_actual];204205if (test.args.character == -1) {206g_state |= 1 << g_test_actual;207}208209emscripten_run_script(test.cmd);210211if (test.args.mouse) {212#if USE_GLFW == 2213if (glfwGetMouseButton(test.args.button) != test.args.action) {214#else215if (glfwGetMouseButton(_mainWindow, test.args.button) != test.args.action) {216#endif217printf("Test %d: FAIL\n", g_test_actual);218g_state &= ~(1 << g_test_actual);219}220} else {221// Keyboard.222#if USE_GLFW == 2223if (test.args.action != -1 && glfwGetKey(test.args.button) != test.args.action) {224#else225if (test.args.action != -1 && glfwGetKey(_mainWindow, test.args.button) != test.args.action) {226#endif227printf("Test %d: FAIL\n", g_test_actual);228g_state &= ~(1 << g_test_actual);229}230}231}232if (g_state != success) {233break;234}235}236237glfwTerminate();238239printf("%d == %d = %d", g_state, success, g_state == success);240if (g_state != success) {241printf("test failed\n");242return 1;243}244printf("success\n");245return 0;246}247248249