Skip to content

Commit

Permalink
Increase joystick test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jul 22, 2014
1 parent 59b5077 commit 9533443
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 53 deletions.
7 changes: 6 additions & 1 deletion platforms/joystick/joystick_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import (
"github.com/hybridgroup/gobot"
)

type joystick interface {
Close()
InstanceID() sdl.JoystickID
}

type JoystickAdaptor struct {
gobot.Adaptor
joystick *sdl.Joystick
joystick joystick
connect func(*JoystickAdaptor)
}

Expand Down
12 changes: 8 additions & 4 deletions platforms/joystick/joystick_adaptor_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package joystick

import (
"github.com/hybridgroup/gobot"
"testing"

"github.com/hybridgroup/gobot"
)

func initTestJoystickAdaptor() *JoystickAdaptor {
return NewJoystickAdaptor("bot")
a := NewJoystickAdaptor("bot")
a.connect = func(j *JoystickAdaptor) {
j.joystick = &testJoystick{}
}
return a
}

func TestJoystickAdaptorConnect(t *testing.T) {
t.SkipNow()
a := initTestJoystickAdaptor()
gobot.Assert(t, a.Connect(), true)
}

func TestJoystickAdaptorFinalize(t *testing.T) {
t.SkipNow()
a := initTestJoystickAdaptor()
a.Connect()
gobot.Assert(t, a.Finalize(), true)
}
94 changes: 53 additions & 41 deletions platforms/joystick/joystick_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package joystick

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"time"

"github.com/hybridgroup/go-sdl2/sdl"
"github.com/hybridgroup/gobot"
Expand All @@ -13,6 +13,7 @@ import (
type JoystickDriver struct {
gobot.Driver
config joystickConfig
poll func() sdl.Event
}

type pair struct {
Expand Down Expand Up @@ -41,6 +42,9 @@ func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *Joystick
"JoystickDriver",
a,
),
poll: func() sdl.Event {
return sdl.PollEvent()
},
}

file, e := ioutil.ReadFile(config)
Expand Down Expand Up @@ -68,50 +72,58 @@ func (j *JoystickDriver) adaptor() *JoystickAdaptor {
}

func (j *JoystickDriver) Start() bool {
go func() {
var event sdl.Event
for {
for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch data := event.(type) {
case *sdl.JoyAxisEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
axis := j.findName(data.Axis, j.config.Axis)
if axis == "" {
fmt.Println("Unknown Axis:", data.Axis)
} else {
gobot.Publish(j.Event(axis), data.Value)
}
}
case *sdl.JoyButtonEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
button := j.findName(data.Button, j.config.Buttons)
if button == "" {
fmt.Println("Unknown Button:", data.Button)
} else {
if data.State == 1 {
gobot.Publish(j.Event(fmt.Sprintf("%s_press", button)), nil)
} else {
gobot.Publish(j.Event(fmt.Sprintf("%s_release", button)), nil)
}
}
}
case *sdl.JoyHatEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
hat := j.findHatName(data.Value, data.Hat, j.config.Hats)
if hat == "" {
fmt.Println("Unknown Hat:", data.Hat, data.Value)
} else {
gobot.Publish(j.Event(hat), true)
}
}
gobot.Every(j.Interval(), func() {
event := j.poll()
if event != nil {
j.handleEvent(event)
}
})
return true
}

func (j *JoystickDriver) handleEvent(event sdl.Event) error {
switch data := event.(type) {
case *sdl.JoyAxisEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
axis := j.findName(data.Axis, j.config.Axis)
if axis == "" {
e := errors.New(fmt.Sprintf("Unknown Axis: %v", data.Axis))
fmt.Println(e.Error())
return e
} else {
gobot.Publish(j.Event(axis), data.Value)
}
}
case *sdl.JoyButtonEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
button := j.findName(data.Button, j.config.Buttons)
if button == "" {
e := errors.New(fmt.Sprintf("Unknown Button: %v", data.Button))
fmt.Println(e.Error())
return e
} else {
if data.State == 1 {
gobot.Publish(j.Event(fmt.Sprintf("%s_press", button)), nil)
} else {
gobot.Publish(j.Event(fmt.Sprintf("%s_release", button)), nil)
}
}
time.Sleep(10 * time.Millisecond)
}
}()
return true
case *sdl.JoyHatEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
hat := j.findHatName(data.Value, data.Hat, j.config.Hats)
if hat == "" {
e := errors.New(fmt.Sprintf("Unknown Hat: %v %v", data.Hat, data.Value))
fmt.Println(e.Error())
return e
} else {
gobot.Publish(j.Event(hat), true)
}
}
}
return nil
}
func (j *JoystickDriver) Init() bool { return true }

func (j *JoystickDriver) Halt() bool { return true }

func (j *JoystickDriver) findName(id uint8, list []pair) string {
Expand Down
109 changes: 102 additions & 7 deletions platforms/joystick/joystick_driver_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,123 @@
package joystick

import (
"github.com/hybridgroup/gobot"
"testing"
"time"

"github.com/hybridgroup/go-sdl2/sdl"
"github.com/hybridgroup/gobot"
)

func initTestJoystickDriver() *JoystickDriver {
return NewJoystickDriver(NewJoystickAdaptor("bot"), "bot", "/dev/null")
a := NewJoystickAdaptor("bot")
a.connect = func(j *JoystickAdaptor) {
j.joystick = &testJoystick{}
}
a.Connect()
d := NewJoystickDriver(a, "bot", "./configs/xbox360_power_a_mini_proex.json")
d.poll = func() sdl.Event {
return new(interface{})
}
return d
}

func TestJoystickDriver(t *testing.T) {
defer func() {
r := recover()
if r != nil {
gobot.Assert(t, "File error: open ./fake_config.json: no such file or directory\n", r)
} else {
t.Errorf("Did not return Unknown Event error")
}
}()
NewJoystickDriver(NewJoystickAdaptor("bot"), "bot", "./fake_config.json")
}
func TestJoystickDriverStart(t *testing.T) {
t.SkipNow()
d := initTestJoystickDriver()
d.SetInterval(1 * time.Millisecond)
gobot.Assert(t, d.Start(), true)
<-time.After(2 * time.Millisecond)
}

func TestJoystickDriverHalt(t *testing.T) {
t.SkipNow()
d := initTestJoystickDriver()
gobot.Assert(t, d.Halt(), true)
}

func TestJoystickDriverInit(t *testing.T) {
t.SkipNow()
func TestJoystickDriverHandleEvent(t *testing.T) {
sem := make(chan bool)
d := initTestJoystickDriver()
gobot.Assert(t, d.Init(), true)
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")
}
8 changes: 8 additions & 0 deletions platforms/joystick/test_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package joystick

import "github.com/hybridgroup/go-sdl2/sdl"

type testJoystick struct{}

func (t *testJoystick) Close() {}
func (t *testJoystick) InstanceID() sdl.JoystickID { return 0 }

0 comments on commit 9533443

Please sign in to comment.