2022-07-13 08:57:40 -07:00
/**
* Joystick test suite
*/
2022-11-26 20:43:38 -08:00
# include <SDL3/SDL.h>
# include <SDL3/SDL_test.h>
2022-07-13 08:57:40 -07:00
# include "../src/joystick/usb_ids.h"
2023-03-08 16:12:45 +01:00
# include "testautomation_suites.h"
2022-07-13 08:57:40 -07:00
/* ================= Test Case Implementation ================== */
/* Test case functions */
/**
2023-11-06 10:26:06 -05:00
* Check virtual joystick creation
2022-07-13 08:57:40 -07:00
*
2024-05-09 13:51:33 -07:00
* \ sa SDL_AttachVirtualJoystick
2022-07-13 08:57:40 -07:00
*/
2023-03-08 16:12:45 +01:00
static int TestVirtualJoystick ( void * arg )
2022-07-13 08:57:40 -07:00
{
SDL_VirtualJoystickDesc desc ;
SDL_Joystick * joystick = NULL ;
2023-11-06 13:07:12 -08:00
SDL_Gamepad * gamepad = NULL ;
2022-12-27 18:10:06 -08:00
SDL_JoystickID device_id ;
2022-07-13 08:57:40 -07:00
2022-12-27 09:46:24 -08:00
SDLTest_AssertCheck ( SDL_InitSubSystem ( SDL_INIT_GAMEPAD ) = = 0 , " SDL_InitSubSystem(SDL_INIT_GAMEPAD) " ) ;
2022-07-13 08:57:40 -07:00
2023-01-09 09:16:10 -08:00
SDL_SetHint ( SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS , " 1 " ) ;
2022-07-13 08:57:40 -07:00
SDL_zero ( desc ) ;
2022-12-27 11:31:54 -08:00
desc . type = SDL_JOYSTICK_TYPE_GAMEPAD ;
2022-12-27 09:46:24 -08:00
desc . naxes = SDL_GAMEPAD_AXIS_MAX ;
desc . nbuttons = SDL_GAMEPAD_BUTTON_MAX ;
2022-07-13 08:57:40 -07:00
desc . vendor_id = USB_VENDOR_NVIDIA ;
2022-10-25 10:30:56 -07:00
desc . product_id = USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V104 ;
2022-07-13 08:57:40 -07:00
desc . name = " Virtual NVIDIA SHIELD Controller " ;
2024-05-09 13:51:33 -07:00
device_id = SDL_AttachVirtualJoystick ( & desc ) ;
2024-06-21 15:08:30 +02:00
SDLTest_AssertCheck ( device_id > 0 , " SDL_AttachVirtualJoystick() -> % " SDL_PRIs32 " (expected > 0) " , device_id ) ;
2022-12-27 18:10:06 -08:00
SDLTest_AssertCheck ( SDL_IsJoystickVirtual ( device_id ) , " SDL_IsJoystickVirtual() " ) ;
if ( device_id > 0 ) {
joystick = SDL_OpenJoystick ( device_id ) ;
2022-12-27 05:50:46 -08:00
SDLTest_AssertCheck ( joystick ! = NULL , " SDL_OpenJoystick() " ) ;
2022-07-13 08:57:40 -07:00
if ( joystick ) {
2024-06-21 15:08:30 +02:00
{
const char * dname = SDL_GetJoystickName ( joystick ) ;
SDLTest_AssertCheck ( SDL_strcmp ( dname , desc . name ) = = 0 , " SDL_GetJoystickName() -> \" %s \" (expected \" %s \" ) " , dname , desc . name ) ;
}
{
Uint16 vendor_id = SDL_GetJoystickVendor ( joystick ) ;
SDLTest_AssertCheck ( vendor_id = = desc . vendor_id , " SDL_GetJoystickVendor() -> 0x%04x (expected 0x%04x) " , vendor_id , desc . vendor_id ) ;
}
{
Uint16 product_id = SDL_GetJoystickProduct ( joystick ) ;
SDLTest_AssertCheck ( product_id = = desc . product_id , " SDL_GetJoystickProduct() -> 0x%04x (expected 0x%04x) " , product_id , desc . product_id ) ;
}
{
Uint16 product_version = SDL_GetJoystickProductVersion ( joystick ) ;
SDLTest_AssertCheck ( product_version = = 0 , " SDL_GetJoystickProductVersion() -> 0x%04x (expected 0x%04x) " , product_version , 0 ) ;
}
{
Uint16 firmware_Version = SDL_GetJoystickFirmwareVersion ( joystick ) ;
SDLTest_AssertCheck ( firmware_Version = = 0 , " SDL_GetJoystickFirmwareVersion() -> 0x%04x (expected 0x%04x) " , firmware_Version , 0 ) ;
}
{
const char * serial = SDL_GetJoystickSerial ( joystick ) ;
SDLTest_AssertCheck ( serial = = NULL , " SDL_GetJoystickSerial() -> %s (expected %s) " , serial , " (null) " ) ;
}
{
SDL_JoystickType type = SDL_GetJoystickType ( joystick ) ;
SDLTest_AssertCheck ( type = = desc . type , " SDL_GetJoystickType() -> %d (expected %d) " , type , desc . type ) ;
}
{
Uint16 naxes = SDL_GetNumJoystickAxes ( joystick ) ;
SDLTest_AssertCheck ( naxes = = desc . naxes , " SDL_GetNumJoystickAxes() -> 0x%04x (expected 0x%04x) " , naxes , desc . naxes ) ;
}
{
int nballs = SDL_GetNumJoystickBalls ( joystick ) ;
SDLTest_AssertCheck ( nballs = = 0 , " SDL_GetNumJoystickBalls() -> %d (expected %d) " , nballs , 0 ) ;
}
{
int nhats = SDL_GetNumJoystickHats ( joystick ) ;
SDLTest_AssertCheck ( nhats = = desc . nhats , " SDL_GetNumJoystickHats() -> %d (expected %d) " , nhats , desc . nhats ) ;
}
{
int nbuttons = SDL_GetNumJoystickButtons ( joystick ) ;
SDLTest_AssertCheck ( nbuttons = = desc . nbuttons , " SDL_GetNumJoystickButtons() -> %d (expected %d) " , nbuttons , desc . nbuttons ) ;
}
2022-07-13 08:57:40 -07:00
2023-11-06 13:07:12 -08:00
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_PRESSED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_PRESSED) " ) ;
2022-12-27 05:50:46 -08:00
SDL_UpdateJoysticks ( ) ;
2023-11-06 13:07:12 -08:00
SDLTest_AssertCheck ( SDL_GetJoystickButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH ) = = SDL_PRESSED , " SDL_GetJoystickButton(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_PRESSED " ) ;
2023-01-09 09:16:10 -08:00
2023-11-06 13:07:12 -08:00
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_RELEASED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_RELEASED) " ) ;
2022-12-27 05:50:46 -08:00
SDL_UpdateJoysticks ( ) ;
2023-11-06 13:07:12 -08:00
SDLTest_AssertCheck ( SDL_GetJoystickButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH ) = = SDL_RELEASED , " SDL_GetJoystickButton(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_RELEASED " ) ;
2024-07-14 08:46:24 -07:00
gamepad = SDL_OpenGamepad ( SDL_GetJoystickID ( joystick ) ) ;
2023-11-06 13:07:12 -08:00
SDLTest_AssertCheck ( gamepad ! = NULL , " SDL_OpenGamepad() succeeded " ) ;
if ( gamepad ) {
2024-06-21 15:08:30 +02:00
{
const char * name = SDL_GetGamepadName ( gamepad ) ;
SDLTest_AssertCheck ( name & & SDL_strcmp ( name , desc . name ) = = 0 , " SDL_GetGamepadName() -> \" %s \" (expected \" %s \" ) " , name , desc . name ) ;
}
{
Uint16 vendor_id = SDL_GetGamepadVendor ( gamepad ) ;
SDLTest_AssertCheck ( vendor_id = = desc . vendor_id , " SDL_GetGamepadVendor() -> 0x%04x (expected 0x%04x) " , vendor_id , desc . vendor_id ) ;
}
{
Uint16 product_id = SDL_GetGamepadProduct ( gamepad ) ;
SDLTest_AssertCheck ( product_id = = desc . product_id , " SDL_GetGamepadProduct() -> 0x%04x (expected 0x%04x) " , product_id , desc . product_id ) ;
}
2023-11-06 13:07:12 -08:00
/* Set an explicit mapping with a different name */
2024-07-14 08:46:24 -07:00
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, " ) ;
2024-06-21 15:08:30 +02:00
{
const char * name = SDL_GetGamepadName ( gamepad ) ;
SDLTest_AssertCheck ( name & & SDL_strcmp ( name , " Virtual Gamepad " ) = = 0 , " SDL_GetGamepadName() -> \" %s \" (expected \" %s \" ) " , name , " Virtual Gamepad " ) ;
}
{
SDL_GamepadButtonLabel label = SDL_GetGamepadButtonLabel ( gamepad , SDL_GAMEPAD_BUTTON_SOUTH ) ;
SDLTest_AssertCheck ( label = = SDL_GAMEPAD_BUTTON_LABEL_A , " SDL_GetGamepadButtonLabel(SDL_GAMEPAD_BUTTON_SOUTH) -> %d (expected %d [%s]) " ,
label , SDL_GAMEPAD_BUTTON_LABEL_A , " SDL_GAMEPAD_BUTTON_LABEL_A " ) ;
}
2023-11-06 13:07:12 -08:00
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 */
2024-06-21 15:08:30 +02:00
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_PRESSED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_PRESSED) == 0 " ) ;
2023-11-06 13:07:12 -08:00
SDL_UpdateJoysticks ( ) ;
SDLTest_AssertCheck ( SDL_GetGamepadButton ( gamepad , SDL_GAMEPAD_BUTTON_SOUTH ) = = SDL_PRESSED , " SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_PRESSED " ) ;
2024-06-21 15:08:30 +02:00
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_RELEASED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_RELEASED) == 0 " ) ;
2023-11-06 13:07:12 -08:00
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 */
2024-07-14 08:46:24 -07:00
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, " ) ;
2024-06-21 15:08:30 +02:00
{
const char * name = SDL_GetGamepadName ( gamepad ) ;
SDLTest_AssertCheck ( name & & SDL_strcmp ( name , " Virtual Nintendo Gamepad " ) = = 0 , " SDL_GetGamepadName() -> \" %s \" (expected \" %s \" ) " , name , " Virtual Nintendo Gamepad " ) ;
}
2023-11-06 13:07:12 -08:00
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 */
2024-06-21 15:08:30 +02:00
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_PRESSED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_PRESSED) == 0 " ) ;
2023-11-06 13:07:12 -08:00
SDL_UpdateJoysticks ( ) ;
SDLTest_AssertCheck ( SDL_GetGamepadButton ( gamepad , SDL_GAMEPAD_BUTTON_SOUTH ) = = SDL_PRESSED , " SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_PRESSED " ) ;
2024-06-21 15:08:30 +02:00
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_RELEASED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_RELEASED) == 0 " ) ;
2023-11-06 13:07:12 -08:00
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 */
2024-07-14 08:46:24 -07:00
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, " ) ;
2024-06-21 15:08:30 +02:00
{
const char * name = SDL_GetGamepadName ( gamepad ) ;
SDLTest_AssertCheck ( SDL_strcmp ( name , " Virtual PS4 Gamepad " ) = = 0 , " SDL_GetGamepadName() -> \" %s \" (expected \" %s \" ) " , name , " Virtual PS4 Gamepad " ) ;
}
2023-11-06 13:07:12 -08:00
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 */
2024-06-21 15:08:30 +02:00
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_PRESSED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_PRESSED) == 0 " ) ;
2023-11-06 13:07:12 -08:00
SDL_UpdateJoysticks ( ) ;
SDLTest_AssertCheck ( SDL_GetGamepadButton ( gamepad , SDL_GAMEPAD_BUTTON_SOUTH ) = = SDL_PRESSED , " SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == SDL_PRESSED " ) ;
2024-06-21 15:08:30 +02:00
SDLTest_AssertCheck ( SDL_SetJoystickVirtualButton ( joystick , SDL_GAMEPAD_BUTTON_SOUTH , SDL_RELEASED ) = = 0 , " SDL_SetJoystickVirtualButton(SDL_GAMEPAD_BUTTON_SOUTH, SDL_RELEASED) == 0 " ) ;
2023-11-06 13:07:12 -08:00
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 08:57:40 -07:00
2022-12-27 05:50:46 -08:00
SDL_CloseJoystick ( joystick ) ;
2022-07-13 08:57:40 -07:00
}
2022-12-27 18:10:06 -08:00
SDLTest_AssertCheck ( SDL_DetachVirtualJoystick ( device_id ) = = 0 , " SDL_DetachVirtualJoystick() " ) ;
2022-07-13 08:57:40 -07:00
}
2022-12-27 18:10:06 -08:00
SDLTest_AssertCheck ( ! SDL_IsJoystickVirtual ( device_id ) , " !SDL_IsJoystickVirtual() " ) ;
2022-07-13 08:57:40 -07:00
2023-01-09 09:16:10 -08:00
SDL_ResetHint ( SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS ) ;
2022-12-27 09:46:24 -08:00
SDL_QuitSubSystem ( SDL_INIT_GAMEPAD ) ;
2022-07-13 08:57:40 -07:00
return TEST_COMPLETED ;
}
/* ================= Test References ================== */
/* Joystick routine test cases */
2022-11-30 12:51:59 -08:00
static const SDLTest_TestCaseReference joystickTest1 = {
( SDLTest_TestCaseFp ) TestVirtualJoystick , " TestVirtualJoystick " , " Test virtual joystick functionality " , TEST_ENABLED
} ;
2022-07-13 08:57:40 -07:00
/* Sequence of Joystick routine test cases */
2022-11-30 12:51:59 -08:00
static const SDLTest_TestCaseReference * joystickTests [ ] = {
2022-07-13 08:57:40 -07:00
& joystickTest1 ,
NULL
} ;
/* Joystick routine test suite (global) */
SDLTest_TestSuiteReference joystickTestSuite = {
" Joystick " ,
NULL ,
joystickTests ,
NULL
} ;