Skip to content

Commit ce52d74

Browse files
committed
Simplify test_glfw_events. NFC
Avoid complex state management and just exit early on any failure.
1 parent 6995a4e commit ce52d74

File tree

1 file changed

+50
-56
lines changed

1 file changed

+50
-56
lines changed

test/browser/test_glfw_events.c

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#else
1111
#include <GLFW/glfw3.h>
1212
#endif
13+
#include <assert.h>
1314
#include <stdio.h>
15+
#include <stdbool.h>
1416
#include <emscripten.h>
1517

1618
#define MULTILINE(...) #__VA_ARGS__
@@ -82,79 +84,76 @@ test_t g_tests[] = {
8284
#endif
8385
};
8486

85-
static unsigned int g_test_actual = 0;
87+
static bool g_callback_received;
88+
static unsigned int g_current_test;
8689
static unsigned int g_test_count = sizeof(g_tests) / sizeof(test_t);
87-
static unsigned int g_state = 0;
8890

8991
#if USE_GLFW == 2
9092
static void on_mouse_button_callback(int button, int action) {
9193
#else
9294
static void on_mouse_button_callback(GLFWwindow* window, int button, int action, int modify) {
9395
#endif
94-
test_args_t args = g_tests[g_test_actual].args;
95-
if (args.button == button && args.action == action)
96-
{
97-
g_state |= 1 << g_test_actual;
98-
}
99-
else
100-
{
101-
printf("Test %d: FAIL\n", g_test_actual);
102-
}
96+
test_args_t args = g_tests[g_current_test].args;
97+
if (args.button == button && args.action == action) {
98+
g_callback_received = true;
99+
} else {
100+
printf("Test %d: FAIL\n", g_current_test);
101+
exit(1);
102+
}
103103
}
104104

105105
#if USE_GLFW == 2
106106
static void on_mouse_move(int x, int y) {
107107
#else
108108
static void on_mouse_move(GLFWwindow* window, double x, double y) {
109109
#endif
110-
test_args_t args = g_tests[g_test_actual].args;
111-
if (args.x == x && args.y == y)
112-
{
113-
g_state |= 1 << g_test_actual;
114-
}
115-
else
116-
{
117-
printf("Test %d: FAIL\n", g_test_actual);
118-
}
110+
test_args_t args = g_tests[g_current_test].args;
111+
if (args.x == x && args.y == y) {
112+
g_callback_received = true;
113+
} else {
114+
printf("Test %d: FAIL\n", g_current_test);
115+
exit(1);
116+
}
119117
}
120118

121119
#if USE_GLFW == 2
122120
static void on_key_callback(int key, int action) {
123121
#else
124122
static void on_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
125123
#endif
126-
test_args_t args = g_tests[g_test_actual].args;
127-
if (args.button == key && args.action == action)
128-
{
129-
g_state |= 1 << g_test_actual;
130-
}
131-
else
132-
{
133-
printf("Test %d: FAIL\n", g_test_actual);
134-
}
124+
test_args_t args = g_tests[g_current_test].args;
125+
if (args.button == key && args.action == action) {
126+
g_callback_received = true;
127+
} else {
128+
printf("Test %d: FAIL\n", g_current_test);
129+
exit(1);
130+
}
135131
}
136132

137133
#if USE_GLFW == 2
138134
static void on_char_callback(int character, int action) {
139135
#else
140136
static void on_char_callback(GLFWwindow* window, unsigned int character) {
141137
#endif
142-
test_args_t args = g_tests[g_test_actual].args;
138+
printf("got character: %d\n", character);
139+
test_args_t args = g_tests[g_current_test].args;
143140
if (args.character != -1 && args.character == character) {
144-
g_state |= 1 << g_test_actual;
141+
g_callback_received = true;
145142
} else {
146-
printf("Test %d: FAIL\n", g_test_actual);
143+
printf("Test %d: FAIL\n", g_current_test);
144+
exit(1);
147145
}
148146

149147
}
150148

151149
#if USE_GLFW == 3
152150
static void on_mouse_wheel(GLFWwindow* window, double x, double y) {
153-
test_args_t args = g_tests[g_test_actual].args;
151+
test_args_t args = g_tests[g_current_test].args;
154152
if (args.x == x && args.y == y) {
155-
g_state |= 1 << g_test_actual;
153+
g_callback_received = true;
156154
} else {
157-
printf("Test %d: FAIL\n", g_test_actual);
155+
printf("Test %d: FAIL\n", g_current_test);
156+
exit(1);
158157
}
159158
}
160159

@@ -197,15 +196,11 @@ int main() {
197196
glfwSetMouseButtonCallback(_mainWindow, p == 0 ? NULL : on_mouse_button_callback);
198197
glfwSetKeyCallback(_mainWindow, p == 0 ? NULL : on_key_callback);
199198
#endif
200-
g_state = p == 0 ? success : 0;
201199

202200
for (int i = 0; i < g_test_count; ++i) {
203-
g_test_actual = i;
204-
test_t test = g_tests[g_test_actual];
205-
206-
if (test.args.character == -1) {
207-
g_state |= 1 << g_test_actual;
208-
}
201+
g_current_test = i;
202+
test_t test = g_tests[g_current_test];
203+
g_callback_received = false;
209204

210205
emscripten_run_script(test.cmd);
211206

@@ -215,8 +210,8 @@ int main() {
215210
#else
216211
if (glfwGetMouseButton(_mainWindow, test.args.button) != test.args.action) {
217212
#endif
218-
printf("Test %d: FAIL\n", g_test_actual);
219-
g_state &= ~(1 << g_test_actual);
213+
printf("Test %d: FAIL\n", g_current_test);
214+
exit(1);
220215
}
221216
} else {
222217
// Keyboard.
@@ -225,23 +220,22 @@ int main() {
225220
#else
226221
if (test.args.action != -1 && glfwGetKey(_mainWindow, test.args.button) != test.args.action) {
227222
#endif
228-
printf("Test %d: FAIL\n", g_test_actual);
229-
g_state &= ~(1 << g_test_actual);
223+
printf("Test %d: FAIL\n", g_current_test);
224+
exit(1);
230225
}
231226
}
232-
}
233-
if (g_state != success) {
234-
break;
227+
228+
if (p == 1) {
229+
if (test.args.character != -1 && !g_callback_received) {
230+
printf("Test %d: FAIL (event not received)\n", g_current_test);
231+
exit(1);
232+
}
233+
printf("Test %d: PASSED\n", g_current_test);
234+
}
235235
}
236236
}
237237

238238
glfwTerminate();
239-
240-
printf("%d == %d = %d", g_state, success, g_state == success);
241-
if (g_state != success) {
242-
printf("test failed\n");
243-
return 1;
244-
}
245-
printf("success\n");
239+
printf("done\n");
246240
return 0;
247241
}

0 commit comments

Comments
 (0)