@@ -18,10 +18,26 @@ typedef struct {
1818static  rect_size_t  browser_window_size  =  { 0 , 0  };
1919static  rect_size_t  glut_init_size  =       { 0 , 0  };
2020static  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 ;
2138
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 );
2541}
2642
2743int  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
6480 * Verify canvas and reshape callback match target size, and also that 
6581 * canvas width, height matches canvas clientWidth, clientHeight 
6682 */ 
67- void  assert_sizes_equal ( rect_size_t   target_size ) {
83+ void  assert_canvas_and_target_sizes_equal ( ) {
6884  /* verify target size match */ 
6985  rect_size_t  canvas_size ;
7086  get_canvas_size (& canvas_size .width , & canvas_size .height );
@@ -77,57 +93,102 @@ void assert_sizes_equal(rect_size_t target_size) {
7793  assert (equal_size (canvas_size , canvas_client_size ));
7894}
7995
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+ 
80106/** 
81107 * Resizing tests 
82108 */ 
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+   }
118165}
119166
120167/** 
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 
122177 */ 
123178void  reshape (int  w , int  h ) {
124179  glut_reshape_size .width  =  w ;
125180  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+   }
126186}
127187
128188int  main (int  argc , char  * argv []) {
129-   /* Make glut initial canvas size be 1/2 of browser window */ 
130189  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 */ 
131192  glut_init_size .width  =  browser_window_size .width  / 2 ;
132193  glut_init_size .height  =  browser_window_size .height  / 2 ;
133194
@@ -137,7 +198,7 @@ int main(int argc, char *argv[]) {
137198  glutCreateWindow ("test_glut_resize.c" );
138199
139200  /* Set up glut callback functions */ 
140-   glutIdleFunc (run_tests );
201+   glutIdleFunc (start_tests );
141202  glutReshapeFunc (reshape );
142203  glutDisplayFunc (NULL );
143204
0 commit comments