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 = (*AnalogSensorDriver)(nil)
|
|
|
|
|
2014-12-17 05:42:48 +08:00
|
|
|
func TestAnalogSensorDriver(t *testing.T) {
|
|
|
|
d := NewAnalogSensorDriver(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 = NewAnalogSensorDriver(newGpioTestAdaptor("adaptor"), "bot", "1", 30*time.Second)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.interval, 30*time.Second)
|
2014-12-17 05:42:48 +08:00
|
|
|
|
|
|
|
testAdaptorAnalogRead = func() (val int, err error) {
|
|
|
|
val = 100
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret := d.Command("Read")(nil).(map[string]interface{})
|
|
|
|
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, ret["val"].(int), 100)
|
|
|
|
gobottest.Assert(t, ret["err"], nil)
|
2014-06-13 11:08:06 +08:00
|
|
|
}
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestAnalogSensorDriverStart(t *testing.T) {
|
2014-12-17 05:42:48 +08:00
|
|
|
sem := make(chan bool, 1)
|
|
|
|
|
|
|
|
d := NewAnalogSensorDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
|
|
|
|
|
2015-07-16 04:00:01 +08:00
|
|
|
testAdaptorAnalogRead = func() (val int, err error) {
|
|
|
|
val = 0
|
|
|
|
return
|
|
|
|
}
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, len(d.Start()), 0)
|
2014-12-17 05:42:48 +08:00
|
|
|
|
2015-07-16 04:00:01 +08:00
|
|
|
// data was received
|
2016-08-30 19:27:50 +08:00
|
|
|
d.Once(d.Event(Data), func(data interface{}) {
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, data.(int), 100)
|
2014-12-17 05:42:48 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
testAdaptorAnalogRead = func() (val int, err error) {
|
|
|
|
val = 100
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2015-07-16 04:00:01 +08:00
|
|
|
case <-time.After(10 * time.Second):
|
2014-12-17 05:42:48 +08:00
|
|
|
t.Errorf("AnalogSensor Event \"Data\" was not published")
|
|
|
|
}
|
|
|
|
|
2015-07-16 04:00:01 +08:00
|
|
|
// read error
|
2016-08-30 19:27:50 +08:00
|
|
|
d.Once(d.Event(Error), func(data interface{}) {
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, data.(error).Error(), "read error")
|
2014-12-17 05:42:48 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
testAdaptorAnalogRead = func() (val int, err error) {
|
|
|
|
err = errors.New("read error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2015-07-16 04:00:01 +08:00
|
|
|
case <-time.After(10 * time.Second):
|
2014-12-17 05:42:48 +08:00
|
|
|
t.Errorf("AnalogSensor Event \"Error\" was not published")
|
|
|
|
}
|
2015-01-03 02:42:53 +08:00
|
|
|
|
2015-07-16 04:00:01 +08:00
|
|
|
// send a halt message
|
2016-08-30 19:27:50 +08:00
|
|
|
d.Once(d.Event(Data), func(data interface{}) {
|
2015-01-03 02:42:53 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
testAdaptorAnalogRead = func() (val int, err error) {
|
|
|
|
val = 200
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d.halt <- true
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
t.Errorf("AnalogSensor Event should not published")
|
2015-07-16 04:00:01 +08:00
|
|
|
case <-time.After(100 * time.Millisecond):
|
2015-01-03 02:42:53 +08:00
|
|
|
}
|
2014-06-13 11:08:06 +08:00
|
|
|
}
|
2014-06-14 03:39:02 +08:00
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestAnalogSensorDriverHalt(t *testing.T) {
|
2014-12-17 05:42:48 +08:00
|
|
|
d := NewAnalogSensorDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
|
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
|
|
|
}
|