Tests for OpenVidPid
This commit is contained in:
@@ -80,12 +80,13 @@ func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, erro
|
|||||||
// it will return a non-nil device and non-nil error. A Device.Close() must
|
// it will return a non-nil device and non-nil error. A Device.Close() must
|
||||||
// be called to release the device if the returned device wasn't nil.
|
// be called to release the device if the returned device wasn't nil.
|
||||||
func (c *Context) OpenDeviceWithVidPid(vid, pid int) (*Device, error) {
|
func (c *Context) OpenDeviceWithVidPid(vid, pid int) (*Device, error) {
|
||||||
dev * Device
|
var found bool
|
||||||
devs, err := ListDevices(func(desc *Descriptor) {
|
devs, err := c.ListDevices(func(desc *Descriptor) bool {
|
||||||
if dev != nil {
|
if found {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if desc.Vendor == ID(vid) && desc.Product == ID(pid) {
|
if desc.Vendor == ID(vid) && desc.Product == ID(pid) {
|
||||||
|
found = true
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
@@ -53,3 +53,32 @@ func TestListDevices(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOpenDeviceWithVidPid(t *testing.T) {
|
||||||
|
orig := libusb
|
||||||
|
defer func() { libusb = orig }()
|
||||||
|
libusb = newFakeLibusb()
|
||||||
|
|
||||||
|
for _, d := range []struct {
|
||||||
|
vid, pid int
|
||||||
|
exists bool
|
||||||
|
}{
|
||||||
|
{0x7777, 0x0003, false},
|
||||||
|
{0x8888, 0x0001, false},
|
||||||
|
{0x8888, 0x0002, true},
|
||||||
|
{0x9999, 0x0001, true},
|
||||||
|
{0x9999, 0x0002, false},
|
||||||
|
} {
|
||||||
|
c := NewContext()
|
||||||
|
dev, err := c.OpenDeviceWithVidPid(d.vid, d.pid)
|
||||||
|
if (dev != nil) != d.exists {
|
||||||
|
t.Errorf("OpenDeviceWithVidPid(%s/%s): device != nil is %v, want %v", ID(d.vid), ID(d.pid), dev != nil, d.exists)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("OpenDeviceWithVidPid(%s/%s): got error %v, want nil", ID(d.vid), ID(d.pid), err)
|
||||||
|
}
|
||||||
|
if dev != nil {
|
||||||
|
dev.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user