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-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2014-06-14 04:35:19 +08:00
|
|
|
)
|
|
|
|
|
2016-09-26 03:36:01 +08:00
|
|
|
var _ gobot.Driver = (*Driver)(nil)
|
2016-08-27 17:56:01 +08:00
|
|
|
|
2014-12-19 06:07:48 +08:00
|
|
|
type NullReadWriteCloser struct{}
|
|
|
|
|
2016-11-08 02:38:18 +08:00
|
|
|
var writeError error
|
2014-12-19 07:13:53 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-26 03:36:01 +08:00
|
|
|
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
|
|
|
}
|
2016-09-26 03:36:01 +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()
|
2016-09-26 03:36:01 +08:00
|
|
|
gobottest.Refute(t, d.Connection(), nil)
|
2014-12-19 07:13:53 +08:00
|
|
|
}
|
2016-09-26 03:36:01 +08:00
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestLeapMotionDriverStart(t *testing.T) {
|
|
|
|
d := initTestLeapMotionDriver()
|
2016-11-08 02:38:18 +08:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
2014-12-19 07:13:53 +08:00
|
|
|
|
|
|
|
d = initTestLeapMotionDriver()
|
|
|
|
writeError = errors.New("write error")
|
2016-11-08 02:38:18 +08:00
|
|
|
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()
|
2016-11-08 02:38:18 +08:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|