Completed implementation.

This commit is contained in:
Anna Rose Wiggins 2025-07-15 15:27:49 -04:00
parent 0915ea059a
commit 58abd4cc34
10 changed files with 260 additions and 66 deletions

View file

@ -5,6 +5,7 @@ import (
"time"
"github.com/holoplot/go-evdev"
"github.com/jonboulle/clockwork"
"github.com/stretchr/testify/suite"
)
@ -36,52 +37,150 @@ func (t *MappingRuleAxisToButtonTests) SetupTest() {
}
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)
t.Run("No Repeat", func() {
testRule := NewMappingRuleAxisToButton(t.base, t.inputRule, t.outputRule, 0, 0)
// 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)
t.Run("Valid Input", func() {
testRule.MatchEvent(t.inputDevice, &evdev.InputEvent{
Type: evdev.EV_ABS,
Code: evdev.ABS_X,
Value: 1001,
}, t.mode)
t.NotEqual(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)
t.Run("Deadzone Input", func() {
testRule.MatchEvent(t.inputDevice, &evdev.InputEvent{
Type: evdev.EV_ABS,
Code: evdev.ABS_X,
Value: 500,
}, t.mode)
t.Equal(NoNextEvent, 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))
t.Run("Repeat", func() {
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: 5500,
}, t.mode)
t.Equal(time.Duration(500*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 (t *MappingRuleAxisToButtonTests) TestTimerEvent() {
t.Run("No Repeat", func() {
// Get event if called immediately
t.Run("Event is available immediately", func() {
testRule, _ := buildTimerRule(t, 0, 0, 0)
event := testRule.TimerEvent()
t.EqualValues(1, event.Value)
t.Equal(true, testRule.pressed)
})
// Off event on second call
t.Run("Event emits off on second call", func() {
testRule, _ := buildTimerRule(t, 0, 0, 0)
testRule.TimerEvent()
event := testRule.TimerEvent()
t.EqualValues(0, event.Value)
t.Equal(false, testRule.pressed)
})
// No further event, even if we wait a while
t.Run("Additional events are not emitted while still active.", func() {
testRule, mockClock := buildTimerRule(t, 0, 0, 0)
testRule.TimerEvent()
testRule.TimerEvent()
mockClock.Advance(10 * time.Millisecond)
event := testRule.TimerEvent()
t.Nil(event)
t.Equal(false, testRule.pressed)
})
})
t.Run("Repeat", func() {
t.Run("No event if called immediately", func() {
testRule, _ := buildTimerRule(t, 100, 10, 50*time.Millisecond)
event := testRule.TimerEvent()
t.Nil(event)
})
t.Run("No event after 49ms", func() {
testRule, mockClock := buildTimerRule(t, 100, 10, 50*time.Millisecond)
mockClock.Advance(49 * time.Millisecond)
event := testRule.TimerEvent()
t.Nil(event)
})
t.Run("Event after 50ms", func() {
testRule, mockClock := buildTimerRule(t, 100, 10, 50*time.Millisecond)
mockClock.Advance(50 * time.Millisecond)
event := testRule.TimerEvent()
t.EqualValues(1, event.Value)
t.Equal(true, testRule.pressed)
})
t.Run("Additional event at 100ms", func() {
testRule, mockClock := buildTimerRule(t, 100, 10, 50*time.Millisecond)
mockClock.Advance(50 * time.Millisecond)
testRule.TimerEvent()
testRule.TimerEvent()
mockClock.Advance(50 * time.Millisecond)
event := testRule.TimerEvent()
t.NotNil(event)
})
})
}
func TestRunnerMappingRuleAxisToButtonTests(t *testing.T) {
suite.Run(t, new(MappingRuleAxisToButtonTests))
}
// buildTimerRule creates a MappingRuleAxisToButton with a mocked clock
func buildTimerRule(t *MappingRuleAxisToButtonTests,
repeatMin,
repeatMax int,
nextEvent time.Duration) (*MappingRuleAxisToButton, *clockwork.FakeClock) {
mockClock := clockwork.NewFakeClock()
testRule := NewMappingRuleAxisToButton(t.base, t.inputRule, t.outputRule, repeatMin, repeatMax)
testRule.clock = mockClock
testRule.lastEvent = testRule.clock.Now()
testRule.nextEvent = nextEvent
if nextEvent != NoNextEvent {
testRule.active = true
}
return testRule, mockClock
}