1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-02 22:17:12 +08:00
hybridgroup.gobot/drivers/i2c/wiichuck_driver_test.go

300 lines
7.9 KiB
Go
Raw Normal View History

2014-04-27 18:54:41 -07:00
package i2c
import (
"strings"
2014-06-13 12:48:00 -07:00
"testing"
2014-09-11 15:38:08 -05:00
"time"
2014-10-28 16:22:05 -07:00
"github.com/stretchr/testify/assert"
"gobot.io/x/gobot/v2"
2014-04-27 18:54:41 -07:00
)
// this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver
// and tests all implementations, so no further tests needed here for gobot.Driver interface
var _ gobot.Driver = (*WiichuckDriver)(nil)
func initTestWiichuckDriverWithStubbedAdaptor() *WiichuckDriver {
d := NewWiichuckDriver(newI2cTestAdaptor())
if err := d.Start(); err != nil {
panic(err)
}
return d
2014-09-11 15:38:08 -05:00
}
func TestNewWiichuckDriver(t *testing.T) {
var di interface{} = NewWiichuckDriver(newI2cTestAdaptor())
d, ok := di.(*WiichuckDriver)
2014-09-11 15:38:08 -05:00
if !ok {
t.Errorf("NewWiichuckDriver() should have returned a *WiichuckDriver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "Wiichuck"))
assert.Equal(t, 0x52, d.defaultAddress)
assert.Equal(t, 10*time.Millisecond, d.interval)
2014-12-22 13:36:52 -08:00
}
2014-06-13 16:01:39 -07:00
func TestWiichuckDriverStart(t *testing.T) {
a := newI2cTestAdaptor()
d := NewWiichuckDriver(a)
a.Testi2cReadImpl(func(b []byte) (int, error) {
copy(b, []byte{1, 2, 3, 4, 5, 6})
return 6, nil
})
2014-09-11 15:38:08 -05:00
numberOfCyclesForEvery := 3
d.interval = 1 * time.Millisecond
sem := make(chan bool)
2014-09-11 15:38:08 -05:00
assert.Nil(t, d.Start())
2014-09-11 15:38:08 -05:00
2014-10-28 16:22:05 -07:00
go func() {
for {
time.Sleep(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
j := d.Joystick()
if (j["sy_origin"] == float64(44)) &&
(j["sx_origin"] == float64(45)) {
2014-10-28 16:22:05 -07:00
sem <- true
return
2014-10-28 16:22:05 -07:00
}
}
}()
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("origin not read correctly")
}
2014-09-11 15:38:08 -05:00
}
func TestWiichuckDriverHalt(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
assert.Nil(t, d.Halt())
2014-09-11 15:38:08 -05:00
}
func TestWiichuckDriverCanParse(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
2014-09-11 15:38:08 -05:00
// ------ When value is not encrypted
decryptedValue := []byte{1, 2, 3, 4, 5, 6}
_ = d.update(decryptedValue)
2014-09-11 15:38:08 -05:00
// - This should be done by WiichuckDriver.parse
assert.Equal(t, float64(45), d.data["sx"])
assert.Equal(t, float64(44), d.data["sy"])
assert.Equal(t, float64(0), d.data["z"])
assert.Equal(t, float64(0), d.data["c"])
}
func TestWiichuckDriverCanAdjustOrigins(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
// ------ When value is not encrypted
decryptedValue := []byte{1, 2, 3, 4, 5, 6}
_ = d.update(decryptedValue)
2014-09-11 15:38:08 -05:00
// - This should be done by WiichuckDriver.adjustOrigins
assert.Equal(t, float64(45), d.Joystick()["sx_origin"])
assert.Equal(t, float64(44), d.Joystick()["sy_origin"])
}
func TestWiichuckDriverCButton(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
// ------ When value is not encrypted
decryptedValue := []byte{1, 2, 3, 4, 5, 6}
_ = d.update(decryptedValue)
2014-09-11 15:38:08 -05:00
// - This should be done by WiichuckDriver.updateButtons
done := make(chan bool)
2014-09-11 15:38:08 -05:00
_ = d.On(d.Event(C), func(data interface{}) {
assert.Equal(t, true, data)
done <- true
2014-09-11 15:38:08 -05:00
})
_ = d.update(decryptedValue)
2014-09-11 15:38:08 -05:00
2015-07-15 13:00:01 -07:00
select {
case <-done:
2015-07-15 13:00:01 -07:00
case <-time.After(10 * time.Second):
2017-09-23 14:16:19 +05:30
t.Errorf("Did not receive 'C' event")
2015-07-15 13:00:01 -07:00
}
}
func TestWiichuckDriverZButton(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
// ------ When value is not encrypted
decryptedValue := []byte{1, 2, 3, 4, 5, 6}
_ = d.update(decryptedValue)
done := make(chan bool)
2015-07-15 13:00:01 -07:00
_ = d.On(d.Event(Z), func(data interface{}) {
assert.Equal(t, true, data)
done <- true
2014-09-11 15:38:08 -05:00
})
_ = d.update(decryptedValue)
2014-09-11 15:38:08 -05:00
2015-07-15 13:00:01 -07:00
select {
case <-done:
2015-07-15 13:00:01 -07:00
case <-time.After(10 * time.Second):
2017-09-23 14:16:19 +05:30
t.Errorf("Did not receive 'Z' event")
2015-07-15 13:00:01 -07:00
}
}
func TestWiichuckDriverUpdateJoystick(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
// ------ When value is not encrypted
decryptedValue := []byte{1, 2, 3, 4, 5, 6}
2015-07-15 13:00:01 -07:00
// - This should be done by WiichuckDriver.updateJoystick
2014-09-11 15:38:08 -05:00
expectedData := map[string]float64{
"x": float64(0),
"y": float64(0),
}
done := make(chan bool)
_ = d.On(d.Event(Joystick), func(data interface{}) {
assert.Equal(t, expectedData, data)
done <- true
2014-09-11 15:38:08 -05:00
})
2015-07-15 13:00:01 -07:00
_ = d.update(decryptedValue)
2015-07-15 13:00:01 -07:00
select {
case <-done:
2015-07-15 13:00:01 -07:00
case <-time.After(10 * time.Second):
2017-09-23 14:16:19 +05:30
t.Errorf("Did not receive 'Joystick' event")
2015-07-15 13:00:01 -07:00
}
}
func TestWiichuckDriverEncrypted(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
2014-09-11 15:38:08 -05:00
encryptedValue := []byte{1, 1, 2, 2, 3, 3}
_ = d.update(encryptedValue)
2014-09-11 15:38:08 -05:00
assert.Equal(t, float64(0), d.data["sx"])
assert.Equal(t, float64(0), d.data["sy"])
assert.Equal(t, float64(0), d.data["z"])
assert.Equal(t, float64(0), d.data["c"])
2014-09-11 15:38:08 -05:00
assert.Equal(t, float64(-1), d.Joystick()["sx_origin"])
assert.Equal(t, float64(-1), d.Joystick()["sy_origin"])
2014-09-11 15:38:08 -05:00
}
func TestWiichuckDriverSetJoystickDefaultValue(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
2014-09-11 15:38:08 -05:00
assert.Equal(t, float64(-1), d.Joystick()["sy_origin"])
2014-09-11 15:38:08 -05:00
d.setJoystickDefaultValue("sy_origin", float64(2))
2014-09-11 15:38:08 -05:00
assert.Equal(t, float64(2), d.Joystick()["sy_origin"])
2014-09-11 15:38:08 -05:00
// when current default value is not -1 it keeps the current value
d.setJoystickDefaultValue("sy_origin", float64(20))
2014-09-11 15:38:08 -05:00
assert.Equal(t, float64(2), d.Joystick()["sy_origin"])
2014-09-11 15:38:08 -05:00
}
func TestWiichuckDriverCalculateJoystickValue(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
2014-09-11 15:38:08 -05:00
assert.Equal(t, float64(15), d.calculateJoystickValue(float64(20), float64(5)))
assert.Equal(t, float64(-1), d.calculateJoystickValue(float64(1), float64(2)))
assert.Equal(t, float64(5), d.calculateJoystickValue(float64(10), float64(5)))
assert.Equal(t, float64(-5), d.calculateJoystickValue(float64(5), float64(10)))
2014-09-11 15:38:08 -05:00
}
func TestWiichuckDriverIsEncrypted(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
2014-09-11 15:38:08 -05:00
encryptedValue := []byte{1, 1, 2, 2, 3, 3}
assert.True(t, d.isEncrypted(encryptedValue))
2014-09-11 15:38:08 -05:00
encryptedValue = []byte{42, 42, 24, 24, 30, 30}
assert.True(t, d.isEncrypted(encryptedValue))
2014-09-11 15:38:08 -05:00
decryptedValue := []byte{1, 2, 3, 4, 5, 6}
assert.False(t, d.isEncrypted(decryptedValue))
2014-09-11 15:38:08 -05:00
decryptedValue = []byte{1, 1, 2, 2, 5, 6}
assert.False(t, d.isEncrypted(decryptedValue))
2014-09-11 15:38:08 -05:00
decryptedValue = []byte{1, 1, 2, 3, 3, 3}
assert.False(t, d.isEncrypted(decryptedValue))
2014-09-11 15:38:08 -05:00
}
func TestWiichuckDriverDecode(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
2014-09-11 15:38:08 -05:00
assert.Equal(t, float64(46), d.decode(byte(0)))
assert.Equal(t, float64(138), d.decode(byte(100)))
assert.Equal(t, float64(246), d.decode(byte(200)))
assert.Equal(t, float64(0), d.decode(byte(254)))
2014-09-11 15:38:08 -05:00
}
func TestWiichuckDriverParse(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
2014-09-11 15:38:08 -05:00
assert.Equal(t, float64(0), d.data["sx"])
assert.Equal(t, float64(0), d.data["sy"])
assert.Equal(t, float64(0), d.data["z"])
assert.Equal(t, float64(0), d.data["c"])
2014-09-11 15:38:08 -05:00
// First pass
d.parse([]byte{12, 23, 34, 45, 56, 67})
2014-09-11 15:38:08 -05:00
assert.Equal(t, float64(50), d.data["sx"])
assert.Equal(t, float64(23), d.data["sy"])
assert.Equal(t, float64(1), d.data["z"])
assert.Equal(t, float64(2), d.data["c"])
2014-09-11 15:38:08 -05:00
// Second pass
d.parse([]byte{70, 81, 92, 103, 204, 205})
2014-09-11 15:38:08 -05:00
assert.Equal(t, float64(104), d.data["sx"])
assert.Equal(t, float64(93), d.data["sy"])
assert.Equal(t, float64(1), d.data["z"])
assert.Equal(t, float64(0), d.data["c"])
2014-09-11 15:38:08 -05:00
}
func TestWiichuckDriverAdjustOrigins(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
2014-09-11 15:38:08 -05:00
assert.Equal(t, float64(-1), d.Joystick()["sy_origin"])
assert.Equal(t, float64(-1), d.Joystick()["sx_origin"])
2014-09-11 15:38:08 -05:00
// First pass
d.parse([]byte{1, 2, 3, 4, 5, 6})
d.adjustOrigins()
2014-09-11 15:38:08 -05:00
assert.Equal(t, float64(44), d.Joystick()["sy_origin"])
assert.Equal(t, float64(45), d.Joystick()["sx_origin"])
2014-09-11 15:38:08 -05:00
// Second pass
d = initTestWiichuckDriverWithStubbedAdaptor()
2014-09-11 15:38:08 -05:00
d.parse([]byte{61, 72, 83, 94, 105, 206})
d.adjustOrigins()
2014-09-11 15:38:08 -05:00
assert.Equal(t, float64(118), d.Joystick()["sy_origin"])
assert.Equal(t, float64(65), d.Joystick()["sx_origin"])
2014-09-11 15:38:08 -05:00
}
func TestWiichuckDriverSetName(t *testing.T) {
d := initTestWiichuckDriverWithStubbedAdaptor()
d.SetName("TESTME")
assert.Equal(t, "TESTME", d.Name())
}
func TestWiichuckDriverOptions(t *testing.T) {
d := NewWiichuckDriver(newI2cTestAdaptor(), WithBus(2))
assert.Equal(t, 2, d.GetBusOrDefault(1))
}