11 /* ================= Test Case Implementation ================== */
13 /* Test case functions */
15 /* Helper to evaluate state returned from SDL_GetMouseState */
16 int _mouseStateCheck(Uint32 state)
18 return (state == 0) ||
19 (state == SDL_BUTTON(SDL_BUTTON_LEFT)) ||
20 (state == SDL_BUTTON(SDL_BUTTON_MIDDLE)) ||
21 (state == SDL_BUTTON(SDL_BUTTON_RIGHT)) ||
22 (state == SDL_BUTTON(SDL_BUTTON_X1)) ||
23 (state == SDL_BUTTON(SDL_BUTTON_X2));
27 * @brief Check call to SDL_GetMouseState
31 mouse_getMouseState(void *arg)
37 /* Pump some events to update mouse state */
39 SDLTest_AssertPass("Call to SDL_PumpEvents()");
41 /* Case where x, y pointer is NULL */
42 state = SDL_GetMouseState(NULL, NULL);
43 SDLTest_AssertPass("Call to SDL_GetMouseState(NULL, NULL)");
44 SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
46 /* Case where x pointer is not NULL */
48 state = SDL_GetMouseState(&x, NULL);
49 SDLTest_AssertPass("Call to SDL_GetMouseState(&x, NULL)");
50 SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
51 SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
53 /* Case where y pointer is not NULL */
55 state = SDL_GetMouseState(NULL, &y);
56 SDLTest_AssertPass("Call to SDL_GetMouseState(NULL, &y)");
57 SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
58 SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
60 /* Case where x and y pointer is not NULL */
63 state = SDL_GetMouseState(&x, &y);
64 SDLTest_AssertPass("Call to SDL_GetMouseState(&x, &y)");
65 SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
66 SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
67 SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
69 return TEST_COMPLETED;
73 * @brief Check call to SDL_GetRelativeMouseState
77 mouse_getRelativeMouseState(void *arg)
83 /* Pump some events to update mouse state */
85 SDLTest_AssertPass("Call to SDL_PumpEvents()");
87 /* Case where x, y pointer is NULL */
88 state = SDL_GetRelativeMouseState(NULL, NULL);
89 SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(NULL, NULL)");
90 SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
92 /* Case where x pointer is not NULL */
94 state = SDL_GetRelativeMouseState(&x, NULL);
95 SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(&x, NULL)");
96 SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
97 SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
99 /* Case where y pointer is not NULL */
101 state = SDL_GetRelativeMouseState(NULL, &y);
102 SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(NULL, &y)");
103 SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
104 SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
106 /* Case where x and y pointer is not NULL */
109 state = SDL_GetRelativeMouseState(&x, &y);
110 SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(&x, &y)");
111 SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
112 SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
113 SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
115 return TEST_COMPLETED;
119 /* XPM definition of mouse Cursor */
120 static const char *_mouseArrowData[] = {
156 /* Helper that creates a new mouse cursor from an XPM */
157 static SDL_Cursor *_initArrowCursor(const char *image[])
165 for ( row=0; row<32; ++row ) {
166 for ( col=0; col<32; ++col ) {
172 data[i] = mask[i] = 0;
174 switch (image[row][col]) {
188 cursor = SDL_CreateCursor(data, mask, 32, 32, 0, 0);
193 * @brief Check call to SDL_CreateCursor and SDL_FreeCursor
195 * @sa http://wiki.libsdl.org/moin.cgi/SDL_CreateCursor
196 * @sa http://wiki.libsdl.org/moin.cgi/SDL_FreeCursor
199 mouse_createFreeCursor(void *arg)
203 /* Create a cursor */
204 cursor = _initArrowCursor(_mouseArrowData);
205 SDLTest_AssertPass("Call to SDL_CreateCursor()");
206 SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");
207 if (cursor == NULL) {
211 /* Free cursor again */
212 SDL_FreeCursor(cursor);
213 SDLTest_AssertPass("Call to SDL_FreeCursor()");
215 return TEST_COMPLETED;
219 * @brief Check call to SDL_CreateColorCursor and SDL_FreeCursor
221 * @sa http://wiki.libsdl.org/moin.cgi/SDL_CreateColorCursor
222 * @sa http://wiki.libsdl.org/moin.cgi/SDL_FreeCursor
225 mouse_createFreeColorCursor(void *arg)
230 /* Get sample surface */
231 face = SDLTest_ImageFace();
232 SDLTest_AssertCheck(face != NULL, "Validate sample input image is not NULL");
233 if (face == NULL) return TEST_ABORTED;
235 /* Create a color cursor from surface */
236 cursor = SDL_CreateColorCursor(face, 0, 0);
237 SDLTest_AssertPass("Call to SDL_CreateColorCursor()");
238 SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateColorCursor() is not NULL");
239 if (cursor == NULL) {
240 SDL_FreeSurface(face);
244 /* Free cursor again */
245 SDL_FreeCursor(cursor);
246 SDLTest_AssertPass("Call to SDL_FreeCursor()");
249 SDL_FreeSurface(face);
251 return TEST_COMPLETED;
254 /* Helper that changes cursor visibility */
255 void _changeCursorVisibility(int state)
261 oldState = SDL_ShowCursor(SDL_QUERY);
262 SDLTest_AssertPass("Call to SDL_ShowCursor(SDL_QUERY)");
264 result = SDL_ShowCursor(state);
265 SDLTest_AssertPass("Call to SDL_ShowCursor(%s)", (state == SDL_ENABLE) ? "SDL_ENABLE" : "SDL_DISABLE");
266 SDLTest_AssertCheck(result == oldState, "Validate result from SDL_ShowCursor(%s), expected: %i, got: %i",
267 (state == SDL_ENABLE) ? "SDL_ENABLE" : "SDL_DISABLE", oldState, result);
269 newState = SDL_ShowCursor(SDL_QUERY);
270 SDLTest_AssertPass("Call to SDL_ShowCursor(SDL_QUERY)");
271 SDLTest_AssertCheck(state == newState, "Validate new state, expected: %i, got: %i",
276 * @brief Check call to SDL_ShowCursor
278 * @sa http://wiki.libsdl.org/moin.cgi/SDL_ShowCursor
281 mouse_showCursor(void *arg)
285 /* Get current state */
286 currentState = SDL_ShowCursor(SDL_QUERY);
287 SDLTest_AssertPass("Call to SDL_ShowCursor(SDL_QUERY)");
288 SDLTest_AssertCheck(currentState == SDL_DISABLE || currentState == SDL_ENABLE,
289 "Validate result is %i or %i, got: %i", SDL_DISABLE, SDL_ENABLE, currentState);
290 if (currentState == SDL_DISABLE) {
291 /* Show the cursor, then hide it again */
292 _changeCursorVisibility(SDL_ENABLE);
293 _changeCursorVisibility(SDL_DISABLE);
294 } else if (currentState == SDL_ENABLE) {
295 /* Hide the cursor, then show it again */
296 _changeCursorVisibility(SDL_DISABLE);
297 _changeCursorVisibility(SDL_ENABLE);
302 return TEST_COMPLETED;
306 * @brief Check call to SDL_SetCursor
308 * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetCursor
311 mouse_setCursor(void *arg)
315 /* Create a cursor */
316 cursor = _initArrowCursor(_mouseArrowData);
317 SDLTest_AssertPass("Call to SDL_CreateCursor()");
318 SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");
319 if (cursor == NULL) {
323 /* Set the arrow cursor */
324 SDL_SetCursor(cursor);
325 SDLTest_AssertPass("Call to SDL_SetCursor(cursor)");
329 SDLTest_AssertPass("Call to SDL_SetCursor(NULL)");
331 /* Free cursor again */
332 SDL_FreeCursor(cursor);
333 SDLTest_AssertPass("Call to SDL_FreeCursor()");
335 return TEST_COMPLETED;
339 * @brief Check call to SDL_GetCursor
341 * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetCursor
344 mouse_getCursor(void *arg)
348 /* Get current cursor */
349 cursor = SDL_GetCursor();
350 SDLTest_AssertPass("Call to SDL_GetCursor()");
351 SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_GetCursor() is not NULL");
353 return TEST_COMPLETED;
357 * @brief Check call to SDL_GetRelativeMouseMode and SDL_SetRelativeMouseMode
359 * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetRelativeMouseMode
360 * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetRelativeMouseMode
363 mouse_getSetRelativeMouseMode(void *arg)
367 SDL_bool initialState;
368 SDL_bool currentState;
370 /* Capture original state so we can revert back to it later */
371 initialState = SDL_GetRelativeMouseMode();
372 SDLTest_AssertPass("Call to SDL_GetRelativeMouseMode()");
374 /* Repeat twice to check D->D transition */
375 for (i=0; i<2; i++) {
376 /* Disable - should always be supported */
377 result = SDL_SetRelativeMouseMode(SDL_FALSE);
378 SDLTest_AssertPass("Call to SDL_SetRelativeMouseMode(FALSE)");
379 SDLTest_AssertCheck(result == 0, "Validate result value from SDL_SetRelativeMouseMode, expected: 0, got: %i", result);
380 currentState = SDL_GetRelativeMouseMode();
381 SDLTest_AssertPass("Call to SDL_GetRelativeMouseMode()");
382 SDLTest_AssertCheck(currentState == SDL_FALSE, "Validate current state is FALSE, got: %i", currentState);
385 /* Repeat twice to check D->E->E transition */
386 for (i=0; i<2; i++) {
387 /* Enable - may not be supported */
388 result = SDL_SetRelativeMouseMode(SDL_TRUE);
389 SDLTest_AssertPass("Call to SDL_SetRelativeMouseMode(TRUE)");
391 SDLTest_AssertCheck(result == 0, "Validate result value from SDL_SetRelativeMouseMode, expected: 0, got: %i", result);
392 currentState = SDL_GetRelativeMouseMode();
393 SDLTest_AssertPass("Call to SDL_GetRelativeMouseMode()");
394 SDLTest_AssertCheck(currentState == SDL_TRUE, "Validate current state is TRUE, got: %i", currentState);
398 /* Disable to check E->D transition */
399 result = SDL_SetRelativeMouseMode(SDL_FALSE);
400 SDLTest_AssertPass("Call to SDL_SetRelativeMouseMode(FALSE)");
401 SDLTest_AssertCheck(result == 0, "Validate result value from SDL_SetRelativeMouseMode, expected: 0, got: %i", result);
402 currentState = SDL_GetRelativeMouseMode();
403 SDLTest_AssertPass("Call to SDL_GetRelativeMouseMode()");
404 SDLTest_AssertCheck(currentState == SDL_FALSE, "Validate current state is FALSE, got: %i", currentState);
406 /* Revert to original state - ignore result */
407 result = SDL_SetRelativeMouseMode(initialState);
409 return TEST_COMPLETED;
412 #define MOUSE_TESTWINDOW_WIDTH 320
413 #define MOUSE_TESTWINDOW_HEIGHT 200
416 * Creates a test window
418 SDL_Window *_createMouseSuiteTestWindow()
420 int posX = 100, posY = 100, width = MOUSE_TESTWINDOW_WIDTH, height = MOUSE_TESTWINDOW_HEIGHT;
422 window = SDL_CreateWindow("mouse_createMouseSuiteTestWindow", posX, posY, width, height, 0);
423 SDLTest_AssertPass("SDL_CreateWindow()");
424 SDLTest_AssertCheck(window != NULL, "Check SDL_CreateWindow result");
429 * Destroy test window
431 void _destroyMouseSuiteTestWindow(SDL_Window *window)
433 if (window != NULL) {
434 SDL_DestroyWindow(window);
436 SDLTest_AssertPass("SDL_DestroyWindow()");
441 * @brief Check call to SDL_WarpMouseInWindow
443 * @sa http://wiki.libsdl.org/moin.cgi/SDL_WarpMouseInWindow
446 mouse_warpMouseInWindow(void *arg)
448 const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT;
449 int numPositions = 6;
450 int xPositions[] = {-1, 0, 1, w-1, w, w+1 };
451 int yPositions[] = {-1, 0, 1, h-1, h, h+1 };
455 /* Create test window */
456 window = _createMouseSuiteTestWindow();
457 if (window == NULL) return TEST_ABORTED;
459 /* Mouse to random position inside window */
460 x = SDLTest_RandomIntegerInRange(1, w-1);
461 y = SDLTest_RandomIntegerInRange(1, h-1);
462 SDL_WarpMouseInWindow(window, x, y);
463 SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
465 /* Same position again */
466 SDL_WarpMouseInWindow(window, x, y);
467 SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
469 /* Mouse to various boundary positions */
470 for (i=0; i<numPositions; i++) {
471 for (j=0; j<numPositions; j++) {
474 SDL_WarpMouseInWindow(window, x, y);
475 SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
477 /* TODO: add tracking of events and check that each call generates a mouse motion event */
479 SDLTest_AssertPass("SDL_PumpEvents()");
484 /* Clean up test window */
485 _destroyMouseSuiteTestWindow(window);
487 return TEST_COMPLETED;
491 * @brief Check call to SDL_GetMouseFocus
493 * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetMouseFocus
496 mouse_getMouseFocus(void *arg)
498 const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT;
501 SDL_Window *focusWindow;
503 /* Get focus - focus non-deterministic */
504 focusWindow = SDL_GetMouseFocus();
505 SDLTest_AssertPass("SDL_GetMouseFocus()");
507 /* Create test window */
508 window = _createMouseSuiteTestWindow();
509 if (window == NULL) return TEST_ABORTED;
511 /* Mouse to random position inside window */
512 x = SDLTest_RandomIntegerInRange(1, w-1);
513 y = SDLTest_RandomIntegerInRange(1, h-1);
514 SDL_WarpMouseInWindow(window, x, y);
515 SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
517 /* Pump events to update focus state */
519 SDLTest_AssertPass("SDL_PumpEvents()");
521 /* Get focus with explicit window setup - focus deterministic */
522 focusWindow = SDL_GetMouseFocus();
523 SDLTest_AssertPass("SDL_GetMouseFocus()");
524 SDLTest_AssertCheck (focusWindow != NULL, "Check returned window value is not NULL");
525 SDLTest_AssertCheck (focusWindow == window, "Check returned window value is test window");
527 /* Mouse to random position outside window */
528 x = SDLTest_RandomIntegerInRange(-9, -1);
529 y = SDLTest_RandomIntegerInRange(-9, -1);
530 SDL_WarpMouseInWindow(window, x, y);
531 SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
533 /* Clean up test window */
534 _destroyMouseSuiteTestWindow(window);
536 /* Pump events to update focus state */
538 SDLTest_AssertPass("SDL_PumpEvents()");
540 /* Get focus for non-existing window */
541 focusWindow = SDL_GetMouseFocus();
542 SDLTest_AssertPass("SDL_GetMouseFocus()");
543 SDLTest_AssertCheck (focusWindow == NULL, "Check returned window value is NULL");
546 return TEST_COMPLETED;
549 /* ================= Test References ================== */
551 /* Mouse test cases */
552 static const SDLTest_TestCaseReference mouseTest1 =
553 { (SDLTest_TestCaseFp)mouse_getMouseState, "mouse_getMouseState", "Check call to SDL_GetMouseState", TEST_ENABLED };
555 static const SDLTest_TestCaseReference mouseTest2 =
556 { (SDLTest_TestCaseFp)mouse_getRelativeMouseState, "mouse_getRelativeMouseState", "Check call to SDL_GetRelativeMouseState", TEST_ENABLED };
558 static const SDLTest_TestCaseReference mouseTest3 =
559 { (SDLTest_TestCaseFp)mouse_createFreeCursor, "mouse_createFreeCursor", "Check call to SDL_CreateCursor and SDL_FreeCursor", TEST_ENABLED };
561 static const SDLTest_TestCaseReference mouseTest4 =
562 { (SDLTest_TestCaseFp)mouse_showCursor, "mouse_showCursor", "Check call to SDL_ShowCursor", TEST_ENABLED };
564 static const SDLTest_TestCaseReference mouseTest5 =
565 { (SDLTest_TestCaseFp)mouse_setCursor, "mouse_setCursor", "Check call to SDL_SetCursor", TEST_ENABLED };
567 static const SDLTest_TestCaseReference mouseTest6 =
568 { (SDLTest_TestCaseFp)mouse_getCursor, "mouse_getCursor", "Check call to SDL_GetCursor", TEST_ENABLED };
570 static const SDLTest_TestCaseReference mouseTest7 =
571 { (SDLTest_TestCaseFp)mouse_warpMouseInWindow, "mouse_warpMouseInWindow", "Check call to SDL_WarpMouseInWindow", TEST_ENABLED };
573 static const SDLTest_TestCaseReference mouseTest8 =
574 { (SDLTest_TestCaseFp)mouse_getMouseFocus, "mouse_getMouseFocus", "Check call to SDL_getMouseFocus", TEST_ENABLED };
576 static const SDLTest_TestCaseReference mouseTest9 =
577 { (SDLTest_TestCaseFp)mouse_createFreeColorCursor, "mouse_createFreeColorCursor", "Check call to SDL_CreateColorCursor and SDL_FreeCursor", TEST_ENABLED };
579 static const SDLTest_TestCaseReference mouseTest10 =
580 { (SDLTest_TestCaseFp)mouse_getSetRelativeMouseMode, "mouse_getSetRelativeMouseMode", "Check call to SDL_GetRelativeMouseMode and SDL_SetRelativeMouseMode", TEST_ENABLED };
582 /* Sequence of Mouse test cases */
583 static const SDLTest_TestCaseReference *mouseTests[] = {
584 &mouseTest1, &mouseTest2, &mouseTest3, &mouseTest4, &mouseTest5, &mouseTest6,
585 &mouseTest7, &mouseTest8, &mouseTest9, &mouseTest10, NULL
588 /* Mouse test suite (global) */
589 SDLTest_TestSuiteReference mouseTestSuite = {