Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Add tests to mouse suite; fix VS compiler warnings in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ferzkopp committed Jan 23, 2013
1 parent 0e5ad00 commit a846570
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 83 deletions.
3 changes: 3 additions & 0 deletions test/testautomation_audio.c
Expand Up @@ -3,6 +3,9 @@
* New/updated tests: aschiffler at ferzkopp dot net
*/

/* quiet windows compiler warnings */
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>

Expand Down
307 changes: 225 additions & 82 deletions test/testautomation_mouse.c
Expand Up @@ -115,79 +115,79 @@ mouse_getRelativeMouseState(void *arg)
return TEST_COMPLETED;
}


/* XPM definition of mouse Cursor */
static const char *_mouseArrowData[] = {
/* pixels */
"X ",
"XX ",
"X.X ",
"X..X ",
"X...X ",
"X....X ",
"X.....X ",
"X......X ",
"X.......X ",
"X........X ",
"X.....XXXXX ",
"X..X..X ",
"X.X X..X ",
"XX X..X ",
"X X..X ",
" X..X ",
" X..X ",
" X..X ",
" XX ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
};

/* Helper that creates a new mouse cursor from an XPM */
static SDL_Cursor *_initArrowCursor(const char *image[])
{
SDL_Cursor *cursor;
int i, row, col;
Uint8 data[4*32];
Uint8 mask[4*32];

i = -1;
for ( row=0; row<32; ++row ) {
for ( col=0; col<32; ++col ) {
if ( col % 8 ) {
data[i] <<= 1;
mask[i] <<= 1;
} else {
++i;
data[i] = mask[i] = 0;
}
switch (image[row][col]) {
case 'X':
data[i] |= 0x01;
mask[i] |= 0x01;
break;
case '.':
mask[i] |= 0x01;
break;
case ' ':
break;
}
}
}

cursor = SDL_CreateCursor(data, mask, 32, 32, 0, 0);
return cursor;
}

/* XPM definition of mouse Cursor */
static const char *_mouseArrowData[] = {
/* pixels */
"X ",
"XX ",
"X.X ",
"X..X ",
"X...X ",
"X....X ",
"X.....X ",
"X......X ",
"X.......X ",
"X........X ",
"X.....XXXXX ",
"X..X..X ",
"X.X X..X ",
"XX X..X ",
"X X..X ",
" X..X ",
" X..X ",
" X..X ",
" XX ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
};

/* Helper that creates a new mouse cursor from an XPM */
static SDL_Cursor *_initArrowCursor(const char *image[])
{
SDL_Cursor *cursor;
int i, row, col;
Uint8 data[4*32];
Uint8 mask[4*32];

i = -1;
for ( row=0; row<32; ++row ) {
for ( col=0; col<32; ++col ) {
if ( col % 8 ) {
data[i] <<= 1;
mask[i] <<= 1;
} else {
++i;
data[i] = mask[i] = 0;
}
switch (image[row][col]) {
case 'X':
data[i] |= 0x01;
mask[i] |= 0x01;
break;
case '.':
mask[i] |= 0x01;
break;
case ' ':
break;
}
}
}

cursor = SDL_CreateCursor(data, mask, 32, 32, 0, 0);
return cursor;
}

/**
* @brief Check call to SDL_CreateCursor and SDL_FreeCursor
Expand All @@ -202,8 +202,8 @@ mouse_createFreeCursor(void *arg)

/* Create a cursor */
cursor = _initArrowCursor(_mouseArrowData);
SDLTest_AssertPass("Call to SDL_CreateCursor()");
SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");
SDLTest_AssertPass("Call to SDL_CreateCursor()");
SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");
if (cursor == NULL) {
return TEST_ABORTED;
}
Expand All @@ -222,10 +222,10 @@ void _changeCursorVisibility(int state)
int newState;
int result;

oldState = SDL_ShowCursor(SDL_QUERY);
oldState = SDL_ShowCursor(SDL_QUERY);
SDLTest_AssertPass("Call to SDL_ShowCursor(SDL_QUERY)");

result = SDL_ShowCursor(state);
result = SDL_ShowCursor(state);
SDLTest_AssertPass("Call to SDL_ShowCursor(%s)", (state == SDL_ENABLE) ? "SDL_ENABLE" : "SDL_DISABLE");
SDLTest_AssertCheck(result == oldState, "Validate result from SDL_ShowCursor(%s), expected: %i, got: %i",
(state == SDL_ENABLE) ? "SDL_ENABLE" : "SDL_DISABLE", oldState, result);
Expand Down Expand Up @@ -278,18 +278,18 @@ mouse_setCursor(void *arg)

/* Create a cursor */
cursor = _initArrowCursor(_mouseArrowData);
SDLTest_AssertPass("Call to SDL_CreateCursor()");
SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");
SDLTest_AssertPass("Call to SDL_CreateCursor()");
SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");
if (cursor == NULL) {
return TEST_ABORTED;
}

/* Set the arrow cursor */
SDL_SetCursor(cursor);
SDL_SetCursor(cursor);
SDLTest_AssertPass("Call to SDL_SetCursor(cursor)");

/* Force redraw */
SDL_SetCursor(NULL);
SDL_SetCursor(NULL);
SDLTest_AssertPass("Call to SDL_SetCursor(NULL)");

/* Free cursor again */
Expand All @@ -299,6 +299,143 @@ mouse_setCursor(void *arg)
return TEST_COMPLETED;
}

#define MOUSE_TESTWINDOW_WIDTH 320
#define MOUSE_TESTWINDOW_HEIGHT 200

/**
* Create s test window
*/
SDL_Window *_createTestWindow()
{
int posX = 100, posY = 100, width = MOUSE_TESTWINDOW_WIDTH, height = MOUSE_TESTWINDOW_HEIGHT;
SDL_Window *window;
window = SDL_CreateWindow("mouse_createTestWindow", posX, posY, width, height, 0);
SDLTest_AssertPass("SDL_CreateWindow()");
SDLTest_AssertCheck(window != NULL, "Check SDL_CreateWindow result");
return window;
}

/*
* Destroy test window
*/
void _destroyTestWindow(SDL_Window *window)
{
if (window != NULL) {
SDL_DestroyWindow(window);
window = NULL;
SDLTest_AssertPass("SDL_DestroyWindow");
}
}

/**
* @brief Check call to SDL_WarpMouseInWindow
*
* @sa http://wiki.libsdl.org/moin.cgi/SDL_WarpMouseInWindow
*/
int
mouse_warpMouseInWindow(void *arg)
{
const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT;
int numPositions = 6;
int xPositions[] = {-1, 0, 1, w-1, w, w+1 };
int yPositions[] = {-1, 0, 1, h-1, h, h+1 };
int x, y, i, j;
SDL_Window *window;

/* Create test window */
window = _createTestWindow();
if (window == NULL) return TEST_ABORTED;

/* Mouse to random position inside window */
x = SDLTest_RandomIntegerInRange(1, w-1);
y = SDLTest_RandomIntegerInRange(1, h-1);
SDL_WarpMouseInWindow(window, x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);

/* Same position again */
SDL_WarpMouseInWindow(window, x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);

/* Mouse to various boundary positions */
for (i=0; i<numPositions; i++) {
for (j=0; j<numPositions; j++) {
x = xPositions[i];
y = yPositions[j];
SDL_WarpMouseInWindow(window, x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);

/* TODO: add tracking of events and check that each call generates a mouse motion event */
SDL_PumpEvents();
SDLTest_AssertPass("SDL_PumpEvents()");
}
}


/* Clean up test window */
_destroyTestWindow(window);

return TEST_COMPLETED;
}

/**
* @brief Check call to SDL_GetMouseFocus
*
* @sa http://wiki.libsdl.org/moin.cgi/SDL_GetMouseFocus
*/
int
mouse_getMouseFocus(void *arg)
{
const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT;
int x, y;
SDL_Window *window;
SDL_Window *focusWindow;

/* Get focus - focus non-deterministic */
focusWindow = SDL_GetMouseFocus();
SDLTest_AssertPass("SDL_GetMouseFocus()");

/* Create test window */
window = _createTestWindow();
if (window == NULL) return TEST_ABORTED;

/* Mouse to random position inside window */
x = SDLTest_RandomIntegerInRange(1, w-1);
y = SDLTest_RandomIntegerInRange(1, h-1);
SDL_WarpMouseInWindow(window, x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);

/* Pump events to update focus state */
SDL_PumpEvents();
SDLTest_AssertPass("SDL_PumpEvents()");

/* Get focus with explicit window setup - focus deterministic */
focusWindow = SDL_GetMouseFocus();
SDLTest_AssertPass("SDL_GetMouseFocus()");
SDLTest_AssertCheck (focusWindow != NULL, "Check returned window value is not NULL");
SDLTest_AssertCheck (focusWindow == window, "Check returned window value is test window");

/* Mouse to random position outside window */
x = SDLTest_RandomIntegerInRange(-9, -1);
y = SDLTest_RandomIntegerInRange(-9, -1);
SDL_WarpMouseInWindow(window, x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);

/* Clean up test window */
_destroyTestWindow(window);

/* Pump events to update focus state */
SDL_PumpEvents();
SDLTest_AssertPass("SDL_PumpEvents()");

/* Get focus for non-existing window */
focusWindow = SDL_GetMouseFocus();
SDLTest_AssertPass("SDL_GetMouseFocus()");
SDLTest_AssertCheck (focusWindow == NULL, "Check returned window value is NULL");


return TEST_COMPLETED;
}

/* ================= Test References ================== */

/* Mouse test cases */
Expand All @@ -317,9 +454,15 @@ static const SDLTest_TestCaseReference mouseTest4 =
static const SDLTest_TestCaseReference mouseTest5 =
{ (SDLTest_TestCaseFp)mouse_setCursor, "mouse_setCursor", "Check call to SDL_SetCursor", TEST_ENABLED };

static const SDLTest_TestCaseReference mouseTest6 =
{ (SDLTest_TestCaseFp)mouse_warpMouseInWindow, "mouse_warpMouseInWindow", "Check call to SDL_WarpMouseInWindow", TEST_ENABLED };

static const SDLTest_TestCaseReference mouseTest7 =
{ (SDLTest_TestCaseFp)mouse_getMouseFocus, "mouse_getMouseFocus", "Check call to SDL_getMouseFocus", TEST_ENABLED };

/* Sequence of Mouse test cases */
static const SDLTest_TestCaseReference *mouseTests[] = {
&mouseTest1, &mouseTest2, &mouseTest3, &mouseTest4, &mouseTest5, NULL
&mouseTest1, &mouseTest2, &mouseTest3, &mouseTest4, &mouseTest5, &mouseTest6, &mouseTest7, NULL
};

/* Mouse test suite (global) */
Expand Down
2 changes: 1 addition & 1 deletion test/testautomation_rwops.c
Expand Up @@ -419,7 +419,7 @@ int
rwops_testFileWriteReadEndian(void)
{
SDL_RWops *rw;
int result;
Sint64 result;
int mode;
size_t objectsWritten;
Uint16 BE16value;
Expand Down

0 comments on commit a846570

Please sign in to comment.