summaryrefslogtreecommitdiffstats
path: root/perl/t/Processor.t
blob: 92bf6fe2a3463ddac300f1e850cade8ecddececd (plain)
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
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl Processor.t'

use warnings;
use strict;
use Test::More tests => 20;

#########################

BEGIN { use_ok('Barcode::ZBar') }

Barcode::ZBar::set_verbosity(32);

#########################

my $proc = Barcode::ZBar::Processor->new();
isa_ok($proc, 'Barcode::ZBar::Processor', 'processor');

#########################

can_ok($proc, qw(init set_config parse_config));

#########################

ok(!$proc->parse_config('enable'), 'configuration');

#########################

my $cnt = 0;
my $explicit_closure = 0;

$proc->set_data_handler(sub {

    ok(!$cnt, 'handler invocations');
    $cnt += 1;

    #########################

    is($_[0], $proc, 'handler processor');

    #########################

    my $image = $_[1];
    isa_ok($image, 'Barcode::ZBar::Image', 'image');

    #########################

    my @symbols = $image->get_symbols();
    is(scalar(@symbols), 1, 'result size');

    #########################

    my $sym = $symbols[0];
    isa_ok($sym, 'Barcode::ZBar::Symbol', 'symbol');

    #########################

    is($sym->get_type(), Barcode::ZBar::Symbol::EAN13, 'result type');

    #########################

    is($sym->get_data(), '9876543210128', 'result data');

    #########################

    ok($sym->get_quality() > 0, 'quality');

    #########################

    my @loc = $sym->get_loc();
    ok(scalar(@loc) >= 4, 'location size');

    # structure checked by Image.t

    ${$_[2]} += 1
}, \$explicit_closure);

#########################

SKIP: {
    skip "no display", 3 unless defined $ENV{'DISPLAY'};

    $proc->init($ENV{VIDEO_DEVICE});
    ok(!$proc->is_visible(), 'initial visibility');

    #########################

    $proc->set_visible();
    ok($proc->is_visible(), 'enabled visiblity');

    #########################

    ok($proc->user_wait(1.1) >= 0, 'wait w/timeout');

    #########################
}

SKIP: {
    # FIXME factor out image read utility
    eval { require Image::Magick };
    skip "Image::Magick not installed", 11 if $@;
    my $im = Image::Magick->new();
    my $err = $im->Read('t/barcode.png');
    die($err) if($err);
    my $image = Barcode::ZBar::Image->new();
    $image->set_format('422P');
    $image->set_size($im->Get(qw(columns rows)));
    $image->set_data($im->ImageToBlob(
        magick => 'YUV',
        'sampling-factor' => '4:2:2',
         interlace => 'Plane')
    );

SKIP: {
    skip "no display", 11 unless defined $ENV{'DISPLAY'};

    my $rc = $proc->process_image($image);
    ok(!$rc, 'process result');

    $proc->user_wait(.9);

    #########################

    is($explicit_closure, 1, 'handler explicit closure');
    }
}

#########################

$proc->set_data_handler();
pass('unset handler');

#########################

# FIXME more processor tests

$proc = undef;
pass('cleanup');

#########################