The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!perl
use strict;
use warnings;
use Test::More;

use SDL;
use SDL::Surface;
use SDL::PixelFormat;
use SDL::Video;

use lib 't/lib';
use SDL::TestTool;

my $videodriver = $ENV{SDL_VIDEODRIVER};
$ENV{SDL_VIDEODRIVER} = 'dummy' unless $ENV{SDL_RELEASE_TESTING};

if ( !SDL::TestTool->init(SDL_INIT_VIDEO) ) {
	plan( skip_all => 'Failed to init video' );
} else {
	plan( tests => 10 );
}

use_ok('SDL::Palette');

can_ok( 'SDL::Palette', qw/ ncolors colors color_index / );

my $display = SDL::Video::set_video_mode( 640, 480, 32, SDL_SWSURFACE );

isa_ok( $display->format, 'SDL::PixelFormat', 'Are we a SDL::PixelFormat?' );

ok( !defined $display->format->palette,
	'Palette is not defined as BitPerPixels is greater then 8'
);

my $disp = SDL::Video::set_video_mode( 640, 480, 8, SDL_SWSURFACE );

SKIP:
{

	skip( 'Cannot open display: ' . SDL::get_error(), 4 ) unless ($disp);
	isa_ok( $disp->format, 'SDL::PixelFormat', 'Are we a SDL::PixelFormat?' );

	isa_ok(
		$disp->format->palette, 'SDL::Palette',
		'Palette is SDL::Palette when BitPerPixels is 8 '
	);

	is( $disp->format->palette->ncolors, 256, '256 colors in palette' );

	my $colors = $disp->format->palette->colors();
	isa_ok( $colors, 'ARRAY', 'Palette->colors is an array' );

	isa_ok( $colors->[0], 'SDL::Color', 'Palette->colors[x] is an SDL::Color' );

	isa_ok(
		$disp->format->palette->color_index(23),
		'SDL::Color', 'Palette->color_index() is a SDL::Color'
	);
}

if ($videodriver) {
	$ENV{SDL_VIDEODRIVER} = $videodriver;
} else {
	delete $ENV{SDL_VIDEODRIVER};
}

sleep(2);