Fix button timing and event decoding and avoid redundant menu refreshes
This commit is contained in:
8 files changed
+276
-44
No files matched your search
@@ -0,0 +1,85 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestInputEventRecordSizes(t *testing.T) {
|
||||
for _, wordSize := range []int{4, 8} {
|
||||
var stream bytes.Buffer
|
||||
for _, value := range []int32{1, 0} {
|
||||
if wordSize == 4 {
|
||||
binary.Write(&stream, binary.LittleEndian, uint32(100))
|
||||
binary.Write(&stream, binary.LittleEndian, uint32(950000))
|
||||
} else {
|
||||
binary.Write(&stream, binary.LittleEndian, uint64(100))
|
||||
binary.Write(&stream, binary.LittleEndian, uint64(950000))
|
||||
}
|
||||
binary.Write(&stream, binary.LittleEndian, uint16(1))
|
||||
binary.Write(&stream, binary.LittleEndian, uint16(28))
|
||||
binary.Write(&stream, binary.LittleEndian, value)
|
||||
}
|
||||
for _, value := range []int32{1, 0} {
|
||||
event, err := readLinuxInputEvent(&stream, wordSize)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if event.Type != 1 || event.Code != 28 || event.Value != value {
|
||||
t.Fatalf("wordSize=%d: misaligned event %+v", wordSize, event)
|
||||
}
|
||||
if !event.Time().Equal(time.Unix(100, 950000000)) {
|
||||
t.Fatal("microseconds decoded incorrectly")
|
||||
}
|
||||
}
|
||||
if _, err := readLinuxInputEvent(&stream, wordSize); !errors.Is(err, io.EOF) {
|
||||
t.Fatalf("got %v, want EOF", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestButtonShortPressAcrossSecondBoundary(t *testing.T) {
|
||||
var b buttonState
|
||||
b.press((linuxInputEvent{Secs: 100, USecs: 950000}).Time())
|
||||
duration, ok := b.release((linuxInputEvent{Secs: 101, USecs: 50000}).Time())
|
||||
if !ok || duration != 100*time.Millisecond {
|
||||
t.Fatalf("got %s, %v, want 100ms short press", duration, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestButtonIgnoresDuplicateAndBouncedEvents(t *testing.T) {
|
||||
var b buttonState
|
||||
start := time.Unix(100, 0)
|
||||
if _, ok := b.release(start); ok {
|
||||
t.Fatal("unpaired release produced a click")
|
||||
}
|
||||
b.press(start)
|
||||
b.press(start.Add(200 * time.Millisecond))
|
||||
if duration, ok := b.release(start.Add(500 * time.Millisecond)); !ok || duration != 500*time.Millisecond {
|
||||
t.Fatal("repeated press reset the long-press timer")
|
||||
}
|
||||
if _, ok := b.release(start.Add(501 * time.Millisecond)); ok {
|
||||
t.Fatal("duplicate release produced a second click")
|
||||
}
|
||||
b.press(start.Add(505 * time.Millisecond))
|
||||
if _, ok := b.release(start.Add(510 * time.Millisecond)); ok {
|
||||
t.Fatal("contact bounce produced another click")
|
||||
}
|
||||
b.press(start.Add(time.Second))
|
||||
if duration, ok := b.release(start.Add(1100 * time.Millisecond)); !ok || duration != 100*time.Millisecond {
|
||||
t.Fatal("subsequent intentional click lost")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTruncatedInputEvent(t *testing.T) {
|
||||
for _, wordSize := range []int{4, 8} {
|
||||
_, err := readLinuxInputEvent(bytes.NewReader(make([]byte, 2*wordSize+7)), wordSize)
|
||||
if !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
t.Fatalf("got %v, want unexpected EOF", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user