sdl2-compat: Sync tests with SDL2

From 66455ae3ca94d7b07de9455bf4d54d2c0109b573 Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Tue, 12 Dec 2023 15:14:12 -0500
Subject: [PATCH] Sync tests with SDL2

---
 test/testautomation_video.c | 106 +++++++++++++++++++++++-------------
 1 file changed, 68 insertions(+), 38 deletions(-)

diff --git a/test/testautomation_video.c b/test/testautomation_video.c
index f340edc..c656055 100644
--- a/test/testautomation_video.c
+++ b/test/testautomation_video.c
@@ -1137,10 +1137,17 @@ int video_getSetWindowPosition(void *arg)
 {
     const char *title = "video_getSetWindowPosition Test Window";
     SDL_Window *window;
+    int maxxVariation, maxyVariation;
     int xVariation, yVariation;
     int referenceX, referenceY;
     int currentX, currentY;
     int desiredX, desiredY;
+    SDL_Rect display_bounds;
+
+    if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "wayland") == 0) {
+      SDLTest_Log("Skipping test: wayland does not support window positioning");
+      return TEST_SKIPPED;
+    }
 
     /* Call against new test window */
     window = _createVideoSuiteTestWindow(title);
@@ -1148,46 +1155,69 @@ int video_getSetWindowPosition(void *arg)
         return TEST_ABORTED;
     }
 
-    for (xVariation = 0; xVariation < 4; xVariation++) {
-        for (yVariation = 0; yVariation < 4; yVariation++) {
+    if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "x11") == 0) {
+      /* The X11 server allows arbitrary window placement, but compositing
+         * window managers such as GNOME and KDE force windows to be within
+         * desktop bounds.
+       */
+      maxxVariation = 2;
+      maxyVariation = 2;
+
+      SDL_GetDisplayUsableBounds(SDL_GetWindowDisplayIndex(window), &display_bounds);
+    } else if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "cocoa") == 0) {
+      /* Platform doesn't allow windows with negative Y desktop bounds */
+      maxxVariation = 4;
+      maxyVariation = 3;
+
+      SDL_GetDisplayUsableBounds(SDL_GetWindowDisplayIndex(window), &display_bounds);
+    } else {
+      /* Platform allows windows to be placed out of bounds */
+      maxxVariation = 4;
+      maxyVariation = 4;
+
+      SDL_GetDisplayBounds(SDL_GetWindowDisplayIndex(window), &display_bounds);
+    }
+
+    for (xVariation = 0; xVariation < maxxVariation; xVariation++) {
+        for (yVariation = 0; yVariation < maxyVariation; yVariation++) {
             switch (xVariation) {
             default:
             case 0:
-                /* Zero X Position */
-                desiredX = 0;
-                break;
+              /* Zero X Position */
+              desiredX = display_bounds.x > 0 ? display_bounds.x : 0;
+              break;
             case 1:
-                /* Random X position inside screen */
-                desiredX = SDLTest_RandomIntegerInRange(1, 100);
-                break;
+              /* Random X position inside screen */
+              desiredX = SDLTest_RandomIntegerInRange(display_bounds.x + 1, display_bounds.x + 100);
+              break;
             case 2:
-                /* Random X position outside screen (positive) */
-                desiredX = SDLTest_RandomIntegerInRange(10000, 11000);
-                break;
+              /* Random X position outside screen (positive) */
+              desiredX = SDLTest_RandomIntegerInRange(10000, 11000);
+              break;
             case 3:
-                /* Random X position outside screen (negative) */
-                desiredX = SDLTest_RandomIntegerInRange(-1000, -100);
-                break;
+              /* Random X position outside screen (negative) */
+              desiredX = SDLTest_RandomIntegerInRange(-1000, -100);
+              break;
             }
 
             switch (yVariation) {
             default:
             case 0:
-                /* Zero X Position */
-                desiredY = 0;
-                break;
+              /* Zero Y Position */
+              desiredY = display_bounds.y > 0 ? display_bounds.y : 0;
+              break;
             case 1:
-                /* Random X position inside screen */
-                desiredY = SDLTest_RandomIntegerInRange(1, 100);
-                break;
+              /* Random Y position inside screen */
+              desiredY = SDLTest_RandomIntegerInRange(display_bounds.y + 1, display_bounds.y + 100);
+              break;
             case 2:
-                /* Random X position outside screen (positive) */
-                desiredY = SDLTest_RandomIntegerInRange(10000, 11000);
-                break;
+              /* Random Y position outside screen (positive) */
+              desiredY = SDLTest_RandomIntegerInRange(10000, 11000);
+              break;
             case 3:
-                /* Random Y position outside screen (negative) */
-                desiredY = SDLTest_RandomIntegerInRange(-1000, -100);
-                break;
+              /* Random Y position outside screen (negative) */
+              desiredY = SDLTest_RandomIntegerInRange(-1000, -100);
+              break;
             }
 
             /* Set position */
@@ -1307,7 +1337,7 @@ int video_getSetWindowSize(void *arg)
     int desiredW, desiredH;
 
     /* Get display bounds for size range */
-    result = SDL_GetDisplayBounds(0, &display);
+    result = SDL_GetDisplayUsableBounds(0, &display);
     SDLTest_AssertPass("SDL_GetDisplayBounds()");
     SDLTest_AssertCheck(result == 0, "Verify return value; expected: 0, got: %d", result);
     if (result != 0) {
@@ -1320,15 +1350,16 @@ int video_getSetWindowSize(void *arg)
         return TEST_ABORTED;
     }
 
-#ifdef __WIN32__
-    /* Platform clips window size to screen size */
-    maxwVariation = 4;
-    maxhVariation = 4;
-#else
-    /* Platform allows window size >= screen size */
-    maxwVariation = 5;
-    maxhVariation = 5;
-#endif
+    if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "windows") == 0 ||
+        SDL_strcmp(SDL_GetCurrentVideoDriver(), "x11") == 0) {
+      /* Platform clips window size to screen size */
+      maxwVariation = 4;
+      maxhVariation = 4;
+    } else {
+      /* Platform allows window size >= screen size */
+      maxwVariation = 5;
+      maxhVariation = 5;
+    }
 
     for (wVariation = 0; wVariation < maxwVariation; wVariation++) {
         for (hVariation = 0; hVariation < maxhVariation; hVariation++) {
@@ -2023,7 +2054,7 @@ int video_setWindowCenteredOnDisplay(void *arg)
             expectedX = (expectedDisplayRect.x + ((expectedDisplayRect.w - w) / 2));
             expectedY = (expectedDisplayRect.y + ((expectedDisplayRect.h - h) / 2));
 
-            window = SDL_CreateWindow(title, x, y, w, h, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
+            window = SDL_CreateWindow(title, x, y, w, h, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_BORDERLESS);
             SDLTest_AssertPass("Call to SDL_CreateWindow('Title',%d,%d,%d,%d,SHOWN)", x, y, w, h);
             SDLTest_AssertCheck(window != NULL, "Validate that returned window struct is not NULL");
 
@@ -2089,7 +2120,6 @@ int video_setWindowCenteredOnDisplay(void *arg)
 
             /* Leave fullscreen desktop */
             result = SDL_SetWindowFullscreen(window, 0);
-            SDL_PumpEvents();
             SDLTest_AssertCheck(result == 0, "Verify return value; expected: 0, got: %d", result);
 
             /* Check window was restored correctly */