2014-08-01 14:39:52 +08:00
|
|
|
package mavlink
|
|
|
|
|
|
|
|
import (
|
2014-12-18 08:31:49 +08:00
|
|
|
"errors"
|
2014-08-01 14:39:52 +08:00
|
|
|
"testing"
|
2014-12-18 08:31:49 +08:00
|
|
|
"time"
|
2014-08-01 14:39:52 +08:00
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
2014-12-18 08:31:49 +08:00
|
|
|
common "github.com/hybridgroup/gobot/platforms/mavlink/common"
|
2014-08-01 14:39:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func initTestMavlinkDriver() *MavlinkDriver {
|
2014-08-05 03:06:54 +08:00
|
|
|
m := NewMavlinkAdaptor("myAdaptor", "/dev/null")
|
2014-11-20 08:03:50 +08:00
|
|
|
m.connect = func(a *MavlinkAdaptor) (err error) { return nil }
|
2014-12-18 08:31:49 +08:00
|
|
|
m.sp = nullReadWriteCloser{}
|
2014-08-05 03:06:54 +08:00
|
|
|
return NewMavlinkDriver(m, "myDriver")
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
2014-12-18 08:31:49 +08:00
|
|
|
func TestMavlinkDriver(t *testing.T) {
|
|
|
|
m := NewMavlinkAdaptor("myAdaptor", "/dev/null")
|
|
|
|
m.sp = nullReadWriteCloser{}
|
|
|
|
m.connect = func(a *MavlinkAdaptor) (err error) { return nil }
|
|
|
|
|
|
|
|
d := NewMavlinkDriver(m, "myDriver")
|
|
|
|
gobot.Assert(t, d.Name(), "myDriver")
|
|
|
|
gobot.Assert(t, d.Connection().Name(), "myAdaptor")
|
|
|
|
gobot.Assert(t, d.interval, 10*time.Millisecond)
|
|
|
|
|
|
|
|
d = NewMavlinkDriver(m, "myDriver", 100*time.Millisecond)
|
|
|
|
gobot.Assert(t, d.interval, 100*time.Millisecond)
|
|
|
|
|
|
|
|
}
|
2014-08-01 14:39:52 +08:00
|
|
|
func TestMavlinkDriverStart(t *testing.T) {
|
|
|
|
d := initTestMavlinkDriver()
|
2014-12-18 08:31:49 +08:00
|
|
|
err := make(chan error, 0)
|
|
|
|
packet := make(chan *common.MAVLinkPacket, 0)
|
|
|
|
message := make(chan common.MAVLinkMessage, 0)
|
|
|
|
|
|
|
|
gobot.Once(d.Event("packet"), func(data interface{}) {
|
|
|
|
packet <- data.(*common.MAVLinkPacket)
|
|
|
|
})
|
|
|
|
|
|
|
|
gobot.Once(d.Event("message"), func(data interface{}) {
|
|
|
|
message <- data.(common.MAVLinkMessage)
|
|
|
|
})
|
|
|
|
|
|
|
|
gobot.On(d.Event("error"), func(data interface{}) {
|
|
|
|
err <- data.(error)
|
|
|
|
})
|
2014-11-20 15:21:19 +08:00
|
|
|
gobot.Assert(t, len(d.Start()), 0)
|
2014-12-18 08:31:49 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case p := <-packet:
|
|
|
|
gobot.Assert(t, d.SendPacket(p), nil)
|
|
|
|
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Errorf("packet was not emitted")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-message:
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Errorf("message was not emitted")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-err:
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Errorf("error was not emitted")
|
|
|
|
}
|
|
|
|
|
|
|
|
payload = []byte{0xFE, 0x09, 0x4E, 0x01, 0x01, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x51, 0x04, 0x03, 0x1C, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
|
|
|
select {
|
|
|
|
case e := <-err:
|
|
|
|
gobot.Assert(t, e, errors.New("Unknown Message ID: 255"))
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Errorf("error was not emitted")
|
|
|
|
}
|
|
|
|
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMavlinkDriverHalt(t *testing.T) {
|
|
|
|
d := initTestMavlinkDriver()
|
2014-11-20 15:21:19 +08:00
|
|
|
gobot.Assert(t, len(d.Halt()), 0)
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|