test/testautomation_mouse.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Sun, 06 Jan 2013 18:19:30 -0800
changeset 6801 f92e3d5f71a2
child 6821 f36771112651
permissions -rw-r--r--
Add new test suite (mouse) with a few new tests
     1 /**
     2  * Mouse test suite
     3  */
     4 
     5 #include <stdio.h>
     6 #include <limits.h>
     7 
     8 #include "SDL.h"
     9 #include "SDL_test.h"
    10 
    11 /* ================= Test Case Implementation ================== */
    12 
    13 /* Test case functions */
    14 
    15 /* Helper to evaluate state returned from SDL_GetMouseState */
    16 int _mouseStateCheck(Uint8 state)
    17 {
    18   return (state == 0) || 
    19          SDL_BUTTON(SDL_BUTTON_LEFT) || 
    20          SDL_BUTTON(SDL_BUTTON_MIDDLE) || 
    21          SDL_BUTTON(SDL_BUTTON_RIGHT) || 
    22          SDL_BUTTON(SDL_BUTTON_X1) || 
    23          SDL_BUTTON(SDL_BUTTON_X2);
    24 }
    25 
    26 /**
    27  * @brief Check call to SDL_GetMouseState
    28  * 
    29  */
    30 int
    31 mouse_getMouseState(void *arg)
    32 {
    33    int x;
    34    int y;
    35    Uint8 state;
    36 
    37    /* Pump some events to update mouse state */
    38    SDL_PumpEvents();
    39    SDLTest_AssertPass("Call to SDL_PumpEvents()");
    40 
    41    /* Case where x, y pointer is NULL */    
    42    state = SDL_GetMouseState(NULL, NULL);
    43    SDLTest_AssertPass("Call to SDL_GetMouseState(NULL, NULL)");
    44    SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
    45 
    46    /* Case where x pointer is not NULL */
    47    x = INT_MIN;
    48    state = SDL_GetMouseState(&x, NULL);
    49    SDLTest_AssertPass("Call to SDL_GetMouseState(&x, NULL)");
    50    SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
    51    SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
    52 
    53    /* Case where y pointer is not NULL */
    54    y = INT_MIN;
    55    state = SDL_GetMouseState(NULL, &y);
    56    SDLTest_AssertPass("Call to SDL_GetMouseState(NULL, &y)");
    57    SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
    58    SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
    59 
    60    /* Case where x and y pointer is not NULL */
    61    x = INT_MIN;
    62    y = INT_MIN;
    63    state = SDL_GetMouseState(&x, &y);
    64    SDLTest_AssertPass("Call to SDL_GetMouseState(&x, &y)");
    65    SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
    66    SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
    67    SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
    68    
    69    return TEST_COMPLETED;
    70 }
    71 
    72 /**
    73  * @brief Check call to SDL_GetRelativeMouseState
    74  * 
    75  */
    76 int
    77 mouse_getRelativeMouseState(void *arg)
    78 {
    79    int x;
    80    int y;
    81    Uint8 state;
    82 
    83    /* Pump some events to update mouse state */
    84    SDL_PumpEvents();
    85    SDLTest_AssertPass("Call to SDL_PumpEvents()");
    86 
    87    /* Case where x, y pointer is NULL */    
    88    state = SDL_GetRelativeMouseState(NULL, NULL);
    89    SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(NULL, NULL)");
    90    SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
    91 
    92    /* Case where x pointer is not NULL */
    93    x = INT_MIN;
    94    state = SDL_GetRelativeMouseState(&x, NULL);
    95    SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(&x, NULL)");
    96    SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
    97    SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
    98 
    99    /* Case where y pointer is not NULL */
   100    y = INT_MIN;
   101    state = SDL_GetRelativeMouseState(NULL, &y);
   102    SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(NULL, &y)");
   103    SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
   104    SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
   105 
   106    /* Case where x and y pointer is not NULL */
   107    x = INT_MIN;
   108    y = INT_MIN;
   109    state = SDL_GetRelativeMouseState(&x, &y);
   110    SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(&x, &y)");
   111    SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
   112    SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
   113    SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
   114    
   115    return TEST_COMPLETED;
   116 }
   117 
   118 
   119 /* XPM definition of mouse Cursor */
   120 static const char *_mouseArrowData[] = {
   121   /* pixels */
   122   "X                               ",
   123   "XX                              ",
   124   "X.X                             ",
   125   "X..X                            ",
   126   "X...X                           ",
   127   "X....X                          ",
   128   "X.....X                         ",
   129   "X......X                        ",
   130   "X.......X                       ",
   131   "X........X                      ",
   132   "X.....XXXXX                     ",
   133   "X..X..X                         ",
   134   "X.X X..X                        ",
   135   "XX  X..X                        ",
   136   "X    X..X                       ",
   137   "     X..X                       ",
   138   "      X..X                      ",
   139   "      X..X                      ",
   140   "       XX                       ",
   141   "                                ",
   142   "                                ",
   143   "                                ",
   144   "                                ",
   145   "                                ",
   146   "                                ",
   147   "                                ",
   148   "                                ",
   149   "                                ",
   150   "                                ",
   151   "                                ",
   152   "                                ",
   153   "                                "
   154 };
   155 
   156 /* Helper that creates a new mouse cursor from an XPM */
   157 static SDL_Cursor *_initArrowCursor(const char *image[])
   158 {
   159   SDL_Cursor *cursor;
   160   int i, row, col;
   161   Uint8 data[4*32];
   162   Uint8 mask[4*32];
   163 
   164   i = -1;
   165   for ( row=0; row<32; ++row ) {
   166     for ( col=0; col<32; ++col ) {
   167       if ( col % 8 ) {
   168         data[i] <<= 1;
   169         mask[i] <<= 1;
   170       } else {
   171         ++i;
   172         data[i] = mask[i] = 0;
   173       }
   174       switch (image[row][col]) {
   175         case 'X':
   176           data[i] |= 0x01;
   177           mask[i] |= 0x01;
   178           break;
   179         case '.':
   180           mask[i] |= 0x01;
   181           break;
   182         case ' ':
   183           break;
   184       }
   185     }
   186   }
   187 
   188   cursor = SDL_CreateCursor(data, mask, 32, 32, 0, 0);
   189   return cursor;
   190 }
   191 
   192 /**
   193  * @brief Check call to SDL_CreateCursor and SDL_FreeCursor
   194  * 
   195  * @sa http://wiki.libsdl.org/moin.cgi/SDL_CreateCursor
   196  * @sa http://wiki.libsdl.org/moin.cgi/SDL_FreeCursor
   197  */
   198 int
   199 mouse_createFreeCursor(void *arg)
   200 {
   201 	SDL_Cursor *cursor;	
   202 
   203 	/* Create a cursor */
   204 	cursor = _initArrowCursor(_mouseArrowData);
   205     SDLTest_AssertPass("Call to SDL_CreateCursor()");
   206     SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");	
   207 	if (cursor == NULL) {
   208 		return TEST_ABORTED;
   209 	}
   210 
   211 	/* Free cursor again */
   212 	SDL_FreeCursor(cursor);
   213 	SDLTest_AssertPass("Call to SDL_FreeCursor()");
   214 
   215 	return TEST_COMPLETED;
   216 }
   217 
   218 /* Helper that changes cursor visibility */
   219 void _changeCursorVisibility(int state)
   220 {
   221 	int oldState;
   222 	int newState;
   223 	int result;
   224 
   225     oldState = SDL_ShowCursor(SDL_QUERY);
   226 	SDLTest_AssertPass("Call to SDL_ShowCursor(SDL_QUERY)");
   227 
   228     result = SDL_ShowCursor(state);
   229 	SDLTest_AssertPass("Call to SDL_ShowCursor(%s)", (state == SDL_ENABLE) ? "SDL_ENABLE" : "SDL_DISABLE");
   230 	SDLTest_AssertCheck(result == oldState, "Validate result from SDL_ShowCursor(%s), expected: %i, got: %i", 
   231 		(state == SDL_ENABLE) ? "SDL_ENABLE" : "SDL_DISABLE", oldState, result);
   232     
   233 	newState = SDL_ShowCursor(SDL_QUERY);
   234 	SDLTest_AssertPass("Call to SDL_ShowCursor(SDL_QUERY)");
   235 	SDLTest_AssertCheck(state == newState, "Validate new state, expected: %i, got: %i", 
   236 		state, newState);
   237 }
   238 
   239 /**
   240  * @brief Check call to SDL_ShowCursor
   241  * 
   242  * @sa http://wiki.libsdl.org/moin.cgi/SDL_ShowCursor
   243  */
   244 int
   245 mouse_showCursor(void *arg)
   246 {
   247 	int currentState;
   248 
   249 	/* Get current state */
   250 	currentState = SDL_ShowCursor(SDL_QUERY);
   251 	SDLTest_AssertPass("Call to SDL_ShowCursor(SDL_QUERY)");
   252 	SDLTest_AssertCheck(currentState == SDL_DISABLE || currentState == SDL_ENABLE, 
   253 		"Validate result is %i or %i, got: %i", SDL_DISABLE, SDL_ENABLE, currentState);
   254 	if (currentState == SDL_DISABLE) {
   255 		/* Show the cursor, then hide it again */
   256 		_changeCursorVisibility(SDL_ENABLE);
   257 		_changeCursorVisibility(SDL_DISABLE);
   258 	} else if (currentState == SDL_ENABLE) {
   259 		/* Hide the cursor, then show it again */
   260 		_changeCursorVisibility(SDL_DISABLE);
   261 		_changeCursorVisibility(SDL_ENABLE);
   262 	} else {
   263 		return TEST_ABORTED;
   264 	}
   265 
   266 	return TEST_COMPLETED;
   267 }
   268 
   269 /**
   270  * @brief Check call to SDL_SetCursor
   271  * 
   272  * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetCursor
   273  */
   274 int
   275 mouse_setCursor(void *arg)
   276 {
   277 	SDL_Cursor *cursor;	
   278 
   279 	/* Create a cursor */
   280 	cursor = _initArrowCursor(_mouseArrowData);
   281     SDLTest_AssertPass("Call to SDL_CreateCursor()");
   282     SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");	
   283 	if (cursor == NULL) {
   284 		return TEST_ABORTED;
   285 	}
   286 
   287 	/* Set the arrow cursor */
   288 	SDL_SetCursor(cursor);
   289 	SDLTest_AssertPass("Call to SDL_SetCursor(cursor)");
   290 
   291 	/* Force redraw */
   292 	SDL_SetCursor(NULL);
   293 	SDLTest_AssertPass("Call to SDL_SetCursor(NULL)");
   294 
   295 	/* Free cursor again */
   296 	SDL_FreeCursor(cursor);
   297 	SDLTest_AssertPass("Call to SDL_FreeCursor()");
   298 
   299 	return TEST_COMPLETED;
   300 }
   301 
   302 /* ================= Test References ================== */
   303 
   304 /* Mouse test cases */
   305 static const SDLTest_TestCaseReference mouseTest1 =
   306 		{ (SDLTest_TestCaseFp)mouse_getMouseState, "mouse_getMouseState", "Check call to SDL_GetMouseState", TEST_ENABLED };
   307 
   308 static const SDLTest_TestCaseReference mouseTest2 =
   309 		{ (SDLTest_TestCaseFp)mouse_getRelativeMouseState, "mouse_getRelativeMouseState", "Check call to SDL_GetRelativeMouseState", TEST_ENABLED };
   310 
   311 static const SDLTest_TestCaseReference mouseTest3 =
   312 		{ (SDLTest_TestCaseFp)mouse_createFreeCursor, "mouse_createFreeCursor", "Check call to SDL_CreateCursor and SDL_FreeCursor", TEST_ENABLED };
   313 
   314 static const SDLTest_TestCaseReference mouseTest4 =
   315 		{ (SDLTest_TestCaseFp)mouse_showCursor, "mouse_showCursor", "Check call to SDL_ShowCursor", TEST_ENABLED };
   316 
   317 static const SDLTest_TestCaseReference mouseTest5 =
   318 		{ (SDLTest_TestCaseFp)mouse_setCursor, "mouse_setCursor", "Check call to SDL_SetCursor", TEST_ENABLED };
   319 
   320 /* Sequence of Mouse test cases */
   321 static const SDLTest_TestCaseReference *mouseTests[] =  {
   322 	&mouseTest1, &mouseTest2, &mouseTest3, &mouseTest4, &mouseTest5, NULL
   323 };
   324 
   325 /* Mouse test suite (global) */
   326 SDLTest_TestSuiteReference mouseTestSuite = {
   327 	"Mouse",
   328 	NULL,
   329 	mouseTests,
   330 	NULL
   331 };