hybridgroup.gobot/platforms/joystick/joystick_driver_test.go

133 lines
2.6 KiB
Go
Raw Normal View History

2014-04-28 09:02:39 +08:00
package joystick
import (
2014-06-14 03:54:21 +08:00
"testing"
2014-07-23 04:55:19 +08:00
"time"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/gobottest"
"github.com/veandco/go-sdl2/sdl"
)
var _ gobot.Driver = (*JoystickDriver)(nil)
2014-06-14 07:01:39 +08:00
func initTestJoystickDriver() *JoystickDriver {
2014-07-23 04:55:19 +08:00
a := NewJoystickAdaptor("bot")
2014-11-20 07:45:59 +08:00
a.connect = func(j *JoystickAdaptor) (err error) {
2014-07-23 04:55:19 +08:00
j.joystick = &testJoystick{}
2014-11-20 07:45:59 +08:00
return nil
2014-07-23 04:55:19 +08: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-14 03:54:21 +08:00
}
2014-06-14 07:01:39 +08:00
func TestJoystickDriverStart(t *testing.T) {
d := initTestJoystickDriver()
d.interval = 1 * time.Millisecond
gobottest.Assert(t, len(d.Start()), 0)
2014-07-23 04:55:19 +08:00
<-time.After(2 * time.Millisecond)
2014-06-14 03:54:21 +08:00
}
2014-06-14 07:01:39 +08:00
func TestJoystickDriverHalt(t *testing.T) {
d := initTestJoystickDriver()
2014-12-23 17:20:44 +08:00
go func() {
<-d.halt
}()
gobottest.Assert(t, len(d.Halt()), 0)
2014-06-14 03:54:21 +08:00
}
2014-07-23 04:55:19 +08:00
func TestJoystickDriverHandleEvent(t *testing.T) {
sem := make(chan bool)
2014-06-14 07:01:39 +08:00
d := initTestJoystickDriver()
2014-11-20 07:45:59 +08:00
d.Start()
2015-07-16 04:00:01 +08:00
// left x stick
gobot.On(d.Event("left_x"), func(data interface{}) {
gobottest.Assert(t, int16(100), data.(int16))
2015-07-16 04:00:01 +08:00
sem <- true
})
2014-07-23 04:55:19 +08:00
d.handleEvent(&sdl.JoyAxisEvent{
Which: 0,
Axis: 0,
Value: 100,
})
2015-07-16 04:00:01 +08:00
select {
case <-sem:
case <-time.After(10 * time.Second):
t.Errorf("Button Event \"left_x\" was not published")
}
// x button press
gobot.On(d.Event("x_press"), func(data interface{}) {
2014-07-23 04:55:19 +08:00
sem <- true
})
d.handleEvent(&sdl.JoyButtonEvent{
Which: 0,
Button: 2,
State: 1,
})
select {
case <-sem:
2015-07-16 04:00:01 +08:00
case <-time.After(10 * time.Second):
2014-07-23 04:55:19 +08:00
t.Errorf("Button Event \"x_press\" was not published")
}
2015-07-16 04:00:01 +08:00
// x button release
gobot.On(d.Event("x_release"), func(data interface{}) {
sem <- true
})
2014-07-23 04:55:19 +08:00
d.handleEvent(&sdl.JoyButtonEvent{
Which: 0,
Button: 2,
State: 0,
})
select {
case <-sem:
2015-07-16 04:00:01 +08:00
case <-time.After(10 * time.Second):
2014-07-23 04:55:19 +08:00
t.Errorf("Button Event \"x_release\" was not published")
}
2015-07-16 04:00:01 +08:00
// down button press
gobot.On(d.Event("down"), func(data interface{}) {
sem <- true
})
2014-07-23 04:55:19 +08:00
d.handleEvent(&sdl.JoyHatEvent{
Which: 0,
Hat: 0,
Value: 4,
})
select {
case <-sem:
2015-07-16 04:00:01 +08:00
case <-time.After(10 * time.Second):
2014-07-23 04:55:19 +08:00
t.Errorf("Hat Event \"down\" was not published")
}
err := d.handleEvent(&sdl.JoyHatEvent{
Which: 0,
Hat: 99,
Value: 4,
})
gobottest.Assert(t, err.Error(), "Unknown Hat: 99 4")
2014-07-23 04:55:19 +08:00
err = d.handleEvent(&sdl.JoyAxisEvent{
Which: 0,
Axis: 99,
Value: 100,
})
gobottest.Assert(t, err.Error(), "Unknown Axis: 99")
2014-07-23 04:55:19 +08:00
err = d.handleEvent(&sdl.JoyButtonEvent{
Which: 0,
Button: 99,
State: 0,
})
gobottest.Assert(t, err.Error(), "Unknown Button: 99")
2014-06-14 03:54:21 +08:00
}