Implement combined axis logic and tests.
This commit is contained in:
parent
a7e78c33f3
commit
49292ff13f
5 changed files with 192 additions and 65 deletions
|
@ -1,6 +1,7 @@
|
|||
package mappingrules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/holoplot/go-evdev"
|
||||
|
@ -29,43 +30,113 @@ func TestRunnerMappingRuleAxisCombined(t *testing.T) {
|
|||
func (t *MappingRuleAxisCombinedTests) SetupTest() {
|
||||
mode := "*"
|
||||
t.mode = &mode
|
||||
t.inputDevice = new(InputDeviceMock)
|
||||
t.inputDevice.On("AbsInfos").Return(map[evdev.EvCode]evdev.AbsInfo{
|
||||
evdev.ABS_X: {
|
||||
Minimum: 0,
|
||||
Maximum: 10000,
|
||||
},
|
||||
evdev.ABS_Y: {
|
||||
Minimum: 0,
|
||||
Maximum: 10000,
|
||||
},
|
||||
|
||||
t.inputDevice = NewInputDeviceMock()
|
||||
t.inputDevice.Stub("AbsInfos").Return(map[evdev.EvCode]evdev.AbsInfo{
|
||||
evdev.ABS_X: {Minimum: 0, Maximum: 10000},
|
||||
evdev.ABS_Y: {Minimum: 0, Maximum: 10000},
|
||||
}, nil)
|
||||
|
||||
t.inputTargetLower, _ = NewRuleTargetAxis("test-input", t.inputDevice, evdev.ABS_X, true, 0, 0)
|
||||
t.inputTargetUpper, _ = NewRuleTargetAxis("test-input", t.inputDevice, evdev.ABS_Y, false, 0, 0)
|
||||
|
||||
t.outputDevice = &evdev.InputDevice{}
|
||||
t.outputTarget, _ = NewRuleTargetAxis("test-output", t.outputDevice, evdev.ABS_X, false, 0, 0)
|
||||
|
||||
t.base = NewMappingRuleBase("", []string{"*"})
|
||||
|
||||
// We clear the AbsInfo call here so it can be cleanly set by the (sub-)tests
|
||||
t.inputDevice.Reset()
|
||||
}
|
||||
|
||||
func (t *MappingRuleAxisCombinedTests) TestMatchEventSplitAxis() {
|
||||
t.inputTargetLower, _ = NewRuleTargetAxisPartial("test-input", t.inputDevice, evdev.ABS_X, true, 0, 0, AxisValueMin, 0)
|
||||
t.inputTargetUpper, _ = NewRuleTargetAxisPartial("test-input", t.inputDevice, evdev.ABS_Y, false, 0, 0, 0, AxisValueMax)
|
||||
func (t *MappingRuleAxisCombinedTests) TearDownTest() {
|
||||
t.inputDevice.Reset()
|
||||
}
|
||||
|
||||
func (t *MappingRuleAxisCombinedTests) TearDownSubTest() {
|
||||
t.inputDevice.Reset()
|
||||
}
|
||||
|
||||
func (t *MappingRuleAxisCombinedTests) TestNewMappingRuleAxisCombined() {
|
||||
t.inputDevice.Stub("AbsInfos").Return(map[evdev.EvCode]evdev.AbsInfo{
|
||||
evdev.ABS_X: {Minimum: 0, Maximum: 10000},
|
||||
evdev.ABS_Y: {Minimum: 0, Maximum: 10000},
|
||||
}, nil)
|
||||
|
||||
rule := NewMappingRuleAxisCombined(t.base, t.inputTargetLower, t.inputTargetUpper, t.outputTarget)
|
||||
t.EqualValues(0, rule.InputLower.OutputMax)
|
||||
t.EqualValues(0, rule.InputUpper.OutputMin)
|
||||
}
|
||||
|
||||
func (t *MappingRuleAxisCombinedTests) TestMatchEvent() {
|
||||
rule := NewMappingRuleAxisCombined(t.base, t.inputTargetLower, t.inputTargetUpper, t.outputTarget)
|
||||
|
||||
t.Run("Lower Input", func() {
|
||||
device, event := rule.MatchEvent(t.inputDevice, &evdev.InputEvent{Type: evdev.EV_ABS, Code: evdev.ABS_X, Value: 10000}, t.mode)
|
||||
t.Equal(t.outputDevice, device)
|
||||
t.EqualValues(0, event.Value)
|
||||
testCases := []struct{ in, out int32 }{
|
||||
{10000, AxisValueMin},
|
||||
{0, 0},
|
||||
{5000, AxisValueMin / 2},
|
||||
}
|
||||
|
||||
_, event = rule.MatchEvent(t.inputDevice, &evdev.InputEvent{Type: evdev.EV_ABS, Code: evdev.ABS_X, Value: 0}, t.mode)
|
||||
t.EqualValues(AxisValueMin, event.Value)
|
||||
for _, testCase := range testCases {
|
||||
t.Run(fmt.Sprintf("%d->%d", testCase.in, testCase.out), func() {
|
||||
t.inputDevice.Stub("AbsInfos").Return(map[evdev.EvCode]evdev.AbsInfo{
|
||||
evdev.ABS_X: {Minimum: 0, Maximum: 10000, Value: testCase.in},
|
||||
evdev.ABS_Y: {Minimum: 0, Maximum: 10000, Value: 0},
|
||||
}, nil)
|
||||
|
||||
_, event = rule.MatchEvent(t.inputDevice, &evdev.InputEvent{Type: evdev.EV_ABS, Code: evdev.ABS_X, Value: 5000}, t.mode)
|
||||
t.EqualValues(0, event.Value)
|
||||
device, event := rule.MatchEvent(t.inputDevice, &evdev.InputEvent{Type: evdev.EV_ABS, Code: evdev.ABS_X, Value: testCase.in}, t.mode)
|
||||
|
||||
t.Equal(t.outputDevice, device)
|
||||
t.InDelta(testCase.out, event.Value, 1)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Upper Input Only", func() {
|
||||
t.Run("Upper Input", func() {
|
||||
testCases := []struct{ in, out int32 }{
|
||||
{10000, AxisValueMax},
|
||||
{0, 0},
|
||||
{5000, AxisValueMax / 2},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(fmt.Sprintf("%d->%d", testCase.in, testCase.out), func() {
|
||||
t.inputDevice.Stub("AbsInfos").Return(map[evdev.EvCode]evdev.AbsInfo{
|
||||
evdev.ABS_X: {Minimum: 0, Maximum: 10000, Value: 0},
|
||||
evdev.ABS_Y: {Minimum: 0, Maximum: 10000, Value: testCase.in},
|
||||
}, nil)
|
||||
|
||||
device, event := rule.MatchEvent(t.inputDevice, &evdev.InputEvent{Type: evdev.EV_ABS, Code: evdev.ABS_Y, Value: testCase.in}, t.mode)
|
||||
|
||||
t.Equal(t.outputDevice, device)
|
||||
t.InDelta(testCase.out, event.Value, 1)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Combined Inputs", func() {
|
||||
testCases := []struct{ x, y, out int32 }{
|
||||
{0, 0, 0},
|
||||
{5000, 5000, 0},
|
||||
{10000, 10000, 0},
|
||||
{5000, 10000, AxisValueMax / 2},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(fmt.Sprintf("%d,%d->%d", testCase.x, testCase.y, testCase.out), func() {
|
||||
t.inputDevice.Stub("AbsInfos").Return(map[evdev.EvCode]evdev.AbsInfo{
|
||||
evdev.ABS_X: {Minimum: 0, Maximum: 10000, Value: testCase.x},
|
||||
evdev.ABS_Y: {Minimum: 0, Maximum: 10000, Value: testCase.y},
|
||||
}, nil)
|
||||
|
||||
device, event := rule.MatchEvent(t.inputDevice, &evdev.InputEvent{Type: evdev.EV_ABS, Code: evdev.ABS_Y, Value: testCase.y}, t.mode)
|
||||
|
||||
t.Equal(t.outputDevice, device)
|
||||
t.InDelta(testCase.out, event.Value, 1)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// TODO: add tests for exception cases
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue