testautomation_joystick.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /* ================= Test Case Implementation ================== */
  8. /* Test case functions */
  9. /**
  10. * @brief Check virtual joystick creation
  11. *
  12. * @sa SDL_AttachVirtualJoystickEx
  13. */
  14. static int
  15. TestVirtualJoystick(void *arg)
  16. {
  17. SDL_VirtualJoystickDesc desc;
  18. SDL_Joystick *joystick = NULL;
  19. SDL_JoystickID device_id;
  20. SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMEPAD) == 0, "SDL_InitSubSystem(SDL_INIT_GAMEPAD)");
  21. SDL_zero(desc);
  22. desc.version = SDL_VIRTUAL_JOYSTICK_DESC_VERSION;
  23. desc.type = SDL_JOYSTICK_TYPE_GAMEPAD;
  24. desc.naxes = SDL_GAMEPAD_AXIS_MAX;
  25. desc.nbuttons = SDL_GAMEPAD_BUTTON_MAX;
  26. desc.vendor_id = USB_VENDOR_NVIDIA;
  27. desc.product_id = USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V104;
  28. desc.name = "Virtual NVIDIA SHIELD Controller";
  29. device_id = SDL_AttachVirtualJoystickEx(&desc);
  30. SDLTest_AssertCheck(device_id > 0, "SDL_AttachVirtualJoystickEx()");
  31. SDLTest_AssertCheck(SDL_IsJoystickVirtual(device_id), "SDL_IsJoystickVirtual()");
  32. if (device_id > 0) {
  33. joystick = SDL_OpenJoystick(device_id);
  34. SDLTest_AssertCheck(joystick != NULL, "SDL_OpenJoystick()");
  35. if (joystick) {
  36. SDLTest_AssertCheck(SDL_strcmp(SDL_GetJoystickName(joystick), desc.name) == 0, "SDL_GetJoystickName()");
  37. SDLTest_AssertCheck(SDL_GetJoystickVendor(joystick) == desc.vendor_id, "SDL_GetJoystickVendor()");
  38. SDLTest_AssertCheck(SDL_GetJoystickProduct(joystick) == desc.product_id, "SDL_GetJoystickProduct()");
  39. SDLTest_AssertCheck(SDL_GetJoystickProductVersion(joystick) == 0, "SDL_GetJoystickProductVersion()");
  40. SDLTest_AssertCheck(SDL_GetJoystickFirmwareVersion(joystick) == 0, "SDL_GetJoystickFirmwareVersion()");
  41. SDLTest_AssertCheck(SDL_GetJoystickSerial(joystick) == NULL, "SDL_GetJoystickSerial()");
  42. SDLTest_AssertCheck(SDL_GetJoystickType(joystick) == desc.type, "SDL_GetJoystickType()");
  43. SDLTest_AssertCheck(SDL_GetNumJoystickAxes(joystick) == desc.naxes, "SDL_GetNumJoystickAxes()");
  44. SDLTest_AssertCheck(SDL_GetNumJoystickHats(joystick) == desc.nhats, "SDL_GetNumJoystickHats()");
  45. SDLTest_AssertCheck(SDL_GetNumJoystickButtons(joystick) == desc.nbuttons, "SDL_GetNumJoystickButtons()");
  46. SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_A, SDL_PRESSED) == 0, "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_A, SDL_PRESSED)");
  47. SDL_UpdateJoysticks();
  48. SDLTest_AssertCheck(SDL_GetJoystickButton(joystick, SDL_GAMEPAD_BUTTON_A) == SDL_PRESSED, "SDL_GetJoystickButton(SDL_GAMEPAD_BUTTON_A) == SDL_PRESSED");
  49. SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_A, SDL_RELEASED) == 0, "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_A, SDL_RELEASED)");
  50. SDL_UpdateJoysticks();
  51. SDLTest_AssertCheck(SDL_GetJoystickButton(joystick, SDL_GAMEPAD_BUTTON_A) == SDL_RELEASED, "SDL_GetJoystickButton(SDL_GAMEPAD_BUTTON_A) == SDL_RELEASED");
  52. SDL_CloseJoystick(joystick);
  53. }
  54. SDLTest_AssertCheck(SDL_DetachVirtualJoystick(device_id) == 0, "SDL_DetachVirtualJoystick()");
  55. }
  56. SDLTest_AssertCheck(!SDL_IsJoystickVirtual(device_id), "!SDL_IsJoystickVirtual()");
  57. SDL_QuitSubSystem(SDL_INIT_GAMEPAD);
  58. return TEST_COMPLETED;
  59. }
  60. /* ================= Test References ================== */
  61. /* Joystick routine test cases */
  62. static const SDLTest_TestCaseReference joystickTest1 = {
  63. (SDLTest_TestCaseFp)TestVirtualJoystick, "TestVirtualJoystick", "Test virtual joystick functionality", TEST_ENABLED
  64. };
  65. /* Sequence of Joystick routine test cases */
  66. static const SDLTest_TestCaseReference *joystickTests[] = {
  67. &joystickTest1,
  68. NULL
  69. };
  70. /* Joystick routine test suite (global) */
  71. SDLTest_TestSuiteReference joystickTestSuite = {
  72. "Joystick",
  73. NULL,
  74. joystickTests,
  75. NULL
  76. };