2014-07-31 23:39:52 -07:00
|
|
|
package mavlink
|
|
|
|
|
|
|
|
import (
|
2014-12-18 15:27:23 -08:00
|
|
|
"io"
|
2017-04-06 10:52:59 +02:00
|
|
|
"strings"
|
2014-07-31 23:39:52 -07:00
|
|
|
"testing"
|
2014-12-17 16:31:49 -08:00
|
|
|
"time"
|
2014-07-31 23:39:52 -07:00
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/gobottest"
|
|
|
|
common "gobot.io/x/gobot/v2/platforms/mavlink/common"
|
2014-07-31 23:39:52 -07:00
|
|
|
)
|
|
|
|
|
2016-09-25 21:44:40 +02:00
|
|
|
var _ gobot.Driver = (*Driver)(nil)
|
2016-08-27 11:56:01 +02:00
|
|
|
|
2016-09-25 21:44:40 +02:00
|
|
|
func initTestMavlinkDriver() *Driver {
|
|
|
|
m := NewAdaptor("/dev/null")
|
2014-12-18 15:27:23 -08:00
|
|
|
m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil }
|
2014-12-17 16:31:49 -08:00
|
|
|
m.sp = nullReadWriteCloser{}
|
2016-09-25 21:44:40 +02:00
|
|
|
return NewDriver(m)
|
2014-07-31 23:39:52 -07:00
|
|
|
}
|
|
|
|
|
2014-12-17 16:31:49 -08:00
|
|
|
func TestMavlinkDriver(t *testing.T) {
|
2016-09-25 21:44:40 +02:00
|
|
|
m := NewAdaptor("/dev/null")
|
2014-12-17 16:31:49 -08:00
|
|
|
m.sp = nullReadWriteCloser{}
|
2014-12-18 15:27:23 -08:00
|
|
|
m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil }
|
2014-12-17 16:31:49 -08:00
|
|
|
|
2016-09-25 21:44:40 +02:00
|
|
|
d := NewDriver(m)
|
|
|
|
gobottest.Refute(t, d.Connection(), nil)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, d.interval, 10*time.Millisecond)
|
2014-12-17 16:31:49 -08:00
|
|
|
|
2016-09-25 21:44:40 +02:00
|
|
|
d = NewDriver(m, 100*time.Millisecond)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, d.interval, 100*time.Millisecond)
|
2014-12-17 16:31:49 -08:00
|
|
|
}
|
2016-08-30 17:10:50 +02:00
|
|
|
|
2017-04-06 10:52:59 +02:00
|
|
|
func TestMavlinkDriverName(t *testing.T) {
|
|
|
|
d := initTestMavlinkDriver()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Mavlink"), true)
|
|
|
|
d.SetName("NewName")
|
|
|
|
gobottest.Assert(t, d.Name(), "NewName")
|
|
|
|
}
|
|
|
|
|
2014-07-31 23:39:52 -07:00
|
|
|
func TestMavlinkDriverStart(t *testing.T) {
|
|
|
|
d := initTestMavlinkDriver()
|
2023-05-19 14:16:22 +02:00
|
|
|
err := make(chan error)
|
|
|
|
packet := make(chan *common.MAVLinkPacket)
|
|
|
|
message := make(chan common.MAVLinkMessage)
|
2014-12-17 16:31:49 -08:00
|
|
|
|
2016-09-12 18:01:31 +02:00
|
|
|
d.On(PacketEvent, func(data interface{}) {
|
2014-12-17 16:31:49 -08:00
|
|
|
packet <- data.(*common.MAVLinkPacket)
|
|
|
|
})
|
2016-09-12 18:01:31 +02:00
|
|
|
d.On(MessageEvent, func(data interface{}) {
|
2014-12-17 16:31:49 -08:00
|
|
|
message <- data.(common.MAVLinkMessage)
|
|
|
|
})
|
2016-09-12 18:01:31 +02:00
|
|
|
d.On(ErrorIOEvent, func(data interface{}) {
|
2014-12-18 12:54:07 -08:00
|
|
|
err <- data.(error)
|
|
|
|
})
|
2016-09-12 18:01:31 +02:00
|
|
|
d.On(ErrorMAVLinkEvent, func(data interface{}) {
|
2014-12-17 16:31:49 -08:00
|
|
|
err <- data.(error)
|
|
|
|
})
|
2014-12-18 12:54:07 -08:00
|
|
|
|
2016-11-07 19:44:57 +01:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
2014-12-17 16:31:49 -08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case p := <-packet:
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, d.SendPacket(p), nil)
|
2014-12-17 16:31:49 -08:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2014-07-31 23:39:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMavlinkDriverHalt(t *testing.T) {
|
|
|
|
d := initTestMavlinkDriver()
|
2016-11-07 19:44:57 +01:00
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2014-07-31 23:39:52 -07:00
|
|
|
}
|