@@ -18,10 +18,26 @@ typedef struct {
18
18
static rect_size_t browser_window_size = { 0 , 0 };
19
19
static rect_size_t glut_init_size = { 0 , 0 };
20
20
static rect_size_t glut_reshape_size = { 0 , 0 };
21
+ static rect_size_t target_size = { 0 , 0 };
22
+
23
+ /*
24
+ * Set run_async_verification to 0 for sync test cases, and 1 for async tests.
25
+ *
26
+ * Callback sequence for test case 1 & 2 (synchronous):
27
+ * glutMainLoop -> GLUT.onSize -> Browser.setCanvasSize -> updateResizeListeners -> GLUT.reshapeFunc
28
+ * glutResizeWindow -> Browser.setCanvasSize -> updateResizeListeners -> GLUT.reshapeFunc
29
+ *
30
+ * Callback sequence for test cases 3-5 (async):
31
+ * window resize -> async update -> GLUT.onSize -> Browser.setCanvasSize -> updateResizeListeners -> GLUT.reshapeFunc
32
+ *
33
+ * Because window resize does not immediately call GLUT.onSize, we wait to run verification of a test until we get
34
+ * confirmation in GLUT.reshapeFunc. And after verification is done, we move on to the next test.
35
+ *
36
+ */
37
+ static int run_async_verification = 0 ;
21
38
22
- void print_size_test (const char * name , rect_size_t rect_size ) {
23
- static int test_count = 0 ;
24
- printf ("Test %d: %s = %d x %d\n" , ++ test_count , name , rect_size .width , rect_size .height );
39
+ void print_size_test (int test_num , const char * name , rect_size_t rect_size ) {
40
+ printf ("Test %d: %s = %d x %d\n" , test_num , name , rect_size .width , rect_size .height );
25
41
}
26
42
27
43
int equal_size (rect_size_t rect_1 , rect_size_t rect_2 ) {
@@ -64,7 +80,7 @@ EM_JS(void, test_resize_with_CSS, (const char* position, const char* width, cons
64
80
* Verify canvas and reshape callback match target size, and also that
65
81
* canvas width, height matches canvas clientWidth, clientHeight
66
82
*/
67
- void assert_sizes_equal ( rect_size_t target_size ) {
83
+ void assert_canvas_and_target_sizes_equal ( ) {
68
84
/* verify target size match */
69
85
rect_size_t canvas_size ;
70
86
get_canvas_size (& canvas_size .width , & canvas_size .height );
@@ -77,57 +93,102 @@ void assert_sizes_equal(rect_size_t target_size) {
77
93
assert (equal_size (canvas_size , canvas_client_size ));
78
94
}
79
95
96
+ /**
97
+ * Verify the result of the previous test and then run the next one
98
+ */
99
+ void verify_test_and_run_next () {
100
+ void run_next_test ();
101
+
102
+ assert_canvas_and_target_sizes_equal ();
103
+ run_next_test ();
104
+ }
105
+
80
106
/**
81
107
* Resizing tests
82
108
*/
83
- void run_tests () {
84
-
85
- /* startup */
86
- print_size_test ("startup, no CSS: canvas == glutReshapeFunc == glutInitWindow size" , glut_init_size );
87
- assert_sizes_equal (glut_init_size );
88
-
89
- /* glutReshapeWindow */
90
- rect_size_t new_reshape_size = { glut_init_size .width + 40 , glut_init_size .height + 20 };
91
- print_size_test ("glut reshape, no CSS: canvas == glutReshapeFunc == glutReshapeWindow size" , new_reshape_size );
92
- glutReshapeWindow (new_reshape_size .width , new_reshape_size .height );
93
- assert_sizes_equal (new_reshape_size );
94
-
95
- /* 100% scale CSS */
96
- print_size_test ("100% window scale CSS: canvas == glutReshapeFunc == browser window size" , browser_window_size );
97
- test_resize_with_CSS ("fixed" , "100%" , "100%" ); /* fixed, so canvas is driven by window size */
98
- assert_sizes_equal (browser_window_size );
99
-
100
- /* specific pixel size CSS */
101
- rect_size_t css_pixels_size = { glut_init_size .width - 20 , glut_init_size .height + 40 };
102
- print_size_test ("specific pixel size CSS: canvas == glutReshapeFunc == CSS specific size" , css_pixels_size );
103
- char css_width [16 ], css_height [16 ];
104
- snprintf (css_width , 16 , "%dpx" , css_pixels_size .width );
105
- snprintf (css_height , 16 , "%dpx" , css_pixels_size .height );
106
- test_resize_with_CSS ("static" , css_width , css_height ); /* static, canvas is driven by CSS size */
107
- assert_sizes_equal (css_pixels_size );
108
-
109
- /* mix of CSS scale and pixel size */
110
- rect_size_t css_mixed_size = { browser_window_size .width * 0.6 , 100 };
111
- print_size_test ("60% width, 100px height CSS: canvas == glutReshapeFunc == CSS mixed size" , css_mixed_size );
112
- test_resize_with_CSS ("fixed" , "60%" , "100px" ); /* fixed, canvas width is driven by window size */
113
- assert_sizes_equal (css_mixed_size );
114
-
115
- /* run tests once */
116
- glutIdleFunc (NULL );
117
- emscripten_force_exit (0 );
109
+ void run_next_test () {
110
+ static int test_num = 0 ;
111
+ ++ test_num ;
112
+
113
+ switch (test_num ) {
114
+ case 1 : {
115
+ /* startup */
116
+ target_size = glut_init_size ;
117
+ print_size_test (test_num , "startup, no CSS: canvas == glutReshapeFunc == glutInitWindow size" , target_size );
118
+ verify_test_and_run_next ();
119
+ break ;
120
+ }
121
+ case 2 : {
122
+ /* glutReshapeWindow */
123
+ target_size .width = glut_init_size .width + 40 ;
124
+ target_size .height = glut_init_size .height + 20 ;
125
+ print_size_test (test_num , "glut reshape, no CSS: canvas == glutReshapeFunc == glutReshapeWindow size" , target_size );
126
+ glutReshapeWindow (target_size .width , target_size .height );
127
+ verify_test_and_run_next ();
128
+ break ;
129
+ }
130
+ case 3 : {
131
+ /* 100% scale CSS */
132
+ target_size = browser_window_size ;
133
+ print_size_test (test_num , "100% window scale CSS: canvas == glutReshapeFunc == browser window size" , target_size );
134
+ run_async_verification = 1 ;
135
+ test_resize_with_CSS ("fixed" , "100%" , "100%" ); /* fixed, so canvas is driven by window size */
136
+ break ;
137
+ }
138
+ case 4 : {
139
+ /* specific pixel size CSS */
140
+ target_size .width = glut_init_size .width - 20 ;
141
+ target_size .height = glut_init_size .height + 40 ;
142
+ print_size_test (test_num , "specific pixel size CSS: canvas == glutReshapeFunc == CSS specific size" , target_size );
143
+ char css_width [16 ], css_height [16 ];
144
+ snprintf (css_width , 16 , "%dpx" , target_size .width );
145
+ snprintf (css_height , 16 , "%dpx" , target_size .height );
146
+ run_async_verification = 1 ;
147
+ test_resize_with_CSS ("static" , css_width , css_height ); /* static, canvas is driven by CSS size */
148
+ break ;
149
+ }
150
+ case 5 : {
151
+ /* mix of CSS scale and pixel size */
152
+ target_size .width = browser_window_size .width ;
153
+ target_size .height = 100 ;
154
+ print_size_test (test_num , "100% width, 100px height CSS: canvas == glutReshapeFunc == CSS mixed size" , target_size );
155
+ run_async_verification = 1 ;
156
+ test_resize_with_CSS ("fixed" , "100%" , "100px" ); /* fixed, canvas width is driven by window size */
157
+ break ;
158
+ }
159
+ default : {
160
+ /* all tests complete */
161
+ emscripten_force_exit (0 );
162
+ break ;
163
+ }
164
+ }
118
165
}
119
166
120
167
/**
121
- * Reshape callback
168
+ * Idle callback - start tests
169
+ */
170
+ void start_tests () {
171
+ glutIdleFunc (NULL );
172
+ run_next_test ();
173
+ }
174
+
175
+ /**
176
+ * Reshape callback - record latest size, verify and run next test if async
122
177
*/
123
178
void reshape (int w , int h ) {
124
179
glut_reshape_size .width = w ;
125
180
glut_reshape_size .height = h ;
181
+
182
+ if (run_async_verification ) {
183
+ run_async_verification = 0 ; /* Only one verification per test */
184
+ verify_test_and_run_next ();
185
+ }
126
186
}
127
187
128
188
int main (int argc , char * argv []) {
129
- /* Make glut initial canvas size be 1/2 of browser window */
130
189
get_browser_window_size (& browser_window_size .width , & browser_window_size .height );
190
+
191
+ /* Make glut initial canvas size be 1/2 of browser window */
131
192
glut_init_size .width = browser_window_size .width / 2 ;
132
193
glut_init_size .height = browser_window_size .height / 2 ;
133
194
@@ -137,7 +198,7 @@ int main(int argc, char *argv[]) {
137
198
glutCreateWindow ("test_glut_resize.c" );
138
199
139
200
/* Set up glut callback functions */
140
- glutIdleFunc (run_tests );
201
+ glutIdleFunc (start_tests );
141
202
glutReshapeFunc (reshape );
142
203
glutDisplayFunc (NULL );
143
204
0 commit comments