joyful/internal/mappingrules/mapping_rule_axis_to_button_test.go

87 lines
2.3 KiB
Go

package mappingrules
import (
"testing"
"time"
"github.com/holoplot/go-evdev"
"github.com/stretchr/testify/suite"
)
type MappingRuleAxisToButtonTests struct {
suite.Suite
inputDevice *InputDeviceMock
inputRule *RuleTargetAxis
outputDevice *evdev.InputDevice
outputRule *RuleTargetButton
mode *string
base MappingRuleBase
}
func (t *MappingRuleAxisToButtonTests) 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,
},
}, nil)
t.inputRule, _ = NewRuleTargetAxis("test-input", t.inputDevice, evdev.ABS_X, false, int32(0), int32(1000))
t.outputDevice = &evdev.InputDevice{}
t.outputRule, _ = NewRuleTargetButton("test-output", t.outputDevice, evdev.ABS_X, false)
t.base = NewMappingRuleBase("", []string{"*"})
}
func (t *MappingRuleAxisToButtonTests) TestMatchEvent() {
testRule := NewMappingRuleAxisToButton(t.base, t.inputRule, t.outputRule, 0, 0)
// A valid input should set a nextevent
testRule.MatchEvent(t.inputDevice, &evdev.InputEvent{
Type: evdev.EV_ABS,
Code: evdev.ABS_X,
Value: 1001,
}, t.mode)
t.NotEqual(NoNextEvent, testRule.nextEvent)
// And a deadzone value should clear it
testRule.MatchEvent(t.inputDevice, &evdev.InputEvent{
Type: evdev.EV_ABS,
Code: evdev.ABS_X,
Value: 500,
}, t.mode)
t.Equal(NoNextEvent, testRule.nextEvent)
testRule = NewMappingRuleAxisToButton(t.base, t.inputRule, t.outputRule, 750, 250)
testRule.MatchEvent(t.inputDevice, &evdev.InputEvent{
Type: evdev.EV_ABS,
Code: evdev.ABS_X,
Value: 10000,
}, t.mode)
t.Equal(time.Duration(250*time.Millisecond), testRule.nextEvent)
testRule.MatchEvent(t.inputDevice, &evdev.InputEvent{
Type: evdev.EV_ABS,
Code: evdev.ABS_X,
Value: 1001,
}, t.mode)
t.True(testRule.nextEvent > time.Duration(700*time.Millisecond))
testRule.MatchEvent(t.inputDevice, &evdev.InputEvent{
Type: evdev.EV_ABS,
Code: evdev.ABS_X,
Value: 5500,
}, t.mode)
t.Equal(time.Duration(500*time.Millisecond), testRule.nextEvent)
}
// TODO: to add TimerEvent tests we need to use an interface to mock time.
// func (t *MappingRuleAxisToButtonTests) TestTimerEvent() {
// // STUB
// }
func TestRunnerMappingRuleAxisToButtonTests(t *testing.T) {
suite.Run(t, new(MappingRuleAxisToButtonTests))
}