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 <aschiffler@ferzkopp.net>
Fri, 11 Jan 2013 20:36:39 -0800
changeset 6808 3ad413bd1cd6
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
--- a/include/SDL_error.h	Thu Jan 10 23:26:49 2013 -0800
+++ b/include/SDL_error.h	Fri Jan 11 20:36:39 2013 -0800
@@ -52,6 +52,7 @@
 /*@{*/
 #define SDL_OutOfMemory()	SDL_Error(SDL_ENOMEM)
 #define SDL_Unsupported()	SDL_Error(SDL_UNSUPPORTED)
+#define SDL_InvalidParamError()	SDL_Error(SDL_INVALIDPARAM)
 typedef enum
 {
     SDL_ENOMEM,
@@ -59,6 +60,7 @@
     SDL_EFWRITE,
     SDL_EFSEEK,
     SDL_UNSUPPORTED,
+    SDL_INVALIDPARAM,
     SDL_LASTERROR
 } SDL_errorcode;
 extern DECLSPEC void SDLCALL SDL_Error(SDL_errorcode code);
--- a/src/SDL_error.c	Thu Jan 10 23:26:49 2013 -0800
+++ b/src/SDL_error.c	Fri Jan 11 20:36:39 2013 -0800
@@ -235,6 +235,9 @@
     case SDL_UNSUPPORTED:
         SDL_SetError("That operation is not supported");
         break;
+    case SDL_INVALIDPARAM:
+        SDL_SetError("Parameter is invalid");
+        break;
     default:
         SDL_SetError("Unknown SDL error");
         break;
--- a/src/video/android/SDL_androidkeyboard.c	Thu Jan 10 23:26:49 2013 -0800
+++ b/src/video/android/SDL_androidkeyboard.c	Fri Jan 11 20:36:39 2013 -0800
@@ -316,6 +316,12 @@
 Android_SetTextInputRect(_THIS, SDL_Rect *rect)
 {
     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
+
+    if (!rect) {
+        SDL_InvalidParamError();
+        return;
+    }
+    
     videodata->textRect = *rect;
 }
 
--- a/src/video/cocoa/SDL_cocoakeyboard.m	Thu Jan 10 23:26:49 2013 -0800
+++ b/src/video/cocoa/SDL_cocoakeyboard.m	Fri Jan 11 20:36:39 2013 -0800
@@ -668,6 +668,11 @@
 {
     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
 
+    if (!rect) {
+	SDL_InvalidParamError();
+	return;
+    }
+
     [data->fieldEdit setInputRect: rect];
 }
 
--- a/src/video/windows/SDL_windowskeyboard.c	Thu Jan 10 23:26:49 2013 -0800
+++ b/src/video/windows/SDL_windowskeyboard.c	Fri Jan 11 20:36:39 2013 -0800
@@ -212,6 +212,12 @@
 WIN_SetTextInputRect(_THIS, SDL_Rect *rect)
 {
     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
+    
+    if (!rect) {
+        SDL_InvalidParamError();
+        return;
+    }
+    
     videodata->ime_rect = *rect;
 }
 
--- a/test/testautomation_keyboard.c	Thu Jan 10 23:26:49 2013 -0800
+++ b/test/testautomation_keyboard.c	Fri Jan 11 20:36:39 2013 -0800
@@ -279,6 +279,132 @@
    return TEST_COMPLETED;
 }
 
+/* Internal function to test SDL_SetTextInputRect */
+void _testSetTextInputRect(SDL_Rect refRect)
+{
+   SDL_Rect testRect;
+   
+   testRect = refRect;
+   SDL_SetTextInputRect(&testRect);
+   SDLTest_AssertPass("Call to SDL_SetTextInputRect with refRect(x:%i,y:%i,w:%i,h:%i)", refRect.x, refRect.y, refRect.w, refRect.h);
+   SDLTest_AssertCheck(
+      (refRect.x == testRect.x) && (refRect.y == testRect.y) && (refRect.w == testRect.w) && (refRect.h == testRect.h), 
+      "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", 
+      refRect.x, refRect.y, refRect.w, refRect.h, 
+      testRect.x, testRect.y, testRect.w, testRect.h);
+}
+
+/**
+ * @brief Check call to SDL_SetTextInputRect
+ * 
+ * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetTextInputRect
+ */
+int
+keyboard_setTextInputRect(void *arg)
+{   
+   SDL_Rect refRect;
+   
+   /* Normal visible refRect, origin inside */
+   refRect.x = SDLTest_RandomIntegerInRange(1, 50);;
+   refRect.y = SDLTest_RandomIntegerInRange(1, 50);;
+   refRect.w = SDLTest_RandomIntegerInRange(10, 50);
+   refRect.h = SDLTest_RandomIntegerInRange(10, 50);
+   _testSetTextInputRect(refRect);
+   
+   /* Normal visible refRect, origin 0,0 */
+   refRect.x = 0;
+   refRect.y = 0;
+   refRect.w = SDLTest_RandomIntegerInRange(10, 50);
+   refRect.h = SDLTest_RandomIntegerInRange(10, 50);
+   _testSetTextInputRect(refRect);
+
+   /* 1Pixel refRect */
+   refRect.x = SDLTest_RandomIntegerInRange(10, 50);;
+   refRect.y = SDLTest_RandomIntegerInRange(10, 50);;
+   refRect.w = 1;
+   refRect.h = 1;
+   _testSetTextInputRect(refRect);
+
+   /* 0pixel refRect */
+   refRect.x = 1;
+   refRect.y = 1;
+   refRect.w = 1;
+   refRect.h = 0;
+   _testSetTextInputRect(refRect);
+
+   /* 0pixel refRect */
+   refRect.x = 1;
+   refRect.y = 1;
+   refRect.w = 0;
+   refRect.h = 1;
+   _testSetTextInputRect(refRect);
+
+   /* 0pixel refRect */
+   refRect.x = 1;
+   refRect.y = 1;
+   refRect.w = 0;
+   refRect.h = 0;
+   _testSetTextInputRect(refRect);
+
+   /* 0pixel refRect */
+   refRect.x = 0;
+   refRect.y = 0;
+   refRect.w = 0;
+   refRect.h = 0;
+   _testSetTextInputRect(refRect);
+
+   /* negative refRect */
+   refRect.x = SDLTest_RandomIntegerInRange(-200, -100);;
+   refRect.y = SDLTest_RandomIntegerInRange(-200, -100);;
+   refRect.w = 50;
+   refRect.h = 50;
+   _testSetTextInputRect(refRect);
+
+   /* oversized refRect */
+   refRect.x = SDLTest_RandomIntegerInRange(1, 50);;
+   refRect.y = SDLTest_RandomIntegerInRange(1, 50);;
+   refRect.w = 5000;
+   refRect.h = 5000;
+   _testSetTextInputRect(refRect);
+
+   /* NULL refRect */
+   SDL_SetTextInputRect(NULL);
+   SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)");
+
+   return TEST_COMPLETED;
+}
+
+/**
+ * @brief Check call to SDL_SetTextInputRect with invalid data
+ * 
+ * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetTextInputRect
+ */
+int
+keyboard_setTextInputRectNegative(void *arg)
+{      
+   const char *expectedError = "Parameter is invalid";
+   const char *error;
+   
+   SDL_ClearError();
+   SDLTest_AssertPass("Call to SDL_ClearError()");
+   
+   /* NULL refRect */
+   SDL_SetTextInputRect(NULL);
+   SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)");
+   error = SDL_GetError();
+   SDLTest_AssertPass("Call to SDL_GetError()");
+   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
+   if (error != NULL) {
+      SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
+          "Validate error message, expected: '%s', got: '%s'", expectedError, error);
+   }
+
+   SDL_ClearError();
+   SDLTest_AssertPass("Call to SDL_ClearError()");
+      
+   return TEST_COMPLETED;
+}
+
 
 
 /* ================= Test References ================== */
@@ -305,9 +431,16 @@
 static const SDLTest_TestCaseReference keyboardTest7 =
 		{ (SDLTest_TestCaseFp)keyboard_startStopTextInput, "keyboard_startStopTextInput", "Check call to SDL_StartTextInput and SDL_StopTextInput", TEST_ENABLED };
 
+static const SDLTest_TestCaseReference keyboardTest8 =
+		{ (SDLTest_TestCaseFp)keyboard_setTextInputRect, "keyboard_setTextInputRect", "Check call to SDL_SetTextInputRect", TEST_ENABLED };
+
+static const SDLTest_TestCaseReference keyboardTest9 =
+		{ (SDLTest_TestCaseFp)keyboard_setTextInputRectNegative, "keyboard_setTextInputRectNegative", "Check call to SDL_SetTextInputRect with invalid data", TEST_ENABLED };
+
 /* Sequence of Keyboard test cases */
 static const SDLTest_TestCaseReference *keyboardTests[] =  {
-	&keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, &keyboardTest5, &keyboardTest6, &keyboardTest7, NULL
+	&keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, &keyboardTest5, &keyboardTest6, 
+	&keyboardTest7, &keyboardTest8, &keyboardTest9, NULL
 };
 
 /* Keyboard test suite (global) */