Add tests for RuleTargetAxis.
This commit is contained in:
parent
8f3b8f4b47
commit
6646044d28
8 changed files with 164 additions and 9 deletions
136
internal/mappingrules/rule_target_axis_test.go
Normal file
136
internal/mappingrules/rule_target_axis_test.go
Normal file
|
@ -0,0 +1,136 @@
|
|||
package mappingrules
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/holoplot/go-evdev"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type RuleTargetAxisTests struct {
|
||||
suite.Suite
|
||||
mock *InputDeviceMock
|
||||
call *mock.Call
|
||||
}
|
||||
|
||||
type InputDeviceMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *InputDeviceMock) AbsInfos() (map[evdev.EvCode]evdev.AbsInfo, error) {
|
||||
args := m.Called()
|
||||
return args.Get(0).(map[evdev.EvCode]evdev.AbsInfo), args.Error(1)
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) SetupSuite() {
|
||||
t.mock = new(InputDeviceMock)
|
||||
t.call = t.mock.On("AbsInfos").Return(map[evdev.EvCode]evdev.AbsInfo{
|
||||
evdev.ABS_X: {
|
||||
Minimum: 0,
|
||||
Maximum: 10000,
|
||||
},
|
||||
evdev.ABS_Y: {
|
||||
Minimum: -10000,
|
||||
Maximum: 10000,
|
||||
},
|
||||
}, nil)
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) TearDownSuite() {
|
||||
t.call.Unset()
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) TestNewRuleTargetAxis() {
|
||||
// RuleTargets should get created
|
||||
ruleTarget, err := NewRuleTargetAxis("", t.mock, evdev.ABS_X, false, 0, 0)
|
||||
t.Nil(err)
|
||||
t.EqualValues(10000, ruleTarget.axisSize)
|
||||
|
||||
ruleTarget, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, 0, 0)
|
||||
t.Nil(err)
|
||||
t.EqualValues(20000, ruleTarget.axisSize)
|
||||
|
||||
// Creating a rule with a deadzone should work and reduce the axisSize
|
||||
ruleTarget, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, -500, 500)
|
||||
t.Nil(err)
|
||||
t.EqualValues(19000, ruleTarget.axisSize)
|
||||
t.EqualValues(-500, ruleTarget.DeadzoneStart)
|
||||
t.EqualValues(500, ruleTarget.DeadzoneEnd)
|
||||
|
||||
// Creating a rule with a deadzone should fail if end > start
|
||||
_, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, 500, -500)
|
||||
t.NotNil(err)
|
||||
|
||||
// Creating a rule on a non-existent axis should err
|
||||
_, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Z, false, 0, 0)
|
||||
t.NotNil(err)
|
||||
}
|
||||
|
||||
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.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.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)))
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) TestMatchEvent() {
|
||||
ruleTarget, _ := NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, -500, 500)
|
||||
validEvent := &evdev.InputEvent{
|
||||
Type: evdev.EV_ABS,
|
||||
Code: evdev.ABS_Y,
|
||||
Value: 800,
|
||||
}
|
||||
deadzoneEvent := &evdev.InputEvent{
|
||||
Type: evdev.EV_ABS,
|
||||
Code: evdev.ABS_Y,
|
||||
Value: 200,
|
||||
}
|
||||
|
||||
// An event on the correct device and axis should match
|
||||
t.True(ruleTarget.MatchEvent(t.mock, validEvent))
|
||||
|
||||
// A value on the wrong device should not match
|
||||
t.False(ruleTarget.MatchEvent(&evdev.InputDevice{}, validEvent))
|
||||
|
||||
// A value in the deadzone should not match
|
||||
t.False(ruleTarget.MatchEvent(t.mock, deadzoneEvent))
|
||||
}
|
||||
|
||||
func (t *RuleTargetAxisTests) TestCreateEvent() {
|
||||
ruleTarget, _ := NewRuleTargetAxis("", t.mock, evdev.ABS_X, false, 0, 0)
|
||||
expected := &evdev.InputEvent{
|
||||
Type: evdev.EV_ABS,
|
||||
Code: evdev.ABS_X,
|
||||
}
|
||||
|
||||
// Basic event creation
|
||||
testValue := int32(3928) // Arbitrarily chosen test value
|
||||
expected.Value = testValue
|
||||
t.EqualValues(expected, ruleTarget.CreateEvent(testValue, nil))
|
||||
|
||||
// Validate axis clamping
|
||||
testValue = int32(64000)
|
||||
expected.Value = MaxAxisValue
|
||||
t.EqualValues(expected, ruleTarget.CreateEvent(testValue, nil))
|
||||
|
||||
testValue = int32(-64000)
|
||||
expected.Value = MinAxisValue
|
||||
t.EqualValues(expected, ruleTarget.CreateEvent(testValue, nil))
|
||||
}
|
||||
|
||||
func TestRunnerRuleTargetAxisTests(t *testing.T) {
|
||||
suite.Run(t, new(RuleTargetAxisTests))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue