1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/platforms/joystick/joystick_driver_test.go

115 lines
2.3 KiB
Go
Raw Normal View History

2014-04-27 18:02:39 -07:00
package joystick
import (
2014-06-13 12:54:21 -07:00
"testing"
2014-07-22 13:55:19 -07:00
"time"
"github.com/hybridgroup/go-sdl2/sdl"
"github.com/hybridgroup/gobot"
)
2014-06-13 16:01:39 -07:00
func initTestJoystickDriver() *JoystickDriver {
2014-07-22 13:55:19 -07:00
a := NewJoystickAdaptor("bot")
2014-11-19 15:45:59 -08:00
a.connect = func(j *JoystickAdaptor) (err error) {
2014-07-22 13:55:19 -07:00
j.joystick = &testJoystick{}
2014-11-19 15:45:59 -08:00
return nil
2014-07-22 13:55:19 -07:00
}
a.Connect()
d := NewJoystickDriver(a, "bot", "./configs/xbox360_power_a_mini_proex.json")
d.poll = func() sdl.Event {
return new(interface{})
}
return d
2014-06-13 12:54:21 -07:00
}
2014-06-13 16:01:39 -07:00
func TestJoystickDriverStart(t *testing.T) {
d := initTestJoystickDriver()
d.interval = 1 * time.Millisecond
gobot.Assert(t, len(d.Start()), 0)
2014-07-22 13:55:19 -07:00
<-time.After(2 * time.Millisecond)
2014-06-13 12:54:21 -07:00
}
2014-06-13 16:01:39 -07:00
func TestJoystickDriverHalt(t *testing.T) {
d := initTestJoystickDriver()
gobot.Assert(t, len(d.Halt()), 0)
2014-06-13 12:54:21 -07:00
}
2014-07-22 13:55:19 -07:00
func TestJoystickDriverHandleEvent(t *testing.T) {
sem := make(chan bool)
2014-06-13 16:01:39 -07:00
d := initTestJoystickDriver()
2014-11-19 15:45:59 -08:00
d.Start()
2014-07-22 13:55:19 -07:00
d.handleEvent(&sdl.JoyAxisEvent{
Which: 0,
Axis: 0,
Value: 100,
})
gobot.On(d.Event("left_x"), func(data interface{}) {
gobot.Assert(t, int16(100), data.(int16))
sem <- true
})
<-sem
d.handleEvent(&sdl.JoyButtonEvent{
Which: 0,
Button: 2,
State: 1,
})
gobot.On(d.Event("x_press"), func(data interface{}) {
sem <- true
})
select {
case <-sem:
case <-time.After(10 * time.Millisecond):
t.Errorf("Button Event \"x_press\" was not published")
}
d.handleEvent(&sdl.JoyButtonEvent{
Which: 0,
Button: 2,
State: 0,
})
gobot.On(d.Event("x_release"), func(data interface{}) {
sem <- true
})
select {
case <-sem:
case <-time.After(10 * time.Millisecond):
t.Errorf("Button Event \"x_release\" was not published")
}
d.handleEvent(&sdl.JoyHatEvent{
Which: 0,
Hat: 0,
Value: 4,
})
gobot.On(d.Event("down"), func(data interface{}) {
sem <- true
})
select {
case <-sem:
case <-time.After(10 * time.Millisecond):
t.Errorf("Hat Event \"down\" was not published")
}
err := d.handleEvent(&sdl.JoyHatEvent{
Which: 0,
Hat: 99,
Value: 4,
})
gobot.Assert(t, err.Error(), "Unknown Hat: 99 4")
err = d.handleEvent(&sdl.JoyAxisEvent{
Which: 0,
Axis: 99,
Value: 100,
})
gobot.Assert(t, err.Error(), "Unknown Axis: 99")
err = d.handleEvent(&sdl.JoyButtonEvent{
Which: 0,
Button: 99,
State: 0,
})
gobot.Assert(t, err.Error(), "Unknown Button: 99")
2014-06-13 12:54:21 -07:00
}