|
| 1 | +# -*- coding: utf-8; -*- |
| 2 | + |
| 3 | +# Copyright (C) 2015 - 2025 Lionel Ott |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +""" |
| 18 | +Integration tests with a profile with macros that have vJoy outputs. |
| 19 | +""" |
| 20 | + |
| 21 | +import sys |
| 22 | + |
| 23 | +sys.path.append(".") |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | +import dill |
| 28 | +from test.integration import app_tester |
| 29 | +from vjoy import vjoy |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(scope="module") |
| 33 | +def profile_name() -> str: |
| 34 | + """Returns the name of the profile to be used for the macro test. |
| 35 | +
|
| 36 | + The profile maps button 1 press to vJoy actions of each type: button press, |
| 37 | + hat direction set, absolute axis set, and relative axis change. |
| 38 | + """ |
| 39 | + return "e2e_macros.xml" |
| 40 | + |
| 41 | + |
| 42 | +class TestMacro: |
| 43 | + """Tests the vJoy actions of macro.py.""" |
| 44 | + |
| 45 | + def test_macro_sequence( |
| 46 | + self, |
| 47 | + subtests, |
| 48 | + tester: app_tester.GremlinAppTester, |
| 49 | + vjoy_control_device: vjoy.VJoy, |
| 50 | + vjoy_di_device: dill.DeviceSummary, |
| 51 | + ): |
| 52 | + """Verifies multiple macro executions triggered by a single button press.""" |
| 53 | + input_button_id = 1 |
| 54 | + vjoy_output = 1 |
| 55 | + cached_value = True |
| 56 | + vjoy_control_device.button(index=input_button_id).is_pressed = True |
| 57 | + tester.assert_button_eventually_equals( |
| 58 | + vjoy_di_device.device_guid, input_button_id, vjoy_output |
| 59 | + ) |
| 60 | + tester.assert_cached_button_eventually_equals( |
| 61 | + vjoy_di_device.device_guid.uuid, input_button_id, cached_value |
| 62 | + ) |
| 63 | + |
| 64 | + def assert_fixed_outputs(step: int): |
| 65 | + with subtests.test("hat northeast", step=step): |
| 66 | + tester.assert_hat_eventually_equals(vjoy_di_device.device_guid, 1, 4500) |
| 67 | + with subtests.test("hat west", step=step): |
| 68 | + tester.assert_hat_eventually_equals( |
| 69 | + vjoy_di_device.device_guid, 2, 27000 |
| 70 | + ) |
| 71 | + with subtests.test("button", step=step): |
| 72 | + tester.assert_button_eventually_equals(vjoy_di_device.device_guid, 3, 1) |
| 73 | + with subtests.test("axis absolute positive", step=step): |
| 74 | + tester.assert_axis_eventually_equals( |
| 75 | + vjoy_di_device.device_guid, 2, tester.AXIS_MAX_INT * 0.7 |
| 76 | + ) |
| 77 | + with subtests.test("axis absolute negative", step=step): |
| 78 | + tester.assert_axis_eventually_equals( |
| 79 | + vjoy_di_device.device_guid, 3, tester.AXIS_MAX_INT * -0.5 |
| 80 | + ) |
| 81 | + |
| 82 | + assert_fixed_outputs(step=1) |
| 83 | + with subtests.test("axis relative positive", step=1): |
| 84 | + tester.assert_axis_eventually_equals( |
| 85 | + vjoy_di_device.device_guid, 5, tester.AXIS_MAX_INT * 0.2 |
| 86 | + ) |
| 87 | + with subtests.test("axis relative negative", step=1): |
| 88 | + tester.assert_axis_eventually_equals( |
| 89 | + vjoy_di_device.device_guid, 6, tester.AXIS_MAX_INT * -0.1 |
| 90 | + ) |
| 91 | + |
| 92 | + vjoy_control_device.button(index=input_button_id).is_pressed = False |
| 93 | + assert_fixed_outputs(step=2) |
| 94 | + with subtests.test("axis relative positive", step=2): |
| 95 | + tester.assert_axis_eventually_equals( |
| 96 | + vjoy_di_device.device_guid, 5, tester.AXIS_MAX_INT * 0.2 |
| 97 | + ) |
| 98 | + with subtests.test("axis relative negative", step=2): |
| 99 | + tester.assert_axis_eventually_equals( |
| 100 | + vjoy_di_device.device_guid, 6, tester.AXIS_MAX_INT * -0.1 |
| 101 | + ) |
| 102 | + |
| 103 | + vjoy_control_device.button(index=input_button_id).is_pressed = True |
| 104 | + assert_fixed_outputs(step=3) |
| 105 | + with subtests.test("axis relative positive", step=3): |
| 106 | + tester.assert_axis_eventually_equals( |
| 107 | + vjoy_di_device.device_guid, 5, tester.AXIS_MAX_INT * 0.2 * 2 |
| 108 | + ) |
| 109 | + with subtests.test("axis relative negative", step=3): |
| 110 | + tester.assert_axis_eventually_equals( |
| 111 | + vjoy_di_device.device_guid, 6, tester.AXIS_MAX_INT * -0.1 * 2 |
| 112 | + ) |
| 113 | + |
| 114 | + vjoy_control_device.button(index=input_button_id).is_pressed = False |
| 115 | + assert_fixed_outputs(step=4) |
| 116 | + vjoy_control_device.button(index=input_button_id).is_pressed = True |
| 117 | + assert_fixed_outputs(step=5) |
| 118 | + with subtests.test("axis relative positive", step=5): |
| 119 | + tester.assert_axis_eventually_equals( |
| 120 | + vjoy_di_device.device_guid, 5, tester.AXIS_MAX_INT * 0.2 * 3 |
| 121 | + ) |
| 122 | + with subtests.test("axis relative negative", step=5): |
| 123 | + tester.assert_axis_eventually_equals( |
| 124 | + vjoy_di_device.device_guid, 6, tester.AXIS_MAX_INT * -0.1 * 3 |
| 125 | + ) |
0 commit comments