testautomation_joystick.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * Joystick test suite
  3. */
  4. #include <SDL3/SDL.h>
  5. #include <SDL3/SDL_test.h>
  6. #include "../src/joystick/usb_ids.h"
  7. #include "testautomation_suites.h"
  8. /* ================= Test Case Implementation ================== */
  9. /* Test case functions */
  10. /**
  11. * Check virtual joystick creation
  12. *
  13. * \sa SDL_AttachVirtualJoystick
  14. */
  15. static int SDLCALL TestVirtualJoystick(void *arg)
  16. {
  17. SDL_VirtualJoystickDesc desc;
  18. SDL_Joystick *joystick = NULL;
  19. SDL_Gamepad *gamepad = NULL;
  20. SDL_JoystickID device_id;
  21. SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMEPAD), "SDL_InitSubSystem(SDL_INIT_GAMEPAD)");
  22. SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
  23. SDL_INIT_INTERFACE(&desc);
  24. desc.type = SDL_JOYSTICK_TYPE_GAMEPAD;
  25. desc.naxes = SDL_GAMEPAD_AXIS_COUNT;
  26. desc.nbuttons = SDL_GAMEPAD_BUTTON_COUNT;
  27. desc.vendor_id = USB_VENDOR_NVIDIA;
  28. desc.product_id = USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V104;
  29. desc.name = "Virtual NVIDIA SHIELD Controller";
  30. device_id = SDL_AttachVirtualJoystick(&desc);
  31. SDLTest_AssertCheck(device_id > 0, "SDL_AttachVirtualJoystick() -> %" SDL_PRIs32 " (expected > 0)", device_id);
  32. SDLTest_AssertCheck(SDL_IsJoystickVirtual(device_id), "SDL_IsJoystickVirtual()");
  33. if (device_id > 0) {
  34. joystick = SDL_OpenJoystick(device_id);
  35. SDLTest_AssertCheck(joystick != NULL, "SDL_OpenJoystick()");
  36. if (joystick) {
  37. {
  38. const char *dname = SDL_GetJoystickName(joystick);
  39. SDLTest_AssertCheck(SDL_strcmp(dname, desc.name) == 0, "SDL_GetJoystickName() -> \"%s\" (expected \"%s\")", dname, desc.name);
  40. }
  41. {
  42. Uint16 vendor_id = SDL_GetJoystickVendor(joystick);
  43. SDLTest_AssertCheck(vendor_id == desc.vendor_id, "SDL_GetJoystickVendor() -> 0x%04x (expected 0x%04x)", vendor_id, desc.vendor_id);
  44. }
  45. {
  46. Uint16 product_id = SDL_GetJoystickProduct(joystick);
  47. SDLTest_AssertCheck(product_id == desc.product_id, "SDL_GetJoystickProduct() -> 0x%04x (expected 0x%04x)", product_id, desc.product_id);
  48. }
  49. {
  50. Uint16 product_version = SDL_GetJoystickProductVersion(joystick);
  51. SDLTest_AssertCheck(product_version == 0, "SDL_GetJoystickProductVersion() -> 0x%04x (expected 0x%04x)", product_version, 0);
  52. }
  53. {
  54. Uint16 firmware_Version = SDL_GetJoystickFirmwareVersion(joystick);
  55. SDLTest_AssertCheck(firmware_Version == 0, "SDL_GetJoystickFirmwareVersion() -> 0x%04x (expected 0x%04x)", firmware_Version, 0);
  56. }
  57. {
  58. const char *serial = SDL_GetJoystickSerial(joystick);
  59. SDLTest_AssertCheck(serial == NULL, "SDL_GetJoystickSerial() -> %s (expected %s)", serial, "(null)");
  60. }
  61. {
  62. SDL_JoystickType type = SDL_GetJoystickType(joystick);
  63. SDLTest_AssertCheck(type == desc.type, "SDL_GetJoystickType() -> %d (expected %d)", type, desc.type);
  64. }
  65. {
  66. Uint16 naxes = SDL_GetNumJoystickAxes(joystick);
  67. SDLTest_AssertCheck(naxes == desc.naxes, "SDL_GetNumJoystickAxes() -> 0x%04x (expected 0x%04x)", naxes, desc.naxes);
  68. }
  69. {
  70. int nballs = SDL_GetNumJoystickBalls(joystick);
  71. SDLTest_AssertCheck(nballs == 0, "SDL_GetNumJoystickBalls() -> %d (expected %d)", nballs, 0);
  72. }
  73. {
  74. int nhats = SDL_GetNumJoystickHats(joystick);
  75. SDLTest_AssertCheck(nhats == desc.nhats, "SDL_GetNumJoystickHats() -> %d (expected %d)", nhats, desc.nhats);
  76. }
  77. {
  78. int nbuttons = SDL_GetNumJoystickButtons(joystick);
  79. SDLTest_AssertCheck(nbuttons == desc.nbuttons, "SDL_GetNumJoystickButtons() -> %d (expected %d)", nbuttons, desc.nbuttons);
  80. }
  81. SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, true), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, true)");
  82. SDL_UpdateJoysticks();
  83. SDLTest_AssertCheck(SDL_GetJoystickButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH) == true, "SDL_GetJoystickButton(SDL_GAMEPAD_BUTTON_SOUTH) == true");
  84. SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, false), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, false)");
  85. SDL_UpdateJoysticks();
  86. SDLTest_AssertCheck(SDL_GetJoystickButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH) == false, "SDL_GetJoystickButton(SDL_GAMEPAD_BUTTON_SOUTH) == false");
  87. gamepad = SDL_OpenGamepad(SDL_GetJoystickID(joystick));
  88. SDLTest_AssertCheck(gamepad != NULL, "SDL_OpenGamepad() succeeded");
  89. if (gamepad) {
  90. {
  91. const char *name = SDL_GetGamepadName(gamepad);
  92. SDLTest_AssertCheck(name && SDL_strcmp(name, desc.name) == 0, "SDL_GetGamepadName() -> \"%s\" (expected \"%s\")", name, desc.name);
  93. }
  94. {
  95. Uint16 vendor_id = SDL_GetGamepadVendor(gamepad);
  96. SDLTest_AssertCheck(vendor_id == desc.vendor_id, "SDL_GetGamepadVendor() -> 0x%04x (expected 0x%04x)", vendor_id, desc.vendor_id);
  97. }
  98. {
  99. Uint16 product_id = SDL_GetGamepadProduct(gamepad);
  100. SDLTest_AssertCheck(product_id == desc.product_id, "SDL_GetGamepadProduct() -> 0x%04x (expected 0x%04x)", product_id, desc.product_id);
  101. }
  102. /* Set an explicit mapping with a different name */
  103. 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,");
  104. {
  105. const char *name = SDL_GetGamepadName(gamepad);
  106. SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual Gamepad") == 0, "SDL_GetGamepadName() ->\"%s\" (expected \"%s\")", name, "Virtual Gamepad");
  107. }
  108. {
  109. SDL_GamepadButtonLabel label = SDL_GetGamepadButtonLabel(gamepad, SDL_GAMEPAD_BUTTON_SOUTH);
  110. SDLTest_AssertCheck(label == SDL_GAMEPAD_BUTTON_LABEL_A, "SDL_GetGamepadButtonLabel(SDL_GAMEPAD_BUTTON_SOUTH) -> %d (expected %d [%s])",
  111. label, SDL_GAMEPAD_BUTTON_LABEL_A, "SDL_GAMEPAD_BUTTON_LABEL_A");
  112. }
  113. 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");
  114. /* Set the south button and verify that the gamepad responds */
  115. SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, true), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, true)");
  116. SDL_UpdateJoysticks();
  117. SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == true, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == true");
  118. SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, false), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, false)");
  119. SDL_UpdateJoysticks();
  120. SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == false, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == false");
  121. /* Set an explicit mapping with legacy Nintendo style buttons */
  122. 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,");
  123. {
  124. const char *name = SDL_GetGamepadName(gamepad);
  125. SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual Nintendo Gamepad") == 0, "SDL_GetGamepadName() -> \"%s\" (expected \"%s\")", name, "Virtual Nintendo Gamepad");
  126. }
  127. 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");
  128. /* Set the south button and verify that the gamepad responds */
  129. SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, true), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, true)");
  130. SDL_UpdateJoysticks();
  131. SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == true, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == true");
  132. SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, false), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, false)");
  133. SDL_UpdateJoysticks();
  134. SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == false, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == false");
  135. /* Set an explicit mapping with PS4 style buttons */
  136. 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,");
  137. {
  138. const char *name = SDL_GetGamepadName(gamepad);
  139. SDLTest_AssertCheck(SDL_strcmp(name, "Virtual PS4 Gamepad") == 0, "SDL_GetGamepadName() -> \"%s\" (expected \"%s\")", name, "Virtual PS4 Gamepad");
  140. }
  141. 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");
  142. /* Set the south button and verify that the gamepad responds */
  143. SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, true), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, true)");
  144. SDL_UpdateJoysticks();
  145. SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == true, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == true");
  146. SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_SOUTH, false), "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, false)");
  147. SDL_UpdateJoysticks();
  148. SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == false, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == false");
  149. SDL_CloseGamepad(gamepad);
  150. }
  151. SDL_CloseJoystick(joystick);
  152. }
  153. SDLTest_AssertCheck(SDL_DetachVirtualJoystick(device_id), "SDL_DetachVirtualJoystick()");
  154. }
  155. SDLTest_AssertCheck(!SDL_IsJoystickVirtual(device_id), "!SDL_IsJoystickVirtual()");
  156. SDL_ResetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS);
  157. SDL_QuitSubSystem(SDL_INIT_GAMEPAD);
  158. return TEST_COMPLETED;
  159. }
  160. /* ================= Test References ================== */
  161. /* Joystick routine test cases */
  162. static const SDLTest_TestCaseReference joystickTest1 = {
  163. TestVirtualJoystick, "TestVirtualJoystick", "Test virtual joystick functionality", TEST_ENABLED
  164. };
  165. /* Sequence of Joystick routine test cases */
  166. static const SDLTest_TestCaseReference *joystickTests[] = {
  167. &joystickTest1,
  168. NULL
  169. };
  170. /* Joystick routine test suite (global) */
  171. SDLTest_TestSuiteReference joystickTestSuite = {
  172. "Joystick",
  173. NULL,
  174. joystickTests,
  175. NULL
  176. };