Skip to content

Commit 3b72132

Browse files
authored
Improve assertions in test_sdl_mouse. NFC (emscripten-core#24178)
This should give us more clues as to why this test is being flaky. Also, remove some unused declarations. Also, use `-DDTEST_SDL_MOUSE_OFFSETS` over `-DDTEST_SDL_MOUSE_OFFSETS=1` for consistency with other usages.
1 parent df1ba7e commit 3b72132

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

test/browser/test_glfw_joystick.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ void error_callback(int error, const char* description);
2020

2121
int joy_connected = -1;
2222

23-
void joystick_callback(int joy, int event)
24-
{
23+
void joystick_callback(int joy, int event) {
2524
if (event == GLFW_CONNECTED) {
2625
printf("Joystick %d was connected: %s\n", joy, glfwGetJoystickName(joy));
2726
joy_connected = joy; // use the most recently connected joystick
@@ -104,15 +103,13 @@ void main_2(void *arg) {
104103
}
105104

106105
int main() {
107-
if (!glfwInit())
108-
{
106+
if (!glfwInit()) {
109107
printf("Could not create window. Test failed.\n");
110108
return 1;
111109
}
112110
glfwWindowHint(GLFW_RESIZABLE , 1);
113111
g_window = glfwCreateWindow(600, 450, "GLFW joystick test", NULL, NULL);
114-
if (!g_window)
115-
{
112+
if (!g_window) {
116113
printf("Could not create window. Test failed.\n");
117114
glfwTerminate();
118115
return 1;

test/browser/test_sdl_mouse.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,33 @@
1111
#include <emscripten.h>
1212

1313
#define abs(x) ((x) < 0 ? -(x) : (x))
14+
1415
void one() {
1516
SDL_Event event;
1617
while (SDL_PollEvent(&event)) {
17-
switch(event.type) {
18+
switch (event.type) {
1819
case SDL_MOUSEMOTION: {
1920
SDL_MouseMotionEvent *m = (SDL_MouseMotionEvent*)&event;
2021
assert(m->state == 0);
2122
int x, y;
2223
SDL_GetMouseState(&x, &y);
2324
assert(x == m->x && y == m->y);
24-
printf("motion: %d,%d %d,%d\n", m->x, m->y, m->xrel, m->yrel);
25+
printf("motion: abs:%d,%d rel:%d,%d\n", m->x, m->y, m->xrel, m->yrel);
26+
static bool first_motion = true;
27+
if (first_motion) {
28+
first_motion = false;
29+
#ifdef TEST_SDL_MOUSE_OFFSETS
30+
assert(abs(m->x-5) <= 1 && abs(m->y-15) <= 1 && abs(m->xrel-5) <= 1 && abs(m->yrel-15) <= 1);
31+
#else
32+
assert(abs(m->x-10) <= 1 && abs(m->y-20) <= 1 && abs(m->xrel-10) <= 1 && abs(m->yrel-20) <= 1);
33+
#endif
34+
} else {
2535
#ifdef TEST_SDL_MOUSE_OFFSETS
26-
assert( (abs(m->x-5) <= 1 && abs(m->y-15) <= 1 && abs(m->xrel-5) <= 1 && abs(m->yrel-15) <= 1)
27-
|| (abs(m->x-25) <= 1 && abs(m->y-72) <= 1 && abs(m->xrel-20) <= 1 && abs(m->yrel-57) <= 1) );
36+
assert(abs(m->x-25) <= 1 && abs(m->y-72) <= 1 && abs(m->xrel-20) <= 1 && abs(m->yrel-57) <= 1);
2837
#else
29-
assert( (abs(m->x-10) <= 1 && abs(m->y-20) <= 1 && abs(m->xrel-10) <= 1 && abs(m->yrel-20) <= 1)
30-
|| (abs(m->x-30) <= 1 && abs(m->y-77) <= 1 && abs(m->xrel-20) <= 1 && abs(m->yrel-57) <= 1) );
38+
assert(abs(m->x-30) <= 1 && abs(m->y-77) <= 1 && abs(m->xrel-20) <= 1 && abs(m->yrel-57) <= 1);
3139
#endif
40+
}
3241
break;
3342
}
3443
case SDL_MOUSEBUTTONDOWN: {
@@ -37,20 +46,22 @@ void one() {
3746
emscripten_force_exit(0);
3847
}
3948
printf("button down: %d,%d %d,%d\n", m->button, m->state, m->x, m->y);
49+
assert(m->button == 1 && m->state == 1);
4050
#ifdef TEST_SDL_MOUSE_OFFSETS
41-
assert(m->button == 1 && m->state == 1 && abs(m->x-5) <= 1 && abs(m->y-15) <= 1);
51+
assert(abs(m->x-5) <= 1 && abs(m->y-15) <= 1);
4252
#else
43-
assert(m->button == 1 && m->state == 1 && abs(m->x-10) <= 1 && abs(m->y-20) <= 1);
53+
assert(abs(m->x-10) <= 1 && abs(m->y-20) <= 1);
4454
#endif
4555
break;
4656
}
4757
case SDL_MOUSEBUTTONUP: {
4858
SDL_MouseButtonEvent *m = (SDL_MouseButtonEvent*)&event;
4959
printf("button up: %d,%d %d,%d\n", m->button, m->state, m->x, m->y);
60+
assert(m->button == 1 && m->state == 0);
5061
#ifdef TEST_SDL_MOUSE_OFFSETS
51-
assert(m->button == 1 && m->state == 0 && abs(m->x-5) <= 1 && abs(m->y-15) <= 1);
62+
assert(abs(m->x-5) <= 1 && abs(m->y-15) <= 1);
5263
#else
53-
assert(m->button == 1 && m->state == 0 && abs(m->x-10) <= 1 && abs(m->y-20) <= 1);
64+
assert(abs(m->x-10) <= 1 && abs(m->y-20) <= 1);
5465
#endif
5566
// Remove another click we want to ignore
5667
assert(SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONDOWN) == 1);
@@ -61,8 +72,6 @@ void one() {
6172
}
6273
}
6374

64-
void main_2(void* arg);
65-
6675
int main() {
6776
SDL_Init(SDL_INIT_VIDEO);
6877
SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);
@@ -84,4 +93,3 @@ int main() {
8493

8594
emscripten_set_main_loop(one, 0, 0);
8695
}
87-

test/browser/test_sdl_resize.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ void loop() {
3838
}
3939
}
4040

41-
void main_2();
42-
4341
int main() {
4442
SDL_Init(SDL_INIT_VIDEO);
4543
SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);

test/test_browser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3062,7 +3062,7 @@ def test_sdl2_mouse_offsets(self):
30623062
</html>
30633063
''')
30643064

3065-
self.compile_btest('browser/test_sdl2_mouse.c', ['-DTEST_SDL_MOUSE_OFFSETS=1', '-O2', '--minify=0', '-o', 'sdl2_mouse.js', '--pre-js', test_file('browser/fake_events.js'), '-sUSE_SDL=2', '-sEXIT_RUNTIME'])
3065+
self.compile_btest('browser/test_sdl2_mouse.c', ['-DTEST_SDL_MOUSE_OFFSETS', '-O2', '--minify=0', '-o', 'sdl2_mouse.js', '--pre-js', test_file('browser/fake_events.js'), '-sUSE_SDL=2', '-sEXIT_RUNTIME'])
30663066
self.run_browser('page.html', '', '/report_result?exit:0')
30673067

30683068
def test_sdl2_threads(self):

0 commit comments

Comments
 (0)