cbind/configuration_test.go

104 lines
2.4 KiB
Go
Raw Normal View History

2020-01-22 00:01:30 +00:00
package cbind
import (
"sync"
"testing"
"time"
2020-08-26 21:45:15 +00:00
"github.com/gdamore/tcell/v2"
2020-01-22 00:01:30 +00:00
)
const pressTimes = 7
func TestConfiguration(t *testing.T) {
t.Parallel()
wg := make([]*sync.WaitGroup, len(testCases))
config := NewConfiguration()
for i, c := range testCases {
wg[i] = new(sync.WaitGroup)
wg[i].Add(pressTimes)
i := i // Capture
if c.key != tcell.KeyRune {
config.SetKey(c.mod, c.key, func(ev *tcell.EventKey) *tcell.EventKey {
wg[i].Done()
return nil
})
} else {
config.SetRune(c.mod, c.ch, func(ev *tcell.EventKey) *tcell.EventKey {
wg[i].Done()
return nil
})
}
}
done := make(chan struct{})
timeout := time.After(5 * time.Second)
go func() {
for i := range testCases {
wg[i].Wait()
}
done <- struct{}{}
}()
go func() {
for j := 0; j < pressTimes; j++ {
2020-02-07 01:37:48 +00:00
for i, c := range testCases {
k := tcell.NewEventKey(c.key, c.ch, c.mod)
if k.Key() != c.key {
2020-02-08 14:08:29 +00:00
t.Fatalf("failed to test capturing keybinds: tcell modified EventKey.Key: expected %d, got %d", c.key, k.Key())
2020-02-07 01:37:48 +00:00
} else if k.Rune() != c.ch {
2020-02-08 14:08:29 +00:00
t.Fatalf("failed to test capturing keybinds: tcell modified EventKey.Rune: expected %d, got %d", c.ch, k.Rune())
2020-02-07 01:37:48 +00:00
} else if k.Modifiers() != c.mod {
2020-02-08 14:08:29 +00:00
t.Fatalf("failed to test capturing keybinds: tcell modified EventKey.Modifiers: expected %d, got %d", c.mod, k.Modifiers())
2020-02-07 01:37:48 +00:00
}
ev := config.Capture(tcell.NewEventKey(c.key, c.ch, c.mod))
if ev != nil {
t.Fatalf("failed to test capturing keybinds: failed to register case %d event %d %d %d", i, c.mod, c.key, c.ch)
}
2020-01-22 00:01:30 +00:00
}
}
}()
select {
case <-timeout:
t.Error("timeout")
case <-done:
// Wait at least one second to catch problems before exiting.
<-time.After(1 * time.Second)
}
}
// Example of creating and using an input configuration.
func ExampleNewConfiguration() {
// Create a new input configuration to store the keybinds.
c := NewConfiguration()
2020-02-07 01:37:48 +00:00
// Set keybind Alt+s.
2020-01-22 00:01:30 +00:00
c.SetRune(tcell.ModAlt, 's', func(ev *tcell.EventKey) *tcell.EventKey {
// Save
return nil
})
2020-02-07 01:37:48 +00:00
// Set keybind Alt+o.
2020-01-22 00:01:30 +00:00
c.SetRune(tcell.ModAlt, 'o', func(ev *tcell.EventKey) *tcell.EventKey {
// Open
return nil
})
// Set keybind Escape.
c.SetKey(tcell.ModNone, tcell.KeyEscape, func(ev *tcell.EventKey) *tcell.EventKey {
// Exit
return nil
})
// Before calling Application.Run, call Application.SetInputCapture:
// app.SetInputCapture(c.Capture)
}