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"
|
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
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
"github.com/hybridgroup/gobot"
|
2016-02-22 13:21:24 +08:00
|
|
|
"github.com/hybridgroup/gobot/gobottest"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
var _ gobot.Driver = (*ButtonDriver)(nil)
|
|
|
|
|
2016-09-13 00:01:31 +08:00
|
|
|
const BUTTON_TEST_DELAY = 150
|
2016-02-09 07:49:19 +08:00
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func initTestButtonDriver() *ButtonDriver {
|
2014-06-16 08:22:50 +08:00
|
|
|
return NewButtonDriver(newGpioTestAdaptor("adaptor"), "bot", "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-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, len(d.Halt()), 0)
|
2014-06-13 11:08:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-17 05:42:48 +08:00
|
|
|
func TestButtonDriver(t *testing.T) {
|
|
|
|
d := NewButtonDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.Name(), "bot")
|
|
|
|
gobottest.Assert(t, d.Connection().Name(), "adaptor")
|
2014-12-17 05:42:48 +08:00
|
|
|
|
|
|
|
d = NewButtonDriver(newGpioTestAdaptor("adaptor"), "bot", "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) {
|
|
|
|
sem := make(chan bool, 0)
|
2014-06-14 07:01:39 +08:00
|
|
|
d := initTestButtonDriver()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, len(d.Start()), 0)
|
2014-12-17 05:42:48 +08:00
|
|
|
|
2016-09-01 18:17:43 +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
|
|
|
|
})
|
|
|
|
|
2016-09-13 00:15:30 +08:00
|
|
|
testAdaptorDigitalRead = func() (val int, err error) {
|
|
|
|
val = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-17 05:42:48 +08:00
|
|
|
select {
|
|
|
|
case <-sem:
|
2016-02-09 07:49:19 +08:00
|
|
|
case <-time.After(BUTTON_TEST_DELAY * time.Millisecond):
|
2014-12-17 05:42:48 +08:00
|
|
|
t.Errorf("Button Event \"Push\" was not published")
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:17:43 +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
|
|
|
|
})
|
|
|
|
|
2016-09-13 00:15:30 +08:00
|
|
|
testAdaptorDigitalRead = func() (val int, err error) {
|
|
|
|
val = 0
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-17 05:42:48 +08:00
|
|
|
select {
|
|
|
|
case <-sem:
|
2016-02-09 07:49:19 +08:00
|
|
|
case <-time.After(BUTTON_TEST_DELAY * time.Millisecond):
|
2014-12-17 05:42:48 +08:00
|
|
|
t.Errorf("Button Event \"Release\" was not published")
|
|
|
|
}
|
|
|
|
|
|
|
|
testAdaptorDigitalRead = func() (val int, err error) {
|
|
|
|
err = errors.New("digital read error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
d.Once(Error, func(data interface{}) {
|
2014-12-17 05:42:48 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
2014-06-13 11:08:06 +08:00
|
|
|
|
2014-12-17 05:42:48 +08:00
|
|
|
select {
|
|
|
|
case <-sem:
|
2016-02-09 07:49:19 +08:00
|
|
|
case <-time.After(BUTTON_TEST_DELAY * 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
|
|
|
|
|
|
|
testAdaptorDigitalRead = func() (val int, err error) {
|
|
|
|
val = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
d.Once(ButtonPush, func(data interface{}) {
|
2015-01-03 02:42:53 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
d.halt <- true
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
t.Errorf("Button Event \"Press\" should not published")
|
2016-02-09 07:49:19 +08:00
|
|
|
case <-time.After(BUTTON_TEST_DELAY * time.Millisecond):
|
2015-01-03 02:42:53 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 11:08:06 +08:00
|
|
|
}
|