hybridgroup.gobot/platforms/leap/leap_motion_driver_test.go

75 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
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
2014-06-14 04:35:19 +08:00
)
var _ gobot.Driver = (*Driver)(nil)
type NullReadWriteCloser struct{}
var writeError error
2014-12-19 07:13:53 +08:00
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
}
func initTestLeapMotionDriver() *Driver {
a := NewAdaptor("")
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 NewDriver(a)
2014-06-14 04:35:19 +08:00
}
2014-12-19 07:13:53 +08:00
func TestLeapMotionDriver(t *testing.T) {
d := initTestLeapMotionDriver()
gobottest.Refute(t, d.Connection(), nil)
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, d.Start(), nil)
2014-12-19 07:13:53 +08:00
d = initTestLeapMotionDriver()
writeError = errors.New("write error")
gobottest.Assert(t, d.Start(), errors.New("write error"))
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, d.Halt(), nil)
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
}