hybridgroup.gobot/drivers/gpio/makey_button_driver_test.go

116 lines
2.4 KiB
Go
Raw Normal View History

2014-12-17 05:42:48 +08:00
package gpio
import (
"errors"
"testing"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
2014-12-17 05:42:48 +08:00
)
var _ gobot.Driver = (*MakeyButtonDriver)(nil)
const MAKEY_TEST_DELAY = 30
2014-12-17 05:42:48 +08:00
func initTestMakeyButtonDriver() *MakeyButtonDriver {
return NewMakeyButtonDriver(newGpioTestAdaptor(), "1")
2014-12-17 05:42:48 +08:00
}
func TestMakeyButtonDriverHalt(t *testing.T) {
d := initTestMakeyButtonDriver()
done := make(chan struct{})
go func() {
<-d.halt
close(done)
}()
gobottest.Assert(t, d.Halt(), nil)
select {
case <-done:
case <-time.After(time.Millisecond):
t.Errorf("MakeyButton was not halted")
}
2014-12-17 05:42:48 +08:00
}
func TestMakeyButtonDriver(t *testing.T) {
d := initTestMakeyButtonDriver()
gobottest.Assert(t, d.Pin(), "1")
gobottest.Refute(t, d.Connection(), nil)
gobottest.Assert(t, d.interval, 10*time.Millisecond)
2014-12-17 05:42:48 +08:00
d = NewMakeyButtonDriver(newGpioTestAdaptor(), "1", 30*time.Second)
gobottest.Assert(t, d.interval, MAKEY_TEST_DELAY*time.Second)
2014-12-17 05:42:48 +08:00
}
func TestMakeyButtonDriverStart(t *testing.T) {
sem := make(chan bool)
2014-12-17 05:42:48 +08:00
d := initTestMakeyButtonDriver()
gobottest.Assert(t, d.Start(), nil)
2014-12-17 05:42:48 +08:00
testAdaptorDigitalRead = func() (val int, err error) {
val = 0
return
}
d.Once(ButtonPush, func(data interface{}) {
gobottest.Assert(t, d.Active, true)
2014-12-17 05:42:48 +08:00
sem <- true
})
select {
case <-sem:
case <-time.After(MAKEY_TEST_DELAY * time.Millisecond):
2014-12-17 05:42:48 +08:00
t.Errorf("MakeyButton Event \"Push\" was not published")
}
testAdaptorDigitalRead = func() (val int, err error) {
val = 1
return
}
d.Once(ButtonRelease, func(data interface{}) {
gobottest.Assert(t, d.Active, false)
2014-12-17 05:42:48 +08:00
sem <- true
})
select {
case <-sem:
case <-time.After(MAKEY_TEST_DELAY * time.Millisecond):
2014-12-17 05:42:48 +08:00
t.Errorf("MakeyButton Event \"Release\" was not published")
}
testAdaptorDigitalRead = func() (val int, err error) {
err = errors.New("digital read error")
return
}
d.Once(Error, func(data interface{}) {
gobottest.Assert(t, data.(error).Error(), "digital read error")
2014-12-17 05:42:48 +08:00
sem <- true
})
select {
case <-sem:
case <-time.After(MAKEY_TEST_DELAY * time.Millisecond):
2014-12-17 05:42:48 +08:00
t.Errorf("MakeyButton Event \"Error\" was not published")
}
// send a halt message
d.Once(ButtonRelease, func(data interface{}) {
sem <- true
})
testAdaptorDigitalRead = func() (val int, err error) {
val = 1
return
}
d.halt <- true
select {
case <-sem:
t.Errorf("MakeyButton Event should not have been published")
case <-time.After(MAKEY_TEST_DELAY * time.Millisecond):
}
2014-12-17 05:42:48 +08:00
}