SDL: Added a virtual joystick automated test

From 7e2a99695858e5c6c744ae3075513958ee6eebf6 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 13 Jul 2022 08:57:40 -0700
Subject: [PATCH] Added a virtual joystick automated test

Useful to verify https://github.com/libsdl-org/SDL/commit/4fa2653394150140c4d69cf66a78cd83e1175f99 on a big-endian system
---
 .../testautomation/testautomation.vcxproj     |  3 +-
 test/Makefile.in                              |  7 +-
 test/testautomation_joystick.c                | 90 +++++++++++++++++++
 test/testautomation_suites.h                  | 10 ++-
 test/watcom.mif                               | 22 ++---
 5 files changed, 114 insertions(+), 18 deletions(-)
 create mode 100644 test/testautomation_joystick.c

diff --git a/VisualC/tests/testautomation/testautomation.vcxproj b/VisualC/tests/testautomation/testautomation.vcxproj
index 0acc54af996..d58dc0b3264 100644
--- a/VisualC/tests/testautomation/testautomation.vcxproj
+++ b/VisualC/tests/testautomation/testautomation.vcxproj
@@ -208,6 +208,7 @@
     <ClCompile Include="..\..\..\test\testautomation_events.c" />
     <ClCompile Include="..\..\..\test\testautomation_guid.c" />
     <ClCompile Include="..\..\..\test\testautomation_hints.c" />
+    <ClCompile Include="..\..\..\test\testautomation_joystick.c" />
     <ClCompile Include="..\..\..\test\testautomation_keyboard.c" />
     <ClCompile Include="..\..\..\test\testautomation_main.c" />
     <ClCompile Include="..\..\..\test\testautomation_math.c" />
@@ -230,4 +231,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
\ No newline at end of file
+</Project>
diff --git a/test/Makefile.in b/test/Makefile.in
index 435e50fd4de..93df6360e84 100644
--- a/test/Makefile.in
+++ b/test/Makefile.in
@@ -138,8 +138,11 @@ testautomation$(EXE): $(srcdir)/testautomation.c \
 		      $(srcdir)/testautomation_clipboard.c \
 		      $(srcdir)/testautomation_events.c \
 		      $(srcdir)/testautomation_guid.c \
+		      $(srcdir)/testautomation_hints.c \
+		      $(srcdir)/testautomation_joystick.c \
 		      $(srcdir)/testautomation_keyboard.c \
 		      $(srcdir)/testautomation_main.c \
+		      $(srcdir)/testautomation_math.c \
 		      $(srcdir)/testautomation_mouse.c \
 		      $(srcdir)/testautomation_pixels.c \
 		      $(srcdir)/testautomation_platform.c \
@@ -151,9 +154,7 @@ testautomation$(EXE): $(srcdir)/testautomation.c \
 		      $(srcdir)/testautomation_surface.c \
 		      $(srcdir)/testautomation_syswm.c \
 		      $(srcdir)/testautomation_timer.c \
-		      $(srcdir)/testautomation_video.c \
-		      $(srcdir)/testautomation_hints.c \
-		      $(srcdir)/testautomation_math.c
+		      $(srcdir)/testautomation_video.c
 	$(CC) -o $@ $^ $(CFLAGS) $(LIBS) 
 
 testmultiaudio$(EXE): $(srcdir)/testmultiaudio.c $(srcdir)/testutils.c
diff --git a/test/testautomation_joystick.c b/test/testautomation_joystick.c
new file mode 100644
index 00000000000..198128e9cac
--- /dev/null
+++ b/test/testautomation_joystick.c
@@ -0,0 +1,90 @@
+/**
+ * Joystick test suite
+ */
+
+#include "SDL.h"
+#include "SDL_test.h"
+#include "../src/joystick/usb_ids.h"
+
+/* ================= Test Case Implementation ================== */
+
+/* Test case functions */
+
+/**
+ * @brief Check virtual joystick creation
+ *
+ * @sa SDL_JoystickAttachVirtualEx
+ */
+static int
+TestVirtualJoystick(void *arg)
+{
+    SDL_VirtualJoystickDesc desc;
+    SDL_Joystick *joystick = NULL;
+    int device_index;
+
+    SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0, "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)");
+
+    SDL_zero(desc);
+    desc.version = SDL_VIRTUAL_JOYSTICK_DESC_VERSION;
+    desc.type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
+    desc.naxes = SDL_CONTROLLER_AXIS_MAX;
+    desc.nbuttons = SDL_CONTROLLER_BUTTON_MAX;
+    desc.vendor_id = USB_VENDOR_NVIDIA;
+    desc.product_id = USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER;
+    desc.name = "Virtual NVIDIA SHIELD Controller";
+    device_index = SDL_JoystickAttachVirtualEx(&desc);
+    SDLTest_AssertCheck(device_index >= 0, "SDL_JoystickAttachVirtualEx()");
+    SDLTest_AssertCheck(SDL_JoystickIsVirtual(device_index), "SDL_JoystickIsVirtual()");
+    if (device_index >= 0) {
+        joystick = SDL_JoystickOpen(device_index);
+        SDLTest_AssertCheck(joystick != NULL, "SDL_JoystickOpen()");
+        if (joystick) {
+            SDLTest_AssertCheck(SDL_strcmp(SDL_JoystickName(joystick), desc.name) == 0, "SDL_JoystickName()");
+            SDLTest_AssertCheck(SDL_JoystickGetVendor(joystick) == desc.vendor_id, "SDL_JoystickGetVendor()");
+            SDLTest_AssertCheck(SDL_JoystickGetProduct(joystick) == desc.product_id, "SDL_JoystickGetProduct()");
+            SDLTest_AssertCheck(SDL_JoystickGetProductVersion(joystick) == 0, "SDL_JoystickGetProductVersion()");
+            SDLTest_AssertCheck(SDL_JoystickGetFirmwareVersion(joystick) == 0, "SDL_JoystickGetFirmwareVersion()");
+            SDLTest_AssertCheck(SDL_JoystickGetSerial(joystick) == NULL, "SDL_JoystickGetSerial()");
+            SDLTest_AssertCheck(SDL_JoystickGetType(joystick) == desc.type, "SDL_JoystickGetType()");
+            SDLTest_AssertCheck(SDL_JoystickNumAxes(joystick) == desc.naxes, "SDL_JoystickNumAxes()");
+            SDLTest_AssertCheck(SDL_JoystickNumBalls(joystick) == 0, "SDL_JoystickNumBalls()");
+            SDLTest_AssertCheck(SDL_JoystickNumHats(joystick) == desc.nhats, "SDL_JoystickNumHats()");
+            SDLTest_AssertCheck(SDL_JoystickNumButtons(joystick) == desc.nbuttons, "SDL_JoystickNumButtons()");
+
+            SDLTest_AssertCheck(SDL_JoystickSetVirtualButton(joystick, SDL_CONTROLLER_BUTTON_A, SDL_PRESSED) == 0, "SDL_JoystickSetVirtualButton(SDL_CONTROLLER_BUTTON_A, SDL_PRESSED)");
+            SDL_JoystickUpdate();
+            SDLTest_AssertCheck(SDL_JoystickGetButton(joystick, SDL_CONTROLLER_BUTTON_A) == SDL_PRESSED, "SDL_JoystickGetButton(SDL_CONTROLLER_BUTTON_A) == SDL_PRESSED");
+            SDLTest_AssertCheck(SDL_JoystickSetVirtualButton(joystick, SDL_CONTROLLER_BUTTON_A, SDL_RELEASED) == 0, "SDL_JoystickSetVirtualButton(SDL_CONTROLLER_BUTTON_A, SDL_RELEASED)");
+            SDL_JoystickUpdate();
+            SDLTest_AssertCheck(SDL_JoystickGetButton(joystick, SDL_CONTROLLER_BUTTON_A) == SDL_RELEASED, "SDL_JoystickGetButton(SDL_CONTROLLER_BUTTON_A) == SDL_RELEASED");
+
+            SDL_JoystickClose(joystick);
+        }
+        SDLTest_AssertCheck(SDL_JoystickDetachVirtual(device_index) == 0, "SDL_JoystickDetachVirtual()");
+    }
+    SDLTest_AssertCheck(!SDL_JoystickIsVirtual(device_index), "!SDL_JoystickIsVirtual()");
+
+    SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
+
+    return TEST_COMPLETED;
+}
+
+/* ================= Test References ================== */
+
+/* Joystick routine test cases */
+static const SDLTest_TestCaseReference joystickTest1 =
+        { (SDLTest_TestCaseFp)TestVirtualJoystick, "TestVirtualJoystick", "Test virtual joystick functionality", TEST_ENABLED };
+
+/* Sequence of Joystick routine test cases */
+static const SDLTest_TestCaseReference *joystickTests[] =  {
+    &joystickTest1,
+    NULL
+};
+
+/* Joystick routine test suite (global) */
+SDLTest_TestSuiteReference joystickTestSuite = {
+    "Joystick",
+    NULL,
+    joystickTests,
+    NULL
+};
diff --git a/test/testautomation_suites.h b/test/testautomation_suites.h
index a03cdbcbc18..7fcce1b1ce4 100644
--- a/test/testautomation_suites.h
+++ b/test/testautomation_suites.h
@@ -13,8 +13,11 @@ extern SDLTest_TestSuiteReference audioTestSuite;
 extern SDLTest_TestSuiteReference clipboardTestSuite;
 extern SDLTest_TestSuiteReference eventsTestSuite;
 extern SDLTest_TestSuiteReference guidTestSuite;
+extern SDLTest_TestSuiteReference hintsTestSuite;
+extern SDLTest_TestSuiteReference joystickTestSuite;
 extern SDLTest_TestSuiteReference keyboardTestSuite;
 extern SDLTest_TestSuiteReference mainTestSuite;
+extern SDLTest_TestSuiteReference mathTestSuite;
 extern SDLTest_TestSuiteReference mouseTestSuite;
 extern SDLTest_TestSuiteReference pixelsTestSuite;
 extern SDLTest_TestSuiteReference platformTestSuite;
@@ -27,8 +30,6 @@ extern SDLTest_TestSuiteReference surfaceTestSuite;
 extern SDLTest_TestSuiteReference syswmTestSuite;
 extern SDLTest_TestSuiteReference timerTestSuite;
 extern SDLTest_TestSuiteReference videoTestSuite;
-extern SDLTest_TestSuiteReference hintsTestSuite;
-extern SDLTest_TestSuiteReference mathTestSuite;
 
 /* All test suites */
 SDLTest_TestSuiteReference *testSuites[] =  {
@@ -36,8 +37,11 @@ SDLTest_TestSuiteReference *testSuites[] =  {
     &clipboardTestSuite,
     &eventsTestSuite,
     &guidTestSuite,
+    &hintsTestSuite,
+    &joystickTestSuite,
     &keyboardTestSuite,
     &mainTestSuite,
+    &mathTestSuite,
     &mouseTestSuite,
     &pixelsTestSuite,
     &platformTestSuite,
@@ -50,8 +54,6 @@ SDLTest_TestSuiteReference *testSuites[] =  {
     &syswmTestSuite,
     &timerTestSuite,
     &videoTestSuite,
-    &hintsTestSuite,
-    &mathTestSuite,
     NULL
 };
 
diff --git a/test/watcom.mif b/test/watcom.mif
index 91036f9a347..6ddab0e23b9 100644
--- a/test/watcom.mif
+++ b/test/watcom.mif
@@ -49,16 +49,18 @@ needs_display = &
 TESTS = $(noninteractive) $(needs_audio) $(needs_display)
 
 # testautomation sources
-TASRCS = testautomation.c testautomation_audio.c testautomation_clipboard.c &
-         testautomation_events.c testautomation_hints.c &
-         testautomation_keyboard.c testautomation_main.c &
-         testautomation_mouse.c testautomation_pixels.c &
-         testautomation_platform.c testautomation_rect.c &
-         testautomation_render.c testautomation_rwops.c &
-         testautomation_sdltest.c testautomation_stdlib.c &
-         testautomation_surface.c testautomation_syswm.c &
-         testautomation_timer.c testautomation_video.c &
-         testautomation_math.c testautomation_guid.c
+TASRCS = testautomation.c &
+	testautomation_audio.c testautomation_clipboard.c &
+	testautomation_events.c testautomation_guid.c &
+	testautomation_hints.c testautomation_joystick.c &
+	testautomation_keyboard.c testautomation_main.c &
+	testautomation_math.c testautomation_mouse.c &
+	testautomation_pixels.c testautomation_platform.c &
+	testautomation_rect.c testautomation_render.c &
+	testautomation_rwops.c testautomation_sdltest.c &
+	testautomation_stdlib.c testautomation_surface.c &
+	testautomation_syswm.c testautomation_timer.c &
+	testautomation_video.c
 
 OBJS = $(TARGETS:.exe=.obj)
 COBJS = $(CSRCS:.c=.obj)