2023-11-16 03:51:52 +08:00
|
|
|
//nolint:forcetypeassert // ok here
|
2014-04-28 09:54:41 +08:00
|
|
|
package i2c
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
import (
|
2014-12-23 05:36:52 +08:00
|
|
|
"errors"
|
2022-12-10 20:10:23 +08:00
|
|
|
"strings"
|
2014-06-14 03:48:00 +08:00
|
|
|
"testing"
|
2014-11-23 11:38:39 +08:00
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-12 21:17:02 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-16 03:51:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
2022-12-10 20:10:23 +08: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
|
2016-12-21 01:59:26 +08:00
|
|
|
var _ gobot.Driver = (*BlinkMDriver)(nil)
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func initTestBlinkMDriverWithStubbedAdaptor() (*BlinkMDriver, *i2cTestAdaptor) {
|
|
|
|
a := newI2cTestAdaptor()
|
|
|
|
d := NewBlinkMDriver(a)
|
|
|
|
if err := d.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d, a
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewBlinkMDriver(t *testing.T) {
|
2022-12-10 20:10:23 +08:00
|
|
|
var di interface{} = NewBlinkMDriver(newI2cTestAdaptor())
|
|
|
|
d, ok := di.(*BlinkMDriver)
|
2014-09-12 04:38:08 +08:00
|
|
|
if !ok {
|
2024-02-13 17:33:46 +08:00
|
|
|
require.Fail(t, "NewBlinkMDriver() should have returned a *BlinkMDriver")
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.NotNil(t, d.Driver)
|
|
|
|
assert.True(t, strings.HasPrefix(d.Name(), "BlinkM"))
|
|
|
|
assert.Equal(t, 0x09, d.defaultAddress)
|
2022-12-10 20:10:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlinkMOptions(t *testing.T) {
|
|
|
|
// This is a general test, that options are applied in constructor by using the common WithBus() option and
|
|
|
|
// least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)".
|
|
|
|
d := NewBlinkMDriver(newI2cTestAdaptor(), WithBus(2))
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 2, d.GetBusOrDefault(1))
|
2022-12-10 20:10:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlinkMStart(t *testing.T) {
|
|
|
|
d := NewBlinkMDriver(newI2cTestAdaptor())
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Start())
|
2022-12-10 20:10:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlinkMHalt(t *testing.T) {
|
|
|
|
d, _ := initTestBlinkMDriverWithStubbedAdaptor()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Halt())
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commands
|
|
|
|
func TestNewBlinkMDriverCommands_Rgb(t *testing.T) {
|
2022-12-10 20:10:23 +08:00
|
|
|
d, _ := initTestBlinkMDriverWithStubbedAdaptor()
|
2014-09-12 04:38:08 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
result := d.Command("Rgb")(rgb)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Nil(t, result)
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewBlinkMDriverCommands_Fade(t *testing.T) {
|
2022-12-10 20:10:23 +08:00
|
|
|
d, _ := initTestBlinkMDriverWithStubbedAdaptor()
|
2017-02-06 07:19:42 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
result := d.Command("Fade")(rgb)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Nil(t, result)
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewBlinkMDriverCommands_FirmwareVersion(t *testing.T) {
|
2022-12-10 20:10:23 +08:00
|
|
|
d, a := initTestBlinkMDriverWithStubbedAdaptor()
|
2014-09-12 04:38:08 +08:00
|
|
|
param := make(map[string]interface{})
|
|
|
|
// When len(data) is 2
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
copy(b, []byte{99, 1})
|
|
|
|
return 2, nil
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
result := d.Command("FirmwareVersion")(param)
|
2014-09-12 04:38:08 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
version, _ := d.FirmwareVersion()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, version, result.(map[string]interface{})["version"].(string))
|
2014-09-12 04:38:08 +08:00
|
|
|
|
|
|
|
// When len(data) is not 2
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
copy(b, []byte{99})
|
|
|
|
return 1, nil
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
2022-12-10 20:10:23 +08:00
|
|
|
result = d.Command("FirmwareVersion")(param)
|
2014-09-12 04:38:08 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
version, _ = d.FirmwareVersion()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, version, result.(map[string]interface{})["version"].(string))
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewBlinkMDriverCommands_Color(t *testing.T) {
|
2022-12-10 20:10:23 +08:00
|
|
|
d, _ := initTestBlinkMDriverWithStubbedAdaptor()
|
2014-09-12 04:38:08 +08:00
|
|
|
param := make(map[string]interface{})
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
result := d.Command("Color")(param)
|
2014-09-12 04:38:08 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
color, _ := d.Color()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, color, result.(map[string]interface{})["color"].([]byte))
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestBlinkMFirmwareVersion(t *testing.T) {
|
|
|
|
d, a := initTestBlinkMDriverWithStubbedAdaptor()
|
2014-09-12 04:38:08 +08:00
|
|
|
// when len(data) is 2
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
copy(b, []byte{99, 1})
|
|
|
|
return 2, nil
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
version, _ := d.FirmwareVersion()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "99.1", version)
|
2014-09-12 04:38:08 +08:00
|
|
|
|
|
|
|
// when len(data) is not 2
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
copy(b, []byte{99})
|
|
|
|
return 1, nil
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
version, _ = d.FirmwareVersion()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "", version)
|
2014-12-23 05:36:52 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cWriteImpl = func([]byte) (int, error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
return 0, errors.New("write error")
|
2014-12-23 05:36:52 +08:00
|
|
|
}
|
|
|
|
|
2023-05-19 20:16:22 +08:00
|
|
|
_, err := d.FirmwareVersion()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, err, "write error")
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestBlinkMColor(t *testing.T) {
|
|
|
|
d, a := initTestBlinkMDriverWithStubbedAdaptor()
|
2014-09-12 04:38:08 +08:00
|
|
|
// when len(data) is 3
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
copy(b, []byte{99, 1, 2})
|
|
|
|
return 3, nil
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
color, _ := d.Color()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, []byte{99, 1, 2}, color)
|
2014-09-12 04:38:08 +08:00
|
|
|
|
|
|
|
// when len(data) is not 3
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
copy(b, []byte{99})
|
|
|
|
return 1, nil
|
2014-09-12 04:38:08 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
color, _ = d.Color()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, []byte{}, color)
|
2014-09-12 04:38:08 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cWriteImpl = func([]byte) (int, error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
return 0, errors.New("write error")
|
2014-12-23 05:36:52 +08:00
|
|
|
}
|
|
|
|
|
2023-05-19 20:16:22 +08:00
|
|
|
_, err := d.Color()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, err, "write error")
|
2014-12-23 05:36:52 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestBlinkMFade(t *testing.T) {
|
|
|
|
d, a := initTestBlinkMDriverWithStubbedAdaptor()
|
|
|
|
a.i2cWriteImpl = func([]byte) (int, error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
return 0, errors.New("write error")
|
2014-12-23 05:36:52 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
err := d.Fade(100, 100, 100)
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, err, "write error")
|
2014-12-23 05:36:52 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestBlinkMRGB(t *testing.T) {
|
|
|
|
d, a := initTestBlinkMDriverWithStubbedAdaptor()
|
|
|
|
a.i2cWriteImpl = func([]byte) (int, error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
return 0, errors.New("write error")
|
2014-12-23 05:36:52 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
err := d.Rgb(100, 100, 100)
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, err, "write error")
|
2014-06-14 03:48:00 +08:00
|
|
|
}
|