2022-07-13 09:57:40 -06:00
/**
* Joystick test suite
*/
2022-11-26 21:43:38 -07:00
# include <SDL3/SDL.h>
# include <SDL3/SDL_test.h>
2022-07-13 09:57:40 -06:00
# include "../src/joystick/usb_ids.h"
2023-03-08 08:12:45 -07:00
# include "testautomation_suites.h"
2022-07-13 09:57:40 -06:00
/* ================= Test Case Implementation ================== */
/* Test case functions */
/**
2023-11-06 08:26:06 -07:00
* Check virtual joystick creation
2022-07-13 09:57:40 -06:00
*
2023-02-01 16:21:53 -07:00
* \ sa SDL_AttachVirtualJoystickEx
2022-07-13 09:57:40 -06:00
*/
2023-03-08 08:12:45 -07:00
static int TestVirtualJoystick ( void * arg )
2022-07-13 09:57:40 -06:00
{
SDL_VirtualJoystickDesc desc ;
SDL_Joystick * joystick = NULL ;
2023-11-06 14:07:12 -07:00
SDL_Gamepad * gamepad = NULL ;
2022-12-27 19:10:06 -07:00
SDL_JoystickID device_id ;
2022-07-13 09:57:40 -06:00
2022-12-27 10:46:24 -07:00
SDLTest_AssertCheck ( SDL_InitSubSystem ( SDL_INIT_GAMEPAD ) = = 0 , " SDL_InitSubSystem(SDL_INIT_GAMEPAD) " ) ;
2022-07-13 09:57:40 -06:00
2023-01-09 10:16:10 -07:00
SDL_SetHint ( SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS , " 1 " ) ;
2022-07-13 09:57:40 -06:00
SDL_zero ( desc ) ;
desc . version = SDL_VIRTUAL_JOYSTICK_DESC_VERSION ;
2022-12-27 12:31:54 -07:00
desc . type = SDL_JOYSTICK_TYPE_GAMEPAD ;
2022-12-27 10:46:24 -07:00
desc . naxes = SDL_GAMEPAD_AXIS_MAX ;
desc . nbuttons = SDL_GAMEPAD_BUTTON_MAX ;
2022-07-13 09:57:40 -06:00
desc . vendor_id = USB_VENDOR_NVIDIA ;
2022-10-25 11:30:56 -06:00
desc . product_id = USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V104 ;
2022-07-13 09:57:40 -06:00
desc . name = " Virtual NVIDIA SHIELD Controller " ;
2022-12-27 19:10:06 -07:00
device_id = SDL_AttachVirtualJoystickEx ( & desc ) ;
SDLTest_AssertCheck ( device_id > 0 , " SDL_AttachVirtualJoystickEx() " ) ;
SDLTest_AssertCheck ( SDL_IsJoystickVirtual ( device_id ) , " SDL_IsJoystickVirtual() " ) ;
if ( device_id > 0 ) {
joystick = SDL_OpenJoystick ( device_id ) ;
2022-12-27 06:50:46 -07:00
SDLTest_AssertCheck ( joystick ! = NULL , " SDL_OpenJoystick() " ) ;
2022-07-13 09:57:40 -06:00
if ( joystick ) {
2022-12-27 06:50:46 -07:00
SDLTest_AssertCheck ( SDL_strcmp ( SDL_GetJoystickName ( joystick ) , desc . name ) = = 0 , " SDL_GetJoystickName() " ) ;
SDLTest_AssertCheck ( SDL_GetJoystickVendor ( joystick ) = = desc . vendor_id , " SDL_GetJoystickVendor() " ) ;
SDLTest_AssertCheck ( SDL_GetJoystickProduct ( joystick ) = = desc . product_id , " SDL_GetJoystickProduct() " ) ;
SDLTest_AssertCheck ( SDL_GetJoystickProductVersion ( joystick ) = = 0 , " SDL_GetJoystickProductVersion() " ) ;
SDLTest_AssertCheck ( SDL_GetJoystickFirmwareVersion ( joystick ) = = 0 , " SDL_GetJoystickFirmwareVersion() " ) ;
SDLTest_AssertCheck ( SDL_GetJoystickSerial ( joystick ) = = NULL , " SDL_GetJoystickSerial() " ) ;
SDLTest_AssertCheck ( SDL_GetJoystickType ( joystick ) = = desc . type , " SDL_GetJoystickType() " ) ;
SDLTest_AssertCheck ( SDL_GetNumJoystickAxes ( joystick ) = = desc . naxes , " SDL_GetNumJoystickAxes() " ) ;
SDLTest_AssertCheck ( SDL_GetNumJoystickHats ( joystick ) = = desc . nhats , " SDL_GetNumJoystickHats() " ) ;
SDLTest_AssertCheck ( SDL_GetNumJoystickButtons ( joystick ) = = desc . nbuttons , " SDL_GetNumJoystickButtons() " ) ;
2022-07-13 09:57:40 -06:00
2023-11-06 14:07:12 -07:00
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_PRESSED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_PRESSED) " ) ;
2022-12-27 06:50:46 -07:00
SDL_UpdateJoysticks ( ) ;
2023-11-06 14:07:12 -07:00
SDLTest_AssertCheck ( SDL_GetJoystickButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH ) = = SDL_PRESSED , " SDL_GetJoystickButton(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_PRESSED " ) ;
2023-01-09 10:16:10 -07:00
2023-11-06 14:07:12 -07:00
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_RELEASED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_RELEASED) " ) ;
2022-12-27 06:50:46 -07:00
SDL_UpdateJoysticks ( ) ;
2023-11-06 14:07:12 -07:00
SDLTest_AssertCheck ( SDL_GetJoystickButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH ) = = SDL_RELEASED , " SDL_GetJoystickButton(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_RELEASED " ) ;
gamepad = SDL_OpenGamepad ( SDL_GetJoystickInstanceID ( joystick ) ) ;
SDLTest_AssertCheck ( gamepad ! = NULL , " SDL_OpenGamepad() succeeded " ) ;
if ( gamepad ) {
SDLTest_AssertCheck ( SDL_strcmp ( SDL_GetGamepadName ( gamepad ) , desc . name ) = = 0 , " SDL_GetGamepadName() " ) ;
SDLTest_AssertCheck ( SDL_GetGamepadVendor ( gamepad ) = = desc . vendor_id , " SDL_GetGamepadVendor() " ) ;
SDLTest_AssertCheck ( SDL_GetGamepadProduct ( gamepad ) = = desc . product_id , " SDL_GetGamepadProduct() " ) ;
/* Set an explicit mapping with a different name */
SDL_SetGamepadMapping ( SDL_GetJoystickInstanceID ( 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, " ) ;
SDLTest_AssertCheck ( SDL_strcmp ( SDL_GetGamepadName ( gamepad ) , " Virtual Gamepad " ) = = 0 , " SDL_GetGamepadName() == Virtual Gamepad " ) ;
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 " ) ;
/* Set the south button and verify that the gamepad responds */
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_PRESSED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_PRESSED) " ) ;
SDL_UpdateJoysticks ( ) ;
SDLTest_AssertCheck ( SDL_GetGamepadButton ( gamepad , SDL_GAMEPAD_BUTTON_SOUTH ) = = SDL_PRESSED , " SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_PRESSED " ) ;
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_RELEASED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_RELEASED) " ) ;
SDL_UpdateJoysticks ( ) ;
SDLTest_AssertCheck ( SDL_GetGamepadButton ( gamepad , SDL_GAMEPAD_BUTTON_SOUTH ) = = SDL_RELEASED , " SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_RELEASED " ) ;
/* Set an explicit mapping with legacy Nintendo style buttons */
SDL_SetGamepadMapping ( SDL_GetJoystickInstanceID ( 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, " ) ;
SDLTest_AssertCheck ( SDL_strcmp ( SDL_GetGamepadName ( gamepad ) , " Virtual Nintendo Gamepad " ) = = 0 , " SDL_GetGamepadName() == Virtual Nintendo Gamepad " ) ;
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 " ) ;
/* Set the south button and verify that the gamepad responds */
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_PRESSED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_PRESSED) " ) ;
SDL_UpdateJoysticks ( ) ;
SDLTest_AssertCheck ( SDL_GetGamepadButton ( gamepad , SDL_GAMEPAD_BUTTON_SOUTH ) = = SDL_PRESSED , " SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_PRESSED " ) ;
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_RELEASED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_RELEASED) " ) ;
SDL_UpdateJoysticks ( ) ;
SDLTest_AssertCheck ( SDL_GetGamepadButton ( gamepad , SDL_GAMEPAD_BUTTON_SOUTH ) = = SDL_RELEASED , " SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_RELEASED " ) ;
/* Set an explicit mapping with PS4 style buttons */
SDL_SetGamepadMapping ( SDL_GetJoystickInstanceID ( joystick ) , " ff0013db5669727475616c2043007601,Virtual PS4 Gamepad,type:ps4,cross:b0,circle:b1,square:b2,triangle: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, " ) ;
SDLTest_AssertCheck ( SDL_strcmp ( SDL_GetGamepadName ( gamepad ) , " Virtual PS4 Gamepad " ) = = 0 , " SDL_GetGamepadName() == Virtual PS4 Gamepad " ) ;
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 " ) ;
/* Set the south button and verify that the gamepad responds */
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_PRESSED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_PRESSED) " ) ;
SDL_UpdateJoysticks ( ) ;
SDLTest_AssertCheck ( SDL_GetGamepadButton ( gamepad , SDL_GAMEPAD_BUTTON_SOUTH ) = = SDL_PRESSED , " SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_PRESSED " ) ;
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_RELEASED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_RELEASED) " ) ;
SDL_UpdateJoysticks ( ) ;
SDLTest_AssertCheck ( SDL_GetGamepadButton ( gamepad , SDL_GAMEPAD_BUTTON_SOUTH ) = = SDL_RELEASED , " SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_RELEASED " ) ;
SDL_CloseGamepad ( gamepad ) ;
}
2022-07-13 09:57:40 -06:00
2022-12-27 06:50:46 -07:00
SDL_CloseJoystick ( joystick ) ;
2022-07-13 09:57:40 -06:00
}
2022-12-27 19:10:06 -07:00
SDLTest_AssertCheck ( SDL_DetachVirtualJoystick ( device_id ) = = 0 , " SDL_DetachVirtualJoystick() " ) ;
2022-07-13 09:57:40 -06:00
}
2022-12-27 19:10:06 -07:00
SDLTest_AssertCheck ( ! SDL_IsJoystickVirtual ( device_id ) , " !SDL_IsJoystickVirtual() " ) ;
2022-07-13 09:57:40 -06:00
2023-01-09 10:16:10 -07:00
SDL_ResetHint ( SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS ) ;
2022-12-27 10:46:24 -07:00
SDL_QuitSubSystem ( SDL_INIT_GAMEPAD ) ;
2022-07-13 09:57:40 -06:00
return TEST_COMPLETED ;
}
/* ================= Test References ================== */
/* Joystick routine test cases */
2022-11-30 13:51:59 -07:00
static const SDLTest_TestCaseReference joystickTest1 = {
( SDLTest_TestCaseFp ) TestVirtualJoystick , " TestVirtualJoystick " , " Test virtual joystick functionality " , TEST_ENABLED
} ;
2022-07-13 09:57:40 -06:00
/* Sequence of Joystick routine test cases */
2022-11-30 13:51:59 -07:00
static const SDLTest_TestCaseReference * joystickTests [ ] = {
2022-07-13 09:57:40 -06:00
& joystickTest1 ,
NULL
} ;
/* Joystick routine test suite (global) */
SDLTest_TestSuiteReference joystickTestSuite = {
" Joystick " ,
NULL ,
joystickTests ,
NULL
} ;