test/testautomation_keyboard.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Fri, 08 Mar 2013 23:04:53 -0800
changeset 6983 b72f56ab9867
parent 6813 b21879e67db2
child 7172 b3569dff1c6e
permissions -rw-r--r--
Fix Bug 1533 - SDL_Keycode value range allows segfaults with negative values; add test coverage to keyboard suite
     1 /**
     2  * Keyboard test suite
     3  */
     4 
     5 #include <stdio.h>
     6 #include <limits.h>
     7 
     8 #include "SDL_config.h"
     9 #include "SDL.h"
    10 #include "SDL_test.h"
    11 
    12 /* ================= Test Case Implementation ================== */
    13 
    14 /* Test case functions */
    15 
    16 /**
    17  * @brief Check call to SDL_GetKeyboardState with and without numkeys reference.
    18  * 
    19  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyboardState
    20  */
    21 int
    22 keyboard_getKeyboardState(void *arg)
    23 {
    24    int numkeys;
    25    Uint8 *state;
    26 
    27    /* Case where numkeys pointer is NULL */    
    28    state = SDL_GetKeyboardState(NULL);
    29    SDLTest_AssertPass("Call to SDL_GetKeyboardState(NULL)");
    30    SDLTest_AssertCheck(state != NULL, "Validate that return value from SDL_GetKeyboardState is not NULL");
    31 
    32    /* Case where numkeys pointer is not NULL */
    33    numkeys = -1;
    34    state = SDL_GetKeyboardState(&numkeys);
    35    SDLTest_AssertPass("Call to SDL_GetKeyboardState(&numkeys)");
    36    SDLTest_AssertCheck(state != NULL, "Validate that return value from SDL_GetKeyboardState is not NULL");
    37    SDLTest_AssertCheck(numkeys >= 0, "Validate that value of numkeys is >= 0, got: %i", numkeys);
    38    
    39    return TEST_COMPLETED;
    40 }
    41 
    42 /**
    43  * @brief Check call to SDL_GetKeyboardFocus
    44  * 
    45  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyboardFocus
    46  */
    47 int
    48 keyboard_getKeyboardFocus(void *arg)
    49 {
    50    SDL_Window* window;
    51 
    52    /* Call, but ignore return value */
    53    window = SDL_GetKeyboardFocus();
    54    SDLTest_AssertPass("Call to SDL_GetKeyboardFocus()");
    55 
    56    return TEST_COMPLETED;
    57 }
    58 
    59 /**
    60  * @brief Check call to SDL_GetKeyFromName for known, unknown and invalid name.
    61  * 
    62  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyFromName
    63  */
    64 int
    65 keyboard_getKeyFromName(void *arg)
    66 {
    67    SDL_Keycode result;
    68 
    69    /* Case where Key is known, 1 character input */
    70    result = SDL_GetKeyFromName("A");
    71    SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/single)");
    72    SDLTest_AssertCheck(result == SDLK_a, "Verify result from call, expected: %i, got: %i", SDLK_a, result);
    73 
    74    /* Case where Key is known, 2 character input */
    75    result = SDL_GetKeyFromName("F1");
    76    SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/double)");
    77    SDLTest_AssertCheck(result == SDLK_F1, "Verify result from call, expected: %i, got: %i", SDLK_F1, result);
    78 
    79    /* Case where Key is known, 3 character input */
    80    result = SDL_GetKeyFromName("End");
    81    SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/triple)");
    82    SDLTest_AssertCheck(result == SDLK_END, "Verify result from call, expected: %i, got: %i", SDLK_END, result);
    83 
    84    /* Case where Key is known, 4 character input */
    85    result = SDL_GetKeyFromName("Find");
    86    SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/quad)");
    87    SDLTest_AssertCheck(result == SDLK_FIND, "Verify result from call, expected: %i, got: %i", SDLK_FIND, result);
    88 
    89    /* Case where Key is known, multiple character input */
    90    result = SDL_GetKeyFromName("AudioStop");
    91    SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/multi)");
    92    SDLTest_AssertCheck(result == SDLK_AUDIOSTOP, "Verify result from call, expected: %i, got: %i", SDLK_AUDIOSTOP, result);
    93 
    94    /* Case where Key is unknown */
    95    result = SDL_GetKeyFromName("NotThere");
    96    SDLTest_AssertPass("Call to SDL_GetKeyFromName(unknown)");
    97    SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
    98 
    99    /* Case where input is NULL/invalid */
   100    result = SDL_GetKeyFromName(NULL);
   101    SDLTest_AssertPass("Call to SDL_GetKeyFromName(NULL)");
   102    SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
   103 
   104    return TEST_COMPLETED;
   105 }
   106 
   107 /* 
   108  * Local helper to check for the invalid scancode error message
   109  */
   110 void
   111 _checkInvalidScancodeError()
   112 {
   113    const char *expectedError = "Parameter 'scancode' is invalid";
   114    const char *error;   
   115    error = SDL_GetError();
   116    SDLTest_AssertPass("Call to SDL_GetError()");
   117    SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   118    if (error != NULL) {
   119       SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
   120           "Validate error message, expected: '%s', got: '%s'", expectedError, error);
   121       SDL_ClearError();
   122       SDLTest_AssertPass("Call to SDL_ClearError()");
   123    }
   124 }
   125 
   126 /**
   127  * @brief Check call to SDL_GetKeyFromScancode
   128  * 
   129  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyFromScancode
   130  */
   131 int
   132 keyboard_getKeyFromScancode(void *arg)
   133 {
   134    SDL_Keycode result;
   135 
   136    /* Case where input is valid */
   137    result = SDL_GetKeyFromScancode(SDL_SCANCODE_A);
   138    SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(valid)");
   139    SDLTest_AssertCheck(result == SDLK_a, "Verify result from call, expected: %i, got: %i", SDLK_a, result);
   140 
   141    /* Case where input is zero */
   142    result = SDL_GetKeyFromScancode(0);
   143    SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(0)");
   144    SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
   145 
   146    /* Clear error message */
   147    SDL_ClearError();
   148    SDLTest_AssertPass("Call to SDL_ClearError()");
   149 
   150    /* Case where input is invalid (too small) */
   151    result = SDL_GetKeyFromScancode(-999);
   152    SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(-999)");
   153    SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
   154    _checkInvalidScancodeError();
   155 
   156    /* Case where input is invalid (too big) */
   157    result = SDL_GetKeyFromScancode(999);
   158    SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(999)");
   159    SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
   160    _checkInvalidScancodeError();
   161 
   162    return TEST_COMPLETED;
   163 }
   164 
   165 /**
   166  * @brief Check call to SDL_GetKeyName
   167  * 
   168  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyName
   169  */
   170 int
   171 keyboard_getKeyName(void *arg)
   172 {   
   173    char *result;
   174    char *expected;
   175 
   176    /* Case where key has a 1 character name */
   177    expected = "3";
   178    result = (char *)SDL_GetKeyName(SDLK_3);
   179    SDLTest_AssertPass("Call to SDL_GetKeyName()");
   180    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   181    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
   182 
   183    /* Case where key has a 2 character name */
   184    expected = "F1";
   185    result = (char *)SDL_GetKeyName(SDLK_F1);
   186    SDLTest_AssertPass("Call to SDL_GetKeyName()");
   187    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   188    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
   189 
   190    /* Case where key has a 3 character name */
   191    expected = "Cut";
   192    result = (char *)SDL_GetKeyName(SDLK_CUT);
   193    SDLTest_AssertPass("Call to SDL_GetKeyName()");
   194    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   195    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
   196 
   197    /* Case where key has a 4 character name */
   198    expected = "Down";
   199    result = (char *)SDL_GetKeyName(SDLK_DOWN);
   200    SDLTest_AssertPass("Call to SDL_GetKeyName()");
   201    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   202    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
   203 
   204    /* Case where key has a N character name */
   205    expected = "BrightnessUp";
   206    result = (char *)SDL_GetKeyName(SDLK_BRIGHTNESSUP);
   207    SDLTest_AssertPass("Call to SDL_GetKeyName()");
   208    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   209    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
   210 
   211    /* Case where key has a N character name with space */
   212    expected = "Keypad MemStore";
   213    result = (char *)SDL_GetKeyName(SDLK_KP_MEMSTORE);
   214    SDLTest_AssertPass("Call to SDL_GetKeyName()");
   215    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   216    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
   217 
   218    return TEST_COMPLETED;
   219 }
   220 
   221 /**
   222  * @brief SDL_GetScancodeName negative cases
   223  * 
   224  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetScancodeName
   225  */
   226 int
   227 keyboard_getScancodeNameNegative(void *arg)
   228 {  
   229    SDL_Scancode scancode;
   230    char *result;
   231    char *expected = "";
   232 
   233    /* Clear error message */
   234    SDL_ClearError();
   235    SDLTest_AssertPass("Call to SDL_ClearError()");
   236 
   237    /* Negative scancode */
   238    scancode = (SDL_Scancode)SDLTest_RandomIntegerInRange(LONG_MIN, -1);
   239    result = (char *)SDL_GetScancodeName(scancode);
   240    SDLTest_AssertPass("Call to SDL_GetScancodeName(%d/negative)", scancode);
   241    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   242    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: '%s', got: '%s'", expected, result);
   243    _checkInvalidScancodeError();
   244 
   245    /* Large scancode */
   246    scancode = (SDL_Scancode)SDLTest_RandomIntegerInRange(SDL_NUM_SCANCODES, LONG_MAX);
   247    result = (char *)SDL_GetScancodeName(scancode);
   248    SDLTest_AssertPass("Call to SDL_GetScancodeName(%d/large)", scancode);
   249    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   250    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: '%s', got: '%s'", expected, result);
   251    _checkInvalidScancodeError();
   252 
   253    return TEST_COMPLETED;
   254 }
   255 
   256 /**
   257  * @brief SDL_GetKeyName negative cases
   258  * 
   259  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyName
   260  */
   261 int
   262 keyboard_getKeyNameNegative(void *arg)
   263 {  
   264    SDL_Keycode keycode;
   265    char *result;
   266    char *expected = "";
   267 
   268    /* Unknown keycode */
   269    keycode = SDLK_UNKNOWN;
   270    result = (char *)SDL_GetKeyName(keycode);
   271    SDLTest_AssertPass("Call to SDL_GetKeyName(%d/unknown)", keycode);
   272    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   273    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: '%s', got: '%s'", expected, result);
   274 
   275    /* Clear error message */
   276    SDL_ClearError();
   277    SDLTest_AssertPass("Call to SDL_ClearError()");
   278 
   279    /* Negative keycode */
   280    keycode = (SDL_Keycode)SDLTest_RandomIntegerInRange(-255, -1);
   281    result = (char *)SDL_GetKeyName(keycode);
   282    SDLTest_AssertPass("Call to SDL_GetKeyName(%d/negative)", keycode);
   283    SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   284    SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: '%s', got: '%s'", expected, result);
   285    _checkInvalidScancodeError();
   286 
   287    SDL_ClearError();
   288    SDLTest_AssertPass("Call to SDL_ClearError()");
   289 
   290    return TEST_COMPLETED;
   291 }
   292 
   293 /**
   294  * @brief Check call to SDL_GetModState and SDL_SetModState
   295  * 
   296  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetModState
   297  * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetModState
   298  */
   299 int
   300 keyboard_getSetModState(void *arg)
   301 {   
   302    SDL_Keymod result;
   303    SDL_Keymod currentState;
   304    SDL_Keymod newState;
   305    SDL_Keymod allStates =     
   306     KMOD_NONE |
   307     KMOD_LSHIFT |
   308     KMOD_RSHIFT |
   309     KMOD_LCTRL |
   310     KMOD_RCTRL |
   311     KMOD_LALT |
   312     KMOD_RALT |
   313     KMOD_LGUI |
   314     KMOD_RGUI |
   315     KMOD_NUM |
   316     KMOD_CAPS |
   317     KMOD_MODE |
   318     KMOD_RESERVED;
   319 
   320    /* Get state, cache for later reset */                                                   
   321    result = SDL_GetModState();
   322    SDLTest_AssertPass("Call to SDL_GetModState()");
   323    SDLTest_AssertCheck(result >=0 && result <= allStates, "Verify result from call is valid, expected: 0 <= result <= %i, got: %i", allStates, result);   
   324    currentState = result;
   325 
   326    /* Set random state */   
   327    newState = SDLTest_RandomIntegerInRange(0, allStates);
   328    SDL_SetModState(newState);
   329    SDLTest_AssertPass("Call to SDL_SetModState(%i)", newState);
   330    result = SDL_GetModState();
   331    SDLTest_AssertPass("Call to SDL_GetModState()");
   332    SDLTest_AssertCheck(result == newState, "Verify result from call is valid, expected: %i, got: %i", newState, result);
   333 
   334    /* Set zero state */
   335    SDL_SetModState(0);
   336    SDLTest_AssertPass("Call to SDL_SetModState(0)");
   337    result = SDL_GetModState();
   338    SDLTest_AssertPass("Call to SDL_GetModState()");
   339    SDLTest_AssertCheck(result == 0, "Verify result from call is valid, expected: 0, got: %i", result);
   340 
   341    /* Revert back to cached current state if needed */
   342    if (currentState != 0) {
   343      SDL_SetModState(currentState);
   344      SDLTest_AssertPass("Call to SDL_SetModState(%i)", currentState);
   345      result = SDL_GetModState();
   346      SDLTest_AssertPass("Call to SDL_GetModState()");
   347      SDLTest_AssertCheck(result == currentState, "Verify result from call is valid, expected: %i, got: %i", currentState, result);
   348    }
   349 
   350    return TEST_COMPLETED;
   351 }
   352 
   353 
   354 /**
   355  * @brief Check call to SDL_StartTextInput and SDL_StopTextInput
   356  * 
   357  * @sa http://wiki.libsdl.org/moin.cgi/SDL_StartTextInput
   358  * @sa http://wiki.libsdl.org/moin.cgi/SDL_StopTextInput
   359  */
   360 int
   361 keyboard_startStopTextInput(void *arg)
   362 {   
   363    /* Start-Stop */
   364    SDL_StartTextInput();
   365    SDLTest_AssertPass("Call to SDL_StartTextInput()");
   366    SDL_StopTextInput();
   367    SDLTest_AssertPass("Call to SDL_StopTextInput()");
   368 
   369    /* Stop-Start */
   370    SDL_StartTextInput();
   371    SDLTest_AssertPass("Call to SDL_StartTextInput()");
   372    
   373    /* Start-Start */
   374    SDL_StartTextInput();
   375    SDLTest_AssertPass("Call to SDL_StartTextInput()");
   376 
   377    /* Stop-Stop */   
   378    SDL_StopTextInput();
   379    SDLTest_AssertPass("Call to SDL_StopTextInput()");
   380    SDL_StopTextInput();
   381    SDLTest_AssertPass("Call to SDL_StopTextInput()");
   382 
   383    return TEST_COMPLETED;
   384 }
   385 
   386 /* Internal function to test SDL_SetTextInputRect */
   387 void _testSetTextInputRect(SDL_Rect refRect)
   388 {
   389    SDL_Rect testRect;
   390    
   391    testRect = refRect;
   392    SDL_SetTextInputRect(&testRect);
   393    SDLTest_AssertPass("Call to SDL_SetTextInputRect with refRect(x:%i,y:%i,w:%i,h:%i)", refRect.x, refRect.y, refRect.w, refRect.h);
   394    SDLTest_AssertCheck(
   395       (refRect.x == testRect.x) && (refRect.y == testRect.y) && (refRect.w == testRect.w) && (refRect.h == testRect.h), 
   396       "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", 
   397       refRect.x, refRect.y, refRect.w, refRect.h, 
   398       testRect.x, testRect.y, testRect.w, testRect.h);
   399 }
   400 
   401 /**
   402  * @brief Check call to SDL_SetTextInputRect
   403  * 
   404  * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetTextInputRect
   405  */
   406 int
   407 keyboard_setTextInputRect(void *arg)
   408 {   
   409    SDL_Rect refRect;
   410    
   411    /* Normal visible refRect, origin inside */
   412    refRect.x = SDLTest_RandomIntegerInRange(1, 50);;
   413    refRect.y = SDLTest_RandomIntegerInRange(1, 50);;
   414    refRect.w = SDLTest_RandomIntegerInRange(10, 50);
   415    refRect.h = SDLTest_RandomIntegerInRange(10, 50);
   416    _testSetTextInputRect(refRect);
   417    
   418    /* Normal visible refRect, origin 0,0 */
   419    refRect.x = 0;
   420    refRect.y = 0;
   421    refRect.w = SDLTest_RandomIntegerInRange(10, 50);
   422    refRect.h = SDLTest_RandomIntegerInRange(10, 50);
   423    _testSetTextInputRect(refRect);
   424 
   425    /* 1Pixel refRect */
   426    refRect.x = SDLTest_RandomIntegerInRange(10, 50);;
   427    refRect.y = SDLTest_RandomIntegerInRange(10, 50);;
   428    refRect.w = 1;
   429    refRect.h = 1;
   430    _testSetTextInputRect(refRect);
   431 
   432    /* 0pixel refRect */
   433    refRect.x = 1;
   434    refRect.y = 1;
   435    refRect.w = 1;
   436    refRect.h = 0;
   437    _testSetTextInputRect(refRect);
   438 
   439    /* 0pixel refRect */
   440    refRect.x = 1;
   441    refRect.y = 1;
   442    refRect.w = 0;
   443    refRect.h = 1;
   444    _testSetTextInputRect(refRect);
   445 
   446    /* 0pixel refRect */
   447    refRect.x = 1;
   448    refRect.y = 1;
   449    refRect.w = 0;
   450    refRect.h = 0;
   451    _testSetTextInputRect(refRect);
   452 
   453    /* 0pixel refRect */
   454    refRect.x = 0;
   455    refRect.y = 0;
   456    refRect.w = 0;
   457    refRect.h = 0;
   458    _testSetTextInputRect(refRect);
   459 
   460    /* negative refRect */
   461    refRect.x = SDLTest_RandomIntegerInRange(-200, -100);;
   462    refRect.y = SDLTest_RandomIntegerInRange(-200, -100);;
   463    refRect.w = 50;
   464    refRect.h = 50;
   465    _testSetTextInputRect(refRect);
   466 
   467    /* oversized refRect */
   468    refRect.x = SDLTest_RandomIntegerInRange(1, 50);;
   469    refRect.y = SDLTest_RandomIntegerInRange(1, 50);;
   470    refRect.w = 5000;
   471    refRect.h = 5000;
   472    _testSetTextInputRect(refRect);
   473 
   474    /* NULL refRect */
   475    SDL_SetTextInputRect(NULL);
   476    SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)");
   477 
   478    return TEST_COMPLETED;
   479 }
   480 
   481 /**
   482  * @brief Check call to SDL_SetTextInputRect with invalid data
   483  * 
   484  * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetTextInputRect
   485  */
   486 int
   487 keyboard_setTextInputRectNegative(void *arg)
   488 {      
   489    /* Some platforms set also an error message; prepare for checking it */
   490 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_COCOA  
   491    const char *expectedError = "Parameter 'rect' is invalid";
   492    const char *error;
   493    
   494    SDL_ClearError();
   495    SDLTest_AssertPass("Call to SDL_ClearError()");
   496 #endif
   497    
   498    /* NULL refRect */
   499    SDL_SetTextInputRect(NULL);
   500    SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)");
   501 
   502    /* Some platforms set also an error message; so check it */
   503 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_COCOA  
   504    error = SDL_GetError();
   505    SDLTest_AssertPass("Call to SDL_GetError()");
   506    SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   507    if (error != NULL) {
   508       SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
   509           "Validate error message, expected: '%s', got: '%s'", expectedError, error);
   510    }
   511 
   512    SDL_ClearError();
   513    SDLTest_AssertPass("Call to SDL_ClearError()");
   514 #endif
   515       
   516    return TEST_COMPLETED;
   517 }
   518 
   519 /**
   520  * @brief Check call to SDL_GetScancodeFromKey
   521  * 
   522  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetScancodeFromKey
   523  * @sa http://wiki.libsdl.org/moin.cgi/SDL_Keycode
   524  */
   525 int
   526 keyboard_getScancodeFromKey(void *arg)
   527 {      
   528    SDL_Scancode scancode;
   529    
   530    /* Regular key */
   531    scancode = SDL_GetScancodeFromKey(SDLK_4);
   532    SDLTest_AssertPass("Call to SDL_GetScancodeFromKey(SDLK_4)");
   533    SDLTest_AssertCheck(scancode == SDL_SCANCODE_4, "Validate return value from SDL_GetScancodeFromKey, expected: %i, got: %i", SDL_SCANCODE_4, scancode); 
   534 
   535    /* Virtual key */
   536    scancode = SDL_GetScancodeFromKey(SDLK_PLUS);
   537    SDLTest_AssertPass("Call to SDL_GetScancodeFromKey(SDLK_PLUS)");
   538    SDLTest_AssertCheck(scancode == 0, "Validate return value from SDL_GetScancodeFromKey, expected: 0, got: %i", scancode); 
   539         
   540    return TEST_COMPLETED;
   541 }
   542 
   543 /**
   544  * @brief Check call to SDL_GetScancodeFromName
   545  * 
   546  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetScancodeFromName
   547  * @sa http://wiki.libsdl.org/moin.cgi/SDL_Keycode
   548  */
   549 int
   550 keyboard_getScancodeFromName(void *arg)
   551 {      
   552    SDL_Scancode scancode;
   553 
   554    /* Regular key, 1 character, first name in list */
   555    scancode = SDL_GetScancodeFromName("A");
   556    SDLTest_AssertPass("Call to SDL_GetScancodeFromName('A')");
   557    SDLTest_AssertCheck(scancode == SDL_SCANCODE_A, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_A, scancode); 
   558 
   559    /* Regular key, 1 character */
   560    scancode = SDL_GetScancodeFromName("4");
   561    SDLTest_AssertPass("Call to SDL_GetScancodeFromName('4')");
   562    SDLTest_AssertCheck(scancode == SDL_SCANCODE_4, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_4, scancode); 
   563 
   564    /* Regular key, 2 characters */
   565    scancode = SDL_GetScancodeFromName("F1");
   566    SDLTest_AssertPass("Call to SDL_GetScancodeFromName('F1')");
   567    SDLTest_AssertCheck(scancode == SDL_SCANCODE_F1, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_F1, scancode); 
   568 
   569    /* Regular key, 3 characters */
   570    scancode = SDL_GetScancodeFromName("End");
   571    SDLTest_AssertPass("Call to SDL_GetScancodeFromName('End')");
   572    SDLTest_AssertCheck(scancode == SDL_SCANCODE_END, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_END, scancode); 
   573 
   574    /* Regular key, 4 characters */
   575    scancode = SDL_GetScancodeFromName("Find");
   576    SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Find')");
   577    SDLTest_AssertCheck(scancode == SDL_SCANCODE_FIND, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_FIND, scancode); 
   578 
   579    /* Regular key, several characters */
   580    scancode = SDL_GetScancodeFromName("Backspace");
   581    SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Backspace')");
   582    SDLTest_AssertCheck(scancode == SDL_SCANCODE_BACKSPACE, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_BACKSPACE, scancode); 
   583 
   584    /* Regular key, several characters with space */
   585    scancode = SDL_GetScancodeFromName("Keypad Enter");
   586    SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Keypad Enter')");
   587    SDLTest_AssertCheck(scancode == SDL_SCANCODE_KP_ENTER, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_KP_ENTER, scancode); 
   588 
   589    /* Regular key, last name in list */
   590    scancode = SDL_GetScancodeFromName("Sleep");
   591    SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Sleep')");
   592    SDLTest_AssertCheck(scancode == SDL_SCANCODE_SLEEP, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_SLEEP, scancode); 
   593    
   594    return TEST_COMPLETED;
   595 }
   596 
   597 /* 
   598  * Local helper to check for the invalid scancode error message
   599  */
   600 void
   601 _checkInvalidNameError()
   602 {
   603    const char *expectedError = "Parameter 'name' is invalid";
   604    const char *error;   
   605    error = SDL_GetError();
   606    SDLTest_AssertPass("Call to SDL_GetError()");
   607    SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   608    if (error != NULL) {
   609       SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
   610           "Validate error message, expected: '%s', got: '%s'", expectedError, error);
   611       SDL_ClearError();
   612       SDLTest_AssertPass("Call to SDL_ClearError()");
   613    }
   614 }
   615 
   616 /**
   617  * @brief Check call to SDL_GetScancodeFromName with invalid data
   618  * 
   619  * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetScancodeFromName
   620  * @sa http://wiki.libsdl.org/moin.cgi/SDL_Keycode
   621  */
   622 int
   623 keyboard_getScancodeFromNameNegative(void *arg)
   624 {      
   625    char *name;
   626    SDL_Scancode scancode;
   627 
   628    /* Clear error message */
   629    SDL_ClearError();
   630    SDLTest_AssertPass("Call to SDL_ClearError()");
   631 
   632    /* Random string input */
   633    name = SDLTest_RandomAsciiStringOfSize(32);
   634    SDLTest_Assert(name != NULL, "Check that random name is not NULL");
   635    if (name == NULL) {
   636       return TEST_ABORTED;
   637    }   
   638    scancode = SDL_GetScancodeFromName((const char *)name);
   639    SDLTest_AssertPass("Call to SDL_GetScancodeFromName('%s')", name);
   640    SDL_free(name);
   641    SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_UNKNOWN, scancode);
   642    _checkInvalidNameError();
   643          
   644    /* Zero length string input */
   645    name = "";
   646    scancode = SDL_GetScancodeFromName((const char *)name);
   647    SDLTest_AssertPass("Call to SDL_GetScancodeFromName(NULL)");
   648    SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_UNKNOWN, scancode);
   649    _checkInvalidNameError();
   650 
   651    /* NULL input */
   652    name = NULL;
   653    scancode = SDL_GetScancodeFromName((const char *)name);
   654    SDLTest_AssertPass("Call to SDL_GetScancodeFromName(NULL)");
   655    SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_UNKNOWN, scancode);
   656    _checkInvalidNameError();
   657    
   658    return TEST_COMPLETED;
   659 }
   660 
   661 
   662 
   663 /* ================= Test References ================== */
   664 
   665 /* Keyboard test cases */
   666 static const SDLTest_TestCaseReference keyboardTest1 =
   667 		{ (SDLTest_TestCaseFp)keyboard_getKeyboardState, "keyboard_getKeyboardState", "Check call to SDL_GetKeyboardState with and without numkeys reference", TEST_ENABLED };
   668 
   669 static const SDLTest_TestCaseReference keyboardTest2 =
   670 		{ (SDLTest_TestCaseFp)keyboard_getKeyboardFocus, "keyboard_getKeyboardFocus", "Check call to SDL_GetKeyboardFocus", TEST_ENABLED };
   671 
   672 static const SDLTest_TestCaseReference keyboardTest3 =
   673 		{ (SDLTest_TestCaseFp)keyboard_getKeyFromName, "keyboard_getKeyFromName", "Check call to SDL_GetKeyFromName for known, unknown and invalid name", TEST_ENABLED };
   674 
   675 static const SDLTest_TestCaseReference keyboardTest4 =
   676 		{ (SDLTest_TestCaseFp)keyboard_getKeyFromScancode, "keyboard_getKeyFromScancode", "Check call to SDL_GetKeyFromScancode", TEST_ENABLED };
   677 
   678 static const SDLTest_TestCaseReference keyboardTest5 =
   679 		{ (SDLTest_TestCaseFp)keyboard_getKeyName, "keyboard_getKeyName", "Check call to SDL_GetKeyName", TEST_ENABLED };
   680 
   681 static const SDLTest_TestCaseReference keyboardTest6 =
   682 		{ (SDLTest_TestCaseFp)keyboard_getSetModState, "keyboard_getSetModState", "Check call to SDL_GetModState and SDL_SetModState", TEST_ENABLED };
   683 
   684 static const SDLTest_TestCaseReference keyboardTest7 =
   685 		{ (SDLTest_TestCaseFp)keyboard_startStopTextInput, "keyboard_startStopTextInput", "Check call to SDL_StartTextInput and SDL_StopTextInput", TEST_ENABLED };
   686 
   687 static const SDLTest_TestCaseReference keyboardTest8 =
   688 		{ (SDLTest_TestCaseFp)keyboard_setTextInputRect, "keyboard_setTextInputRect", "Check call to SDL_SetTextInputRect", TEST_ENABLED };
   689 
   690 static const SDLTest_TestCaseReference keyboardTest9 =
   691 		{ (SDLTest_TestCaseFp)keyboard_setTextInputRectNegative, "keyboard_setTextInputRectNegative", "Check call to SDL_SetTextInputRect with invalid data", TEST_ENABLED };
   692 
   693 static const SDLTest_TestCaseReference keyboardTest10 =
   694 		{ (SDLTest_TestCaseFp)keyboard_getScancodeFromKey, "keyboard_getScancodeFromKey", "Check call to SDL_GetScancodeFromKey", TEST_ENABLED };
   695 
   696 static const SDLTest_TestCaseReference keyboardTest11 =
   697 		{ (SDLTest_TestCaseFp)keyboard_getScancodeFromName, "keyboard_getScancodeFromName", "Check call to SDL_GetScancodeFromName", TEST_ENABLED };
   698 
   699 static const SDLTest_TestCaseReference keyboardTest12 =
   700 		{ (SDLTest_TestCaseFp)keyboard_getScancodeFromNameNegative, "keyboard_getScancodeFromNameNegative", "Check call to SDL_GetScancodeFromName with invalid data", TEST_ENABLED };
   701 
   702 static const SDLTest_TestCaseReference keyboardTest13 =
   703 		{ (SDLTest_TestCaseFp)keyboard_getKeyNameNegative, "keyboard_getKeyNameNegative", "Check call to SDL_GetKeyName with invalid data", TEST_ENABLED };
   704 
   705 static const SDLTest_TestCaseReference keyboardTest14 =
   706 		{ (SDLTest_TestCaseFp)keyboard_getScancodeNameNegative, "keyboard_getScancodeNameNegative", "Check call to SDL_GetScancodeName with invalid data", TEST_ENABLED };
   707 
   708 /* Sequence of Keyboard test cases */
   709 static const SDLTest_TestCaseReference *keyboardTests[] =  {
   710 	&keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, &keyboardTest5, &keyboardTest6, 
   711 	&keyboardTest7, &keyboardTest8, &keyboardTest9, &keyboardTest10, &keyboardTest11, &keyboardTest12, 
   712 	&keyboardTest13, &keyboardTest14, NULL
   713 };
   714 
   715 /* Keyboard test suite (global) */
   716 SDLTest_TestSuiteReference keyboardTestSuite = {
   717 	"Keyboard",
   718 	NULL,
   719 	keyboardTests,
   720 	NULL
   721 };