test/testautomation_keyboard.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Thu, 10 Jan 2013 23:26:49 -0800
changeset 6807 e3610bc90cf3
parent 6803 fbe83a6d10fa
child 6808 3ad413bd1cd6
permissions -rw-r--r--
Added 3 more test cases to keyboard suite
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@6807
   133
/**
aschiffler@6807
   134
 * @brief Check call to SDL_GetKeyName
aschiffler@6807
   135
 * 
aschiffler@6807
   136
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyName
aschiffler@6807
   137
 */
aschiffler@6807
   138
int
aschiffler@6807
   139
keyboard_getKeyName(void *arg)
aschiffler@6807
   140
{   
aschiffler@6807
   141
   char *result;
aschiffler@6807
   142
   char *expected;
aschiffler@6807
   143
aschiffler@6807
   144
   /* Case where key has a 1 character name */
aschiffler@6807
   145
   expected = "3";
aschiffler@6807
   146
   result = (char *)SDL_GetKeyName(SDLK_3);
aschiffler@6807
   147
   SDLTest_AssertPass("Call to SDL_GetKeyName()");
aschiffler@6807
   148
   SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
aschiffler@6807
   149
   SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
aschiffler@6807
   150
aschiffler@6807
   151
   /* Case where key has a 2 character name */
aschiffler@6807
   152
   expected = "F1";
aschiffler@6807
   153
   result = (char *)SDL_GetKeyName(SDLK_F1);
aschiffler@6807
   154
   SDLTest_AssertPass("Call to SDL_GetKeyName()");
aschiffler@6807
   155
   SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
aschiffler@6807
   156
   SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
aschiffler@6807
   157
aschiffler@6807
   158
   /* Case where key has a 3 character name */
aschiffler@6807
   159
   expected = "Cut";
aschiffler@6807
   160
   result = (char *)SDL_GetKeyName(SDLK_CUT);
aschiffler@6807
   161
   SDLTest_AssertPass("Call to SDL_GetKeyName()");
aschiffler@6807
   162
   SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
aschiffler@6807
   163
   SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
aschiffler@6807
   164
aschiffler@6807
   165
   /* Case where key has a 4 character name */
aschiffler@6807
   166
   expected = "Down";
aschiffler@6807
   167
   result = (char *)SDL_GetKeyName(SDLK_DOWN);
aschiffler@6807
   168
   SDLTest_AssertPass("Call to SDL_GetKeyName()");
aschiffler@6807
   169
   SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
aschiffler@6807
   170
   SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
aschiffler@6807
   171
aschiffler@6807
   172
   /* Case where key has a N character name */
aschiffler@6807
   173
   expected = "BrightnessUp";
aschiffler@6807
   174
   result = (char *)SDL_GetKeyName(SDLK_BRIGHTNESSUP);
aschiffler@6807
   175
   SDLTest_AssertPass("Call to SDL_GetKeyName()");
aschiffler@6807
   176
   SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
aschiffler@6807
   177
   SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
aschiffler@6807
   178
aschiffler@6807
   179
   /* Case where key has a N character name with space */
aschiffler@6807
   180
   expected = "Keypad MemStore";
aschiffler@6807
   181
   result = (char *)SDL_GetKeyName(SDLK_KP_MEMSTORE);
aschiffler@6807
   182
   SDLTest_AssertPass("Call to SDL_GetKeyName()");
aschiffler@6807
   183
   SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL");
aschiffler@6807
   184
   SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s", expected, result);
aschiffler@6807
   185
aschiffler@6807
   186
   return TEST_COMPLETED;
aschiffler@6807
   187
}
aschiffler@6807
   188
aschiffler@6807
   189
/**
aschiffler@6807
   190
 * @brief Check call to SDL_GetModState and SDL_SetModState
aschiffler@6807
   191
 * 
aschiffler@6807
   192
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetModState
aschiffler@6807
   193
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetModState
aschiffler@6807
   194
 */
aschiffler@6807
   195
int
aschiffler@6807
   196
keyboard_getSetModState(void *arg)
aschiffler@6807
   197
{   
aschiffler@6807
   198
   SDL_Keymod result;
aschiffler@6807
   199
   SDL_Keymod currentState;
aschiffler@6807
   200
   SDL_Keymod newState;
aschiffler@6807
   201
   SDL_Keymod allStates =     
aschiffler@6807
   202
    KMOD_NONE |
aschiffler@6807
   203
    KMOD_LSHIFT |
aschiffler@6807
   204
    KMOD_RSHIFT |
aschiffler@6807
   205
    KMOD_LCTRL |
aschiffler@6807
   206
    KMOD_RCTRL |
aschiffler@6807
   207
    KMOD_LALT |
aschiffler@6807
   208
    KMOD_RALT |
aschiffler@6807
   209
    KMOD_LGUI |
aschiffler@6807
   210
    KMOD_RGUI |
aschiffler@6807
   211
    KMOD_NUM |
aschiffler@6807
   212
    KMOD_CAPS |
aschiffler@6807
   213
    KMOD_MODE |
aschiffler@6807
   214
    KMOD_RESERVED;
aschiffler@6807
   215
aschiffler@6807
   216
   /* Get state, cache for later reset */                                                   
aschiffler@6807
   217
   result = SDL_GetModState();
aschiffler@6807
   218
   SDLTest_AssertPass("Call to SDL_GetModState()");
aschiffler@6807
   219
   SDLTest_AssertCheck(result >=0 && result <= allStates, "Verify result from call is valid, expected: 0 <= result <= %i, got: %i", allStates, result);   
aschiffler@6807
   220
   currentState = result;
aschiffler@6807
   221
aschiffler@6807
   222
   /* Set random state */   
aschiffler@6807
   223
   newState = SDLTest_RandomIntegerInRange(0, allStates);
aschiffler@6807
   224
   SDL_SetModState(newState);
aschiffler@6807
   225
   SDLTest_AssertPass("Call to SDL_SetModState(%i)", newState);
aschiffler@6807
   226
   result = SDL_GetModState();
aschiffler@6807
   227
   SDLTest_AssertPass("Call to SDL_GetModState()");
aschiffler@6807
   228
   SDLTest_AssertCheck(result == newState, "Verify result from call is valid, expected: %i, got: %i", newState, result);
aschiffler@6807
   229
aschiffler@6807
   230
   /* Set zero state */
aschiffler@6807
   231
   SDL_SetModState(0);
aschiffler@6807
   232
   SDLTest_AssertPass("Call to SDL_SetModState(0)");
aschiffler@6807
   233
   result = SDL_GetModState();
aschiffler@6807
   234
   SDLTest_AssertPass("Call to SDL_GetModState()");
aschiffler@6807
   235
   SDLTest_AssertCheck(result == 0, "Verify result from call is valid, expected: 0, got: %i", result);
aschiffler@6807
   236
aschiffler@6807
   237
   /* Revert back to cached current state if needed */
aschiffler@6807
   238
   if (currentState != 0) {
aschiffler@6807
   239
     SDL_SetModState(currentState);
aschiffler@6807
   240
     SDLTest_AssertPass("Call to SDL_SetModState(%i)", currentState);
aschiffler@6807
   241
     result = SDL_GetModState();
aschiffler@6807
   242
     SDLTest_AssertPass("Call to SDL_GetModState()");
aschiffler@6807
   243
     SDLTest_AssertCheck(result == currentState, "Verify result from call is valid, expected: %i, got: %i", currentState, result);
aschiffler@6807
   244
   }
aschiffler@6807
   245
aschiffler@6807
   246
   return TEST_COMPLETED;
aschiffler@6807
   247
}
aschiffler@6807
   248
aschiffler@6807
   249
aschiffler@6807
   250
/**
aschiffler@6807
   251
 * @brief Check call to SDL_StartTextInput and SDL_StopTextInput
aschiffler@6807
   252
 * 
aschiffler@6807
   253
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_StartTextInput
aschiffler@6807
   254
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_StopTextInput
aschiffler@6807
   255
 */
aschiffler@6807
   256
int
aschiffler@6807
   257
keyboard_startStopTextInput(void *arg)
aschiffler@6807
   258
{   
aschiffler@6807
   259
   /* Start-Stop */
aschiffler@6807
   260
   SDL_StartTextInput();
aschiffler@6807
   261
   SDLTest_AssertPass("Call to SDL_StartTextInput()");
aschiffler@6807
   262
   SDL_StopTextInput();
aschiffler@6807
   263
   SDLTest_AssertPass("Call to SDL_StopTextInput()");
aschiffler@6807
   264
aschiffler@6807
   265
   /* Stop-Start */
aschiffler@6807
   266
   SDL_StartTextInput();
aschiffler@6807
   267
   SDLTest_AssertPass("Call to SDL_StartTextInput()");
aschiffler@6807
   268
   
aschiffler@6807
   269
   /* Start-Start */
aschiffler@6807
   270
   SDL_StartTextInput();
aschiffler@6807
   271
   SDLTest_AssertPass("Call to SDL_StartTextInput()");
aschiffler@6807
   272
aschiffler@6807
   273
   /* Stop-Stop */   
aschiffler@6807
   274
   SDL_StopTextInput();
aschiffler@6807
   275
   SDLTest_AssertPass("Call to SDL_StopTextInput()");
aschiffler@6807
   276
   SDL_StopTextInput();
aschiffler@6807
   277
   SDLTest_AssertPass("Call to SDL_StopTextInput()");
aschiffler@6807
   278
aschiffler@6807
   279
   return TEST_COMPLETED;
aschiffler@6807
   280
}
aschiffler@6807
   281
aschiffler@6803
   282
aschiffler@6803
   283
aschiffler@6800
   284
/* ================= Test References ================== */
aschiffler@6800
   285
aschiffler@6800
   286
/* Keyboard test cases */
aschiffler@6800
   287
static const SDLTest_TestCaseReference keyboardTest1 =
aschiffler@6803
   288
		{ (SDLTest_TestCaseFp)keyboard_getKeyboardState, "keyboard_getKeyboardState", "Check call to SDL_GetKeyboardState with and without numkeys reference", TEST_ENABLED };
aschiffler@6803
   289
aschiffler@6803
   290
static const SDLTest_TestCaseReference keyboardTest2 =
aschiffler@6803
   291
		{ (SDLTest_TestCaseFp)keyboard_getKeyboardFocus, "keyboard_getKeyboardFocus", "Check call to SDL_GetKeyboardFocus", TEST_ENABLED };
aschiffler@6803
   292
aschiffler@6803
   293
static const SDLTest_TestCaseReference keyboardTest3 =
aschiffler@6803
   294
		{ (SDLTest_TestCaseFp)keyboard_getKeyFromName, "keyboard_getKeyFromName", "Check call to SDL_GetKeyFromName for known, unknown and invalid name", TEST_ENABLED };
aschiffler@6803
   295
aschiffler@6803
   296
static const SDLTest_TestCaseReference keyboardTest4 =
aschiffler@6803
   297
		{ (SDLTest_TestCaseFp)keyboard_getKeyFromScancode, "keyboard_getKeyFromScancode", "Check call to SDL_GetKeyFromScancode", TEST_ENABLED };
aschiffler@6800
   298
aschiffler@6807
   299
static const SDLTest_TestCaseReference keyboardTest5 =
aschiffler@6807
   300
		{ (SDLTest_TestCaseFp)keyboard_getKeyName, "keyboard_getKeyName", "Check call to SDL_GetKeyName", TEST_ENABLED };
aschiffler@6807
   301
aschiffler@6807
   302
static const SDLTest_TestCaseReference keyboardTest6 =
aschiffler@6807
   303
		{ (SDLTest_TestCaseFp)keyboard_getSetModState, "keyboard_getSetModState", "Check call to SDL_GetModState and SDL_SetModState", TEST_ENABLED };
aschiffler@6807
   304
aschiffler@6807
   305
static const SDLTest_TestCaseReference keyboardTest7 =
aschiffler@6807
   306
		{ (SDLTest_TestCaseFp)keyboard_startStopTextInput, "keyboard_startStopTextInput", "Check call to SDL_StartTextInput and SDL_StopTextInput", TEST_ENABLED };
aschiffler@6807
   307
aschiffler@6800
   308
/* Sequence of Keyboard test cases */
aschiffler@6800
   309
static const SDLTest_TestCaseReference *keyboardTests[] =  {
aschiffler@6807
   310
	&keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, &keyboardTest5, &keyboardTest6, &keyboardTest7, NULL
aschiffler@6800
   311
};
aschiffler@6800
   312
aschiffler@6800
   313
/* Keyboard test suite (global) */
aschiffler@6800
   314
SDLTest_TestSuiteReference keyboardTestSuite = {
aschiffler@6800
   315
	"Keyboard",
aschiffler@6800
   316
	NULL,
aschiffler@6800
   317
	keyboardTests,
aschiffler@6800
   318
	NULL
aschiffler@6800
   319
};