2017-03-01 03:03:20 +08:00
|
|
|
package microbit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-04-06 03:56:41 +08:00
|
|
|
"time"
|
2017-03-01 03:03:20 +08:00
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*ButtonDriver)(nil)
|
|
|
|
|
|
|
|
func initTestButtonDriver() *ButtonDriver {
|
2017-04-05 18:40:17 +08:00
|
|
|
d := NewButtonDriver(NewBleTestAdaptor())
|
2017-03-01 03:03:20 +08:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestButtonDriver(t *testing.T) {
|
|
|
|
d := initTestButtonDriver()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Microbit Button"), true)
|
2017-04-05 18:40:17 +08:00
|
|
|
d.SetName("NewName")
|
|
|
|
gobottest.Assert(t, d.Name(), "NewName")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestButtonDriverStartAndHalt(t *testing.T) {
|
|
|
|
d := initTestButtonDriver()
|
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2017-03-01 03:03:20 +08:00
|
|
|
}
|
2017-04-06 03:56:41 +08:00
|
|
|
|
|
|
|
func TestButtonDriverReadData(t *testing.T) {
|
|
|
|
sem := make(chan bool, 0)
|
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewButtonDriver(a)
|
|
|
|
d.Start()
|
|
|
|
d.On(ButtonB, func(data interface{}) {
|
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
a.TestReceiveNotification([]byte{1}, nil)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Errorf("Microbit Event \"ButtonB\" was not published")
|
|
|
|
}
|
|
|
|
}
|