diff options
Diffstat (limited to 'contrib/SDL-3.2.8/test/testautomation_mouse.c')
| -rw-r--r-- | contrib/SDL-3.2.8/test/testautomation_mouse.c | 689 |
1 files changed, 689 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/test/testautomation_mouse.c b/contrib/SDL-3.2.8/test/testautomation_mouse.c new file mode 100644 index 0000000..9efa630 --- /dev/null +++ b/contrib/SDL-3.2.8/test/testautomation_mouse.c | |||
| @@ -0,0 +1,689 @@ | |||
| 1 | /** | ||
| 2 | * Mouse test suite | ||
| 3 | */ | ||
| 4 | #include <limits.h> | ||
| 5 | #include <float.h> | ||
| 6 | |||
| 7 | #include <SDL3/SDL.h> | ||
| 8 | #include <SDL3/SDL_test.h> | ||
| 9 | #include "testautomation_suites.h" | ||
| 10 | #include "testautomation_images.h" | ||
| 11 | |||
| 12 | /* ================= Test Case Implementation ================== */ | ||
| 13 | |||
| 14 | /* Test case functions */ | ||
| 15 | |||
| 16 | /* Helper to evaluate state returned from SDL_GetMouseState */ | ||
| 17 | static int mouseStateCheck(Uint32 state) | ||
| 18 | { | ||
| 19 | return (state == 0) || | ||
| 20 | (state == SDL_BUTTON_MASK(SDL_BUTTON_LEFT)) || | ||
| 21 | (state == SDL_BUTTON_MASK(SDL_BUTTON_MIDDLE)) || | ||
| 22 | (state == SDL_BUTTON_MASK(SDL_BUTTON_RIGHT)) || | ||
| 23 | (state == SDL_BUTTON_MASK(SDL_BUTTON_X1)) || | ||
| 24 | (state == SDL_BUTTON_MASK(SDL_BUTTON_X2)); | ||
| 25 | } | ||
| 26 | |||
| 27 | /** | ||
| 28 | * Check call to SDL_GetMouseState | ||
| 29 | * | ||
| 30 | */ | ||
| 31 | static int SDLCALL mouse_getMouseState(void *arg) | ||
| 32 | { | ||
| 33 | float x; | ||
| 34 | float y; | ||
| 35 | SDL_MouseButtonFlags state; | ||
| 36 | |||
| 37 | /* Pump some events to update mouse state */ | ||
| 38 | SDL_PumpEvents(); | ||
| 39 | SDLTest_AssertPass("Call to SDL_PumpEvents()"); | ||
| 40 | |||
| 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: %" SDL_PRIu32, state); | ||
| 45 | |||
| 46 | /* Case where x pointer is not NULL */ | ||
| 47 | x = -FLT_MAX; | ||
| 48 | state = SDL_GetMouseState(&x, NULL); | ||
| 49 | SDLTest_AssertPass("Call to SDL_GetMouseState(&x, NULL)"); | ||
| 50 | SDLTest_AssertCheck(x > -FLT_MAX, "Validate that value of x is > -FLT_MAX, got: %g", x); | ||
| 51 | SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state); | ||
| 52 | |||
| 53 | /* Case where y pointer is not NULL */ | ||
| 54 | y = -FLT_MAX; | ||
| 55 | state = SDL_GetMouseState(NULL, &y); | ||
| 56 | SDLTest_AssertPass("Call to SDL_GetMouseState(NULL, &y)"); | ||
| 57 | SDLTest_AssertCheck(y > -FLT_MAX, "Validate that value of y is > -FLT_MAX, got: %g", y); | ||
| 58 | SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state); | ||
| 59 | |||
| 60 | /* Case where x and y pointer is not NULL */ | ||
| 61 | x = -FLT_MAX; | ||
| 62 | y = -FLT_MAX; | ||
| 63 | state = SDL_GetMouseState(&x, &y); | ||
| 64 | SDLTest_AssertPass("Call to SDL_GetMouseState(&x, &y)"); | ||
| 65 | SDLTest_AssertCheck(x > -FLT_MAX, "Validate that value of x is > -FLT_MAX, got: %g", x); | ||
| 66 | SDLTest_AssertCheck(y > -FLT_MAX, "Validate that value of y is > -FLT_MAX, got: %g", y); | ||
| 67 | SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state); | ||
| 68 | |||
| 69 | return TEST_COMPLETED; | ||
| 70 | } | ||
| 71 | |||
| 72 | /** | ||
| 73 | * Check call to SDL_GetRelativeMouseState | ||
| 74 | * | ||
| 75 | */ | ||
| 76 | static int SDLCALL mouse_getRelativeMouseState(void *arg) | ||
| 77 | { | ||
| 78 | float x; | ||
| 79 | float y; | ||
| 80 | SDL_MouseButtonFlags state; | ||
| 81 | |||
| 82 | /* Pump some events to update mouse state */ | ||
| 83 | SDL_PumpEvents(); | ||
| 84 | SDLTest_AssertPass("Call to SDL_PumpEvents()"); | ||
| 85 | |||
| 86 | /* Case where x, y pointer is NULL */ | ||
| 87 | state = SDL_GetRelativeMouseState(NULL, NULL); | ||
| 88 | SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(NULL, NULL)"); | ||
| 89 | SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state); | ||
| 90 | |||
| 91 | /* Case where x pointer is not NULL */ | ||
| 92 | x = -FLT_MAX; | ||
| 93 | state = SDL_GetRelativeMouseState(&x, NULL); | ||
| 94 | SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(&x, NULL)"); | ||
| 95 | SDLTest_AssertCheck(x > -FLT_MAX, "Validate that value of x is > -FLT_MAX, got: %g", x); | ||
| 96 | SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state); | ||
| 97 | |||
| 98 | /* Case where y pointer is not NULL */ | ||
| 99 | y = -FLT_MAX; | ||
| 100 | state = SDL_GetRelativeMouseState(NULL, &y); | ||
| 101 | SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(NULL, &y)"); | ||
| 102 | SDLTest_AssertCheck(y > -FLT_MAX, "Validate that value of y is > -FLT_MAX, got: %g", y); | ||
| 103 | SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state); | ||
| 104 | |||
| 105 | /* Case where x and y pointer is not NULL */ | ||
| 106 | x = -FLT_MAX; | ||
| 107 | y = -FLT_MAX; | ||
| 108 | state = SDL_GetRelativeMouseState(&x, &y); | ||
| 109 | SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(&x, &y)"); | ||
| 110 | SDLTest_AssertCheck(x > -FLT_MAX, "Validate that value of x is > -FLT_MAX, got: %g", x); | ||
| 111 | SDLTest_AssertCheck(y > -FLT_MAX, "Validate that value of y is > -FLT_MAX, got: %g", y); | ||
| 112 | SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state); | ||
| 113 | |||
| 114 | return TEST_COMPLETED; | ||
| 115 | } | ||
| 116 | |||
| 117 | /* XPM definition of mouse Cursor */ | ||
| 118 | static const char *g_mouseArrowData[] = { | ||
| 119 | /* pixels */ | ||
| 120 | "X ", | ||
| 121 | "XX ", | ||
| 122 | "X.X ", | ||
| 123 | "X..X ", | ||
| 124 | "X...X ", | ||
| 125 | "X....X ", | ||
| 126 | "X.....X ", | ||
| 127 | "X......X ", | ||
| 128 | "X.......X ", | ||
| 129 | "X........X ", | ||
| 130 | "X.....XXXXX ", | ||
| 131 | "X..X..X ", | ||
| 132 | "X.X X..X ", | ||
| 133 | "XX X..X ", | ||
| 134 | "X X..X ", | ||
| 135 | " X..X ", | ||
| 136 | " X..X ", | ||
| 137 | " X..X ", | ||
| 138 | " XX ", | ||
| 139 | " ", | ||
| 140 | " ", | ||
| 141 | " ", | ||
| 142 | " ", | ||
| 143 | " ", | ||
| 144 | " ", | ||
| 145 | " ", | ||
| 146 | " ", | ||
| 147 | " ", | ||
| 148 | " ", | ||
| 149 | " ", | ||
| 150 | " ", | ||
| 151 | " " | ||
| 152 | }; | ||
| 153 | |||
| 154 | /* Helper that creates a new mouse cursor from an XPM */ | ||
| 155 | static SDL_Cursor *initArrowCursor(const char *image[]) | ||
| 156 | { | ||
| 157 | SDL_Cursor *cursor; | ||
| 158 | int i, row, col; | ||
| 159 | Uint8 data[4 * 32]; | ||
| 160 | Uint8 mask[4 * 32]; | ||
| 161 | |||
| 162 | i = -1; | ||
| 163 | for (row = 0; row < 32; ++row) { | ||
| 164 | for (col = 0; col < 32; ++col) { | ||
| 165 | if (col % 8) { | ||
| 166 | data[i] <<= 1; | ||
| 167 | mask[i] <<= 1; | ||
| 168 | } else { | ||
| 169 | ++i; | ||
| 170 | data[i] = mask[i] = 0; | ||
| 171 | } | ||
| 172 | switch (image[row][col]) { | ||
| 173 | case 'X': | ||
| 174 | data[i] |= 0x01; | ||
| 175 | mask[i] |= 0x01; | ||
| 176 | break; | ||
| 177 | case '.': | ||
| 178 | mask[i] |= 0x01; | ||
| 179 | break; | ||
| 180 | case ' ': | ||
| 181 | break; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | cursor = SDL_CreateCursor(data, mask, 32, 32, 0, 0); | ||
| 187 | return cursor; | ||
| 188 | } | ||
| 189 | |||
| 190 | /** | ||
| 191 | * Check call to SDL_CreateCursor and SDL_DestroyCursor | ||
| 192 | * | ||
| 193 | * \sa SDL_CreateCursor | ||
| 194 | * \sa SDL_DestroyCursor | ||
| 195 | */ | ||
| 196 | static int SDLCALL mouse_createFreeCursor(void *arg) | ||
| 197 | { | ||
| 198 | SDL_Cursor *cursor; | ||
| 199 | |||
| 200 | /* Create a cursor */ | ||
| 201 | cursor = initArrowCursor(g_mouseArrowData); | ||
| 202 | SDLTest_AssertPass("Call to SDL_CreateCursor()"); | ||
| 203 | SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL"); | ||
| 204 | if (cursor == NULL) { | ||
| 205 | return TEST_ABORTED; | ||
| 206 | } | ||
| 207 | |||
| 208 | /* Free cursor again */ | ||
| 209 | SDLTest_AssertPass("About to call SDL_DestroyCursor()"); | ||
| 210 | SDL_DestroyCursor(cursor); | ||
| 211 | SDLTest_AssertPass("Call to SDL_DestroyCursor()"); | ||
| 212 | |||
| 213 | return TEST_COMPLETED; | ||
| 214 | } | ||
| 215 | |||
| 216 | /** | ||
| 217 | * Check call to SDL_CreateColorCursor and SDL_DestroyCursor | ||
| 218 | * | ||
| 219 | * \sa SDL_CreateColorCursor | ||
| 220 | * \sa SDL_DestroyCursor | ||
| 221 | */ | ||
| 222 | static int SDLCALL mouse_createFreeColorCursor(void *arg) | ||
| 223 | { | ||
| 224 | SDL_Surface *face; | ||
| 225 | SDL_Cursor *cursor; | ||
| 226 | |||
| 227 | /* Get sample surface */ | ||
| 228 | face = SDLTest_ImageFace(); | ||
| 229 | SDLTest_AssertCheck(face != NULL, "Validate sample input image is not NULL"); | ||
| 230 | if (face == NULL) { | ||
| 231 | return TEST_ABORTED; | ||
| 232 | } | ||
| 233 | |||
| 234 | /* Create a color cursor from surface */ | ||
| 235 | cursor = SDL_CreateColorCursor(face, 0, 0); | ||
| 236 | SDLTest_AssertPass("Call to SDL_CreateColorCursor()"); | ||
| 237 | SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateColorCursor() is not NULL"); | ||
| 238 | if (cursor == NULL) { | ||
| 239 | SDL_DestroySurface(face); | ||
| 240 | return TEST_ABORTED; | ||
| 241 | } | ||
| 242 | |||
| 243 | /* Free cursor again */ | ||
| 244 | SDLTest_AssertPass("About to call SDL_DestroyCursor()"); | ||
| 245 | SDL_DestroyCursor(cursor); | ||
| 246 | SDLTest_AssertPass("Call to SDL_DestroyCursor()"); | ||
| 247 | |||
| 248 | /* Clean up */ | ||
| 249 | SDL_DestroySurface(face); | ||
| 250 | |||
| 251 | return TEST_COMPLETED; | ||
| 252 | } | ||
| 253 | |||
| 254 | /* Helper that changes cursor visibility */ | ||
| 255 | static void changeCursorVisibility(bool state) | ||
| 256 | { | ||
| 257 | bool newState; | ||
| 258 | |||
| 259 | if (state) { | ||
| 260 | SDL_ShowCursor(); | ||
| 261 | } else { | ||
| 262 | SDL_HideCursor(); | ||
| 263 | } | ||
| 264 | SDLTest_AssertPass("Call to %s", state ? "SDL_ShowCursor()" : "SDL_HideCursor()"); | ||
| 265 | |||
| 266 | newState = SDL_CursorVisible(); | ||
| 267 | SDLTest_AssertPass("Call to SDL_CursorVisible()"); | ||
| 268 | SDLTest_AssertCheck(state == newState, "Validate new state, expected: %s, got: %s", | ||
| 269 | state ? "true" : "false", | ||
| 270 | newState ? "true" : "false"); | ||
| 271 | } | ||
| 272 | |||
| 273 | /** | ||
| 274 | * Check call to SDL_ShowCursor | ||
| 275 | * | ||
| 276 | * \sa SDL_ShowCursor | ||
| 277 | */ | ||
| 278 | static int SDLCALL mouse_showCursor(void *arg) | ||
| 279 | { | ||
| 280 | bool currentState; | ||
| 281 | |||
| 282 | /* Get current state */ | ||
| 283 | currentState = SDL_CursorVisible(); | ||
| 284 | SDLTest_AssertPass("Call to SDL_CursorVisible()"); | ||
| 285 | if (currentState) { | ||
| 286 | /* Hide the cursor, then show it again */ | ||
| 287 | changeCursorVisibility(false); | ||
| 288 | changeCursorVisibility(true); | ||
| 289 | } else { | ||
| 290 | /* Show the cursor, then hide it again */ | ||
| 291 | changeCursorVisibility(true); | ||
| 292 | changeCursorVisibility(false); | ||
| 293 | } | ||
| 294 | |||
| 295 | return TEST_COMPLETED; | ||
| 296 | } | ||
| 297 | |||
| 298 | /** | ||
| 299 | * Check call to SDL_SetCursor | ||
| 300 | * | ||
| 301 | * \sa SDL_SetCursor | ||
| 302 | */ | ||
| 303 | static int SDLCALL mouse_setCursor(void *arg) | ||
| 304 | { | ||
| 305 | SDL_Cursor *cursor; | ||
| 306 | |||
| 307 | /* Create a cursor */ | ||
| 308 | cursor = initArrowCursor(g_mouseArrowData); | ||
| 309 | SDLTest_AssertPass("Call to SDL_CreateCursor()"); | ||
| 310 | SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL"); | ||
| 311 | if (cursor == NULL) { | ||
| 312 | return TEST_ABORTED; | ||
| 313 | } | ||
| 314 | |||
| 315 | /* Set the arrow cursor */ | ||
| 316 | SDL_SetCursor(cursor); | ||
| 317 | SDLTest_AssertPass("Call to SDL_SetCursor(cursor)"); | ||
| 318 | |||
| 319 | /* Force redraw */ | ||
| 320 | SDL_SetCursor(NULL); | ||
| 321 | SDLTest_AssertPass("Call to SDL_SetCursor(NULL)"); | ||
| 322 | |||
| 323 | /* Free cursor again */ | ||
| 324 | SDLTest_AssertPass("About to call SDL_DestroyCursor()"); | ||
| 325 | SDL_DestroyCursor(cursor); | ||
| 326 | SDLTest_AssertPass("Call to SDL_DestroyCursor()"); | ||
| 327 | |||
| 328 | return TEST_COMPLETED; | ||
| 329 | } | ||
| 330 | |||
| 331 | /** | ||
| 332 | * Check call to SDL_GetCursor | ||
| 333 | * | ||
| 334 | * \sa SDL_GetCursor | ||
| 335 | */ | ||
| 336 | static int SDLCALL mouse_getCursor(void *arg) | ||
| 337 | { | ||
| 338 | SDL_Cursor *cursor; | ||
| 339 | |||
| 340 | /* Get current cursor */ | ||
| 341 | cursor = SDL_GetCursor(); | ||
| 342 | SDLTest_AssertPass("Call to SDL_GetCursor()"); | ||
| 343 | SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_GetCursor() is not NULL"); | ||
| 344 | |||
| 345 | return TEST_COMPLETED; | ||
| 346 | } | ||
| 347 | |||
| 348 | #define MOUSE_TESTWINDOW_WIDTH 320 | ||
| 349 | #define MOUSE_TESTWINDOW_HEIGHT 200 | ||
| 350 | |||
| 351 | /** | ||
| 352 | * Creates a test window | ||
| 353 | */ | ||
| 354 | static SDL_Window *createMouseSuiteTestWindow(void) | ||
| 355 | { | ||
| 356 | int width = MOUSE_TESTWINDOW_WIDTH, height = MOUSE_TESTWINDOW_HEIGHT; | ||
| 357 | SDL_Window *window; | ||
| 358 | window = SDL_CreateWindow("mousecreateMouseSuiteTestWindow", width, height, 0); | ||
| 359 | SDLTest_AssertPass("SDL_CreateWindow()"); | ||
| 360 | SDLTest_AssertCheck(window != NULL, "Check SDL_CreateWindow result"); | ||
| 361 | return window; | ||
| 362 | } | ||
| 363 | |||
| 364 | /** | ||
| 365 | * Destroy test window | ||
| 366 | */ | ||
| 367 | static void destroyMouseSuiteTestWindow(SDL_Window *window) | ||
| 368 | { | ||
| 369 | if (window) { | ||
| 370 | SDL_DestroyWindow(window); | ||
| 371 | window = NULL; | ||
| 372 | SDLTest_AssertPass("SDL_DestroyWindow()"); | ||
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | /** | ||
| 377 | * Check call to SDL_GetWindowRelativeMouseMode and SDL_SetWindowRelativeMouseMode | ||
| 378 | * | ||
| 379 | * \sa SDL_GetWindowRelativeMouseMode | ||
| 380 | * \sa SDL_SetWindowRelativeMouseMode | ||
| 381 | */ | ||
| 382 | static int SDLCALL mouse_getSetRelativeMouseMode(void *arg) | ||
| 383 | { | ||
| 384 | SDL_Window *window; | ||
| 385 | int result; | ||
| 386 | int i; | ||
| 387 | bool initialState; | ||
| 388 | bool currentState; | ||
| 389 | |||
| 390 | /* Create test window */ | ||
| 391 | window = createMouseSuiteTestWindow(); | ||
| 392 | if (!window) { | ||
| 393 | return TEST_ABORTED; | ||
| 394 | } | ||
| 395 | |||
| 396 | /* Capture original state so we can revert back to it later */ | ||
| 397 | initialState = SDL_GetWindowRelativeMouseMode(window); | ||
| 398 | SDLTest_AssertPass("Call to SDL_GetWindowRelativeMouseMode(window)"); | ||
| 399 | |||
| 400 | /* Repeat twice to check D->D transition */ | ||
| 401 | for (i = 0; i < 2; i++) { | ||
| 402 | /* Disable - should always be supported */ | ||
| 403 | result = SDL_SetWindowRelativeMouseMode(window, false); | ||
| 404 | SDLTest_AssertPass("Call to SDL_SetWindowRelativeMouseMode(window, FALSE)"); | ||
| 405 | SDLTest_AssertCheck(result == true, "Validate result value from SDL_SetWindowRelativeMouseMode, expected: true, got: %i", result); | ||
| 406 | currentState = SDL_GetWindowRelativeMouseMode(window); | ||
| 407 | SDLTest_AssertPass("Call to SDL_GetWindowRelativeMouseMode(window)"); | ||
| 408 | SDLTest_AssertCheck(currentState == false, "Validate current state is FALSE, got: %i", currentState); | ||
| 409 | } | ||
| 410 | |||
| 411 | /* Repeat twice to check D->E->E transition */ | ||
| 412 | for (i = 0; i < 2; i++) { | ||
| 413 | /* Enable - may not be supported */ | ||
| 414 | result = SDL_SetWindowRelativeMouseMode(window, true); | ||
| 415 | SDLTest_AssertPass("Call to SDL_SetWindowRelativeMouseMode(window, TRUE)"); | ||
| 416 | if (result != -1) { | ||
| 417 | SDLTest_AssertCheck(result == true, "Validate result value from SDL_SetWindowRelativeMouseMode, expected: true, got: %i", result); | ||
| 418 | currentState = SDL_GetWindowRelativeMouseMode(window); | ||
| 419 | SDLTest_AssertPass("Call to SDL_GetWindowRelativeMouseMode(window)"); | ||
| 420 | SDLTest_AssertCheck(currentState == true, "Validate current state is TRUE, got: %i", currentState); | ||
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | /* Disable to check E->D transition */ | ||
| 425 | result = SDL_SetWindowRelativeMouseMode(window, false); | ||
| 426 | SDLTest_AssertPass("Call to SDL_SetWindowRelativeMouseMode(window, FALSE)"); | ||
| 427 | SDLTest_AssertCheck(result == true, "Validate result value from SDL_SetWindowRelativeMouseMode, expected: true, got: %i", result); | ||
| 428 | currentState = SDL_GetWindowRelativeMouseMode(window); | ||
| 429 | SDLTest_AssertPass("Call to SDL_GetWindowRelativeMouseMode(window)"); | ||
| 430 | SDLTest_AssertCheck(currentState == false, "Validate current state is FALSE, got: %i", currentState); | ||
| 431 | |||
| 432 | /* Revert to original state - ignore result */ | ||
| 433 | result = SDL_SetWindowRelativeMouseMode(window, initialState); | ||
| 434 | |||
| 435 | /* Clean up test window */ | ||
| 436 | destroyMouseSuiteTestWindow(window); | ||
| 437 | |||
| 438 | return TEST_COMPLETED; | ||
| 439 | } | ||
| 440 | |||
| 441 | /** | ||
| 442 | * Check call to SDL_WarpMouseInWindow | ||
| 443 | * | ||
| 444 | * \sa SDL_WarpMouseInWindow | ||
| 445 | */ | ||
| 446 | static int SDLCALL mouse_warpMouseInWindow(void *arg) | ||
| 447 | { | ||
| 448 | const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT; | ||
| 449 | int numPositions = 6; | ||
| 450 | float xPositions[6]; | ||
| 451 | float yPositions[6]; | ||
| 452 | float x, y; | ||
| 453 | int i, j; | ||
| 454 | SDL_Window *window; | ||
| 455 | |||
| 456 | xPositions[0] = -1; | ||
| 457 | xPositions[1] = 0; | ||
| 458 | xPositions[2] = 1; | ||
| 459 | xPositions[3] = (float)w - 1; | ||
| 460 | xPositions[4] = (float)w; | ||
| 461 | xPositions[5] = (float)w + 1; | ||
| 462 | yPositions[0] = -1; | ||
| 463 | yPositions[1] = 0; | ||
| 464 | yPositions[2] = 1; | ||
| 465 | yPositions[3] = (float)h - 1; | ||
| 466 | yPositions[4] = (float)h; | ||
| 467 | yPositions[5] = (float)h + 1; | ||
| 468 | /* Create test window */ | ||
| 469 | window = createMouseSuiteTestWindow(); | ||
| 470 | if (!window) { | ||
| 471 | return TEST_ABORTED; | ||
| 472 | } | ||
| 473 | |||
| 474 | /* Mouse to random position inside window */ | ||
| 475 | x = (float)SDLTest_RandomIntegerInRange(1, w - 1); | ||
| 476 | y = (float)SDLTest_RandomIntegerInRange(1, h - 1); | ||
| 477 | SDL_WarpMouseInWindow(window, x, y); | ||
| 478 | SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y); | ||
| 479 | |||
| 480 | /* Same position again */ | ||
| 481 | SDL_WarpMouseInWindow(window, x, y); | ||
| 482 | SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y); | ||
| 483 | |||
| 484 | /* Mouse to various boundary positions */ | ||
| 485 | for (i = 0; i < numPositions; i++) { | ||
| 486 | for (j = 0; j < numPositions; j++) { | ||
| 487 | x = xPositions[i]; | ||
| 488 | y = yPositions[j]; | ||
| 489 | SDL_WarpMouseInWindow(window, x, y); | ||
| 490 | SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y); | ||
| 491 | |||
| 492 | /* TODO: add tracking of events and check that each call generates a mouse motion event */ | ||
| 493 | SDL_PumpEvents(); | ||
| 494 | SDLTest_AssertPass("SDL_PumpEvents()"); | ||
| 495 | } | ||
| 496 | } | ||
| 497 | |||
| 498 | /* Clean up test window */ | ||
| 499 | destroyMouseSuiteTestWindow(window); | ||
| 500 | |||
| 501 | return TEST_COMPLETED; | ||
| 502 | } | ||
| 503 | |||
| 504 | /** | ||
| 505 | * Check call to SDL_GetMouseFocus | ||
| 506 | * | ||
| 507 | * \sa SDL_GetMouseFocus | ||
| 508 | */ | ||
| 509 | static int SDLCALL mouse_getMouseFocus(void *arg) | ||
| 510 | { | ||
| 511 | const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT; | ||
| 512 | float x, y; | ||
| 513 | SDL_Window *window; | ||
| 514 | SDL_Window *focusWindow; | ||
| 515 | const char *xdg_session = SDL_getenv("XDG_SESSION_TYPE"); | ||
| 516 | const bool env_is_wayland = !SDL_strcmp(xdg_session ? xdg_session : "", "wayland"); | ||
| 517 | |||
| 518 | /* Get focus - focus non-deterministic */ | ||
| 519 | focusWindow = SDL_GetMouseFocus(); | ||
| 520 | SDLTest_AssertPass("SDL_GetMouseFocus()"); | ||
| 521 | |||
| 522 | /* Create test window */ | ||
| 523 | window = createMouseSuiteTestWindow(); | ||
| 524 | if (!window) { | ||
| 525 | return TEST_ABORTED; | ||
| 526 | } | ||
| 527 | |||
| 528 | /* Delay a brief period to allow the window to actually appear on the desktop. */ | ||
| 529 | SDL_Delay(100); | ||
| 530 | |||
| 531 | /* Warping the pointer when it is outside the window on a Wayland desktop usually doesn't work, so this test will be skipped. */ | ||
| 532 | if (!env_is_wayland) { | ||
| 533 | /* Mouse to random position inside window */ | ||
| 534 | x = (float)SDLTest_RandomIntegerInRange(1, w - 1); | ||
| 535 | y = (float)SDLTest_RandomIntegerInRange(1, h - 1); | ||
| 536 | SDL_WarpMouseInWindow(window, x, y); | ||
| 537 | SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y); | ||
| 538 | |||
| 539 | /* Pump events to update focus state */ | ||
| 540 | SDL_Delay(100); | ||
| 541 | SDL_PumpEvents(); | ||
| 542 | SDLTest_AssertPass("SDL_PumpEvents()"); | ||
| 543 | |||
| 544 | /* Get focus with explicit window setup - focus deterministic */ | ||
| 545 | focusWindow = SDL_GetMouseFocus(); | ||
| 546 | SDLTest_AssertPass("SDL_GetMouseFocus()"); | ||
| 547 | SDLTest_AssertCheck(focusWindow != NULL, "Check returned window value is not NULL"); | ||
| 548 | SDLTest_AssertCheck(focusWindow == window, "Check returned window value is test window"); | ||
| 549 | |||
| 550 | /* Mouse to random position outside window */ | ||
| 551 | x = (float)SDLTest_RandomIntegerInRange(-9, -1); | ||
| 552 | y = (float)SDLTest_RandomIntegerInRange(-9, -1); | ||
| 553 | SDL_WarpMouseInWindow(window, x, y); | ||
| 554 | SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y); | ||
| 555 | } else { | ||
| 556 | SDLTest_Log("Skipping mouse warp focus tests: warping the mouse pointer when outside the window is unreliable on Wayland/XWayland"); | ||
| 557 | } | ||
| 558 | |||
| 559 | /* Clean up test window */ | ||
| 560 | destroyMouseSuiteTestWindow(window); | ||
| 561 | |||
| 562 | /* Pump events to update focus state */ | ||
| 563 | SDL_PumpEvents(); | ||
| 564 | SDLTest_AssertPass("SDL_PumpEvents()"); | ||
| 565 | |||
| 566 | /* Get focus for non-existing window */ | ||
| 567 | focusWindow = SDL_GetMouseFocus(); | ||
| 568 | SDLTest_AssertPass("SDL_GetMouseFocus()"); | ||
| 569 | SDLTest_AssertCheck(focusWindow == NULL, "Check returned window value is NULL"); | ||
| 570 | |||
| 571 | return TEST_COMPLETED; | ||
| 572 | } | ||
| 573 | |||
| 574 | /** | ||
| 575 | * Check call to SDL_GetDefaultCursor | ||
| 576 | * | ||
| 577 | * \sa SDL_GetDefaultCursor | ||
| 578 | */ | ||
| 579 | static int SDLCALL mouse_getDefaultCursor(void *arg) | ||
| 580 | { | ||
| 581 | SDL_Cursor *cursor; | ||
| 582 | |||
| 583 | /* Get current cursor */ | ||
| 584 | cursor = SDL_GetDefaultCursor(); | ||
| 585 | SDLTest_AssertPass("Call to SDL_GetDefaultCursor()"); | ||
| 586 | SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_GetDefaultCursor() is not NULL"); | ||
| 587 | |||
| 588 | return TEST_COMPLETED; | ||
| 589 | } | ||
| 590 | |||
| 591 | /** | ||
| 592 | * Check call to SDL_GetGlobalMouseState | ||
| 593 | * | ||
| 594 | * \sa SDL_GetGlobalMouseState | ||
| 595 | */ | ||
| 596 | static int SDLCALL mouse_getGlobalMouseState(void *arg) | ||
| 597 | { | ||
| 598 | float x; | ||
| 599 | float y; | ||
| 600 | SDL_MouseButtonFlags state; | ||
| 601 | |||
| 602 | x = -FLT_MAX; | ||
| 603 | y = -FLT_MAX; | ||
| 604 | |||
| 605 | /* Get current cursor */ | ||
| 606 | state = SDL_GetGlobalMouseState(&x, &y); | ||
| 607 | SDLTest_AssertPass("Call to SDL_GetGlobalMouseState()"); | ||
| 608 | SDLTest_AssertCheck(x > -FLT_MAX, "Validate that value of x is > -FLT_MAX, got: %.f", x); | ||
| 609 | SDLTest_AssertCheck(y > -FLT_MAX, "Validate that value of y is > -FLT_MAX, got: %.f", y); | ||
| 610 | SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state); | ||
| 611 | |||
| 612 | return TEST_COMPLETED; | ||
| 613 | } | ||
| 614 | |||
| 615 | /* ================= Test References ================== */ | ||
| 616 | |||
| 617 | /* Mouse test cases */ | ||
| 618 | static const SDLTest_TestCaseReference mouseTestGetMouseState = { | ||
| 619 | mouse_getMouseState, "mouse_getMouseState", "Check call to SDL_GetMouseState", TEST_ENABLED | ||
| 620 | }; | ||
| 621 | |||
| 622 | static const SDLTest_TestCaseReference mouseTestGetRelativeMouseState = { | ||
| 623 | mouse_getRelativeMouseState, "mouse_getRelativeMouseState", "Check call to SDL_GetRelativeMouseState", TEST_ENABLED | ||
| 624 | }; | ||
| 625 | |||
| 626 | static const SDLTest_TestCaseReference mouseTestCreateFreeCursor = { | ||
| 627 | mouse_createFreeCursor, "mouse_createFreeCursor", "Check call to SDL_CreateCursor and SDL_DestroyCursor", TEST_ENABLED | ||
| 628 | }; | ||
| 629 | |||
| 630 | static const SDLTest_TestCaseReference mouseTestShowCursor = { | ||
| 631 | mouse_showCursor, "mouse_showCursor", "Check call to SDL_ShowCursor", TEST_ENABLED | ||
| 632 | }; | ||
| 633 | |||
| 634 | static const SDLTest_TestCaseReference mouseTestSetCursor = { | ||
| 635 | mouse_setCursor, "mouse_setCursor", "Check call to SDL_SetCursor", TEST_ENABLED | ||
| 636 | }; | ||
| 637 | |||
| 638 | static const SDLTest_TestCaseReference mouseTestGetCursor = { | ||
| 639 | mouse_getCursor, "mouse_getCursor", "Check call to SDL_GetCursor", TEST_ENABLED | ||
| 640 | }; | ||
| 641 | |||
| 642 | static const SDLTest_TestCaseReference mouseTestWarpMouseInWindow = { | ||
| 643 | mouse_warpMouseInWindow, "mouse_warpMouseInWindow", "Check call to SDL_WarpMouseInWindow", TEST_ENABLED | ||
| 644 | }; | ||
| 645 | |||
| 646 | static const SDLTest_TestCaseReference mouseTestGetMouseFocus = { | ||
| 647 | mouse_getMouseFocus, "mouse_getMouseFocus", "Check call to SDL_GetMouseFocus", TEST_ENABLED | ||
| 648 | }; | ||
| 649 | |||
| 650 | static const SDLTest_TestCaseReference mouseTestCreateFreeColorCursor = { | ||
| 651 | mouse_createFreeColorCursor, "mouse_createFreeColorCursor", "Check call to SDL_CreateColorCursor and SDL_DestroyCursor", TEST_ENABLED | ||
| 652 | }; | ||
| 653 | |||
| 654 | static const SDLTest_TestCaseReference mouseTestGetSetRelativeMouseMode = { | ||
| 655 | mouse_getSetRelativeMouseMode, "mouse_getSetRelativeMouseMode", "Check call to SDL_GetWindowRelativeMouseMode and SDL_SetWindowRelativeMouseMode", TEST_ENABLED | ||
| 656 | }; | ||
| 657 | |||
| 658 | static const SDLTest_TestCaseReference mouseTestGetDefaultCursor = { | ||
| 659 | mouse_getDefaultCursor, "mouse_getDefaultCursor", "Check call to SDL_GetDefaultCursor", TEST_ENABLED | ||
| 660 | }; | ||
| 661 | |||
| 662 | static const SDLTest_TestCaseReference mouseTestGetGlobalMouseState = { | ||
| 663 | mouse_getGlobalMouseState, "mouse_getGlobalMouseState", "Check call to SDL_GetGlobalMouseState", TEST_ENABLED | ||
| 664 | }; | ||
| 665 | |||
| 666 | /* Sequence of Mouse test cases */ | ||
| 667 | static const SDLTest_TestCaseReference *mouseTests[] = { | ||
| 668 | &mouseTestGetMouseState, | ||
| 669 | &mouseTestGetRelativeMouseState, | ||
| 670 | &mouseTestCreateFreeCursor, | ||
| 671 | &mouseTestShowCursor, | ||
| 672 | &mouseTestSetCursor, | ||
| 673 | &mouseTestGetCursor, | ||
| 674 | &mouseTestWarpMouseInWindow, | ||
| 675 | &mouseTestGetMouseFocus, | ||
| 676 | &mouseTestCreateFreeColorCursor, | ||
| 677 | &mouseTestGetSetRelativeMouseMode, | ||
| 678 | &mouseTestGetDefaultCursor, | ||
| 679 | &mouseTestGetGlobalMouseState, | ||
| 680 | NULL | ||
| 681 | }; | ||
| 682 | |||
| 683 | /* Mouse test suite (global) */ | ||
| 684 | SDLTest_TestSuiteReference mouseTestSuite = { | ||
| 685 | "Mouse", | ||
| 686 | NULL, | ||
| 687 | mouseTests, | ||
| 688 | NULL | ||
| 689 | }; | ||
