1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-14 19:29:32 +08:00
hybridgroup.gobot/platforms/sphero/sphero_driver_test.go

159 lines
3.5 KiB
Go
Raw Normal View History

2014-04-26 18:07:04 -07:00
package sphero
import (
2014-12-25 09:26:29 -08:00
"bytes"
"encoding/binary"
2014-06-13 14:26:18 -07:00
"testing"
"github.com/hybridgroup/gobot/gobottest"
)
2014-06-13 16:01:39 -07:00
func initTestSpheroDriver() *SpheroDriver {
2014-06-13 14:26:18 -07:00
a := NewSpheroAdaptor("bot", "/dev/null")
2014-12-17 14:32:12 -08:00
a.sp = nullReadWriteCloser{}
2014-06-13 16:01:39 -07:00
return NewSpheroDriver(a, "bot")
2014-06-13 14:26:18 -07:00
}
2014-12-17 14: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-17 14:32:12 -08:00
ret = d.Command("Roll")(
map[string]interface{}{"speed": 100.0, "heading": 100.0},
)
gobottest.Assert(t, ret, nil)
2014-12-17 14:32:12 -08:00
ret = d.Command("SetBackLED")(
map[string]interface{}{"level": 100.0},
)
gobottest.Assert(t, ret, nil)
2014-12-17 14: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-17 14:32:12 -08:00
ret = d.Command("SetHeading")(
map[string]interface{}{"heading": 100.0},
)
gobottest.Assert(t, ret, nil)
2014-12-17 14:32:12 -08:00
ret = d.Command("SetRotationRate")(
map[string]interface{}{"level": 100.0},
)
gobottest.Assert(t, ret, nil)
2014-12-17 14:32:12 -08:00
ret = d.Command("SetStabilization")(
map[string]interface{}{"enable": true},
)
gobottest.Assert(t, ret, nil)
2014-12-17 14:32:12 -08:00
ret = d.Command("SetStabilization")(
map[string]interface{}{"enable": false},
)
gobottest.Assert(t, ret, nil)
2014-12-17 14:32:12 -08:00
ret = d.Command("Stop")(nil)
gobottest.Assert(t, ret, nil)
2014-12-17 14:32:12 -08:00
ret = d.Command("GetRGB")(nil)
gobottest.Assert(t, ret.([]byte), []byte{})
2014-12-17 14:32:12 -08:00
ret = d.Command("ReadLocator")(nil)
gobottest.Assert(t, ret, []int16{})
gobottest.Assert(t, d.Name(), "bot")
gobottest.Assert(t, d.Connection().Name(), "bot")
2014-12-17 14:32:12 -08:00
}
2014-06-13 16:01:39 -07:00
func TestSpheroDriverStart(t *testing.T) {
d := initTestSpheroDriver()
gobottest.Assert(t, len(d.Start()), 0)
2014-06-13 14:26:18 -07:00
}
2014-06-13 16:01:39 -07:00
func TestSpheroDriverHalt(t *testing.T) {
d := initTestSpheroDriver()
d.adaptor().connected = true
gobottest.Assert(t, len(d.Halt()), 0)
2014-06-13 14:26:18 -07:00
}
2014-12-25 09: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-25 09: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-25 09: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-25 09: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)
}
}
}