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
authorAndreas Schiffler
Fri, 11 Jan 2013 20:36:39 -0800
changeset 68083ad413bd1cd6
parent 6807 e3610bc90cf3
child 6809 54cca01b6006
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
include/SDL_error.h
src/SDL_error.c
src/video/android/SDL_androidkeyboard.c
src/video/cocoa/SDL_cocoakeyboard.m
src/video/windows/SDL_windowskeyboard.c
test/testautomation_keyboard.c
     1.1 --- a/include/SDL_error.h	Thu Jan 10 23:26:49 2013 -0800
     1.2 +++ b/include/SDL_error.h	Fri Jan 11 20:36:39 2013 -0800
     1.3 @@ -52,6 +52,7 @@
     1.4  /*@{*/
     1.5  #define SDL_OutOfMemory()	SDL_Error(SDL_ENOMEM)
     1.6  #define SDL_Unsupported()	SDL_Error(SDL_UNSUPPORTED)
     1.7 +#define SDL_InvalidParamError()	SDL_Error(SDL_INVALIDPARAM)
     1.8  typedef enum
     1.9  {
    1.10      SDL_ENOMEM,
    1.11 @@ -59,6 +60,7 @@
    1.12      SDL_EFWRITE,
    1.13      SDL_EFSEEK,
    1.14      SDL_UNSUPPORTED,
    1.15 +    SDL_INVALIDPARAM,
    1.16      SDL_LASTERROR
    1.17  } SDL_errorcode;
    1.18  extern DECLSPEC void SDLCALL SDL_Error(SDL_errorcode code);
     2.1 --- a/src/SDL_error.c	Thu Jan 10 23:26:49 2013 -0800
     2.2 +++ b/src/SDL_error.c	Fri Jan 11 20:36:39 2013 -0800
     2.3 @@ -235,6 +235,9 @@
     2.4      case SDL_UNSUPPORTED:
     2.5          SDL_SetError("That operation is not supported");
     2.6          break;
     2.7 +    case SDL_INVALIDPARAM:
     2.8 +        SDL_SetError("Parameter is invalid");
     2.9 +        break;
    2.10      default:
    2.11          SDL_SetError("Unknown SDL error");
    2.12          break;
     3.1 --- a/src/video/android/SDL_androidkeyboard.c	Thu Jan 10 23:26:49 2013 -0800
     3.2 +++ b/src/video/android/SDL_androidkeyboard.c	Fri Jan 11 20:36:39 2013 -0800
     3.3 @@ -316,6 +316,12 @@
     3.4  Android_SetTextInputRect(_THIS, SDL_Rect *rect)
     3.5  {
     3.6      SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
     3.7 +
     3.8 +    if (!rect) {
     3.9 +        SDL_InvalidParamError();
    3.10 +        return;
    3.11 +    }
    3.12 +    
    3.13      videodata->textRect = *rect;
    3.14  }
    3.15  
     4.1 --- a/src/video/cocoa/SDL_cocoakeyboard.m	Thu Jan 10 23:26:49 2013 -0800
     4.2 +++ b/src/video/cocoa/SDL_cocoakeyboard.m	Fri Jan 11 20:36:39 2013 -0800
     4.3 @@ -668,6 +668,11 @@
     4.4  {
     4.5      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
     4.6  
     4.7 +    if (!rect) {
     4.8 +	SDL_InvalidParamError();
     4.9 +	return;
    4.10 +    }
    4.11 +
    4.12      [data->fieldEdit setInputRect: rect];
    4.13  }
    4.14  
     5.1 --- a/src/video/windows/SDL_windowskeyboard.c	Thu Jan 10 23:26:49 2013 -0800
     5.2 +++ b/src/video/windows/SDL_windowskeyboard.c	Fri Jan 11 20:36:39 2013 -0800
     5.3 @@ -212,6 +212,12 @@
     5.4  WIN_SetTextInputRect(_THIS, SDL_Rect *rect)
     5.5  {
     5.6      SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
     5.7 +    
     5.8 +    if (!rect) {
     5.9 +        SDL_InvalidParamError();
    5.10 +        return;
    5.11 +    }
    5.12 +    
    5.13      videodata->ime_rect = *rect;
    5.14  }
    5.15  
     6.1 --- a/test/testautomation_keyboard.c	Thu Jan 10 23:26:49 2013 -0800
     6.2 +++ b/test/testautomation_keyboard.c	Fri Jan 11 20:36:39 2013 -0800
     6.3 @@ -279,6 +279,132 @@
     6.4     return TEST_COMPLETED;
     6.5  }
     6.6  
     6.7 +/* Internal function to test SDL_SetTextInputRect */
     6.8 +void _testSetTextInputRect(SDL_Rect refRect)
     6.9 +{
    6.10 +   SDL_Rect testRect;
    6.11 +   
    6.12 +   testRect = refRect;
    6.13 +   SDL_SetTextInputRect(&testRect);
    6.14 +   SDLTest_AssertPass("Call to SDL_SetTextInputRect with refRect(x:%i,y:%i,w:%i,h:%i)", refRect.x, refRect.y, refRect.w, refRect.h);
    6.15 +   SDLTest_AssertCheck(
    6.16 +      (refRect.x == testRect.x) && (refRect.y == testRect.y) && (refRect.w == testRect.w) && (refRect.h == testRect.h), 
    6.17 +      "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", 
    6.18 +      refRect.x, refRect.y, refRect.w, refRect.h, 
    6.19 +      testRect.x, testRect.y, testRect.w, testRect.h);
    6.20 +}
    6.21 +
    6.22 +/**
    6.23 + * @brief Check call to SDL_SetTextInputRect
    6.24 + * 
    6.25 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetTextInputRect
    6.26 + */
    6.27 +int
    6.28 +keyboard_setTextInputRect(void *arg)
    6.29 +{   
    6.30 +   SDL_Rect refRect;
    6.31 +   
    6.32 +   /* Normal visible refRect, origin inside */
    6.33 +   refRect.x = SDLTest_RandomIntegerInRange(1, 50);;
    6.34 +   refRect.y = SDLTest_RandomIntegerInRange(1, 50);;
    6.35 +   refRect.w = SDLTest_RandomIntegerInRange(10, 50);
    6.36 +   refRect.h = SDLTest_RandomIntegerInRange(10, 50);
    6.37 +   _testSetTextInputRect(refRect);
    6.38 +   
    6.39 +   /* Normal visible refRect, origin 0,0 */
    6.40 +   refRect.x = 0;
    6.41 +   refRect.y = 0;
    6.42 +   refRect.w = SDLTest_RandomIntegerInRange(10, 50);
    6.43 +   refRect.h = SDLTest_RandomIntegerInRange(10, 50);
    6.44 +   _testSetTextInputRect(refRect);
    6.45 +
    6.46 +   /* 1Pixel refRect */
    6.47 +   refRect.x = SDLTest_RandomIntegerInRange(10, 50);;
    6.48 +   refRect.y = SDLTest_RandomIntegerInRange(10, 50);;
    6.49 +   refRect.w = 1;
    6.50 +   refRect.h = 1;
    6.51 +   _testSetTextInputRect(refRect);
    6.52 +
    6.53 +   /* 0pixel refRect */
    6.54 +   refRect.x = 1;
    6.55 +   refRect.y = 1;
    6.56 +   refRect.w = 1;
    6.57 +   refRect.h = 0;
    6.58 +   _testSetTextInputRect(refRect);
    6.59 +
    6.60 +   /* 0pixel refRect */
    6.61 +   refRect.x = 1;
    6.62 +   refRect.y = 1;
    6.63 +   refRect.w = 0;
    6.64 +   refRect.h = 1;
    6.65 +   _testSetTextInputRect(refRect);
    6.66 +
    6.67 +   /* 0pixel refRect */
    6.68 +   refRect.x = 1;
    6.69 +   refRect.y = 1;
    6.70 +   refRect.w = 0;
    6.71 +   refRect.h = 0;
    6.72 +   _testSetTextInputRect(refRect);
    6.73 +
    6.74 +   /* 0pixel refRect */
    6.75 +   refRect.x = 0;
    6.76 +   refRect.y = 0;
    6.77 +   refRect.w = 0;
    6.78 +   refRect.h = 0;
    6.79 +   _testSetTextInputRect(refRect);
    6.80 +
    6.81 +   /* negative refRect */
    6.82 +   refRect.x = SDLTest_RandomIntegerInRange(-200, -100);;
    6.83 +   refRect.y = SDLTest_RandomIntegerInRange(-200, -100);;
    6.84 +   refRect.w = 50;
    6.85 +   refRect.h = 50;
    6.86 +   _testSetTextInputRect(refRect);
    6.87 +
    6.88 +   /* oversized refRect */
    6.89 +   refRect.x = SDLTest_RandomIntegerInRange(1, 50);;
    6.90 +   refRect.y = SDLTest_RandomIntegerInRange(1, 50);;
    6.91 +   refRect.w = 5000;
    6.92 +   refRect.h = 5000;
    6.93 +   _testSetTextInputRect(refRect);
    6.94 +
    6.95 +   /* NULL refRect */
    6.96 +   SDL_SetTextInputRect(NULL);
    6.97 +   SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)");
    6.98 +
    6.99 +   return TEST_COMPLETED;
   6.100 +}
   6.101 +
   6.102 +/**
   6.103 + * @brief Check call to SDL_SetTextInputRect with invalid data
   6.104 + * 
   6.105 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetTextInputRect
   6.106 + */
   6.107 +int
   6.108 +keyboard_setTextInputRectNegative(void *arg)
   6.109 +{      
   6.110 +   const char *expectedError = "Parameter is invalid";
   6.111 +   const char *error;
   6.112 +   
   6.113 +   SDL_ClearError();
   6.114 +   SDLTest_AssertPass("Call to SDL_ClearError()");
   6.115 +   
   6.116 +   /* NULL refRect */
   6.117 +   SDL_SetTextInputRect(NULL);
   6.118 +   SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)");
   6.119 +   error = SDL_GetError();
   6.120 +   SDLTest_AssertPass("Call to SDL_GetError()");
   6.121 +   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   6.122 +   if (error != NULL) {
   6.123 +      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
   6.124 +          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
   6.125 +   }
   6.126 +
   6.127 +   SDL_ClearError();
   6.128 +   SDLTest_AssertPass("Call to SDL_ClearError()");
   6.129 +      
   6.130 +   return TEST_COMPLETED;
   6.131 +}
   6.132 +
   6.133  
   6.134  
   6.135  /* ================= Test References ================== */
   6.136 @@ -305,9 +431,16 @@
   6.137  static const SDLTest_TestCaseReference keyboardTest7 =
   6.138  		{ (SDLTest_TestCaseFp)keyboard_startStopTextInput, "keyboard_startStopTextInput", "Check call to SDL_StartTextInput and SDL_StopTextInput", TEST_ENABLED };
   6.139  
   6.140 +static const SDLTest_TestCaseReference keyboardTest8 =
   6.141 +		{ (SDLTest_TestCaseFp)keyboard_setTextInputRect, "keyboard_setTextInputRect", "Check call to SDL_SetTextInputRect", TEST_ENABLED };
   6.142 +
   6.143 +static const SDLTest_TestCaseReference keyboardTest9 =
   6.144 +		{ (SDLTest_TestCaseFp)keyboard_setTextInputRectNegative, "keyboard_setTextInputRectNegative", "Check call to SDL_SetTextInputRect with invalid data", TEST_ENABLED };
   6.145 +
   6.146  /* Sequence of Keyboard test cases */
   6.147  static const SDLTest_TestCaseReference *keyboardTests[] =  {
   6.148 -	&keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, &keyboardTest5, &keyboardTest6, &keyboardTest7, NULL
   6.149 +	&keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, &keyboardTest5, &keyboardTest6, 
   6.150 +	&keyboardTest7, &keyboardTest8, &keyboardTest9, NULL
   6.151  };
   6.152  
   6.153  /* Keyboard test suite (global) */