hybridgroup.gobot/platforms/leap/leap_motion_driver_test.go

73 lines
1.8 KiB
Go
Raw Normal View History

2014-06-14 04:35:19 +08:00
package leap
import (
2014-12-19 07:13:53 +08:00
"errors"
2014-11-08 05:15:45 +08:00
"io"
2014-06-14 04:35:19 +08:00
"io/ioutil"
"testing"
2014-11-08 05:15:45 +08:00
"github.com/hybridgroup/gobot/gobottest"
2014-06-14 04:35:19 +08:00
)
type NullReadWriteCloser struct{}
2014-12-19 07:13:53 +08:00
var writeError error = nil
func (NullReadWriteCloser) Write(p []byte) (int, error) {
2014-12-19 07:13:53 +08:00
return len(p), writeError
}
func (NullReadWriteCloser) Read(b []byte) (int, error) {
return len(b), nil
}
func (NullReadWriteCloser) Close() error {
return nil
}
2014-06-14 07:01:39 +08:00
func initTestLeapMotionDriver() *LeapMotionDriver {
2014-11-08 05:15:45 +08:00
a := NewLeapMotionAdaptor("bot", "")
2014-12-19 07:13:53 +08:00
a.connect = func(port string) (io.ReadWriteCloser, error) {
return &NullReadWriteCloser{}, nil
2014-11-08 05:15:45 +08:00
}
a.Connect()
2014-12-19 07:13:53 +08:00
receive = func(ws io.ReadWriteCloser, buf *[]byte) {
2014-11-08 05:15:45 +08:00
file, _ := ioutil.ReadFile("./test/support/example_frame.json")
2014-12-19 07:13:53 +08:00
copy(*buf, file)
2014-11-08 05:15:45 +08:00
}
return NewLeapMotionDriver(a, "bot")
2014-06-14 04:35:19 +08:00
}
2014-12-19 07:13:53 +08:00
func TestLeapMotionDriver(t *testing.T) {
d := initTestLeapMotionDriver()
gobottest.Assert(t, d.Name(), "bot")
gobottest.Assert(t, d.Connection().Name(), "bot")
2014-12-19 07:13:53 +08:00
}
2014-06-14 07:01:39 +08:00
func TestLeapMotionDriverStart(t *testing.T) {
d := initTestLeapMotionDriver()
gobottest.Assert(t, len(d.Start()), 0)
2014-12-19 07:13:53 +08:00
d = initTestLeapMotionDriver()
writeError = errors.New("write error")
gobottest.Assert(t, d.Start()[0], errors.New("write error"))
2014-12-19 07:13:53 +08:00
2014-06-14 04:35:19 +08:00
}
2014-06-14 07:01:39 +08:00
func TestLeapMotionDriverHalt(t *testing.T) {
d := initTestLeapMotionDriver()
gobottest.Assert(t, len(d.Halt()), 0)
2014-06-14 04:35:19 +08:00
}
2014-06-14 07:01:39 +08:00
func TestLeapMotionDriverParser(t *testing.T) {
d := initTestLeapMotionDriver()
2014-06-14 04:35:19 +08:00
file, _ := ioutil.ReadFile("./test/support/example_frame.json")
parsedFrame := d.ParseFrame(file)
if parsedFrame.Hands == nil || parsedFrame.Pointables == nil || parsedFrame.Gestures == nil {
t.Errorf("ParseFrame incorrectly parsed frame")
}
gobottest.Assert(t, parsedFrame.Timestamp, 4729292670)
gobottest.Assert(t, parsedFrame.Hands[0].X(), 117.546)
gobottest.Assert(t, parsedFrame.Hands[0].Y(), 236.007)
gobottest.Assert(t, parsedFrame.Hands[0].Z(), 76.3394)
2014-06-14 04:35:19 +08:00
}