2014-04-28 10:34:16 +08:00
|
|
|
package gpio
|
|
|
|
|
|
|
|
import (
|
2014-12-17 05:42:48 +08:00
|
|
|
"errors"
|
2017-03-21 18:55:46 +08:00
|
|
|
"strings"
|
2014-06-13 11:08:06 +08:00
|
|
|
"testing"
|
2014-07-10 09:19:58 +08:00
|
|
|
|
2016-12-23 16:37:09 +08:00
|
|
|
"gobot.io/x/gobot"
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot/gobottest"
|
2014-04-28 10:34:16 +08:00
|
|
|
)
|
|
|
|
|
2016-12-23 16:37:09 +08:00
|
|
|
var _ gobot.Driver = (*LedDriver)(nil)
|
|
|
|
|
2017-04-02 23:15:45 +08:00
|
|
|
func initTestLedDriver() *LedDriver {
|
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
a.testAdaptorDigitalWrite = func() (err error) {
|
2014-12-17 05:42:48 +08:00
|
|
|
return nil
|
|
|
|
}
|
2017-04-02 23:15:45 +08:00
|
|
|
a.testAdaptorPwmWrite = func() (err error) {
|
2014-12-17 05:42:48 +08:00
|
|
|
return nil
|
|
|
|
}
|
2017-04-02 23:15:45 +08:00
|
|
|
return NewLedDriver(a, "1")
|
2014-12-17 05:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLedDriver(t *testing.T) {
|
|
|
|
var err interface{}
|
2017-04-02 23:15:45 +08:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := NewLedDriver(a, "1")
|
2014-12-17 05:42:48 +08:00
|
|
|
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.Pin(), "1")
|
2016-09-25 19:36:18 +08:00
|
|
|
gobottest.Refute(t, d.Connection(), nil)
|
2014-12-17 05:42:48 +08:00
|
|
|
|
2017-04-02 23:15:45 +08:00
|
|
|
a.testAdaptorDigitalWrite = func() (err error) {
|
2014-12-17 05:42:48 +08:00
|
|
|
return errors.New("write error")
|
|
|
|
}
|
2017-04-02 23:15:45 +08:00
|
|
|
a.testAdaptorPwmWrite = func() (err error) {
|
2014-12-17 05:42:48 +08:00
|
|
|
return errors.New("pwm error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.Command("Toggle")(nil)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err.(error), errors.New("write error"))
|
2014-12-17 05:42:48 +08:00
|
|
|
|
|
|
|
err = d.Command("On")(nil)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err.(error), errors.New("write error"))
|
2014-12-17 05:42:48 +08:00
|
|
|
|
|
|
|
err = d.Command("Off")(nil)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err.(error), errors.New("write error"))
|
2014-12-17 05:42:48 +08:00
|
|
|
|
|
|
|
err = d.Command("Brightness")(map[string]interface{}{"level": 100.0})
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err.(error), errors.New("pwm error"))
|
2014-12-17 05:42:48 +08:00
|
|
|
|
2014-06-13 11:08:06 +08:00
|
|
|
}
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestLedDriverStart(t *testing.T) {
|
2017-04-02 23:15:45 +08:00
|
|
|
d := initTestLedDriver()
|
2016-11-08 00:12:14 +08:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
2014-06-13 11:08:06 +08:00
|
|
|
}
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestLedDriverHalt(t *testing.T) {
|
2017-04-02 23:15:45 +08:00
|
|
|
d := initTestLedDriver()
|
2016-11-08 00:12:14 +08:00
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2014-06-13 11:08:06 +08:00
|
|
|
}
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestLedDriverToggle(t *testing.T) {
|
2017-04-02 23:15:45 +08:00
|
|
|
d := initTestLedDriver()
|
2014-06-14 07:01:39 +08:00
|
|
|
d.Off()
|
|
|
|
d.Toggle()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.State(), true)
|
2014-06-14 07:01:39 +08:00
|
|
|
d.Toggle()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.State(), false)
|
2014-06-13 11:08:06 +08:00
|
|
|
}
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestLedDriverBrightness(t *testing.T) {
|
2017-04-02 23:15:45 +08:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := NewLedDriver(a, "1")
|
|
|
|
a.testAdaptorPwmWrite = func() (err error) {
|
2014-12-17 05:42:48 +08:00
|
|
|
err = errors.New("pwm error")
|
|
|
|
return
|
|
|
|
}
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.Brightness(150), errors.New("pwm error"))
|
2014-06-13 11:08:06 +08:00
|
|
|
}
|
2017-03-21 18:55:46 +08:00
|
|
|
|
|
|
|
func TestLEDDriverDefaultName(t *testing.T) {
|
2017-04-02 23:15:45 +08:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := NewLedDriver(a, "1")
|
2017-03-21 18:55:46 +08:00
|
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "LED"), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLEDDriverSetName(t *testing.T) {
|
2017-04-02 23:15:45 +08:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := NewLedDriver(a, "1")
|
2017-03-21 18:55:46 +08:00
|
|
|
d.SetName("mybot")
|
|
|
|
gobottest.Assert(t, d.Name(), "mybot")
|
|
|
|
}
|