2016-10-22 21:17:49 +08:00
|
|
|
package gpio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-03-21 19:27:12 +08:00
|
|
|
"strings"
|
2016-10-22 21:17:49 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2016-10-22 21:17:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*PIRMotionDriver)(nil)
|
|
|
|
|
2017-04-21 17:13:56 +08:00
|
|
|
const motionTestDelay = 150
|
2016-10-22 21:17:49 +08:00
|
|
|
|
|
|
|
func initTestPIRMotionDriver() *PIRMotionDriver {
|
|
|
|
return NewPIRMotionDriver(newGpioTestAdaptor(), "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPIRMotionDriverHalt(t *testing.T) {
|
|
|
|
d := initTestPIRMotionDriver()
|
|
|
|
go func() {
|
|
|
|
<-d.halt
|
|
|
|
}()
|
2016-11-08 00:12:14 +08:00
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2016-10-22 21:17:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPIRMotionDriver(t *testing.T) {
|
|
|
|
d := NewPIRMotionDriver(newGpioTestAdaptor(), "1")
|
|
|
|
gobottest.Refute(t, d.Connection(), nil)
|
|
|
|
|
|
|
|
d = NewPIRMotionDriver(newGpioTestAdaptor(), "1", 30*time.Second)
|
|
|
|
gobottest.Assert(t, d.interval, 30*time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPIRMotionDriverStart(t *testing.T) {
|
|
|
|
sem := make(chan bool, 0)
|
2017-04-02 23:15:45 +08:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := NewPIRMotionDriver(a, "1")
|
|
|
|
|
2016-11-08 00:12:14 +08:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
2016-10-22 21:17:49 +08:00
|
|
|
|
|
|
|
d.Once(MotionDetected, func(data interface{}) {
|
|
|
|
gobottest.Assert(t, d.Active, true)
|
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
2017-04-02 23:42:01 +08:00
|
|
|
a.TestAdaptorDigitalRead(func() (val int, err error) {
|
2016-10-22 21:17:49 +08:00
|
|
|
val = 1
|
|
|
|
return
|
2017-04-02 23:42:01 +08:00
|
|
|
})
|
2016-10-22 21:17:49 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2017-04-21 17:13:56 +08:00
|
|
|
case <-time.After(motionTestDelay * time.Millisecond):
|
2016-10-22 21:17:49 +08:00
|
|
|
t.Errorf("PIRMotionDriver Event \"MotionDetected\" was not published")
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Once(MotionStopped, func(data interface{}) {
|
|
|
|
gobottest.Assert(t, d.Active, false)
|
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
2017-04-02 23:42:01 +08:00
|
|
|
a.TestAdaptorDigitalRead(func() (val int, err error) {
|
2016-10-22 21:17:49 +08:00
|
|
|
val = 0
|
|
|
|
return
|
2017-04-02 23:42:01 +08:00
|
|
|
})
|
2016-10-22 21:17:49 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2017-04-21 17:13:56 +08:00
|
|
|
case <-time.After(motionTestDelay * time.Millisecond):
|
2016-10-22 21:17:49 +08:00
|
|
|
t.Errorf("PIRMotionDriver Event \"MotionStopped\" was not published")
|
|
|
|
}
|
|
|
|
|
2017-04-21 17:13:56 +08:00
|
|
|
d.Once(Error, func(data interface{}) {
|
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
2017-04-02 23:42:01 +08:00
|
|
|
a.TestAdaptorDigitalRead(func() (val int, err error) {
|
2016-10-22 21:17:49 +08:00
|
|
|
err = errors.New("digital read error")
|
|
|
|
return
|
2017-04-02 23:42:01 +08:00
|
|
|
})
|
2016-10-22 21:17:49 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2017-04-21 17:13:56 +08:00
|
|
|
case <-time.After(motionTestDelay * time.Millisecond):
|
2016-10-22 21:17:49 +08:00
|
|
|
t.Errorf("PIRMotionDriver Event \"Error\" was not published")
|
|
|
|
}
|
|
|
|
}
|
2017-03-21 19:27:12 +08:00
|
|
|
|
|
|
|
func TestPIRDriverDefaultName(t *testing.T) {
|
|
|
|
d := initTestPIRMotionDriver()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "PIR"), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPIRDriverSetName(t *testing.T) {
|
|
|
|
d := initTestPIRMotionDriver()
|
|
|
|
d.SetName("mybot")
|
|
|
|
gobottest.Assert(t, d.Name(), "mybot")
|
|
|
|
}
|