1use strict;
2use Test::Simple tests => 6;
3use Image::Grab qw(grab);
4use File::Compare;
5use Cwd;
6
7use constant IMAGE => cwd . "/t/data/perl.gif";
8use constant IMAGE_URL => "file://" . IMAGE;
9use constant PAGE  => cwd . "/t/data/bkgrd.html";
10use constant PAGE_URL  => "file://" . PAGE;
11use constant BKGRD => cwd . "/t/data/background.jpg";
12use constant BKGRD_URL => "file://" . BKGRD;
13use constant OUTFILE => cwd . "/test.gif";
14
15my $pic = new Image::Grab;
16
17ok(UNIVERSAL::isa($pic, "Image::Grab"));
18
19my $image = grab(URL=>IMAGE_URL);
20open(F, ">" . OUTFILE) || die"image.jpg: $!";
21binmode F;  # for MSDOS derivations.
22print F $image;
23close F;
24ok(compare(OUTFILE, IMAGE) == 0);
25unlink OUTFILE;
26undef $pic;
27
28# You can also pass new arguments:
29my $pic2 = Image::Grab->new(SEARCH_URL=>PAGE_URL,
30                            REGEXP    =>'.*\.jpg');
31ok([$pic2->getAllURLs]->[0] eq BKGRD_URL);
32undef $pic2;
33
34# The simplest OO case of a grab
35my $pic3 = Image::Grab->new;
36$pic3->url(IMAGE_URL);
37$pic3->grab;
38ok(defined $pic3->image && $pic3->image ne ''); # Not great...
39
40# Now to save the image to disk
41open(F, ">" . OUTFILE) || die"image.jpg: $!";
42binmode F;  # for MSDOS derivations.
43print F $pic3->image;
44close F;
45ok(compare(OUTFILE, IMAGE) == 0);
46unlink OUTFILE;
47undef $pic3;
48
49my $pic4 = Image::Grab->new;
50$pic4->regexp('.*\.gif');
51$pic4->search_url(PAGE_URL);
52$pic4->grab;
53open(F, ">" . OUTFILE) || die"image.jpg: $!";
54binmode F;  # for MSDOS derivations.
55print F $pic4->image;
56close F;
57ok(compare(OUTFILE, IMAGE) == 0);
58unlink OUTFILE;
59undef $pic4;
60
61