Add a few keyboard tests; fix missing input validation in SDL_GetKeyFromName
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Mon, 07 Jan 2013 07:39:15 -0800
changeset 6803 fbe83a6d10fa
parent 6801 f92e3d5f71a2
child 6804 a91717ac8ff1
Add a few keyboard tests; fix missing input validation in SDL_GetKeyFromName
src/events/SDL_keyboard.c
test/testautomation_keyboard.c
--- a/src/events/SDL_keyboard.c	Sun Jan 06 18:19:30 2013 -0800
+++ b/src/events/SDL_keyboard.c	Mon Jan 07 07:39:15 2013 -0800
@@ -953,6 +953,9 @@
 {
 	SDL_Keycode key;
 
+        /* Check input */
+        if (name == NULL) return SDLK_UNKNOWN;
+        
 	/* If it's a single UTF-8 character, then that's the keycode itself */
 	key = *(const unsigned char *)name;
 	if (key >= 0xF0) {
--- a/test/testautomation_keyboard.c	Sun Jan 06 18:19:30 2013 -0800
+++ b/test/testautomation_keyboard.c	Mon Jan 07 07:39:15 2013 -0800
@@ -9,16 +9,12 @@
 
 /* ================= Test Case Implementation ================== */
 
-/*!
- * TODO: Add tests for keyboard here
- *
- */
-
 /* Test case functions */
 
 /**
- * @brief Check call to SDL_GetKeyboardState
+ * @brief Check call to SDL_GetKeyboardState with and without numkeys reference.
  * 
+ * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyboardState
  */
 int
 keyboard_getKeyboardState(void *arg)
@@ -41,15 +37,119 @@
    return TEST_COMPLETED;
 }
 
+/**
+ * @brief Check call to SDL_GetKeyboardFocus
+ * 
+ * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyboardFocus
+ */
+int
+keyboard_getKeyboardFocus(void *arg)
+{
+   SDL_Window* window;
+
+   /* Call, but ignore return value */
+   window = SDL_GetKeyboardFocus();
+   SDLTest_AssertPass("Call to SDL_GetKeyboardFocus()");
+
+   return TEST_COMPLETED;
+}
+
+/**
+ * @brief Check call to SDL_GetKeyFromName for known, unknown and invalid name.
+ * 
+ * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyFromName
+ */
+int
+keyboard_getKeyFromName(void *arg)
+{
+   SDL_Keycode result;
+
+   /* Case where Key is known, 1 character input */
+   result = SDL_GetKeyFromName("A");
+   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/single)");
+   SDLTest_AssertCheck(result == SDLK_a, "Verify result from call, expected: %i, got: %i", SDLK_a, result);
+
+   /* Case where Key is known, 2 character input */
+   result = SDL_GetKeyFromName("F1");
+   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/double)");
+   SDLTest_AssertCheck(result == SDLK_F1, "Verify result from call, expected: %i, got: %i", SDLK_F1, result);
+
+   /* Case where Key is known, 3 character input */
+   result = SDL_GetKeyFromName("End");
+   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/triple)");
+   SDLTest_AssertCheck(result == SDLK_END, "Verify result from call, expected: %i, got: %i", SDLK_END, result);
+
+   /* Case where Key is known, 4 character input */
+   result = SDL_GetKeyFromName("Find");
+   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/quad)");
+   SDLTest_AssertCheck(result == SDLK_FIND, "Verify result from call, expected: %i, got: %i", SDLK_FIND, result);
+
+   /* Case where Key is known, multiple character input */
+   result = SDL_GetKeyFromName("AudioStop");
+   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/multi)");
+   SDLTest_AssertCheck(result == SDLK_AUDIOSTOP, "Verify result from call, expected: %i, got: %i", SDLK_AUDIOSTOP, result);
+
+   /* Case where Key is unknown */
+   result = SDL_GetKeyFromName("NotThere");
+   SDLTest_AssertPass("Call to SDL_GetKeyFromName(unknown)");
+   SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
+
+   /* Case where input is NULL/invalid */
+   result = SDL_GetKeyFromName(NULL);
+   SDLTest_AssertPass("Call to SDL_GetKeyFromName(NULL)");
+   SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
+
+   return TEST_COMPLETED;
+}
+
+/**
+ * @brief Check call to SDL_GetKeyFromScancode
+ * 
+ * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyFromScancode
+ */
+int
+keyboard_getKeyFromScancode(void *arg)
+{
+   SDL_Keycode result;
+
+   /* Case where input is valid */
+   result = SDL_GetKeyFromScancode(SDL_SCANCODE_A);
+   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(valid)");
+   SDLTest_AssertCheck(result == SDLK_a, "Verify result from call, expected: %i, got: %i", SDLK_a, result);
+
+   /* Case where input is zero */
+   result = SDL_GetKeyFromScancode(0);
+   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(zero)");
+   SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
+
+   /* Case where input is invalid */
+   result = SDL_GetKeyFromScancode(-999);
+   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(invalid)");
+   SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
+
+   return TEST_COMPLETED;
+}
+
+
+
 /* ================= Test References ================== */
 
 /* Keyboard test cases */
 static const SDLTest_TestCaseReference keyboardTest1 =
-		{ (SDLTest_TestCaseFp)keyboard_getKeyboardState, "keyboard_getKeyboardState", "Check call to SDL_GetKeyboardState", TEST_ENABLED };
+		{ (SDLTest_TestCaseFp)keyboard_getKeyboardState, "keyboard_getKeyboardState", "Check call to SDL_GetKeyboardState with and without numkeys reference", TEST_ENABLED };
+
+static const SDLTest_TestCaseReference keyboardTest2 =
+		{ (SDLTest_TestCaseFp)keyboard_getKeyboardFocus, "keyboard_getKeyboardFocus", "Check call to SDL_GetKeyboardFocus", TEST_ENABLED };
+
+static const SDLTest_TestCaseReference keyboardTest3 =
+		{ (SDLTest_TestCaseFp)keyboard_getKeyFromName, "keyboard_getKeyFromName", "Check call to SDL_GetKeyFromName for known, unknown and invalid name", TEST_ENABLED };
+
+static const SDLTest_TestCaseReference keyboardTest4 =
+		{ (SDLTest_TestCaseFp)keyboard_getKeyFromScancode, "keyboard_getKeyFromScancode", "Check call to SDL_GetKeyFromScancode", TEST_ENABLED };
 
 /* Sequence of Keyboard test cases */
 static const SDLTest_TestCaseReference *keyboardTests[] =  {
-	&keyboardTest1, NULL
+	&keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, NULL
 };
 
 /* Keyboard test suite (global) */