🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
4.1 KiB

  1. /**
  2. * Joystick test suite
  3. */
  4. #include "SDL.h"
  5. #include "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_JoystickAttachVirtualEx
  13. */
  14. static int
  15. TestVirtualJoystick(void *arg)
  16. {
  17. SDL_VirtualJoystickDesc desc;
  18. SDL_Joystick *joystick = NULL;
  19. int device_index;
  20. SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0, "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)");
  21. SDL_zero(desc);
  22. desc.version = SDL_VIRTUAL_JOYSTICK_DESC_VERSION;
  23. desc.type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
  24. desc.naxes = SDL_CONTROLLER_AXIS_MAX;
  25. desc.nbuttons = SDL_CONTROLLER_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_index = SDL_JoystickAttachVirtualEx(&desc);
  30. SDLTest_AssertCheck(device_index >= 0, "SDL_JoystickAttachVirtualEx()");
  31. SDLTest_AssertCheck(SDL_JoystickIsVirtual(device_index), "SDL_JoystickIsVirtual()");
  32. if (device_index >= 0) {
  33. joystick = SDL_JoystickOpen(device_index);
  34. SDLTest_AssertCheck(joystick != NULL, "SDL_JoystickOpen()");
  35. if (joystick) {
  36. SDLTest_AssertCheck(SDL_strcmp(SDL_JoystickName(joystick), desc.name) == 0, "SDL_JoystickName()");
  37. SDLTest_AssertCheck(SDL_JoystickGetVendor(joystick) == desc.vendor_id, "SDL_JoystickGetVendor()");
  38. SDLTest_AssertCheck(SDL_JoystickGetProduct(joystick) == desc.product_id, "SDL_JoystickGetProduct()");
  39. SDLTest_AssertCheck(SDL_JoystickGetProductVersion(joystick) == 0, "SDL_JoystickGetProductVersion()");
  40. SDLTest_AssertCheck(SDL_JoystickGetFirmwareVersion(joystick) == 0, "SDL_JoystickGetFirmwareVersion()");
  41. SDLTest_AssertCheck(SDL_JoystickGetSerial(joystick) == NULL, "SDL_JoystickGetSerial()");
  42. SDLTest_AssertCheck(SDL_JoystickGetType(joystick) == desc.type, "SDL_JoystickGetType()");
  43. SDLTest_AssertCheck(SDL_JoystickNumAxes(joystick) == desc.naxes, "SDL_JoystickNumAxes()");
  44. SDLTest_AssertCheck(SDL_JoystickNumBalls(joystick) == 0, "SDL_JoystickNumBalls()");
  45. SDLTest_AssertCheck(SDL_JoystickNumHats(joystick) == desc.nhats, "SDL_JoystickNumHats()");
  46. SDLTest_AssertCheck(SDL_JoystickNumButtons(joystick) == desc.nbuttons, "SDL_JoystickNumButtons()");
  47. SDLTest_AssertCheck(SDL_JoystickSetVirtualButton(joystick, SDL_CONTROLLER_BUTTON_A, SDL_PRESSED) == 0, "SDL_JoystickSetVirtualButton(SDL_CONTROLLER_BUTTON_A, SDL_PRESSED)");
  48. SDL_JoystickUpdate();
  49. SDLTest_AssertCheck(SDL_JoystickGetButton(joystick, SDL_CONTROLLER_BUTTON_A) == SDL_PRESSED, "SDL_JoystickGetButton(SDL_CONTROLLER_BUTTON_A) == SDL_PRESSED");
  50. SDLTest_AssertCheck(SDL_JoystickSetVirtualButton(joystick, SDL_CONTROLLER_BUTTON_A, SDL_RELEASED) == 0, "SDL_JoystickSetVirtualButton(SDL_CONTROLLER_BUTTON_A, SDL_RELEASED)");
  51. SDL_JoystickUpdate();
  52. SDLTest_AssertCheck(SDL_JoystickGetButton(joystick, SDL_CONTROLLER_BUTTON_A) == SDL_RELEASED, "SDL_JoystickGetButton(SDL_CONTROLLER_BUTTON_A) == SDL_RELEASED");
  53. SDL_JoystickClose(joystick);
  54. }
  55. SDLTest_AssertCheck(SDL_JoystickDetachVirtual(device_index) == 0, "SDL_JoystickDetachVirtual()");
  56. }
  57. SDLTest_AssertCheck(!SDL_JoystickIsVirtual(device_index), "!SDL_JoystickIsVirtual()");
  58. SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
  59. return TEST_COMPLETED;
  60. }
  61. /* ================= Test References ================== */
  62. /* Joystick routine test cases */
  63. static const SDLTest_TestCaseReference joystickTest1 =
  64. { (SDLTest_TestCaseFp)TestVirtualJoystick, "TestVirtualJoystick", "Test virtual joystick functionality", TEST_ENABLED };
  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. };