Don't fail target creation when AbsInfo errors.

This commit is contained in:
Anna Rose Wiggins 2025-07-10 18:15:11 -04:00
parent 6646044d28
commit 681e1fef70
2 changed files with 30 additions and 14 deletions

View file

@ -1,6 +1,7 @@
package mappingrules
import (
"errors"
"testing"
"github.com/holoplot/go-evdev"
@ -23,7 +24,7 @@ func (m *InputDeviceMock) AbsInfos() (map[evdev.EvCode]evdev.AbsInfo, error) {
return args.Get(0).(map[evdev.EvCode]evdev.AbsInfo), args.Error(1)
}
func (t *RuleTargetAxisTests) SetupSuite() {
func (t *RuleTargetAxisTests) SetupTest() {
t.mock = new(InputDeviceMock)
t.call = t.mock.On("AbsInfos").Return(map[evdev.EvCode]evdev.AbsInfo{
evdev.ABS_X: {
@ -37,7 +38,7 @@ func (t *RuleTargetAxisTests) SetupSuite() {
}, nil)
}
func (t *RuleTargetAxisTests) TearDownSuite() {
func (t *RuleTargetAxisTests) TearDownTest() {
t.call.Unset()
}
@ -65,25 +66,32 @@ func (t *RuleTargetAxisTests) TestNewRuleTargetAxis() {
// Creating a rule on a non-existent axis should err
_, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Z, false, 0, 0)
t.NotNil(err)
// If Absinfo has an error, we should create a device with permissive bounds
t.call.Unset()
t.mock.On("AbsInfos").Return(nil, errors.New("Test Error"))
ruleTarget, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, -500, 500)
t.Nil(err)
t.Equal(AxisValueMax-AxisValueMin, ruleTarget.axisSize)
}
func (t *RuleTargetAxisTests) TestNormalizeValue() {
// Basic normalization should work
ruleTarget, _ := NewRuleTargetAxis("", t.mock, evdev.ABS_X, false, 0, 0)
t.Equal(MaxAxisValue, ruleTarget.NormalizeValue(int32(10000)))
t.Equal(MinAxisValue, ruleTarget.NormalizeValue(int32(0)))
t.Equal(AxisValueMax, ruleTarget.NormalizeValue(int32(10000)))
t.Equal(AxisValueMin, ruleTarget.NormalizeValue(int32(0)))
t.EqualValues(0, ruleTarget.NormalizeValue(int32(5000)))
// Normalization with a deadzone should work
ruleTarget, _ = NewRuleTargetAxis("", t.mock, evdev.ABS_X, false, 0, 5000)
t.Equal(MaxAxisValue, ruleTarget.NormalizeValue(int32(10000)))
t.Equal(AxisValueMax, ruleTarget.NormalizeValue(int32(10000)))
t.True(ruleTarget.NormalizeValue(int32(5001)) < int32(-31000))
t.EqualValues(0, ruleTarget.NormalizeValue(int32(7500)))
// Normalization on an inverted axis should work
ruleTarget, _ = NewRuleTargetAxis("", t.mock, evdev.ABS_X, true, 0, 0)
t.Equal(MaxAxisValue, ruleTarget.NormalizeValue(int32(0)))
t.Equal(MinAxisValue, ruleTarget.NormalizeValue(int32(10000)))
t.Equal(AxisValueMax, ruleTarget.NormalizeValue(int32(0)))
t.Equal(AxisValueMin, ruleTarget.NormalizeValue(int32(10000)))
}
func (t *RuleTargetAxisTests) TestMatchEvent() {
@ -123,11 +131,11 @@ func (t *RuleTargetAxisTests) TestCreateEvent() {
// Validate axis clamping
testValue = int32(64000)
expected.Value = MaxAxisValue
expected.Value = AxisValueMax
t.EqualValues(expected, ruleTarget.CreateEvent(testValue, nil))
testValue = int32(-64000)
expected.Value = MinAxisValue
expected.Value = AxisValueMin
t.EqualValues(expected, ruleTarget.CreateEvent(testValue, nil))
}