1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-29 13:49:14 +08:00
hybridgroup.gobot/platforms/gpio/button_driver_test.go

81 lines
1.6 KiB
Go
Raw Normal View History

2014-04-27 19:34:16 -07:00
package gpio
import (
2014-12-16 13:42:48 -08:00
"errors"
2014-06-12 20:08:06 -07:00
"testing"
2014-12-16 13:42:48 -08:00
"time"
2014-09-27 11:45:52 -07:00
"github.com/hybridgroup/gobot"
)
2014-06-13 16:01:39 -07:00
func initTestButtonDriver() *ButtonDriver {
return NewButtonDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
2014-06-12 20:08:06 -07:00
}
2014-06-13 16:01:39 -07:00
func TestButtonDriverHalt(t *testing.T) {
d := initTestButtonDriver()
gobot.Assert(t, len(d.Halt()), 0)
2014-06-12 20:08:06 -07:00
}
2014-12-16 13:42:48 -08:00
func TestButtonDriver(t *testing.T) {
d := NewButtonDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
gobot.Assert(t, d.Name(), "bot")
gobot.Assert(t, d.Connection().Name(), "adaptor")
d = NewButtonDriver(newGpioTestAdaptor("adaptor"), "bot", "1", 30*time.Second)
gobot.Assert(t, d.interval, 30*time.Second)
}
func TestButtonDriverStart(t *testing.T) {
sem := make(chan bool, 0)
2014-06-13 16:01:39 -07:00
d := initTestButtonDriver()
2014-12-16 13:42:48 -08:00
gobot.Assert(t, len(d.Start()), 0)
testAdaptorDigitalRead = func() (val int, err error) {
val = 1
return
}
gobot.On(d.Event(Push), func(data interface{}) {
gobot.Assert(t, d.Active, true)
sem <- true
})
select {
case <-sem:
case <-time.After(15 * time.Millisecond):
t.Errorf("Button Event \"Push\" was not published")
}
testAdaptorDigitalRead = func() (val int, err error) {
val = 0
return
}
gobot.On(d.Event(Release), func(data interface{}) {
gobot.Assert(t, d.Active, false)
sem <- true
})
select {
case <-sem:
case <-time.After(15 * time.Millisecond):
t.Errorf("Button Event \"Release\" was not published")
}
testAdaptorDigitalRead = func() (val int, err error) {
err = errors.New("digital read error")
return
}
gobot.On(d.Event(Error), func(data interface{}) {
sem <- true
})
2014-06-12 20:08:06 -07:00
2014-12-16 13:42:48 -08:00
select {
case <-sem:
case <-time.After(15 * time.Millisecond):
t.Errorf("Button Event \"Error\" was not published")
}
2014-06-12 20:08:06 -07:00
}