2014-04-28 10:34:16 +08:00
|
|
|
package gpio
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
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-12-17 05:42:48 +08:00
|
|
|
"time"
|
2014-09-28 02:45:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/gobottest"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
var _ gobot.Driver = (*ButtonDriver)(nil)
|
|
|
|
|
2017-04-21 17:13:56 +08:00
|
|
|
const buttonTestDelay = 250
|
2016-02-09 07:49:19 +08:00
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func initTestButtonDriver() *ButtonDriver {
|
2016-09-25 19:36:18 +08:00
|
|
|
return NewButtonDriver(newGpioTestAdaptor(), "1")
|
2014-06-13 11:08:06 +08:00
|
|
|
}
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestButtonDriverHalt(t *testing.T) {
|
|
|
|
d := initTestButtonDriver()
|
2015-01-03 02:42:53 +08:00
|
|
|
go func() {
|
|
|
|
<-d.halt
|
|
|
|
}()
|
2016-11-08 00:12:14 +08:00
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2014-06-13 11:08:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-17 05:42:48 +08:00
|
|
|
func TestButtonDriver(t *testing.T) {
|
2016-09-25 19:36:18 +08:00
|
|
|
d := NewButtonDriver(newGpioTestAdaptor(), "1")
|
|
|
|
gobottest.Refute(t, d.Connection(), nil)
|
2014-12-17 05:42:48 +08:00
|
|
|
|
2016-09-25 19:36:18 +08:00
|
|
|
d = NewButtonDriver(newGpioTestAdaptor(), "1", 30*time.Second)
|
2016-05-14 06:26:47 +08:00
|
|
|
gobottest.Assert(t, d.interval, 30*time.Second)
|
2014-12-17 05:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestButtonDriverStart(t *testing.T) {
|
2023-05-19 20:16:22 +08:00
|
|
|
sem := make(chan bool)
|
2017-04-02 23:15:45 +08:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := NewButtonDriver(a, "1")
|
2014-12-17 05:42:48 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Once(ButtonPush, func(data interface{}) {
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.Active, true)
|
2014-12-17 05:42:48 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
2020-03-31 04:36:32 +08:00
|
|
|
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
|
2016-09-13 00:15:30 +08:00
|
|
|
val = 1
|
|
|
|
return
|
2017-04-02 23:42:01 +08:00
|
|
|
})
|
2016-09-13 00:15:30 +08:00
|
|
|
|
2017-05-03 18:04:37 +08:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
|
|
|
|
2014-12-17 05:42:48 +08:00
|
|
|
select {
|
|
|
|
case <-sem:
|
2017-04-21 17:13:56 +08:00
|
|
|
case <-time.After(buttonTestDelay * time.Millisecond):
|
2014-12-17 05:42:48 +08:00
|
|
|
t.Errorf("Button Event \"Push\" was not published")
|
|
|
|
}
|
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Once(ButtonRelease, func(data interface{}) {
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.Active, false)
|
2014-12-17 05:42:48 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
2020-03-31 04:36:32 +08:00
|
|
|
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
|
2016-09-13 00:15:30 +08:00
|
|
|
val = 0
|
|
|
|
return
|
2017-04-02 23:42:01 +08:00
|
|
|
})
|
2016-09-13 00:15:30 +08:00
|
|
|
|
2014-12-17 05:42:48 +08:00
|
|
|
select {
|
|
|
|
case <-sem:
|
2017-04-21 17:13:56 +08:00
|
|
|
case <-time.After(buttonTestDelay * time.Millisecond):
|
2014-12-17 05:42:48 +08:00
|
|
|
t.Errorf("Button Event \"Release\" was not published")
|
|
|
|
}
|
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Once(Error, func(data interface{}) {
|
2017-04-21 17:13:56 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
2020-03-31 04:36:32 +08:00
|
|
|
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
|
2014-12-17 05:42:48 +08:00
|
|
|
err = errors.New("digital read error")
|
|
|
|
return
|
2017-04-02 23:42:01 +08:00
|
|
|
})
|
2014-12-17 05:42:48 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2017-04-21 17:13:56 +08:00
|
|
|
case <-time.After(buttonTestDelay * time.Millisecond):
|
2014-12-17 05:42:48 +08:00
|
|
|
t.Errorf("Button Event \"Error\" was not published")
|
|
|
|
}
|
2015-01-03 02:42:53 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Once(ButtonPush, func(data interface{}) {
|
2017-04-21 17:13:56 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
2017-04-27 23:19:32 +08:00
|
|
|
d.halt <- true
|
|
|
|
|
2020-03-31 04:36:32 +08:00
|
|
|
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
|
2015-01-03 02:42:53 +08:00
|
|
|
val = 1
|
|
|
|
return
|
2017-04-02 23:42:01 +08:00
|
|
|
})
|
2015-01-03 02:42:53 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
t.Errorf("Button Event \"Press\" should not published")
|
2017-04-21 17:13:56 +08:00
|
|
|
case <-time.After(buttonTestDelay * time.Millisecond):
|
2015-01-03 02:42:53 +08:00
|
|
|
}
|
2014-06-13 11:08:06 +08:00
|
|
|
}
|
2017-03-21 18:55:46 +08:00
|
|
|
|
2017-10-22 16:28:39 +08:00
|
|
|
func TestButtonDriverDefaultState(t *testing.T) {
|
2023-05-19 20:16:22 +08:00
|
|
|
sem := make(chan bool)
|
2017-10-22 16:28:39 +08:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := NewButtonDriver(a, "1")
|
|
|
|
d.DefaultState = 1
|
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Once(ButtonPush, func(data interface{}) {
|
2017-10-22 16:28:39 +08:00
|
|
|
gobottest.Assert(t, d.Active, true)
|
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
2020-03-31 04:36:32 +08:00
|
|
|
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
|
2017-10-22 16:28:39 +08:00
|
|
|
val = 0
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
case <-time.After(buttonTestDelay * time.Millisecond):
|
|
|
|
t.Errorf("Button Event \"Push\" was not published")
|
|
|
|
}
|
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Once(ButtonRelease, func(data interface{}) {
|
2017-10-22 16:28:39 +08:00
|
|
|
gobottest.Assert(t, d.Active, false)
|
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
2020-03-31 04:36:32 +08:00
|
|
|
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
|
2017-10-22 16:28:39 +08:00
|
|
|
val = 1
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
case <-time.After(buttonTestDelay * time.Millisecond):
|
|
|
|
t.Errorf("Button Event \"Release\" was not published")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 18:55:46 +08:00
|
|
|
func TestButtonDriverDefaultName(t *testing.T) {
|
|
|
|
g := initTestButtonDriver()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(g.Name(), "Button"), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestButtonDriverSetName(t *testing.T) {
|
|
|
|
g := initTestButtonDriver()
|
|
|
|
g.SetName("mybot")
|
|
|
|
gobottest.Assert(t, g.Name(), "mybot")
|
|
|
|
}
|