2014-04-27 18:02:39 -07:00
|
|
|
package joystick
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
import (
|
2017-04-06 10:00:58 +02:00
|
|
|
"strings"
|
2014-06-13 12:54:21 -07:00
|
|
|
"testing"
|
2014-07-22 13:55:19 -07:00
|
|
|
"time"
|
|
|
|
|
2017-03-27 14:10:37 -04:00
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2016-09-25 21:07:36 +02:00
|
|
|
var _ gobot.Driver = (*Driver)(nil)
|
2016-08-26 14:23:03 +02:00
|
|
|
|
2016-09-25 21:07:36 +02:00
|
|
|
func initTestDriver() *Driver {
|
|
|
|
a := NewAdaptor()
|
|
|
|
a.connect = func(j *Adaptor) (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()
|
2016-09-25 21:07:36 +02:00
|
|
|
d := NewDriver(a, "./configs/xbox360_power_a_mini_proex.json")
|
2014-07-22 13:55:19 -07:00
|
|
|
d.poll = func() sdl.Event {
|
2018-05-25 12:42:29 +02:00
|
|
|
return nil
|
2014-07-22 13:55:19 -07:00
|
|
|
}
|
|
|
|
return d
|
2014-06-13 12:54:21 -07:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2017-04-06 10:00:58 +02:00
|
|
|
func TestJoystickDriverName(t *testing.T) {
|
|
|
|
d := initTestDriver()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Joystick"), true)
|
|
|
|
d.SetName("NewName")
|
|
|
|
gobottest.Assert(t, d.Name(), "NewName")
|
|
|
|
}
|
|
|
|
|
2016-09-25 21:07:36 +02:00
|
|
|
func TestDriverStart(t *testing.T) {
|
|
|
|
d := initTestDriver()
|
2014-11-22 19:54:32 -08:00
|
|
|
d.interval = 1 * time.Millisecond
|
2016-11-07 19:32:12 +01:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
|
|
|
time.Sleep(2 * time.Millisecond)
|
2014-06-13 12:54:21 -07:00
|
|
|
}
|
|
|
|
|
2016-09-25 21:07:36 +02:00
|
|
|
func TestDriverHalt(t *testing.T) {
|
|
|
|
d := initTestDriver()
|
2014-12-23 01:20:44 -08:00
|
|
|
go func() {
|
|
|
|
<-d.halt
|
|
|
|
}()
|
2016-11-07 19:32:12 +01:00
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2014-06-13 12:54:21 -07:00
|
|
|
}
|
|
|
|
|
2016-09-25 21:07:36 +02:00
|
|
|
func TestDriverHandleEvent(t *testing.T) {
|
2014-07-22 13:55:19 -07:00
|
|
|
sem := make(chan bool)
|
2016-09-25 21:07:36 +02:00
|
|
|
d := initTestDriver()
|
2014-11-19 15:45:59 -08:00
|
|
|
d.Start()
|
2015-07-15 13:00:01 -07:00
|
|
|
|
|
|
|
// left x stick
|
2016-08-30 17:10:50 +02:00
|
|
|
d.On(d.Event("left_x"), func(data interface{}) {
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, int16(100), data.(int16))
|
2015-07-15 13:00:01 -07:00
|
|
|
sem <- true
|
|
|
|
})
|
2014-07-22 13:55:19 -07:00
|
|
|
d.handleEvent(&sdl.JoyAxisEvent{
|
|
|
|
Which: 0,
|
|
|
|
Axis: 0,
|
|
|
|
Value: 100,
|
|
|
|
})
|
2015-07-15 13:00:01 -07:00
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Errorf("Button Event \"left_x\" was not published")
|
|
|
|
}
|
|
|
|
|
|
|
|
// x button press
|
2016-08-30 17:10:50 +02:00
|
|
|
d.On(d.Event("x_press"), func(data interface{}) {
|
2014-07-22 13:55:19 -07:00
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
d.handleEvent(&sdl.JoyButtonEvent{
|
|
|
|
Which: 0,
|
|
|
|
Button: 2,
|
|
|
|
State: 1,
|
|
|
|
})
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2015-07-15 13:00:01 -07:00
|
|
|
case <-time.After(10 * time.Second):
|
2014-07-22 13:55:19 -07:00
|
|
|
t.Errorf("Button Event \"x_press\" was not published")
|
|
|
|
}
|
2015-07-15 13:00:01 -07:00
|
|
|
|
|
|
|
// x button release
|
2016-08-30 17:10:50 +02:00
|
|
|
d.On(d.Event("x_release"), func(data interface{}) {
|
2015-07-15 13:00:01 -07:00
|
|
|
sem <- true
|
|
|
|
})
|
2014-07-22 13:55:19 -07:00
|
|
|
d.handleEvent(&sdl.JoyButtonEvent{
|
|
|
|
Which: 0,
|
|
|
|
Button: 2,
|
|
|
|
State: 0,
|
|
|
|
})
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2015-07-15 13:00:01 -07:00
|
|
|
case <-time.After(10 * time.Second):
|
2014-07-22 13:55:19 -07:00
|
|
|
t.Errorf("Button Event \"x_release\" was not published")
|
|
|
|
}
|
2015-07-15 13:00:01 -07:00
|
|
|
|
|
|
|
// down button press
|
2018-05-25 01:07:07 -07:00
|
|
|
d.On(d.Event("down_press"), func(data interface{}) {
|
2015-07-15 13:00:01 -07:00
|
|
|
sem <- true
|
|
|
|
})
|
2014-07-22 13:55:19 -07:00
|
|
|
d.handleEvent(&sdl.JoyHatEvent{
|
|
|
|
Which: 0,
|
|
|
|
Hat: 0,
|
|
|
|
Value: 4,
|
|
|
|
})
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2015-07-15 13:00:01 -07:00
|
|
|
case <-time.After(10 * time.Second):
|
2014-07-22 13:55:19 -07:00
|
|
|
t.Errorf("Hat Event \"down\" was not published")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.handleEvent(&sdl.JoyHatEvent{
|
|
|
|
Which: 0,
|
|
|
|
Hat: 99,
|
|
|
|
Value: 4,
|
|
|
|
})
|
|
|
|
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, err.Error(), "Unknown Hat: 99 4")
|
2014-07-22 13:55:19 -07:00
|
|
|
|
|
|
|
err = d.handleEvent(&sdl.JoyAxisEvent{
|
|
|
|
Which: 0,
|
|
|
|
Axis: 99,
|
|
|
|
Value: 100,
|
|
|
|
})
|
|
|
|
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, err.Error(), "Unknown Axis: 99")
|
2014-07-22 13:55:19 -07:00
|
|
|
|
|
|
|
err = d.handleEvent(&sdl.JoyButtonEvent{
|
|
|
|
Which: 0,
|
|
|
|
Button: 99,
|
|
|
|
State: 0,
|
|
|
|
})
|
|
|
|
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, err.Error(), "Unknown Button: 99")
|
2014-06-13 12:54:21 -07:00
|
|
|
}
|