1#!perl -T
2
3use lib "t";
4use TestTools;
5use Test::More tests => 8;
6use Device::USB;
7use strict;
8use warnings;
9
10my $usb = Device::USB->new();
11ok( defined $usb, "Object successfully created" );
12
13my $bus = ($usb->list_busses())[0];
14
15SKIP:
16{
17    skip "No USB buses found.", 7 unless defined $bus;
18
19    eval { $bus->find_device_if() };
20    like( $@, qr/Missing predicate/, "Requires a predicate." );
21
22    eval { $bus->find_device_if( 1 ) };
23    like( $@, qr/Predicate must be/, "Requires a code reference." );
24
25    my $busses = $usb->list_busses();
26    ok( defined $busses, "USB busses found" );
27
28    my ($found_bus, $found_device) =
29        TestTools::find_an_installed_device_and_bus( 0, @{$busses} );
30
31    skip "No USB devices installed", 4 unless defined $found_device;
32
33    my $vendor = $found_device->idVendor();
34    my $product = $found_device->idProduct();
35
36    my $dev = $found_bus->find_device_if(
37        sub { $vendor == $_->idVendor() && $product == $_->idProduct() }
38    );
39
40    ok( defined $dev, "Device found." );
41    is_deeply( $dev, $found_device, "first device matches" );
42
43    my $count = @{$busses};
44    skip "Only one USB device installed", 2 if $count < 2;
45
46    ($found_bus, $found_device) =
47        TestTools::find_an_installed_device_and_bus( 1, @{$busses} );
48
49    skip "No accessible device found", 2 unless defined $found_device;
50    $vendor = $found_device->idVendor();
51    $product = $found_device->idProduct();
52
53    $dev = $found_bus->find_device_if(
54        sub { $vendor == $_->idVendor() && $product == $_->idProduct() }
55    );
56
57    ok( defined $dev, "Device found." );
58    is_deeply( $dev, $found_device, "second device matches" );
59}
60
61