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
|
#!/usr/bin/perl -w
#
# Copyright (c) 2005 by the gtk2-perl team (see the file AUTHORS)
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307 USA.
#
# $Header: /cvsroot/gtk2-perl/gtk2-perl-xs/Gtk2/t/GdkPixbufLoader.t,v 1.3 2007/06/16 12:45:05 kaffeetisch Exp $
use strict;
use warnings;
use Gtk2;
use Test::More;
unless (Gtk2->CHECK_VERSION (2, 2, 0)) {
plan skip_all => "This doesn't seem to work on 2.0";
} else {
plan tests => 29;
}
# first, a helper.
sub make_ppm_data {
my ($width, $height) = @_;
my $header = "P6\n$width $height\n255\n";
my $data = pack 'a*', map { $_ % 255 } 1..($width * $height * 3);
return $header.$data;
}
# now, let's test!
my $loader = Gtk2::Gdk::PixbufLoader->new;
isa_ok ($loader, 'Glib::Object');
isa_ok ($loader, 'Gtk2::Gdk::PixbufLoader');
$loader->write (make_ppm_data (10, 15));
$loader->close;
my $pixbuf = $loader->get_pixbuf;
isa_ok ($pixbuf, 'Gtk2::Gdk::Pixbuf');
is ($pixbuf->get_width, 10, 'image width');
is ($pixbuf->get_height, 15, 'image height');
# looks like you get an animation regardless of whether the file is animated.
isa_ok ($loader->get_animation, 'Gtk2::Gdk::PixbufAnimation');
SKIP: {
skip 'get_format was added in 2.2.0', 3
if Gtk2->check_version (2, 2, 0);
my $format = $loader->get_format;
isa_ok ($format, 'HASH');
isa_ok ($format, 'Gtk2::Gdk::PixbufFormat');
is ($format->{name}, 'pnm');
}
$loader = undef;
$pixbuf = undef;
# bad explicit type should throw an exception.
eval { $loader = Gtk2::Gdk::PixbufLoader->new_with_type ('something bogus'); };
is ($loader, undef);
ok ($@, 'got an exception');
isa_ok ($@, 'Glib::Error');
is ($@->value, 'unknown-type');
foreach (Gtk2::Gdk::PixbufLoader->new_with_type ('png'),
Gtk2::Gdk::PixbufLoader::new_with_type ('png')) {
# we should get an error when writing ppm data to a png loader.
eval { $_->write (make_ppm_data (20, 20)); };
ok ($@, 'got an exception');
isa_ok ($@, 'Glib::Error');
is ($@->value, 'corrupt-image');
$_->close;
}
SKIP: {
skip 'gdk_pixbuf_loader_set_size was added in 2.2.0', 2
if Gtk2->check_version (2, 2, 0);
# set_size can be used to do load-time scaling.
$loader = Gtk2::Gdk::PixbufLoader->new;
$loader->set_size (48, 32);
$loader->write (make_ppm_data (96, 64));
$loader->close;
$pixbuf = $loader->get_pixbuf;
is ($pixbuf->get_width, 48);
is ($pixbuf->get_height, 32);
$loader = undef;
}
SKIP: {
skip 'new_with_mime_type was added in 2.4.0, but only works with 2.6.0', 4
if Gtk2->check_version (2, 6, 0);
foreach (Gtk2::Gdk::PixbufLoader->new_with_mime_type ('image/x-portable-pixmap'),
Gtk2::Gdk::PixbufLoader::new_with_mime_type ('image/x-portable-pixmap')) {
isa_ok ($_, 'Glib::Object');
isa_ok ($_, 'Gtk2::Gdk::PixbufLoader');
$_->write (make_ppm_data (64, 64));
$_->close;
$_ = undef;
}
}
# test chunked writing and signals and such
SKIP: {
my $filename = 'gtk-demo/gnome-foot.png';
skip "can't locate test image file", 4
unless -f $filename;
open IN, $filename or die "can't open $filename: $!";
binmode IN;
$loader = Gtk2::Gdk::PixbufLoader->new;
$loader->signal_connect (size_prepared => sub { ok (1, 'size-prepared') });
$loader->signal_connect (area_prepared => sub { ok (1, 'area-prepared') });
my $area_updated = 0;
$loader->signal_connect (area_updated => sub { $area_updated++ });
$loader->signal_connect (closed => sub { ok (1, 'closed') });
my $data;
while (sysread IN, $data, 64) {
$loader->write ($data);
}
ok ($area_updated, 'got some area-updated signals');
close IN;
$loader->close;
$loader = undef;
}
|