hybridgroup.gobot/platforms/pebble/pebble_driver_test.go

72 lines
1.5 KiB
Go
Raw Normal View History

2014-05-03 06:22:05 +08:00
package pebble
import (
2014-06-14 05:18:57 +08:00
"testing"
2014-11-05 12:55:24 +08:00
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
2014-05-03 06:22:05 +08:00
)
var _ gobot.Driver = (*Driver)(nil)
func initTestDriver() *Driver {
return NewDriver(NewAdaptor())
2014-06-14 05:18:57 +08:00
}
func TestDriverStart(t *testing.T) {
d := initTestDriver()
gobottest.Assert(t, d.Start(), nil)
2014-06-14 05:18:57 +08:00
}
func TestDriverHalt(t *testing.T) {
d := initTestDriver()
gobottest.Assert(t, d.Halt(), nil)
2014-06-14 05:18:57 +08:00
}
func TestDriver(t *testing.T) {
d := initTestDriver()
2014-12-18 06:04:18 +08:00
gobottest.Assert(t, d.Name(), "Pebble")
gobottest.Assert(t, d.Connection().Name(), "Pebble")
2014-12-18 06:04:18 +08:00
2014-11-05 12:55:24 +08:00
sem := make(chan bool)
2014-06-14 07:01:39 +08:00
d.SendNotification("Hello")
d.SendNotification("World")
gobottest.Assert(t, d.Messages[0], "Hello")
gobottest.Assert(t, d.PendingMessage(), "Hello")
gobottest.Assert(t, d.PendingMessage(), "World")
gobottest.Assert(t, d.PendingMessage(), "")
2014-11-05 12:55:24 +08:00
d.On(d.Event("button"), func(data interface{}) {
2014-11-05 12:55:24 +08:00
sem <- true
})
d.PublishEvent("button", "")
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("Button Event was not published")
}
d.On(d.Event("accel"), func(data interface{}) {
2014-11-05 12:55:24 +08:00
sem <- true
})
d.Command("publish_event")(map[string]interface{}{"name": "accel", "data": "100"})
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("Accel Event was not published")
}
d.Command("send_notification")(map[string]interface{}{"message": "Hey buddy!"})
gobottest.Assert(t, d.Messages[0], "Hey buddy!")
2014-11-05 12:55:24 +08:00
message := d.Command("pending_message")(map[string]interface{}{})
gobottest.Assert(t, message, "Hey buddy!")
2014-06-14 05:18:57 +08:00
}