test/testautomation_keyboard.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Fri, 11 Jan 2013 20:36:39 -0800
changeset 6808 3ad413bd1cd6
parent 6807 e3610bc90cf3
child 6811 60d95fcfaf3c
permissions -rw-r--r--
Add new internal error message for invalid parameters; add validation of input rect in SDL_SetTextInputRect; add test cases for SDL_SetTextInputRect to keyboard suite
     1 /**
     2  * Keyboard test suite
     3  */
     4 
     5 #include <stdio.h>
     6 
     7 #include "SDL.h"
     8 #include "SDL_test.h"
     9 
    10 /* ================= Test Case Implementation ================== */
    11 
    12 /* Test case functions */
    13 
    14 /**
    15  * @brief Check call to SDL_GetKeyboardState with and without numkeys reference.
    16  * 
    17  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyboardState
    18  */
    19 int
    20 keyboard_getKeyboardState(void *arg)
    21 {
    22    int numkeys;
    23    Uint8 *state;
    24 
    25    /* Case where numkeys pointer is NULL */    
    26    state = SDL_GetKeyboardState(NULL);
    27    SDLTest_AssertPass("Call to SDL_GetKeyboardState(NULL)");
    28    SDLTest_AssertCheck(state != NULL, "Validate that return value from SDL_GetKeyboardState is not NULL");
    29 
    30    /* Case where numkeys pointer is not NULL */
    31    numkeys = -1;
    32    state = SDL_GetKeyboardState(&numkeys);
    33    SDLTest_AssertPass("Call to SDL_GetKeyboardState(&numkeys)");
    34    SDLTest_AssertCheck(state != NULL, "Validate that return value from SDL_GetKeyboardState is not NULL");
    35    SDLTest_AssertCheck(numkeys >= 0, "Validate that value of numkeys is >= 0, got: %i", numkeys);
    36    
    37    return TEST_COMPLETED;
    38 }
    39 
    40 /**
    41  * @brief Check call to SDL_GetKeyboardFocus
    42  * 
    43  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyboardFocus
    44  */
    45 int
    46 keyboard_getKeyboardFocus(void *arg)
    47 {
    48    SDL_Window* window;
    49 
    50    /* Call, but ignore return value */
    51    window = SDL_GetKeyboardFocus();
    52    SDLTest_AssertPass("Call to SDL_GetKeyboardFocus()");
    53 
    54    return TEST_COMPLETED;
    55 }
    56 
    57 /**
    58  * @brief Check call to SDL_GetKeyFromName for known, unknown and invalid name.
    59  * 
    60  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyFromName
    61  */
    62 int
    63 keyboard_getKeyFromName(void *arg)
    64 {
    65    SDL_Keycode result;
    66 
    67    /* Case where Key is known, 1 character input */
    68    result = SDL_GetKeyFromName("A");
    69    SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/single)");
    70    SDLTest_AssertCheck(result == SDLK_a, "Verify result from call, expected: %i, got: %i", SDLK_a, result);
    71 
    72    /* Case where Key is known, 2 character input */
    73    result = SDL_GetKeyFromName("F1");
    74    SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/double)");
    75    SDLTest_AssertCheck(result == SDLK_F1, "Verify result from call, expected: %i, got: %i", SDLK_F1, result);
    76 
    77    /* Case where Key is known, 3 character input */
    78    result = SDL_GetKeyFromName("End");
    79    SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/triple)");
    80    SDLTest_AssertCheck(result == SDLK_END, "Verify result from call, expected: %i, got: %i", SDLK_END, result);
    81 
    82    /* Case where Key is known, 4 character input */
    83    result = SDL_GetKeyFromName("Find");
    84    SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/quad)");
    85    SDLTest_AssertCheck(result == SDLK_FIND, "Verify result from call, expected: %i, got: %i", SDLK_FIND, result);
    86 
    87    /* Case where Key is known, multiple character input */
    88    result = SDL_GetKeyFromName("AudioStop");
    89    SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/multi)");
    90    SDLTest_AssertCheck(result == SDLK_AUDIOSTOP, "Verify result from call, expected: %i, got: %i", SDLK_AUDIOSTOP, result);
    91 
    92    /* Case where Key is unknown */
    93    result = SDL_GetKeyFromName("NotThere");
    94    SDLTest_AssertPass("Call to SDL_GetKeyFromName(unknown)");
    95    SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
    96 
    97    /* Case where input is NULL/invalid */
    98    result = SDL_GetKeyFromName(NULL);
    99    SDLTest_AssertPass("Call to SDL_GetKeyFromName(NULL)");
   100    SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
   101 
   102    return TEST_COMPLETED;
   103 }
   104 
   105 /**
   106  * @brief Check call to SDL_GetKeyFromScancode
   107  * 
   108  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyFromScancode
   109  */
   110 int
   111 keyboard_getKeyFromScancode(void *arg)
   112 {
   113    SDL_Keycode result;
   114 
   115    /* Case where input is valid */
   116    result = SDL_GetKeyFromScancode(SDL_SCANCODE_A);
   117    SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(valid)");
   118    SDLTest_AssertCheck(result == SDLK_a, "Verify result from call, expected: %i, got: %i", SDLK_a, result);
   119 
   120    /* Case where input is zero */
   121    result = SDL_GetKeyFromScancode(0);
   122    SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(zero)");
   123    SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
   124 
   125    /* Case where input is invalid */
   126    result = SDL_GetKeyFromScancode(-999);
   127    SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(invalid)");
   128    SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
   129 
   130    return TEST_COMPLETED;
   131 }
   132 
   133 /**
   134  * @brief Check call to SDL_GetKeyName
   135  * 
   136  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyName
   137  */
   138 int
   139 keyboard_getKeyName(void *arg)
   140 {   
   141    char *result;
   142    char *expected;
   143 
   144    /* Case where key has a 1 character name */
   145    expected = "3";
   146    result = (char *)SDL_GetKeyName(SDLK_3);
   147    SDLTest_AssertPass("Call to SDL_GetKeyName()");
   148    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   149    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
   150 
   151    /* Case where key has a 2 character name */
   152    expected = "F1";
   153    result = (char *)SDL_GetKeyName(SDLK_F1);
   154    SDLTest_AssertPass("Call to SDL_GetKeyName()");
   155    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   156    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
   157 
   158    /* Case where key has a 3 character name */
   159    expected = "Cut";
   160    result = (char *)SDL_GetKeyName(SDLK_CUT);
   161    SDLTest_AssertPass("Call to SDL_GetKeyName()");
   162    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   163    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
   164 
   165    /* Case where key has a 4 character name */
   166    expected = "Down";
   167    result = (char *)SDL_GetKeyName(SDLK_DOWN);
   168    SDLTest_AssertPass("Call to SDL_GetKeyName()");
   169    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   170    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
   171 
   172    /* Case where key has a N character name */
   173    expected = "BrightnessUp";
   174    result = (char *)SDL_GetKeyName(SDLK_BRIGHTNESSUP);
   175    SDLTest_AssertPass("Call to SDL_GetKeyName()");
   176    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   177    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
   178 
   179    /* Case where key has a N character name with space */
   180    expected = "Keypad MemStore";
   181    result = (char *)SDL_GetKeyName(SDLK_KP_MEMSTORE);
   182    SDLTest_AssertPass("Call to SDL_GetKeyName()");
   183    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   184    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
   185 
   186    return TEST_COMPLETED;
   187 }
   188 
   189 /**
   190  * @brief Check call to SDL_GetModState and SDL_SetModState
   191  * 
   192  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetModState
   193  * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetModState
   194  */
   195 int
   196 keyboard_getSetModState(void *arg)
   197 {   
   198    SDL_Keymod result;
   199    SDL_Keymod currentState;
   200    SDL_Keymod newState;
   201    SDL_Keymod allStates =     
   202     KMOD_NONE |
   203     KMOD_LSHIFT |
   204     KMOD_RSHIFT |
   205     KMOD_LCTRL |
   206     KMOD_RCTRL |
   207     KMOD_LALT |
   208     KMOD_RALT |
   209     KMOD_LGUI |
   210     KMOD_RGUI |
   211     KMOD_NUM |
   212     KMOD_CAPS |
   213     KMOD_MODE |
   214     KMOD_RESERVED;
   215 
   216    /* Get state, cache for later reset */                                                   
   217    result = SDL_GetModState();
   218    SDLTest_AssertPass("Call to SDL_GetModState()");
   219    SDLTest_AssertCheck(result >=0 && result <= allStates, "Verify result from call is valid, expected: 0 <= result <= %i, got: %i", allStates, result);   
   220    currentState = result;
   221 
   222    /* Set random state */   
   223    newState = SDLTest_RandomIntegerInRange(0, allStates);
   224    SDL_SetModState(newState);
   225    SDLTest_AssertPass("Call to SDL_SetModState(%i)", newState);
   226    result = SDL_GetModState();
   227    SDLTest_AssertPass("Call to SDL_GetModState()");
   228    SDLTest_AssertCheck(result == newState, "Verify result from call is valid, expected: %i, got: %i", newState, result);
   229 
   230    /* Set zero state */
   231    SDL_SetModState(0);
   232    SDLTest_AssertPass("Call to SDL_SetModState(0)");
   233    result = SDL_GetModState();
   234    SDLTest_AssertPass("Call to SDL_GetModState()");
   235    SDLTest_AssertCheck(result == 0, "Verify result from call is valid, expected: 0, got: %i", result);
   236 
   237    /* Revert back to cached current state if needed */
   238    if (currentState != 0) {
   239      SDL_SetModState(currentState);
   240      SDLTest_AssertPass("Call to SDL_SetModState(%i)", currentState);
   241      result = SDL_GetModState();
   242      SDLTest_AssertPass("Call to SDL_GetModState()");
   243      SDLTest_AssertCheck(result == currentState, "Verify result from call is valid, expected: %i, got: %i", currentState, result);
   244    }
   245 
   246    return TEST_COMPLETED;
   247 }
   248 
   249 
   250 /**
   251  * @brief Check call to SDL_StartTextInput and SDL_StopTextInput
   252  * 
   253  * @sa http://wiki.libsdl.org/moin.cgi/SDL_StartTextInput
   254  * @sa http://wiki.libsdl.org/moin.cgi/SDL_StopTextInput
   255  */
   256 int
   257 keyboard_startStopTextInput(void *arg)
   258 {   
   259    /* Start-Stop */
   260    SDL_StartTextInput();
   261    SDLTest_AssertPass("Call to SDL_StartTextInput()");
   262    SDL_StopTextInput();
   263    SDLTest_AssertPass("Call to SDL_StopTextInput()");
   264 
   265    /* Stop-Start */
   266    SDL_StartTextInput();
   267    SDLTest_AssertPass("Call to SDL_StartTextInput()");
   268    
   269    /* Start-Start */
   270    SDL_StartTextInput();
   271    SDLTest_AssertPass("Call to SDL_StartTextInput()");
   272 
   273    /* Stop-Stop */   
   274    SDL_StopTextInput();
   275    SDLTest_AssertPass("Call to SDL_StopTextInput()");
   276    SDL_StopTextInput();
   277    SDLTest_AssertPass("Call to SDL_StopTextInput()");
   278 
   279    return TEST_COMPLETED;
   280 }
   281 
   282 /* Internal function to test SDL_SetTextInputRect */
   283 void _testSetTextInputRect(SDL_Rect refRect)
   284 {
   285    SDL_Rect testRect;
   286    
   287    testRect = refRect;
   288    SDL_SetTextInputRect(&testRect);
   289    SDLTest_AssertPass("Call to SDL_SetTextInputRect with refRect(x:%i,y:%i,w:%i,h:%i)", refRect.x, refRect.y, refRect.w, refRect.h);
   290    SDLTest_AssertCheck(
   291       (refRect.x == testRect.x) && (refRect.y == testRect.y) && (refRect.w == testRect.w) && (refRect.h == testRect.h), 
   292       "Check that input data was not modified, expected: x:%i,y:%i,w:%i,h:%i, got: x:%i,y:%i,w:%i,h:%i", 
   293       refRect.x, refRect.y, refRect.w, refRect.h, 
   294       testRect.x, testRect.y, testRect.w, testRect.h);
   295 }
   296 
   297 /**
   298  * @brief Check call to SDL_SetTextInputRect
   299  * 
   300  * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetTextInputRect
   301  */
   302 int
   303 keyboard_setTextInputRect(void *arg)
   304 {   
   305    SDL_Rect refRect;
   306    
   307    /* Normal visible refRect, origin inside */
   308    refRect.x = SDLTest_RandomIntegerInRange(1, 50);;
   309    refRect.y = SDLTest_RandomIntegerInRange(1, 50);;
   310    refRect.w = SDLTest_RandomIntegerInRange(10, 50);
   311    refRect.h = SDLTest_RandomIntegerInRange(10, 50);
   312    _testSetTextInputRect(refRect);
   313    
   314    /* Normal visible refRect, origin 0,0 */
   315    refRect.x = 0;
   316    refRect.y = 0;
   317    refRect.w = SDLTest_RandomIntegerInRange(10, 50);
   318    refRect.h = SDLTest_RandomIntegerInRange(10, 50);
   319    _testSetTextInputRect(refRect);
   320 
   321    /* 1Pixel refRect */
   322    refRect.x = SDLTest_RandomIntegerInRange(10, 50);;
   323    refRect.y = SDLTest_RandomIntegerInRange(10, 50);;
   324    refRect.w = 1;
   325    refRect.h = 1;
   326    _testSetTextInputRect(refRect);
   327 
   328    /* 0pixel refRect */
   329    refRect.x = 1;
   330    refRect.y = 1;
   331    refRect.w = 1;
   332    refRect.h = 0;
   333    _testSetTextInputRect(refRect);
   334 
   335    /* 0pixel refRect */
   336    refRect.x = 1;
   337    refRect.y = 1;
   338    refRect.w = 0;
   339    refRect.h = 1;
   340    _testSetTextInputRect(refRect);
   341 
   342    /* 0pixel refRect */
   343    refRect.x = 1;
   344    refRect.y = 1;
   345    refRect.w = 0;
   346    refRect.h = 0;
   347    _testSetTextInputRect(refRect);
   348 
   349    /* 0pixel refRect */
   350    refRect.x = 0;
   351    refRect.y = 0;
   352    refRect.w = 0;
   353    refRect.h = 0;
   354    _testSetTextInputRect(refRect);
   355 
   356    /* negative refRect */
   357    refRect.x = SDLTest_RandomIntegerInRange(-200, -100);;
   358    refRect.y = SDLTest_RandomIntegerInRange(-200, -100);;
   359    refRect.w = 50;
   360    refRect.h = 50;
   361    _testSetTextInputRect(refRect);
   362 
   363    /* oversized refRect */
   364    refRect.x = SDLTest_RandomIntegerInRange(1, 50);;
   365    refRect.y = SDLTest_RandomIntegerInRange(1, 50);;
   366    refRect.w = 5000;
   367    refRect.h = 5000;
   368    _testSetTextInputRect(refRect);
   369 
   370    /* NULL refRect */
   371    SDL_SetTextInputRect(NULL);
   372    SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)");
   373 
   374    return TEST_COMPLETED;
   375 }
   376 
   377 /**
   378  * @brief Check call to SDL_SetTextInputRect with invalid data
   379  * 
   380  * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetTextInputRect
   381  */
   382 int
   383 keyboard_setTextInputRectNegative(void *arg)
   384 {      
   385    const char *expectedError = "Parameter is invalid";
   386    const char *error;
   387    
   388    SDL_ClearError();
   389    SDLTest_AssertPass("Call to SDL_ClearError()");
   390    
   391    /* NULL refRect */
   392    SDL_SetTextInputRect(NULL);
   393    SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)");
   394    error = SDL_GetError();
   395    SDLTest_AssertPass("Call to SDL_GetError()");
   396    SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   397    if (error != NULL) {
   398       SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
   399           "Validate error message, expected: '%s', got: '%s'", expectedError, error);
   400    }
   401 
   402    SDL_ClearError();
   403    SDLTest_AssertPass("Call to SDL_ClearError()");
   404       
   405    return TEST_COMPLETED;
   406 }
   407 
   408 
   409 
   410 /* ================= Test References ================== */
   411 
   412 /* Keyboard test cases */
   413 static const SDLTest_TestCaseReference keyboardTest1 =
   414 		{ (SDLTest_TestCaseFp)keyboard_getKeyboardState, "keyboard_getKeyboardState", "Check call to SDL_GetKeyboardState with and without numkeys reference", TEST_ENABLED };
   415 
   416 static const SDLTest_TestCaseReference keyboardTest2 =
   417 		{ (SDLTest_TestCaseFp)keyboard_getKeyboardFocus, "keyboard_getKeyboardFocus", "Check call to SDL_GetKeyboardFocus", TEST_ENABLED };
   418 
   419 static const SDLTest_TestCaseReference keyboardTest3 =
   420 		{ (SDLTest_TestCaseFp)keyboard_getKeyFromName, "keyboard_getKeyFromName", "Check call to SDL_GetKeyFromName for known, unknown and invalid name", TEST_ENABLED };
   421 
   422 static const SDLTest_TestCaseReference keyboardTest4 =
   423 		{ (SDLTest_TestCaseFp)keyboard_getKeyFromScancode, "keyboard_getKeyFromScancode", "Check call to SDL_GetKeyFromScancode", TEST_ENABLED };
   424 
   425 static const SDLTest_TestCaseReference keyboardTest5 =
   426 		{ (SDLTest_TestCaseFp)keyboard_getKeyName, "keyboard_getKeyName", "Check call to SDL_GetKeyName", TEST_ENABLED };
   427 
   428 static const SDLTest_TestCaseReference keyboardTest6 =
   429 		{ (SDLTest_TestCaseFp)keyboard_getSetModState, "keyboard_getSetModState", "Check call to SDL_GetModState and SDL_SetModState", TEST_ENABLED };
   430 
   431 static const SDLTest_TestCaseReference keyboardTest7 =
   432 		{ (SDLTest_TestCaseFp)keyboard_startStopTextInput, "keyboard_startStopTextInput", "Check call to SDL_StartTextInput and SDL_StopTextInput", TEST_ENABLED };
   433 
   434 static const SDLTest_TestCaseReference keyboardTest8 =
   435 		{ (SDLTest_TestCaseFp)keyboard_setTextInputRect, "keyboard_setTextInputRect", "Check call to SDL_SetTextInputRect", TEST_ENABLED };
   436 
   437 static const SDLTest_TestCaseReference keyboardTest9 =
   438 		{ (SDLTest_TestCaseFp)keyboard_setTextInputRectNegative, "keyboard_setTextInputRectNegative", "Check call to SDL_SetTextInputRect with invalid data", TEST_ENABLED };
   439 
   440 /* Sequence of Keyboard test cases */
   441 static const SDLTest_TestCaseReference *keyboardTests[] =  {
   442 	&keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, &keyboardTest5, &keyboardTest6, 
   443 	&keyboardTest7, &keyboardTest8, &keyboardTest9, NULL
   444 };
   445 
   446 /* Keyboard test suite (global) */
   447 SDLTest_TestSuiteReference keyboardTestSuite = {
   448 	"Keyboard",
   449 	NULL,
   450 	keyboardTests,
   451 	NULL
   452 };