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"
|
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-12 21:17:02 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-16 03:51:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2014-05-03 06:22:05 +08:00
|
|
|
)
|
|
|
|
|
2016-10-02 00:01:30 +08:00
|
|
|
var _ gobot.Driver = (*Driver)(nil)
|
2016-07-14 01:01:36 +08:00
|
|
|
|
2016-10-02 00:01:30 +08:00
|
|
|
func initTestDriver() *Driver {
|
|
|
|
return NewDriver(NewAdaptor())
|
2014-06-14 05:18:57 +08:00
|
|
|
}
|
2014-05-24 06:09:18 +08:00
|
|
|
|
2016-10-02 00:01:30 +08:00
|
|
|
func TestDriverStart(t *testing.T) {
|
|
|
|
d := initTestDriver()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Start())
|
2014-06-14 05:18:57 +08:00
|
|
|
}
|
2014-05-24 06:09:18 +08:00
|
|
|
|
2016-10-02 00:01:30 +08:00
|
|
|
func TestDriverHalt(t *testing.T) {
|
|
|
|
d := initTestDriver()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Halt())
|
2014-06-14 05:18:57 +08:00
|
|
|
}
|
2014-05-24 06:09:18 +08:00
|
|
|
|
2016-10-02 00:01:30 +08:00
|
|
|
func TestDriver(t *testing.T) {
|
|
|
|
d := initTestDriver()
|
2014-12-18 06:04:18 +08:00
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "Pebble", d.Name())
|
|
|
|
assert.Equal(t, "Pebble", d.Connection().Name())
|
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")
|
2014-05-24 06:09:18 +08:00
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "Hello", d.Messages[0])
|
|
|
|
assert.Equal(t, "Hello", d.PendingMessage())
|
|
|
|
assert.Equal(t, "World", d.PendingMessage())
|
|
|
|
assert.Equal(t, "", d.PendingMessage())
|
2014-11-05 12:55:24 +08:00
|
|
|
|
2023-06-13 01:51:25 +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")
|
|
|
|
}
|
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = 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!"})
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "Hey buddy!", d.Messages[0])
|
2014-11-05 12:55:24 +08:00
|
|
|
|
|
|
|
message := d.Command("pending_message")(map[string]interface{}{})
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "Hey buddy!", message)
|
2014-06-14 05:18:57 +08:00
|
|
|
}
|