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
|
|
|
|
2016-02-22 13:21:24 +08:00
|
|
|
"github.com/hybridgroup/gobot/gobottest"
|
2014-06-14 04:35:19 +08:00
|
|
|
)
|
|
|
|
|
2014-12-19 06:07:48 +08:00
|
|
|
type NullReadWriteCloser struct{}
|
|
|
|
|
2014-12-19 07:13:53 +08:00
|
|
|
var writeError error = nil
|
|
|
|
|
2014-12-19 06:07:48 +08:00
|
|
|
func (NullReadWriteCloser) Write(p []byte) (int, error) {
|
2014-12-19 07:13:53 +08:00
|
|
|
return len(p), writeError
|
2014-12-19 06:07:48 +08:00
|
|
|
}
|
|
|
|
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()
|
2016-02-22 13:21:24 +08:00
|
|
|
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()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, len(d.Start()), 0)
|
2014-12-19 07:13:53 +08:00
|
|
|
|
|
|
|
d = initTestLeapMotionDriver()
|
|
|
|
writeError = errors.New("write error")
|
2016-02-22 13:21:24 +08:00
|
|
|
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()
|
2016-02-22 13:21:24 +08:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2016-02-22 13:21:24 +08:00
|
|
|
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
|
|
|
}
|