test/testautomation_keyboard.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 07 Jan 2013 07:39:15 -0800
changeset 6803 fbe83a6d10fa
parent 6800 26c6795ec1a4
child 6807 e3610bc90cf3
permissions -rw-r--r--
Add a few keyboard tests; fix missing input validation in SDL_GetKeyFromName
aschiffler@6800
     1
/**
aschiffler@6800
     2
 * Keyboard test suite
aschiffler@6800
     3
 */
aschiffler@6800
     4
aschiffler@6800
     5
#include <stdio.h>
aschiffler@6800
     6
aschiffler@6800
     7
#include "SDL.h"
aschiffler@6800
     8
#include "SDL_test.h"
aschiffler@6800
     9
aschiffler@6800
    10
/* ================= Test Case Implementation ================== */
aschiffler@6800
    11
aschiffler@6800
    12
/* Test case functions */
aschiffler@6800
    13
aschiffler@6800
    14
/**
aschiffler@6803
    15
 * @brief Check call to SDL_GetKeyboardState with and without numkeys reference.
aschiffler@6800
    16
 * 
aschiffler@6803
    17
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyboardState
aschiffler@6800
    18
 */
aschiffler@6800
    19
int
aschiffler@6800
    20
keyboard_getKeyboardState(void *arg)
aschiffler@6800
    21
{
aschiffler@6800
    22
   int numkeys;
aschiffler@6800
    23
   Uint8 *state;
aschiffler@6800
    24
aschiffler@6800
    25
   /* Case where numkeys pointer is NULL */    
aschiffler@6800
    26
   state = SDL_GetKeyboardState(NULL);
aschiffler@6800
    27
   SDLTest_AssertPass("Call to SDL_GetKeyboardState(NULL)");
aschiffler@6800
    28
   SDLTest_AssertCheck(state != NULL, "Validate that return value from SDL_GetKeyboardState is not NULL");
aschiffler@6800
    29
aschiffler@6800
    30
   /* Case where numkeys pointer is not NULL */
aschiffler@6800
    31
   numkeys = -1;
aschiffler@6800
    32
   state = SDL_GetKeyboardState(&numkeys);
aschiffler@6800
    33
   SDLTest_AssertPass("Call to SDL_GetKeyboardState(&numkeys)");
aschiffler@6800
    34
   SDLTest_AssertCheck(state != NULL, "Validate that return value from SDL_GetKeyboardState is not NULL");
aschiffler@6800
    35
   SDLTest_AssertCheck(numkeys >= 0, "Validate that value of numkeys is >= 0, got: %i", numkeys);
aschiffler@6800
    36
   
aschiffler@6800
    37
   return TEST_COMPLETED;
aschiffler@6800
    38
}
aschiffler@6800
    39
aschiffler@6803
    40
/**
aschiffler@6803
    41
 * @brief Check call to SDL_GetKeyboardFocus
aschiffler@6803
    42
 * 
aschiffler@6803
    43
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyboardFocus
aschiffler@6803
    44
 */
aschiffler@6803
    45
int
aschiffler@6803
    46
keyboard_getKeyboardFocus(void *arg)
aschiffler@6803
    47
{
aschiffler@6803
    48
   SDL_Window* window;
aschiffler@6803
    49
aschiffler@6803
    50
   /* Call, but ignore return value */
aschiffler@6803
    51
   window = SDL_GetKeyboardFocus();
aschiffler@6803
    52
   SDLTest_AssertPass("Call to SDL_GetKeyboardFocus()");
aschiffler@6803
    53
aschiffler@6803
    54
   return TEST_COMPLETED;
aschiffler@6803
    55
}
aschiffler@6803
    56
aschiffler@6803
    57
/**
aschiffler@6803
    58
 * @brief Check call to SDL_GetKeyFromName for known, unknown and invalid name.
aschiffler@6803
    59
 * 
aschiffler@6803
    60
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyFromName
aschiffler@6803
    61
 */
aschiffler@6803
    62
int
aschiffler@6803
    63
keyboard_getKeyFromName(void *arg)
aschiffler@6803
    64
{
aschiffler@6803
    65
   SDL_Keycode result;
aschiffler@6803
    66
aschiffler@6803
    67
   /* Case where Key is known, 1 character input */
aschiffler@6803
    68
   result = SDL_GetKeyFromName("A");
aschiffler@6803
    69
   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/single)");
aschiffler@6803
    70
   SDLTest_AssertCheck(result == SDLK_a, "Verify result from call, expected: %i, got: %i", SDLK_a, result);
aschiffler@6803
    71
aschiffler@6803
    72
   /* Case where Key is known, 2 character input */
aschiffler@6803
    73
   result = SDL_GetKeyFromName("F1");
aschiffler@6803
    74
   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/double)");
aschiffler@6803
    75
   SDLTest_AssertCheck(result == SDLK_F1, "Verify result from call, expected: %i, got: %i", SDLK_F1, result);
aschiffler@6803
    76
aschiffler@6803
    77
   /* Case where Key is known, 3 character input */
aschiffler@6803
    78
   result = SDL_GetKeyFromName("End");
aschiffler@6803
    79
   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/triple)");
aschiffler@6803
    80
   SDLTest_AssertCheck(result == SDLK_END, "Verify result from call, expected: %i, got: %i", SDLK_END, result);
aschiffler@6803
    81
aschiffler@6803
    82
   /* Case where Key is known, 4 character input */
aschiffler@6803
    83
   result = SDL_GetKeyFromName("Find");
aschiffler@6803
    84
   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/quad)");
aschiffler@6803
    85
   SDLTest_AssertCheck(result == SDLK_FIND, "Verify result from call, expected: %i, got: %i", SDLK_FIND, result);
aschiffler@6803
    86
aschiffler@6803
    87
   /* Case where Key is known, multiple character input */
aschiffler@6803
    88
   result = SDL_GetKeyFromName("AudioStop");
aschiffler@6803
    89
   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/multi)");
aschiffler@6803
    90
   SDLTest_AssertCheck(result == SDLK_AUDIOSTOP, "Verify result from call, expected: %i, got: %i", SDLK_AUDIOSTOP, result);
aschiffler@6803
    91
aschiffler@6803
    92
   /* Case where Key is unknown */
aschiffler@6803
    93
   result = SDL_GetKeyFromName("NotThere");
aschiffler@6803
    94
   SDLTest_AssertPass("Call to SDL_GetKeyFromName(unknown)");
aschiffler@6803
    95
   SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
aschiffler@6803
    96
aschiffler@6803
    97
   /* Case where input is NULL/invalid */
aschiffler@6803
    98
   result = SDL_GetKeyFromName(NULL);
aschiffler@6803
    99
   SDLTest_AssertPass("Call to SDL_GetKeyFromName(NULL)");
aschiffler@6803
   100
   SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
aschiffler@6803
   101
aschiffler@6803
   102
   return TEST_COMPLETED;
aschiffler@6803
   103
}
aschiffler@6803
   104
aschiffler@6803
   105
/**
aschiffler@6803
   106
 * @brief Check call to SDL_GetKeyFromScancode
aschiffler@6803
   107
 * 
aschiffler@6803
   108
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyFromScancode
aschiffler@6803
   109
 */
aschiffler@6803
   110
int
aschiffler@6803
   111
keyboard_getKeyFromScancode(void *arg)
aschiffler@6803
   112
{
aschiffler@6803
   113
   SDL_Keycode result;
aschiffler@6803
   114
aschiffler@6803
   115
   /* Case where input is valid */
aschiffler@6803
   116
   result = SDL_GetKeyFromScancode(SDL_SCANCODE_A);
aschiffler@6803
   117
   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(valid)");
aschiffler@6803
   118
   SDLTest_AssertCheck(result == SDLK_a, "Verify result from call, expected: %i, got: %i", SDLK_a, result);
aschiffler@6803
   119
aschiffler@6803
   120
   /* Case where input is zero */
aschiffler@6803
   121
   result = SDL_GetKeyFromScancode(0);
aschiffler@6803
   122
   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(zero)");
aschiffler@6803
   123
   SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
aschiffler@6803
   124
aschiffler@6803
   125
   /* Case where input is invalid */
aschiffler@6803
   126
   result = SDL_GetKeyFromScancode(-999);
aschiffler@6803
   127
   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(invalid)");
aschiffler@6803
   128
   SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
aschiffler@6803
   129
aschiffler@6803
   130
   return TEST_COMPLETED;
aschiffler@6803
   131
}
aschiffler@6803
   132
aschiffler@6803
   133
aschiffler@6803
   134
aschiffler@6800
   135
/* ================= Test References ================== */
aschiffler@6800
   136
aschiffler@6800
   137
/* Keyboard test cases */
aschiffler@6800
   138
static const SDLTest_TestCaseReference keyboardTest1 =
aschiffler@6803
   139
		{ (SDLTest_TestCaseFp)keyboard_getKeyboardState, "keyboard_getKeyboardState", "Check call to SDL_GetKeyboardState with and without numkeys reference", TEST_ENABLED };
aschiffler@6803
   140
aschiffler@6803
   141
static const SDLTest_TestCaseReference keyboardTest2 =
aschiffler@6803
   142
		{ (SDLTest_TestCaseFp)keyboard_getKeyboardFocus, "keyboard_getKeyboardFocus", "Check call to SDL_GetKeyboardFocus", TEST_ENABLED };
aschiffler@6803
   143
aschiffler@6803
   144
static const SDLTest_TestCaseReference keyboardTest3 =
aschiffler@6803
   145
		{ (SDLTest_TestCaseFp)keyboard_getKeyFromName, "keyboard_getKeyFromName", "Check call to SDL_GetKeyFromName for known, unknown and invalid name", TEST_ENABLED };
aschiffler@6803
   146
aschiffler@6803
   147
static const SDLTest_TestCaseReference keyboardTest4 =
aschiffler@6803
   148
		{ (SDLTest_TestCaseFp)keyboard_getKeyFromScancode, "keyboard_getKeyFromScancode", "Check call to SDL_GetKeyFromScancode", TEST_ENABLED };
aschiffler@6800
   149
aschiffler@6800
   150
/* Sequence of Keyboard test cases */
aschiffler@6800
   151
static const SDLTest_TestCaseReference *keyboardTests[] =  {
aschiffler@6803
   152
	&keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, NULL
aschiffler@6800
   153
};
aschiffler@6800
   154
aschiffler@6800
   155
/* Keyboard test suite (global) */
aschiffler@6800
   156
SDLTest_TestSuiteReference keyboardTestSuite = {
aschiffler@6800
   157
	"Keyboard",
aschiffler@6800
   158
	NULL,
aschiffler@6800
   159
	keyboardTests,
aschiffler@6800
   160
	NULL
aschiffler@6800
   161
};