hybridgroup.gobot/platforms/sphero/sphero_driver_test.go

170 lines
3.8 KiB
Go
Raw Normal View History

2014-04-27 09:07:04 +08:00
package sphero
import (
2014-12-26 01:26:29 +08:00
"bytes"
"encoding/binary"
"strings"
2014-06-14 05:26:18 +08:00
"testing"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*SpheroDriver)(nil)
2014-06-14 07:01:39 +08:00
func initTestSpheroDriver() *SpheroDriver {
a, _ := initTestSpheroAdaptor()
a.Connect()
return NewSpheroDriver(a)
2014-06-14 05:26:18 +08:00
}
func TestSpheroDriverName(t *testing.T) {
d := initTestSpheroDriver()
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Sphero"), true)
d.SetName("NewName")
gobottest.Assert(t, d.Name(), "NewName")
}
2014-12-18 06:32:12 +08:00
func TestSpheroDriver(t *testing.T) {
d := initTestSpheroDriver()
var ret interface{}
ret = d.Command("SetRGB")(
map[string]interface{}{"r": 100.0, "g": 100.0, "b": 100.0},
)
gobottest.Assert(t, ret, nil)
2014-12-18 06:32:12 +08:00
ret = d.Command("Roll")(
map[string]interface{}{"speed": 100.0, "heading": 100.0},
)
gobottest.Assert(t, ret, nil)
2014-12-18 06:32:12 +08:00
ret = d.Command("SetBackLED")(
map[string]interface{}{"level": 100.0},
)
gobottest.Assert(t, ret, nil)
2014-12-18 06:32:12 +08:00
ret = d.Command("ConfigureLocator")(
map[string]interface{}{"Flags": 1.0, "X": 100.0, "Y": 100.0, "YawTare": 100.0},
)
gobottest.Assert(t, ret, nil)
2014-12-18 06:32:12 +08:00
ret = d.Command("SetHeading")(
map[string]interface{}{"heading": 100.0},
)
gobottest.Assert(t, ret, nil)
2014-12-18 06:32:12 +08:00
ret = d.Command("SetRotationRate")(
map[string]interface{}{"level": 100.0},
)
gobottest.Assert(t, ret, nil)
2014-12-18 06:32:12 +08:00
ret = d.Command("SetStabilization")(
map[string]interface{}{"enable": true},
)
gobottest.Assert(t, ret, nil)
2014-12-18 06:32:12 +08:00
ret = d.Command("SetStabilization")(
map[string]interface{}{"enable": false},
)
gobottest.Assert(t, ret, nil)
2014-12-18 06:32:12 +08:00
ret = d.Command("Stop")(nil)
gobottest.Assert(t, ret, nil)
2014-12-18 06:32:12 +08:00
ret = d.Command("GetRGB")(nil)
gobottest.Assert(t, ret.([]byte), []byte{})
2014-12-18 06:32:12 +08:00
ret = d.Command("ReadLocator")(nil)
gobottest.Assert(t, ret, []int16{})
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Sphero"), true)
gobottest.Assert(t, strings.HasPrefix(d.Connection().Name(), "Sphero"), true)
2014-12-18 06:32:12 +08:00
}
2014-06-14 07:01:39 +08:00
func TestSpheroDriverStart(t *testing.T) {
d := initTestSpheroDriver()
gobottest.Assert(t, d.Start(), nil)
2014-06-14 05:26:18 +08:00
}
2014-06-14 07:01:39 +08:00
func TestSpheroDriverHalt(t *testing.T) {
d := initTestSpheroDriver()
d.adaptor().connected = true
gobottest.Assert(t, d.Halt(), nil)
2014-06-14 05:26:18 +08:00
}
2014-12-26 01:26:29 +08:00
func TestSpheroDriverSetDataStreaming(t *testing.T) {
d := initTestSpheroDriver()
d.SetDataStreaming(DefaultDataStreamingConfig())
data := <-d.packetChannel
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, DefaultDataStreamingConfig())
gobottest.Assert(t, data.body, buf.Bytes())
2014-12-26 01:26:29 +08:00
ret := d.Command("SetDataStreaming")(
map[string]interface{}{
"N": 100.0,
"M": 200.0,
"Mask": 300.0,
"Pcnt": 255.0,
"Mask2": 400.0,
},
)
gobottest.Assert(t, ret, nil)
2014-12-26 01:26:29 +08:00
data = <-d.packetChannel
dconfig := DataStreamingConfig{N: 100, M: 200, Mask: 300, Pcnt: 255, Mask2: 400}
buf = new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, dconfig)
gobottest.Assert(t, data.body, buf.Bytes())
2014-12-26 01:26:29 +08:00
}
func TestConfigureLocator(t *testing.T) {
d := initTestSpheroDriver()
d.ConfigureLocator(DefaultLocatorConfig())
data := <-d.packetChannel
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, DefaultLocatorConfig())
gobottest.Assert(t, data.body, buf.Bytes())
ret := d.Command("ConfigureLocator")(
map[string]interface{}{
"Flags": 1.0,
"X": 100.0,
"Y": 100.0,
"YawTare": 0.0,
},
)
gobottest.Assert(t, ret, nil)
data = <-d.packetChannel
lconfig := LocatorConfig{Flags: 1, X: 100, Y: 100, YawTare: 0}
buf = new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, lconfig)
gobottest.Assert(t, data.body, buf.Bytes())
}
func TestCalculateChecksum(t *testing.T) {
tests := []struct {
data []byte
checksum byte
}{
{[]byte{0x00}, 0xff},
{[]byte{0xf0, 0x0f}, 0x00},
}
for _, tt := range tests {
actual := calculateChecksum(tt.data)
if actual != tt.checksum {
t.Errorf("Expected %x, got %x for data %x.", tt.checksum, actual, tt.data)
}
}
}