File: 05-find_device_if.t

package info (click to toggle)
libdevice-usb-perl 0.38-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 408 kB
  • sloc: perl: 1,819; makefile: 11
file content (81 lines) | stat: -rw-r--r-- 2,146 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!perl -T

use lib "t";
use TestTools;
use Test::More tests => 11;
use Device::USB;
use strict;
use warnings;

my $usb = Device::USB->new();

ok( defined $usb, "Object successfully created" );
can_ok( $usb, "find_device_if" );

ok( !defined $usb->find_device_if(
            sub { 0xFFFF == $_->idVendor() && 0xFFFF == $_->idProduct() }
        ),
    "No device found"
);

eval { $usb->find_device_if() };
like( $@, qr/Missing predicate/, "Requires a predicate." );

eval { $usb->find_device_if( 1 ) };
like( $@, qr/Predicate must be/, "Requires a code reference." );

my $busses = $usb->list_busses();
ok( defined $busses, "USB busses found" );

my $found_device = TestTools::find_an_installed_device( 0, @{$busses} );

SKIP:
{
    skip "No USB devices installed", 5 unless defined $found_device;

    my $vendor = $found_device->idVendor();
    my $product = $found_device->idProduct();

    my $dev = $usb->find_device_if(
        sub { $vendor == $_->idVendor() && $product == $_->idProduct() }
    );

    ok( defined $dev, "Device found." );
    is_deeply( $dev, $found_device, "first device matches" );

    my $count = @{$busses};
    skip "Only one USB device installed", 3 if $count < 2;

    $found_device = undef;
    for(my $i = 1; $i < $count; ++$i)
    {
        my $dev = TestTools::find_an_installed_device( $i, @{$busses} );
        next unless defined $dev;

        # New vendor/product combination
        if($vendor != $dev->idVendor() || $product != $dev->idProduct())
        {
            $found_device = $dev;
            last;
        }
    }

    skip "No accessible device found", 3 unless defined $found_device;
    $vendor = $found_device->idVendor();
    $product = $found_device->idProduct();

    $dev = $usb->find_device_if(
        sub { $vendor == $_->idVendor() && $product == $_->idProduct() }
    );

    ok( defined $dev, "Device found." );
    is_deeply( $dev, $found_device, "second device matches" );

    my $hub = $usb->find_device_if(
        sub { Device::USB::CLASS_HUB == $_->bDeviceClass() }
    );
    ok( $hub && Device::USB::CLASS_HUB == $hub->bDeviceClass(),
        "Hub found."
    );
}