2014-12-17 05:42:48 +08:00
|
|
|
package gpio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2014-12-17 05:42:48 +08:00
|
|
|
)
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
var _ gobot.Driver = (*MakeyButtonDriver)(nil)
|
|
|
|
|
2016-02-17 08:48:25 +08:00
|
|
|
const MAKEY_TEST_DELAY = 30
|
2016-02-09 07:49:19 +08:00
|
|
|
|
2014-12-17 05:42:48 +08:00
|
|
|
func initTestMakeyButtonDriver() *MakeyButtonDriver {
|
2016-09-25 19:36:18 +08:00
|
|
|
return NewMakeyButtonDriver(newGpioTestAdaptor(), "1")
|
2014-12-17 05:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeyButtonDriverHalt(t *testing.T) {
|
|
|
|
d := initTestMakeyButtonDriver()
|
2016-10-27 17:34:52 +08:00
|
|
|
done := make(chan struct{})
|
2015-01-03 02:42:53 +08:00
|
|
|
go func() {
|
|
|
|
<-d.halt
|
2016-10-27 17:34:52 +08:00
|
|
|
close(done)
|
2015-01-03 02:42:53 +08:00
|
|
|
}()
|
2016-11-08 00:12:14 +08:00
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2016-10-27 17:34:52 +08:00
|
|
|
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) {
|
2016-10-27 17:34:52 +08:00
|
|
|
d := initTestMakeyButtonDriver()
|
2016-09-25 19:36:18 +08:00
|
|
|
gobottest.Assert(t, d.Pin(), "1")
|
|
|
|
gobottest.Refute(t, d.Connection(), nil)
|
2016-10-27 17:34:52 +08:00
|
|
|
gobottest.Assert(t, d.interval, 10*time.Millisecond)
|
2014-12-17 05:42:48 +08:00
|
|
|
|
2016-09-25 19:36:18 +08:00
|
|
|
d = NewMakeyButtonDriver(newGpioTestAdaptor(), "1", 30*time.Second)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.interval, MAKEY_TEST_DELAY*time.Second)
|
2014-12-17 05:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeyButtonDriverStart(t *testing.T) {
|
2016-10-27 17:34:52 +08:00
|
|
|
sem := make(chan bool)
|
2014-12-17 05:42:48 +08:00
|
|
|
d := initTestMakeyButtonDriver()
|
2016-11-08 00:12:14 +08:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
2014-12-17 05:42:48 +08:00
|
|
|
|
|
|
|
testAdaptorDigitalRead = func() (val int, err error) {
|
|
|
|
val = 0
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2016-02-09 07:49:19 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2016-02-09 07:49:19 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
d.Once(Error, func(data interface{}) {
|
2016-10-27 17:34:52 +08:00
|
|
|
gobottest.Assert(t, data.(error).Error(), "digital read error")
|
2014-12-17 05:42:48 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2016-02-09 07:49:19 +08:00
|
|
|
case <-time.After(MAKEY_TEST_DELAY * time.Millisecond):
|
2014-12-17 05:42:48 +08:00
|
|
|
t.Errorf("MakeyButton Event \"Error\" was not published")
|
|
|
|
}
|
2015-01-03 02:42:53 +08:00
|
|
|
|
2016-10-27 17:34:52 +08:00
|
|
|
// send a halt message
|
2016-09-01 18:17:43 +08:00
|
|
|
d.Once(ButtonRelease, func(data interface{}) {
|
2015-01-03 02:42:53 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
2016-10-27 17:34:52 +08:00
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
testAdaptorDigitalRead = func() (val int, err error) {
|
|
|
|
val = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d.halt <- true
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2016-02-09 07:49:19 +08:00
|
|
|
t.Errorf("MakeyButton Event should not have been published")
|
|
|
|
case <-time.After(MAKEY_TEST_DELAY * time.Millisecond):
|
2015-01-03 02:42:53 +08:00
|
|
|
}
|
2014-12-17 05:42:48 +08:00
|
|
|
}
|