test/testautomation_keyboard.c
changeset 6983 b72f56ab9867
parent 6813 b21879e67db2
child 7172 b3569dff1c6e
     1.1 --- a/test/testautomation_keyboard.c	Fri Mar 08 16:27:05 2013 -0800
     1.2 +++ b/test/testautomation_keyboard.c	Fri Mar 08 23:04:53 2013 -0800
     1.3 @@ -3,6 +3,7 @@
     1.4   */
     1.5  
     1.6  #include <stdio.h>
     1.7 +#include <limits.h>
     1.8  
     1.9  #include "SDL_config.h"
    1.10  #include "SDL.h"
    1.11 @@ -103,6 +104,25 @@
    1.12     return TEST_COMPLETED;
    1.13  }
    1.14  
    1.15 +/* 
    1.16 + * Local helper to check for the invalid scancode error message
    1.17 + */
    1.18 +void
    1.19 +_checkInvalidScancodeError()
    1.20 +{
    1.21 +   const char *expectedError = "Parameter 'scancode' is invalid";
    1.22 +   const char *error;   
    1.23 +   error = SDL_GetError();
    1.24 +   SDLTest_AssertPass("Call to SDL_GetError()");
    1.25 +   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
    1.26 +   if (error != NULL) {
    1.27 +      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
    1.28 +          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
    1.29 +      SDL_ClearError();
    1.30 +      SDLTest_AssertPass("Call to SDL_ClearError()");
    1.31 +   }
    1.32 +}
    1.33 +
    1.34  /**
    1.35   * @brief Check call to SDL_GetKeyFromScancode
    1.36   * 
    1.37 @@ -111,8 +131,6 @@
    1.38  int
    1.39  keyboard_getKeyFromScancode(void *arg)
    1.40  {
    1.41 -   const char *expectedError = "Parameter 'scancode' is invalid";
    1.42 -   const char *error;   
    1.43     SDL_Keycode result;
    1.44  
    1.45     /* Case where input is valid */
    1.46 @@ -125,6 +143,7 @@
    1.47     SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(0)");
    1.48     SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
    1.49  
    1.50 +   /* Clear error message */
    1.51     SDL_ClearError();
    1.52     SDLTest_AssertPass("Call to SDL_ClearError()");
    1.53  
    1.54 @@ -132,31 +151,13 @@
    1.55     result = SDL_GetKeyFromScancode(-999);
    1.56     SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(-999)");
    1.57     SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
    1.58 -   error = SDL_GetError();
    1.59 -   SDLTest_AssertPass("Call to SDL_GetError()");
    1.60 -   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
    1.61 -   if (error != NULL) {
    1.62 -      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
    1.63 -          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
    1.64 -   }
    1.65 -
    1.66 -   SDL_ClearError();
    1.67 -   SDLTest_AssertPass("Call to SDL_ClearError()");
    1.68 +   _checkInvalidScancodeError();
    1.69  
    1.70     /* Case where input is invalid (too big) */
    1.71     result = SDL_GetKeyFromScancode(999);
    1.72     SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(999)");
    1.73     SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
    1.74 -   error = SDL_GetError();
    1.75 -   SDLTest_AssertPass("Call to SDL_GetError()");
    1.76 -   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
    1.77 -   if (error != NULL) {
    1.78 -      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
    1.79 -          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
    1.80 -   }
    1.81 -
    1.82 -   SDL_ClearError();
    1.83 -   SDLTest_AssertPass("Call to SDL_ClearError()");
    1.84 +   _checkInvalidScancodeError();
    1.85  
    1.86     return TEST_COMPLETED;
    1.87  }
    1.88 @@ -218,6 +219,78 @@
    1.89  }
    1.90  
    1.91  /**
    1.92 + * @brief SDL_GetScancodeName negative cases
    1.93 + * 
    1.94 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetScancodeName
    1.95 + */
    1.96 +int
    1.97 +keyboard_getScancodeNameNegative(void *arg)
    1.98 +{  
    1.99 +   SDL_Scancode scancode;
   1.100 +   char *result;
   1.101 +   char *expected = "";
   1.102 +
   1.103 +   /* Clear error message */
   1.104 +   SDL_ClearError();
   1.105 +   SDLTest_AssertPass("Call to SDL_ClearError()");
   1.106 +
   1.107 +   /* Negative scancode */
   1.108 +   scancode = (SDL_Scancode)SDLTest_RandomIntegerInRange(LONG_MIN, -1);
   1.109 +   result = (char *)SDL_GetScancodeName(scancode);
   1.110 +   SDLTest_AssertPass("Call to SDL_GetScancodeName(%d/negative)", scancode);
   1.111 +   SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   1.112 +   SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: '%s', got: '%s'", expected, result);
   1.113 +   _checkInvalidScancodeError();
   1.114 +
   1.115 +   /* Large scancode */
   1.116 +   scancode = (SDL_Scancode)SDLTest_RandomIntegerInRange(SDL_NUM_SCANCODES, LONG_MAX);
   1.117 +   result = (char *)SDL_GetScancodeName(scancode);
   1.118 +   SDLTest_AssertPass("Call to SDL_GetScancodeName(%d/large)", scancode);
   1.119 +   SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   1.120 +   SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: '%s', got: '%s'", expected, result);
   1.121 +   _checkInvalidScancodeError();
   1.122 +
   1.123 +   return TEST_COMPLETED;
   1.124 +}
   1.125 +
   1.126 +/**
   1.127 + * @brief SDL_GetKeyName negative cases
   1.128 + * 
   1.129 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyName
   1.130 + */
   1.131 +int
   1.132 +keyboard_getKeyNameNegative(void *arg)
   1.133 +{  
   1.134 +   SDL_Keycode keycode;
   1.135 +   char *result;
   1.136 +   char *expected = "";
   1.137 +
   1.138 +   /* Unknown keycode */
   1.139 +   keycode = SDLK_UNKNOWN;
   1.140 +   result = (char *)SDL_GetKeyName(keycode);
   1.141 +   SDLTest_AssertPass("Call to SDL_GetKeyName(%d/unknown)", keycode);
   1.142 +   SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   1.143 +   SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: '%s', got: '%s'", expected, result);
   1.144 +
   1.145 +   /* Clear error message */
   1.146 +   SDL_ClearError();
   1.147 +   SDLTest_AssertPass("Call to SDL_ClearError()");
   1.148 +
   1.149 +   /* Negative keycode */
   1.150 +   keycode = (SDL_Keycode)SDLTest_RandomIntegerInRange(-255, -1);
   1.151 +   result = (char *)SDL_GetKeyName(keycode);
   1.152 +   SDLTest_AssertPass("Call to SDL_GetKeyName(%d/negative)", keycode);
   1.153 +   SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
   1.154 +   SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: '%s', got: '%s'", expected, result);
   1.155 +   _checkInvalidScancodeError();
   1.156 +
   1.157 +   SDL_ClearError();
   1.158 +   SDLTest_AssertPass("Call to SDL_ClearError()");
   1.159 +
   1.160 +   return TEST_COMPLETED;
   1.161 +}
   1.162 +
   1.163 +/**
   1.164   * @brief Check call to SDL_GetModState and SDL_SetModState
   1.165   * 
   1.166   * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetModState
   1.167 @@ -521,6 +594,25 @@
   1.168     return TEST_COMPLETED;
   1.169  }
   1.170  
   1.171 +/* 
   1.172 + * Local helper to check for the invalid scancode error message
   1.173 + */
   1.174 +void
   1.175 +_checkInvalidNameError()
   1.176 +{
   1.177 +   const char *expectedError = "Parameter 'name' is invalid";
   1.178 +   const char *error;   
   1.179 +   error = SDL_GetError();
   1.180 +   SDLTest_AssertPass("Call to SDL_GetError()");
   1.181 +   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   1.182 +   if (error != NULL) {
   1.183 +      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
   1.184 +          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
   1.185 +      SDL_ClearError();
   1.186 +      SDLTest_AssertPass("Call to SDL_ClearError()");
   1.187 +   }
   1.188 +}
   1.189 +
   1.190  /**
   1.191   * @brief Check call to SDL_GetScancodeFromName with invalid data
   1.192   * 
   1.193 @@ -532,9 +624,8 @@
   1.194  {      
   1.195     char *name;
   1.196     SDL_Scancode scancode;
   1.197 -   const char *expectedError = "Parameter 'name' is invalid";
   1.198 -   const char *error;
   1.199  
   1.200 +   /* Clear error message */
   1.201     SDL_ClearError();
   1.202     SDLTest_AssertPass("Call to SDL_ClearError()");
   1.203  
   1.204 @@ -548,48 +639,21 @@
   1.205     SDLTest_AssertPass("Call to SDL_GetScancodeFromName('%s')", name);
   1.206     SDL_free(name);
   1.207     SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_UNKNOWN, scancode);
   1.208 -   error = SDL_GetError();
   1.209 -   SDLTest_AssertPass("Call to SDL_GetError()");
   1.210 -   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   1.211 -   if (error != NULL) {
   1.212 -      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
   1.213 -          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
   1.214 -   }
   1.215 -
   1.216 -   SDL_ClearError();
   1.217 -   SDLTest_AssertPass("Call to SDL_ClearError()");
   1.218 +   _checkInvalidNameError();
   1.219           
   1.220     /* Zero length string input */
   1.221     name = "";
   1.222     scancode = SDL_GetScancodeFromName((const char *)name);
   1.223     SDLTest_AssertPass("Call to SDL_GetScancodeFromName(NULL)");
   1.224     SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_UNKNOWN, scancode);
   1.225 -   error = SDL_GetError();
   1.226 -   SDLTest_AssertPass("Call to SDL_GetError()");
   1.227 -   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   1.228 -   if (error != NULL) {
   1.229 -      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
   1.230 -          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
   1.231 -   }
   1.232 -
   1.233 -   SDL_ClearError();
   1.234 -   SDLTest_AssertPass("Call to SDL_ClearError()");
   1.235 +   _checkInvalidNameError();
   1.236  
   1.237     /* NULL input */
   1.238     name = NULL;
   1.239     scancode = SDL_GetScancodeFromName((const char *)name);
   1.240     SDLTest_AssertPass("Call to SDL_GetScancodeFromName(NULL)");
   1.241     SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_UNKNOWN, scancode);
   1.242 -   error = SDL_GetError();
   1.243 -   SDLTest_AssertPass("Call to SDL_GetError()");
   1.244 -   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   1.245 -   if (error != NULL) {
   1.246 -      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
   1.247 -          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
   1.248 -   }
   1.249 -
   1.250 -   SDL_ClearError();
   1.251 -   SDLTest_AssertPass("Call to SDL_ClearError()");
   1.252 +   _checkInvalidNameError();
   1.253     
   1.254     return TEST_COMPLETED;
   1.255  }
   1.256 @@ -635,10 +699,17 @@
   1.257  static const SDLTest_TestCaseReference keyboardTest12 =
   1.258  		{ (SDLTest_TestCaseFp)keyboard_getScancodeFromNameNegative, "keyboard_getScancodeFromNameNegative", "Check call to SDL_GetScancodeFromName with invalid data", TEST_ENABLED };
   1.259  
   1.260 +static const SDLTest_TestCaseReference keyboardTest13 =
   1.261 +		{ (SDLTest_TestCaseFp)keyboard_getKeyNameNegative, "keyboard_getKeyNameNegative", "Check call to SDL_GetKeyName with invalid data", TEST_ENABLED };
   1.262 +
   1.263 +static const SDLTest_TestCaseReference keyboardTest14 =
   1.264 +		{ (SDLTest_TestCaseFp)keyboard_getScancodeNameNegative, "keyboard_getScancodeNameNegative", "Check call to SDL_GetScancodeName with invalid data", TEST_ENABLED };
   1.265 +
   1.266  /* Sequence of Keyboard test cases */
   1.267  static const SDLTest_TestCaseReference *keyboardTests[] =  {
   1.268  	&keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, &keyboardTest5, &keyboardTest6, 
   1.269 -	&keyboardTest7, &keyboardTest8, &keyboardTest9, &keyboardTest10, &keyboardTest11, &keyboardTest12, NULL
   1.270 +	&keyboardTest7, &keyboardTest8, &keyboardTest9, &keyboardTest10, &keyboardTest11, &keyboardTest12, 
   1.271 +	&keyboardTest13, &keyboardTest14, NULL
   1.272  };
   1.273  
   1.274  /* Keyboard test suite (global) */