summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testautomation_joystick.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
committer3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
commit6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch)
tree34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/test/testautomation_joystick.c
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testautomation_joystick.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testautomation_joystick.c204
1 files changed, 204 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testautomation_joystick.c b/src/contrib/SDL-3.2.20/test/testautomation_joystick.c
new file mode 100644
index 0000000..274ba79
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testautomation_joystick.c
@@ -0,0 +1,204 @@
1/**
2 * Joystick test suite
3 */
4
5#include <SDL3/SDL.h>
6#include <SDL3/SDL_test.h>
7#include "../src/joystick/usb_ids.h"
8#include "testautomation_suites.h"
9
10/* ================= Test Case Implementation ================== */
11
12/* Test case functions */
13
14/**
15 * Check virtual joystick creation
16 *
17 * \sa SDL_AttachVirtualJoystick
18 */
19static int SDLCALL TestVirtualJoystick(void *arg)
20{
21 SDL_VirtualJoystickDesc desc;
22 SDL_Joystick *joystick = NULL;
23 SDL_Gamepad *gamepad = NULL;
24 SDL_JoystickID device_id;
25
26 SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMEPAD), "SDL_InitSubSystem(SDL_INIT_GAMEPAD)");
27
28 SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
29
30 SDL_INIT_INTERFACE(&desc);
31 desc.type = SDL_JOYSTICK_TYPE_GAMEPAD;
32 desc.naxes = SDL_GAMEPAD_AXIS_COUNT;
33 desc.nbuttons = SDL_GAMEPAD_BUTTON_COUNT;
34 desc.vendor_id = USB_VENDOR_NVIDIA;
35 desc.product_id = USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V104;
36 desc.name = "Virtual NVIDIA SHIELD Controller";
37 device_id = SDL_AttachVirtualJoystick(&desc);
38 SDLTest_AssertCheck(device_id > 0, "SDL_AttachVirtualJoystick() -> %" SDL_PRIs32 " (expected > 0)", device_id);
39 SDLTest_AssertCheck(SDL_IsJoystickVirtual(device_id), "SDL_IsJoystickVirtual()");
40 if (device_id > 0) {
41 joystick = SDL_OpenJoystick(device_id);
42 SDLTest_AssertCheck(joystick != NULL, "SDL_OpenJoystick()");
43 if (joystick) {
44 {
45 const char *dname = SDL_GetJoystickName(joystick);
46 SDLTest_AssertCheck(SDL_strcmp(dname, desc.name) == 0, "SDL_GetJoystickName() -> \"%s\" (expected \"%s\")", dname, desc.name);
47 }
48 {
49 Uint16 vendor_id = SDL_GetJoystickVendor(joystick);
50 SDLTest_AssertCheck(vendor_id == desc.vendor_id, "SDL_GetJoystickVendor() -> 0x%04x (expected 0x%04x)", vendor_id, desc.vendor_id);
51 }
52 {
53 Uint16 product_id = SDL_GetJoystickProduct(joystick);
54 SDLTest_AssertCheck(product_id == desc.product_id, "SDL_GetJoystickProduct() -> 0x%04x (expected 0x%04x)", product_id, desc.product_id);
55 }
56 {
57 Uint16 product_version = SDL_GetJoystickProductVersion(joystick);
58 SDLTest_AssertCheck(product_version == 0, "SDL_GetJoystickProductVersion() -> 0x%04x (expected 0x%04x)", product_version, 0);
59 }
60 {
61 Uint16 firmware_Version = SDL_GetJoystickFirmwareVersion(joystick);
62 SDLTest_AssertCheck(firmware_Version == 0, "SDL_GetJoystickFirmwareVersion() -> 0x%04x (expected 0x%04x)", firmware_Version, 0);
63 }
64 {
65 const char *serial = SDL_GetJoystickSerial(joystick);
66 SDLTest_AssertCheck(serial == NULL, "SDL_GetJoystickSerial() -> %s (expected %s)", serial, "(null)");
67 }
68 {
69 SDL_JoystickType type = SDL_GetJoystickType(joystick);
70 SDLTest_AssertCheck(type == desc.type, "SDL_GetJoystickType() -> %d (expected %d)", type, desc.type);
71 }
72 {
73 Uint16 naxes = SDL_GetNumJoystickAxes(joystick);
74 SDLTest_AssertCheck(naxes == desc.naxes, "SDL_GetNumJoystickAxes() -> 0x%04x (expected 0x%04x)", naxes, desc.naxes);
75 }
76 {
77 int nballs = SDL_GetNumJoystickBalls(joystick);
78 SDLTest_AssertCheck(nballs == 0, "SDL_GetNumJoystickBalls() -> %d (expected %d)", nballs, 0);
79 }
80 {
81 int nhats = SDL_GetNumJoystickHats(joystick);
82 SDLTest_AssertCheck(nhats == desc.nhats, "SDL_GetNumJoystickHats() -> %d (expected %d)", nhats, desc.nhats);
83 }
84 {
85 int nbuttons = SDL_GetNumJoystickButtons(joystick);
86 SDLTest_AssertCheck(nbuttons == desc.nbuttons, "SDL_GetNumJoystickButtons() -> %d (expected %d)", nbuttons, desc.nbuttons);
87 }
88
89 SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, true), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, true)");
90 SDL_UpdateJoysticks();
91 SDLTest_AssertCheck(SDL_GetJoystickButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH) == true, "SDL_GetJoystickButton(SDL_GAMEPAD_BUTTON_SOUTH) == true");
92
93 SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, false), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, false)");
94 SDL_UpdateJoysticks();
95 SDLTest_AssertCheck(SDL_GetJoystickButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH) == false, "SDL_GetJoystickButton(SDL_GAMEPAD_BUTTON_SOUTH) == false");
96
97 gamepad = SDL_OpenGamepad(SDL_GetJoystickID(joystick));
98 SDLTest_AssertCheck(gamepad != NULL, "SDL_OpenGamepad() succeeded");
99 if (gamepad) {
100 {
101 const char *name = SDL_GetGamepadName(gamepad);
102 SDLTest_AssertCheck(name && SDL_strcmp(name, desc.name) == 0, "SDL_GetGamepadName() -> \"%s\" (expected \"%s\")", name, desc.name);
103 }
104 {
105 Uint16 vendor_id = SDL_GetGamepadVendor(gamepad);
106 SDLTest_AssertCheck(vendor_id == desc.vendor_id, "SDL_GetGamepadVendor() -> 0x%04x (expected 0x%04x)", vendor_id, desc.vendor_id);
107 }
108 {
109 Uint16 product_id = SDL_GetGamepadProduct(gamepad);
110 SDLTest_AssertCheck(product_id == desc.product_id, "SDL_GetGamepadProduct() -> 0x%04x (expected 0x%04x)", product_id, desc.product_id);
111 }
112
113 /* Set an explicit mapping with a different name */
114 SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff0013db5669727475616c2043007601,Virtual Gamepad,a:b0,b:b1,x:b2,y:b3,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,");
115 {
116 const char *name = SDL_GetGamepadName(gamepad);
117 SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual Gamepad") == 0, "SDL_GetGamepadName() ->\"%s\" (expected \"%s\")", name, "Virtual Gamepad");
118 }
119 {
120 SDL_GamepadButtonLabel label = SDL_GetGamepadButtonLabel(gamepad, SDL_GAMEPAD_BUTTON_SOUTH);
121 SDLTest_AssertCheck(label == SDL_GAMEPAD_BUTTON_LABEL_A, "SDL_GetGamepadButtonLabel(SDL_GAMEPAD_BUTTON_SOUTH) -> %d (expected %d [%s])",
122 label, SDL_GAMEPAD_BUTTON_LABEL_A, "SDL_GAMEPAD_BUTTON_LABEL_A");
123 }
124 SDLTest_AssertCheck(SDL_GetGamepadButtonLabel(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == SDL_GAMEPAD_BUTTON_LABEL_A, "SDL_GetGamepadButtonLabel(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_GAMEPAD_BUTTON_LABEL_A");
125
126 /* Set the south button and verify that the gamepad responds */
127 SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, true), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, true)");
128 SDL_UpdateJoysticks();
129 SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == true, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == true");
130
131 SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, false), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, false)");
132 SDL_UpdateJoysticks();
133 SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == false, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == false");
134
135 /* Set an explicit mapping with legacy Nintendo style buttons */
136 SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff0013db5669727475616c2043007601,Virtual Nintendo Gamepad,a:b1,b:b0,x:b3,y:b2,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,hint:SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1,");
137 {
138 const char *name = SDL_GetGamepadName(gamepad);
139 SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual Nintendo Gamepad") == 0, "SDL_GetGamepadName() -> \"%s\" (expected \"%s\")", name, "Virtual Nintendo Gamepad");
140 }
141 SDLTest_AssertCheck(SDL_GetGamepadButtonLabel(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == SDL_GAMEPAD_BUTTON_LABEL_B, "SDL_GetGamepadButtonLabel(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_GAMEPAD_BUTTON_LABEL_B");
142
143 /* Set the south button and verify that the gamepad responds */
144 SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, true), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, true)");
145 SDL_UpdateJoysticks();
146 SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == true, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == true");
147
148 SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, false), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, false)");
149 SDL_UpdateJoysticks();
150 SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == false, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == false");
151
152 /* Set an explicit mapping with PS4 style buttons */
153 SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff0013db5669727475616c2043007601,Virtual PS4 Gamepad,type:ps4,a:b0,b:b1,x:b2,y:b3,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,");
154 {
155 const char *name = SDL_GetGamepadName(gamepad);
156 SDLTest_AssertCheck(SDL_strcmp(name, "Virtual PS4 Gamepad") == 0, "SDL_GetGamepadName() -> \"%s\" (expected \"%s\")", name, "Virtual PS4 Gamepad");
157 }
158 SDLTest_AssertCheck(SDL_GetGamepadButtonLabel(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == SDL_GAMEPAD_BUTTON_LABEL_CROSS, "SDL_GetGamepadButtonLabel(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_GAMEPAD_BUTTON_LABEL_CROSS");
159
160 /* Set the south button and verify that the gamepad responds */
161 SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, true), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, true)");
162 SDL_UpdateJoysticks();
163 SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == true, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == true");
164
165 SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, false), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, false)");
166 SDL_UpdateJoysticks();
167 SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == false, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == false");
168
169 SDL_CloseGamepad(gamepad);
170 }
171
172 SDL_CloseJoystick(joystick);
173 }
174 SDLTest_AssertCheck(SDL_DetachVirtualJoystick(device_id), "SDL_DetachVirtualJoystick()");
175 }
176 SDLTest_AssertCheck(!SDL_IsJoystickVirtual(device_id), "!SDL_IsJoystickVirtual()");
177
178 SDL_ResetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS);
179
180 SDL_QuitSubSystem(SDL_INIT_GAMEPAD);
181
182 return TEST_COMPLETED;
183}
184
185/* ================= Test References ================== */
186
187/* Joystick routine test cases */
188static const SDLTest_TestCaseReference joystickTest1 = {
189 TestVirtualJoystick, "TestVirtualJoystick", "Test virtual joystick functionality", TEST_ENABLED
190};
191
192/* Sequence of Joystick routine test cases */
193static const SDLTest_TestCaseReference *joystickTests[] = {
194 &joystickTest1,
195 NULL
196};
197
198/* Joystick routine test suite (global) */
199SDLTest_TestSuiteReference joystickTestSuite = {
200 "Joystick",
201 NULL,
202 joystickTests,
203 NULL
204};