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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
use Image::Sane ':all';
use Test::More;
use Try::Tiny;
BEGIN { use_ok('Image::Sane') }
#########################
plan skip_all => 'libsane 1.0.19 or better required'
unless Image::Sane->get_version_scalar > 1.000018;
my @array = Image::Sane->get_version;
is( $#array, 2, 'get_version' );
try {
@array = Image::Sane->get_devices;
pass 'get_devices';
}
catch {
fail 'get_devices';
};
my $test;
try {
$test = Image::Sane::Device->open('test');
pass 'open';
}
catch {
fail 'open';
};
my $n = $test->get_option(0);
isnt( $n, 0, 'get number of options' );
my $options = $test->get_option_descriptor(21);
my $status;
if ( $options->{name} eq 'enable-test-options' ) {
try {
$info = $test->set_option( 21, SANE_TRUE );
pass 'enable-test-options';
}
catch {
fail 'enable-test-options';
};
for ( my $i = 0 ; $i < $n ; $i++ ) {
my $options = $test->get_option_descriptor($i);
isnt( $options, undef, 'get_option_descriptor' );
if ( $options->{cap} & SANE_CAP_SOFT_SELECT ) {
my $in;
if ( $options->{constraint_type} == SANE_CONSTRAINT_RANGE ) {
if ( $options->{max_values} == 1 ) {
$in = $options->{constraint}{min};
}
else {
for ( my $i = 0 ; $i < $options->{max_values} ; $i++ ) {
$in->[$i] = $options->{constraint}{min};
}
}
}
elsif ($options->{constraint_type} == SANE_CONSTRAINT_STRING_LIST
or $options->{constraint_type} == SANE_CONSTRAINT_WORD_LIST )
{
if ( $options->{max_values} == 1 ) {
$in = $options->{constraint}[0];
}
else {
for ( my $i = 0 ; $i < $options->{max_values} ; $i++ ) {
$in->[$i] = $options->{constraint}[0];
}
}
}
elsif ($options->{type} == SANE_TYPE_BOOL
or $options->{type} == SANE_TYPE_BUTTON )
{
$in = SANE_TRUE;
}
elsif ( $options->{type} == SANE_TYPE_STRING ) {
$in = 'this is a string with no constraint';
}
elsif ( $options->{type} == SANE_TYPE_INT ) {
if ( $options->{max_values} == 1 ) {
$in = 12345678;
}
else {
for ( my $i = 0 ; $i < $options->{max_values} ; $i++ ) {
$in->[$i] = 12345678;
}
}
}
elsif ( $options->{type} == SANE_TYPE_FIXED ) {
if ( $options->{max_values} == 1 ) {
$in = 1234.5678;
}
else {
for ( my $i = 0 ; $i < $options->{max_values} ; $i++ ) {
$in->[$i] = 1234.5678;
}
}
}
if ( defined $in ) {
SKIP: {
skip 'Pressing buttons produces too much output', 1
if $options->{type} == SANE_TYPE_BUTTON;
$status = SANE_STATUS_GOOD;
try {
$info = $test->set_option( $i, $in );
}
catch {
$status = $_->status;
};
}
if ( $options->{cap} & SANE_CAP_INACTIVE ) {
is( $status, SANE_STATUS_INVAL,
"set_option $options->{name}" );
}
else {
is( $status, SANE_STATUS_GOOD,
"set_option $options->{name}" );
}
if ( $options->{type} != SANE_TYPE_BUTTON ) {
$status = SANE_STATUS_GOOD;
try {
$out = $test->get_option($i);
}
catch {
$status = $_->status;
};
if ( $options->{cap} & SANE_CAP_INACTIVE ) {
is( $status, SANE_STATUS_INVAL,
"get_option $options->{name}" );
}
elsif ( $info & SANE_INFO_INEXACT ) {
is( $status, SANE_STATUS_GOOD,
"get_option $options->{name}" );
}
elsif ( $options->{type} == SANE_TYPE_FIXED ) {
if ( $in == 0 ) {
is( abs($out) < 1.e-6,
1, "get_option $options->{name}" );
}
else {
is( abs( $out - $in ) / $in < 1.e-6,
1, 'get_option' );
}
}
else {
is_deeply( $out, $in, 'get_option' );
}
}
}
}
if ( $options->{cap} & SANE_CAP_AUTOMATIC
and not $options->{cap} & SANE_CAP_INACTIVE )
{
try {
$info = $test->set_auto($i);
pass 'set_auto';
}
catch {
fail 'set_auto';
}
}
}
}
done_testing();
|