test/testautomation_mouse.c
changeset 6821 f36771112651
parent 6801 f92e3d5f71a2
child 6822 18f6818ba281
     1.1 --- a/test/testautomation_mouse.c	Thu Jan 17 11:54:14 2013 +0000
     1.2 +++ b/test/testautomation_mouse.c	Wed Jan 23 08:03:19 2013 -0800
     1.3 @@ -115,79 +115,79 @@
     1.4     return TEST_COMPLETED;
     1.5  }
     1.6  
     1.7 -
     1.8 -/* XPM definition of mouse Cursor */
     1.9 -static const char *_mouseArrowData[] = {
    1.10 -  /* pixels */
    1.11 -  "X                               ",
    1.12 -  "XX                              ",
    1.13 -  "X.X                             ",
    1.14 -  "X..X                            ",
    1.15 -  "X...X                           ",
    1.16 -  "X....X                          ",
    1.17 -  "X.....X                         ",
    1.18 -  "X......X                        ",
    1.19 -  "X.......X                       ",
    1.20 -  "X........X                      ",
    1.21 -  "X.....XXXXX                     ",
    1.22 -  "X..X..X                         ",
    1.23 -  "X.X X..X                        ",
    1.24 -  "XX  X..X                        ",
    1.25 -  "X    X..X                       ",
    1.26 -  "     X..X                       ",
    1.27 -  "      X..X                      ",
    1.28 -  "      X..X                      ",
    1.29 -  "       XX                       ",
    1.30 -  "                                ",
    1.31 -  "                                ",
    1.32 -  "                                ",
    1.33 -  "                                ",
    1.34 -  "                                ",
    1.35 -  "                                ",
    1.36 -  "                                ",
    1.37 -  "                                ",
    1.38 -  "                                ",
    1.39 -  "                                ",
    1.40 -  "                                ",
    1.41 -  "                                ",
    1.42 -  "                                "
    1.43 -};
    1.44 -
    1.45 -/* Helper that creates a new mouse cursor from an XPM */
    1.46 -static SDL_Cursor *_initArrowCursor(const char *image[])
    1.47 -{
    1.48 -  SDL_Cursor *cursor;
    1.49 -  int i, row, col;
    1.50 -  Uint8 data[4*32];
    1.51 -  Uint8 mask[4*32];
    1.52 -
    1.53 -  i = -1;
    1.54 -  for ( row=0; row<32; ++row ) {
    1.55 -    for ( col=0; col<32; ++col ) {
    1.56 -      if ( col % 8 ) {
    1.57 -        data[i] <<= 1;
    1.58 -        mask[i] <<= 1;
    1.59 -      } else {
    1.60 -        ++i;
    1.61 -        data[i] = mask[i] = 0;
    1.62 -      }
    1.63 -      switch (image[row][col]) {
    1.64 -        case 'X':
    1.65 -          data[i] |= 0x01;
    1.66 -          mask[i] |= 0x01;
    1.67 -          break;
    1.68 -        case '.':
    1.69 -          mask[i] |= 0x01;
    1.70 -          break;
    1.71 -        case ' ':
    1.72 -          break;
    1.73 -      }
    1.74 -    }
    1.75 -  }
    1.76 -
    1.77 -  cursor = SDL_CreateCursor(data, mask, 32, 32, 0, 0);
    1.78 -  return cursor;
    1.79 -}
    1.80 +
    1.81 +/* XPM definition of mouse Cursor */
    1.82 +static const char *_mouseArrowData[] = {
    1.83 +  /* pixels */
    1.84 +  "X                               ",
    1.85 +  "XX                              ",
    1.86 +  "X.X                             ",
    1.87 +  "X..X                            ",
    1.88 +  "X...X                           ",
    1.89 +  "X....X                          ",
    1.90 +  "X.....X                         ",
    1.91 +  "X......X                        ",
    1.92 +  "X.......X                       ",
    1.93 +  "X........X                      ",
    1.94 +  "X.....XXXXX                     ",
    1.95 +  "X..X..X                         ",
    1.96 +  "X.X X..X                        ",
    1.97 +  "XX  X..X                        ",
    1.98 +  "X    X..X                       ",
    1.99 +  "     X..X                       ",
   1.100 +  "      X..X                      ",
   1.101 +  "      X..X                      ",
   1.102 +  "       XX                       ",
   1.103 +  "                                ",
   1.104 +  "                                ",
   1.105 +  "                                ",
   1.106 +  "                                ",
   1.107 +  "                                ",
   1.108 +  "                                ",
   1.109 +  "                                ",
   1.110 +  "                                ",
   1.111 +  "                                ",
   1.112 +  "                                ",
   1.113 +  "                                ",
   1.114 +  "                                ",
   1.115 +  "                                "
   1.116 +};
   1.117 +
   1.118 +/* Helper that creates a new mouse cursor from an XPM */
   1.119 +static SDL_Cursor *_initArrowCursor(const char *image[])
   1.120 +{
   1.121 +  SDL_Cursor *cursor;
   1.122 +  int i, row, col;
   1.123 +  Uint8 data[4*32];
   1.124 +  Uint8 mask[4*32];
   1.125 +
   1.126 +  i = -1;
   1.127 +  for ( row=0; row<32; ++row ) {
   1.128 +    for ( col=0; col<32; ++col ) {
   1.129 +      if ( col % 8 ) {
   1.130 +        data[i] <<= 1;
   1.131 +        mask[i] <<= 1;
   1.132 +      } else {
   1.133 +        ++i;
   1.134 +        data[i] = mask[i] = 0;
   1.135 +      }
   1.136 +      switch (image[row][col]) {
   1.137 +        case 'X':
   1.138 +          data[i] |= 0x01;
   1.139 +          mask[i] |= 0x01;
   1.140 +          break;
   1.141 +        case '.':
   1.142 +          mask[i] |= 0x01;
   1.143 +          break;
   1.144 +        case ' ':
   1.145 +          break;
   1.146 +      }
   1.147 +    }
   1.148 +  }
   1.149 +
   1.150 +  cursor = SDL_CreateCursor(data, mask, 32, 32, 0, 0);
   1.151 +  return cursor;
   1.152 +}
   1.153  
   1.154  /**
   1.155   * @brief Check call to SDL_CreateCursor and SDL_FreeCursor
   1.156 @@ -202,8 +202,8 @@
   1.157  
   1.158  	/* Create a cursor */
   1.159  	cursor = _initArrowCursor(_mouseArrowData);
   1.160 -    SDLTest_AssertPass("Call to SDL_CreateCursor()");
   1.161 -    SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");	
   1.162 +        SDLTest_AssertPass("Call to SDL_CreateCursor()");
   1.163 +        SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");	
   1.164  	if (cursor == NULL) {
   1.165  		return TEST_ABORTED;
   1.166  	}
   1.167 @@ -222,10 +222,10 @@
   1.168  	int newState;
   1.169  	int result;
   1.170  
   1.171 -    oldState = SDL_ShowCursor(SDL_QUERY);
   1.172 +        oldState = SDL_ShowCursor(SDL_QUERY);
   1.173  	SDLTest_AssertPass("Call to SDL_ShowCursor(SDL_QUERY)");
   1.174  
   1.175 -    result = SDL_ShowCursor(state);
   1.176 +        result = SDL_ShowCursor(state);
   1.177  	SDLTest_AssertPass("Call to SDL_ShowCursor(%s)", (state == SDL_ENABLE) ? "SDL_ENABLE" : "SDL_DISABLE");
   1.178  	SDLTest_AssertCheck(result == oldState, "Validate result from SDL_ShowCursor(%s), expected: %i, got: %i", 
   1.179  		(state == SDL_ENABLE) ? "SDL_ENABLE" : "SDL_DISABLE", oldState, result);
   1.180 @@ -278,18 +278,18 @@
   1.181  
   1.182  	/* Create a cursor */
   1.183  	cursor = _initArrowCursor(_mouseArrowData);
   1.184 -    SDLTest_AssertPass("Call to SDL_CreateCursor()");
   1.185 -    SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");	
   1.186 +        SDLTest_AssertPass("Call to SDL_CreateCursor()");
   1.187 +        SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");	
   1.188  	if (cursor == NULL) {
   1.189  		return TEST_ABORTED;
   1.190  	}
   1.191  
   1.192  	/* Set the arrow cursor */
   1.193 -	SDL_SetCursor(cursor);
   1.194 +	SDL_SetCursor(cursor);
   1.195  	SDLTest_AssertPass("Call to SDL_SetCursor(cursor)");
   1.196  
   1.197  	/* Force redraw */
   1.198 -	SDL_SetCursor(NULL);
   1.199 +	SDL_SetCursor(NULL);
   1.200  	SDLTest_AssertPass("Call to SDL_SetCursor(NULL)");
   1.201  
   1.202  	/* Free cursor again */
   1.203 @@ -299,6 +299,143 @@
   1.204  	return TEST_COMPLETED;
   1.205  }
   1.206  
   1.207 +#define MOUSE_TESTWINDOW_WIDTH	320
   1.208 +#define MOUSE_TESTWINDOW_HEIGHT 200
   1.209 +
   1.210 +/**
   1.211 + * Create s test window
   1.212 + */
   1.213 +SDL_Window *_createTestWindow()
   1.214 +{
   1.215 +  int posX = 100, posY = 100, width = MOUSE_TESTWINDOW_WIDTH, height = MOUSE_TESTWINDOW_HEIGHT;
   1.216 +  SDL_Window *window;
   1.217 +  window = SDL_CreateWindow("mouse_createTestWindow", posX, posY, width, height, 0);
   1.218 +  SDLTest_AssertPass("SDL_CreateWindow()");
   1.219 +  SDLTest_AssertCheck(window != NULL, "Check SDL_CreateWindow result");
   1.220 +  return window;
   1.221 +}
   1.222 +
   1.223 +/*
   1.224 + * Destroy test window  
   1.225 + */
   1.226 +void _destroyTestWindow(SDL_Window *window)
   1.227 +{
   1.228 +  if (window != NULL) {  
   1.229 +     SDL_DestroyWindow(window);
   1.230 +     window = NULL;
   1.231 +     SDLTest_AssertPass("SDL_DestroyWindow");
   1.232 +  }
   1.233 +}
   1.234 +
   1.235 +/**
   1.236 + * @brief Check call to SDL_WarpMouseInWindow
   1.237 + * 
   1.238 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_WarpMouseInWindow
   1.239 + */
   1.240 +int
   1.241 +mouse_warpMouseInWindow(void *arg)
   1.242 +{
   1.243 +  	const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT;
   1.244 +  	int numPositions = 6;
   1.245 +  	int xPositions[] = {-1, 0, 1, w-1, w, w+1 };  
   1.246 +  	int yPositions[] = {-1, 0, 1, h-1, h, h+1 };
   1.247 +	int x, y, i, j;
   1.248 +	SDL_Window *window;
   1.249 +	
   1.250 +	/* Create test window */
   1.251 +	window = _createTestWindow();
   1.252 +	if (window == NULL) return TEST_ABORTED;
   1.253 +
   1.254 +	/* Mouse to random position inside window */	
   1.255 +	x = SDLTest_RandomIntegerInRange(1, w-1);
   1.256 +	y = SDLTest_RandomIntegerInRange(1, h-1);
   1.257 +	SDL_WarpMouseInWindow(window, x, y);
   1.258 +	SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
   1.259 +
   1.260 +        /* Same position again */
   1.261 +   	SDL_WarpMouseInWindow(window, x, y);
   1.262 +	SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
   1.263 +
   1.264 +	/* Mouse to various boundary positions */
   1.265 +	for (i=0; i<numPositions; i++) {	
   1.266 +	  for (j=0; j<numPositions; j++) {	
   1.267 +	    x = xPositions[i];
   1.268 +	    y = yPositions[j];
   1.269 +	    SDL_WarpMouseInWindow(window, x, y);
   1.270 +	    SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
   1.271 +
   1.272 +	    /* TODO: add tracking of events and check that each call generates a mouse motion event */
   1.273 +	    SDL_PumpEvents();
   1.274 +	    SDLTest_AssertPass("SDL_PumpEvents()");
   1.275 +	  }
   1.276 +	}
   1.277 +	
   1.278 +
   1.279 +        /* Clean up test window */	
   1.280 +	_destroyTestWindow(window);
   1.281 +	
   1.282 +	return TEST_COMPLETED;
   1.283 +}
   1.284 +
   1.285 +/**
   1.286 + * @brief Check call to SDL_GetMouseFocus
   1.287 + * 
   1.288 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetMouseFocus
   1.289 + */
   1.290 +int
   1.291 +mouse_getMouseFocus(void *arg)
   1.292 +{
   1.293 +  	const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT;
   1.294 +  	int x, y;
   1.295 +	SDL_Window *window;
   1.296 +	SDL_Window *focusWindow;	
   1.297 +
   1.298 +	/* Get focus - focus non-deterministic */
   1.299 +	focusWindow = SDL_GetMouseFocus();
   1.300 +	SDLTest_AssertPass("SDL_GetMouseFocus()");
   1.301 +
   1.302 +        /* Create test window */	
   1.303 +	window = _createTestWindow();
   1.304 +	if (window == NULL) return TEST_ABORTED;
   1.305 +
   1.306 +	/* Mouse to random position inside window */	
   1.307 +	x = SDLTest_RandomIntegerInRange(1, w-1);
   1.308 +	y = SDLTest_RandomIntegerInRange(1, h-1);
   1.309 +	SDL_WarpMouseInWindow(window, x, y);
   1.310 +	SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
   1.311 +
   1.312 +	/* Pump events to update focus state */
   1.313 +	SDL_PumpEvents();
   1.314 +	SDLTest_AssertPass("SDL_PumpEvents()");
   1.315 +
   1.316 +        /* Get focus with explicit window setup - focus deterministic */	
   1.317 +	focusWindow = SDL_GetMouseFocus();
   1.318 +	SDLTest_AssertPass("SDL_GetMouseFocus()");
   1.319 +	SDLTest_AssertCheck (focusWindow != NULL, "Check returned window value is not NULL");
   1.320 +	SDLTest_AssertCheck (focusWindow == window, "Check returned window value is test window");
   1.321 +
   1.322 +	/* Mouse to random position outside window */	
   1.323 +	x = SDLTest_RandomIntegerInRange(-9, -1);
   1.324 +	y = SDLTest_RandomIntegerInRange(-9, -1);
   1.325 +	SDL_WarpMouseInWindow(window, x, y);
   1.326 +	SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
   1.327 +
   1.328 +        /* Clean up test window */	
   1.329 +	_destroyTestWindow(window);
   1.330 +
   1.331 +	/* Pump events to update focus state */
   1.332 +	SDL_PumpEvents();
   1.333 +	SDLTest_AssertPass("SDL_PumpEvents()");
   1.334 +
   1.335 +        /* Get focus for non-existing window */	
   1.336 +	focusWindow = SDL_GetMouseFocus();
   1.337 +	SDLTest_AssertPass("SDL_GetMouseFocus()");
   1.338 +	SDLTest_AssertCheck (focusWindow == NULL, "Check returned window value is NULL");
   1.339 +
   1.340 +	
   1.341 +	return TEST_COMPLETED;
   1.342 +}
   1.343 +
   1.344  /* ================= Test References ================== */
   1.345  
   1.346  /* Mouse test cases */
   1.347 @@ -317,9 +454,15 @@
   1.348  static const SDLTest_TestCaseReference mouseTest5 =
   1.349  		{ (SDLTest_TestCaseFp)mouse_setCursor, "mouse_setCursor", "Check call to SDL_SetCursor", TEST_ENABLED };
   1.350  
   1.351 +static const SDLTest_TestCaseReference mouseTest6 =
   1.352 +		{ (SDLTest_TestCaseFp)mouse_warpMouseInWindow, "mouse_warpMouseInWindow", "Check call to SDL_WarpMouseInWindow", TEST_ENABLED };
   1.353 +
   1.354 +static const SDLTest_TestCaseReference mouseTest7 =
   1.355 +		{ (SDLTest_TestCaseFp)mouse_getMouseFocus, "mouse_getMouseFocus", "Check call to SDL_getMouseFocus", TEST_ENABLED };
   1.356 +
   1.357  /* Sequence of Mouse test cases */
   1.358  static const SDLTest_TestCaseReference *mouseTests[] =  {
   1.359 -	&mouseTest1, &mouseTest2, &mouseTest3, &mouseTest4, &mouseTest5, NULL
   1.360 +	&mouseTest1, &mouseTest2, &mouseTest3, &mouseTest4, &mouseTest5, &mouseTest6, &mouseTest7, NULL
   1.361  };
   1.362  
   1.363  /* Mouse test suite (global) */