From 17b1a8441cdb437db3febf631c613587d96af17b Mon Sep 17 00:00:00 2001 From: Andreas Schiffler Date: Fri, 11 Jan 2013 20:36:39 -0800 Subject: [PATCH] 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 | 2 + src/SDL_error.c | 3 + src/video/android/SDL_androidkeyboard.c | 6 ++ src/video/cocoa/SDL_cocoakeyboard.m | 5 + src/video/windows/SDL_windowskeyboard.c | 6 ++ test/testautomation_keyboard.c | 135 +++++++++++++++++++++++- 6 files changed, 156 insertions(+), 1 deletion(-) diff --git a/include/SDL_error.h b/include/SDL_error.h index 16e5f08d1..5bec2afa8 100644 --- a/include/SDL_error.h +++ b/include/SDL_error.h @@ -52,6 +52,7 @@ extern DECLSPEC void SDLCALL SDL_ClearError(void); /*@{*/ #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 @@ typedef enum SDL_EFWRITE, SDL_EFSEEK, SDL_UNSUPPORTED, + SDL_INVALIDPARAM, SDL_LASTERROR } SDL_errorcode; extern DECLSPEC void SDLCALL SDL_Error(SDL_errorcode code); diff --git a/src/SDL_error.c b/src/SDL_error.c index 306dfe2ba..30d3da67d 100644 --- a/src/SDL_error.c +++ b/src/SDL_error.c @@ -235,6 +235,9 @@ SDL_Error(SDL_errorcode code) 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; diff --git a/src/video/android/SDL_androidkeyboard.c b/src/video/android/SDL_androidkeyboard.c index b1fe43a0e..be5a1d483 100644 --- a/src/video/android/SDL_androidkeyboard.c +++ b/src/video/android/SDL_androidkeyboard.c @@ -316,6 +316,12 @@ void Android_SetTextInputRect(_THIS, SDL_Rect *rect) { SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata; + + if (!rect) { + SDL_InvalidParamError(); + return; + } + videodata->textRect = *rect; } diff --git a/src/video/cocoa/SDL_cocoakeyboard.m b/src/video/cocoa/SDL_cocoakeyboard.m index 1714ea6db..9c23ebae7 100644 --- a/src/video/cocoa/SDL_cocoakeyboard.m +++ b/src/video/cocoa/SDL_cocoakeyboard.m @@ -668,6 +668,11 @@ - (NSArray *) validAttributesForMarkedText { SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; + if (!rect) { + SDL_InvalidParamError(); + return; + } + [data->fieldEdit setInputRect: rect]; } diff --git a/src/video/windows/SDL_windowskeyboard.c b/src/video/windows/SDL_windowskeyboard.c index 182741bdd..fbe1a8349 100644 --- a/src/video/windows/SDL_windowskeyboard.c +++ b/src/video/windows/SDL_windowskeyboard.c @@ -212,6 +212,12 @@ void WIN_SetTextInputRect(_THIS, SDL_Rect *rect) { SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata; + + if (!rect) { + SDL_InvalidParamError(); + return; + } + videodata->ime_rect = *rect; } diff --git a/test/testautomation_keyboard.c b/test/testautomation_keyboard.c index 810ca39c8..02da89a76 100644 --- a/test/testautomation_keyboard.c +++ b/test/testautomation_keyboard.c @@ -279,6 +279,132 @@ keyboard_startStopTextInput(void *arg) 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 keyboardTest6 = 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) */