test/testautomation_keyboard.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 07 Jan 2013 07:39:15 -0800
changeset 6803 fbe83a6d10fa
parent 6800 26c6795ec1a4
child 6807 e3610bc90cf3
permissions -rw-r--r--
Add a few keyboard tests; fix missing input validation in SDL_GetKeyFromName
     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 
   135 /* ================= Test References ================== */
   136 
   137 /* Keyboard test cases */
   138 static const SDLTest_TestCaseReference keyboardTest1 =
   139 		{ (SDLTest_TestCaseFp)keyboard_getKeyboardState, "keyboard_getKeyboardState", "Check call to SDL_GetKeyboardState with and without numkeys reference", TEST_ENABLED };
   140 
   141 static const SDLTest_TestCaseReference keyboardTest2 =
   142 		{ (SDLTest_TestCaseFp)keyboard_getKeyboardFocus, "keyboard_getKeyboardFocus", "Check call to SDL_GetKeyboardFocus", TEST_ENABLED };
   143 
   144 static const SDLTest_TestCaseReference keyboardTest3 =
   145 		{ (SDLTest_TestCaseFp)keyboard_getKeyFromName, "keyboard_getKeyFromName", "Check call to SDL_GetKeyFromName for known, unknown and invalid name", TEST_ENABLED };
   146 
   147 static const SDLTest_TestCaseReference keyboardTest4 =
   148 		{ (SDLTest_TestCaseFp)keyboard_getKeyFromScancode, "keyboard_getKeyFromScancode", "Check call to SDL_GetKeyFromScancode", TEST_ENABLED };
   149 
   150 /* Sequence of Keyboard test cases */
   151 static const SDLTest_TestCaseReference *keyboardTests[] =  {
   152 	&keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, NULL
   153 };
   154 
   155 /* Keyboard test suite (global) */
   156 SDLTest_TestSuiteReference keyboardTestSuite = {
   157 	"Keyboard",
   158 	NULL,
   159 	keyboardTests,
   160 	NULL
   161 };