Debian Codegraph @

test/testautomation_joystick.c

raw
1 /**
2 * Joystick test suite
3 */
4
5 #include "SDL.h"
6 #include "SDL_test.h"
7
8 #if 0
9 #include "../src/joystick/usb_ids.h"
10 #else
11 #define USB_VENDOR_NVIDIA 0x0955
12 #define USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V104 0x7214
13 #endif
14
15 /* ================= Test Case Implementation ================== */
16
17 /* Test case functions */
18
19 /**
20 * @brief Check virtual joystick creation
21 *
22 * @sa SDL_JoystickAttachVirtualEx
23 */
24 static int
25 TestVirtualJoystick(void *arg)
26 {
27 SDL_VirtualJoystickDesc desc;
28 SDL_Joystick *joystick = NULL;
29 int device_index;
30
31 SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0, "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)");
32
33 SDL_zero(desc);
34 desc.version = SDL_VIRTUAL_JOYSTICK_DESC_VERSION;
35 desc.type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
36 desc.naxes = SDL_CONTROLLER_AXIS_MAX;
37 desc.nbuttons = SDL_CONTROLLER_BUTTON_MAX;
38 desc.vendor_id = USB_VENDOR_NVIDIA;
39 desc.product_id = USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V104;
40 desc.name = "Virtual NVIDIA SHIELD Controller";
41 device_index = SDL_JoystickAttachVirtualEx(&desc);
42 SDLTest_AssertCheck(device_index >= 0, "SDL_JoystickAttachVirtualEx()");
43 SDLTest_AssertCheck(SDL_JoystickIsVirtual(device_index), "SDL_JoystickIsVirtual()");
44 if (device_index >= 0) {
45 joystick = SDL_JoystickOpen(device_index);
46 SDLTest_AssertCheck(joystick != NULL, "SDL_JoystickOpen()");
47 if (joystick) {
48 SDLTest_AssertCheck(SDL_strcmp(SDL_JoystickName(joystick), desc.name) == 0, "SDL_JoystickName()");
49 SDLTest_AssertCheck(SDL_JoystickGetVendor(joystick) == desc.vendor_id, "SDL_JoystickGetVendor()");
50 SDLTest_AssertCheck(SDL_JoystickGetProduct(joystick) == desc.product_id, "SDL_JoystickGetProduct()");
51 SDLTest_AssertCheck(SDL_JoystickGetProductVersion(joystick) == 0, "SDL_JoystickGetProductVersion()");
52 SDLTest_AssertCheck(SDL_JoystickGetFirmwareVersion(joystick) == 0, "SDL_JoystickGetFirmwareVersion()");
53 SDLTest_AssertCheck(SDL_JoystickGetSerial(joystick) == NULL, "SDL_JoystickGetSerial()");
54 SDLTest_AssertCheck(SDL_JoystickGetType(joystick) == desc.type, "SDL_JoystickGetType()");
55 SDLTest_AssertCheck(SDL_JoystickNumAxes(joystick) == desc.naxes, "SDL_JoystickNumAxes()");
56 SDLTest_AssertCheck(SDL_JoystickNumBalls(joystick) == 0, "SDL_JoystickNumBalls()");
57 SDLTest_AssertCheck(SDL_JoystickNumHats(joystick) == desc.nhats, "SDL_JoystickNumHats()");
58 SDLTest_AssertCheck(SDL_JoystickNumButtons(joystick) == desc.nbuttons, "SDL_JoystickNumButtons()");
59
60 SDLTest_AssertCheck(SDL_JoystickSetVirtualButton(joystick, SDL_CONTROLLER_BUTTON_A, SDL_PRESSED) == 0, "SDL_JoystickSetVirtualButton(SDL_CONTROLLER_BUTTON_A, SDL_PRESSED)");
61 SDL_JoystickUpdate();
62 SDLTest_AssertCheck(SDL_JoystickGetButton(joystick, SDL_CONTROLLER_BUTTON_A) == SDL_PRESSED, "SDL_JoystickGetButton(SDL_CONTROLLER_BUTTON_A) == SDL_PRESSED");
63 SDLTest_AssertCheck(SDL_JoystickSetVirtualButton(joystick, SDL_CONTROLLER_BUTTON_A, SDL_RELEASED) == 0, "SDL_JoystickSetVirtualButton(SDL_CONTROLLER_BUTTON_A, SDL_RELEASED)");
64 SDL_JoystickUpdate();
65 SDLTest_AssertCheck(SDL_JoystickGetButton(joystick, SDL_CONTROLLER_BUTTON_A) == SDL_RELEASED, "SDL_JoystickGetButton(SDL_CONTROLLER_BUTTON_A) == SDL_RELEASED");
66
67 SDL_JoystickClose(joystick);
68 }
69 SDLTest_AssertCheck(SDL_JoystickDetachVirtual(device_index) == 0, "SDL_JoystickDetachVirtual()");
70 }
71 SDLTest_AssertCheck(!SDL_JoystickIsVirtual(device_index), "!SDL_JoystickIsVirtual()");
72
73 SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
74
75 return TEST_COMPLETED;
76 }
77
78 /* ================= Test References ================== */
79
80 /* Joystick routine test cases */
81 static const SDLTest_TestCaseReference joystickTest1 = {
82 (SDLTest_TestCaseFp)TestVirtualJoystick, "TestVirtualJoystick", "Test virtual joystick functionality", TEST_ENABLED
83 };
84
85 /* Sequence of Joystick routine test cases */
86 static const SDLTest_TestCaseReference *joystickTests[] = {
87 &joystickTest1,
88 NULL
89 };
90
91 /* Joystick routine test suite (global) */
92 SDLTest_TestSuiteReference joystickTestSuite = {
93 "Joystick",
94 NULL,
95 joystickTests,
96 NULL
97 };