testautomation_joystick.c 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_AttachVirtualJoystickEx
  14. */
  15. static int 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_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
  22. SDL_zero(desc);
  23. desc.version = SDL_VIRTUAL_JOYSTICK_DESC_VERSION;
  24. desc.type = SDL_JOYSTICK_TYPE_GAMEPAD;
  25. desc.naxes = SDL_GAMEPAD_AXIS_MAX;
  26. desc.nbuttons = SDL_GAMEPAD_BUTTON_MAX;
  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_AttachVirtualJoystickEx(&desc);
  31. SDLTest_AssertCheck(device_id > 0, "SDL_AttachVirtualJoystickEx()");
  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. SDLTest_AssertCheck(SDL_strcmp(SDL_GetJoystickName(joystick), desc.name) == 0, "SDL_GetJoystickName()");
  38. SDLTest_AssertCheck(SDL_GetJoystickVendor(joystick) == desc.vendor_id, "SDL_GetJoystickVendor()");
  39. SDLTest_AssertCheck(SDL_GetJoystickProduct(joystick) == desc.product_id, "SDL_GetJoystickProduct()");
  40. SDLTest_AssertCheck(SDL_GetJoystickProductVersion(joystick) == 0, "SDL_GetJoystickProductVersion()");
  41. SDLTest_AssertCheck(SDL_GetJoystickFirmwareVersion(joystick) == 0, "SDL_GetJoystickFirmwareVersion()");
  42. SDLTest_AssertCheck(SDL_GetJoystickSerial(joystick) == NULL, "SDL_GetJoystickSerial()");
  43. SDLTest_AssertCheck(SDL_GetJoystickType(joystick) == desc.type, "SDL_GetJoystickType()");
  44. SDLTest_AssertCheck(SDL_GetNumJoystickAxes(joystick) == desc.naxes, "SDL_GetNumJoystickAxes()");
  45. SDLTest_AssertCheck(SDL_GetNumJoystickHats(joystick) == desc.nhats, "SDL_GetNumJoystickHats()");
  46. SDLTest_AssertCheck(SDL_GetNumJoystickButtons(joystick) == desc.nbuttons, "SDL_GetNumJoystickButtons()");
  47. SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_A, SDL_PRESSED) == 0, "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_A, SDL_PRESSED)");
  48. SDL_UpdateJoysticks();
  49. SDLTest_AssertCheck(SDL_GetJoystickButton(joystick, SDL_GAMEPAD_BUTTON_A) == SDL_PRESSED, "SDL_GetJoystickButton(SDL_GAMEPAD_BUTTON_A) == SDL_PRESSED");
  50. SDLTest_AssertCheck(SDL_SetJoystickVirtualButton(joystick, SDL_GAMEPAD_BUTTON_A, SDL_RELEASED) == 0, "SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_A, SDL_RELEASED)");
  51. SDL_UpdateJoysticks();
  52. SDLTest_AssertCheck(SDL_GetJoystickButton(joystick, SDL_GAMEPAD_BUTTON_A) == SDL_RELEASED, "SDL_GetJoystickButton(SDL_GAMEPAD_BUTTON_A) == SDL_RELEASED");
  53. SDL_CloseJoystick(joystick);
  54. }
  55. SDLTest_AssertCheck(SDL_DetachVirtualJoystick(device_id) == 0, "SDL_DetachVirtualJoystick()");
  56. }
  57. SDLTest_AssertCheck(!SDL_IsJoystickVirtual(device_id), "!SDL_IsJoystickVirtual()");
  58. SDL_ResetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS);
  59. SDL_QuitSubSystem(SDL_INIT_GAMEPAD);
  60. return TEST_COMPLETED;
  61. }
  62. /* ================= Test References ================== */
  63. /* Joystick routine test cases */
  64. static const SDLTest_TestCaseReference joystickTest1 = {
  65. (SDLTest_TestCaseFp)TestVirtualJoystick, "TestVirtualJoystick", "Test virtual joystick functionality", TEST_ENABLED
  66. };
  67. /* Sequence of Joystick routine test cases */
  68. static const SDLTest_TestCaseReference *joystickTests[] = {
  69. &joystickTest1,
  70. NULL
  71. };
  72. /* Joystick routine test suite (global) */
  73. SDLTest_TestSuiteReference joystickTestSuite = {
  74. "Joystick",
  75. NULL,
  76. joystickTests,
  77. NULL
  78. };