forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
177 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |