From f611572da1e20669b19334d2223faaa405c65ed7 Mon Sep 17 00:00:00 2001 From: Emmanuel Rodriguez Date: Mon, 23 Mar 2009 21:58:14 +0100 Subject: [PATCH] New unit tests --- bindings/perl/Champlain/t/ChamplainMarker.t | 77 +++++++++ .../Champlain/t/ChamplainNetworkMapSource.t | 48 ++++++ bindings/perl/Champlain/t/ChamplainTile.t | 89 +++++++++++ bindings/perl/Champlain/t/ChamplainView.t | 150 ++++++++++++++++++ .../perl/Champlain/t/Gtk2ChamplainViewEmbed.t | 29 ++++ 5 files changed, 393 insertions(+) create mode 100644 bindings/perl/Champlain/t/ChamplainMarker.t create mode 100644 bindings/perl/Champlain/t/ChamplainNetworkMapSource.t create mode 100644 bindings/perl/Champlain/t/ChamplainTile.t create mode 100644 bindings/perl/Champlain/t/ChamplainView.t create mode 100644 bindings/perl/Champlain/t/Gtk2ChamplainViewEmbed.t diff --git a/bindings/perl/Champlain/t/ChamplainMarker.t b/bindings/perl/Champlain/t/ChamplainMarker.t new file mode 100644 index 0000000..71f0650 --- /dev/null +++ b/bindings/perl/Champlain/t/ChamplainMarker.t @@ -0,0 +1,77 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Clutter::TestHelper tests => 7; + +use Champlain ':coords'; +use File::Spec; + +# Path to a valid image +my $FILENAME = File::Spec->catfile('examples', 'images', 'who.png'); + + +exit tests(); + + +sub tests { + test_new(); + test_new_with_label(); + test_new_with_image(); + test_new_with_image_full(); + return 0; +} + + +sub test_new { + my $marker = Champlain::Marker->new(); + isa_ok($marker, 'Champlain::Marker'); +} + + +sub test_new_with_label { + # Create a label without specifying the colors + my $marker_label = Champlain::Marker->new_with_label( + "Home", "Airmole 14", undef, undef + ); + isa_ok($marker_label, 'Champlain::Marker'); + + # Create a label without specifying the colors + my $marker_label_color = Champlain::Marker->new_with_label( + "Home", "Airmole 14", + Clutter::Color->new(0xf3, 0x94, 0x07, 0xbb), # orange + Clutter::Color->new(0xff, 0xff, 0xff, 0xff), + ); + isa_ok($marker_label_color, 'Champlain::Marker'); +} + + +sub test_new_with_image { + my $marker = Champlain::Marker->new_with_image($FILENAME); + isa_ok($marker, 'Champlain::Marker'); + + # Assert that using a file that doesn't exist throws an exception + eval { + $marker = Champlain::Marker->new_with_image("does-not-exist.gif"); + }; + isa_ok($@, "Glib::File::Error"); +} + + +sub test_new_with_image_full { + my $marker = Champlain::Marker->new_with_image_full( + $FILENAME, + 64, 64, # width, height + 10, 10 # x, y + ); + isa_ok($marker, 'Champlain::Marker'); + + # Assert that using a file that doesn't exist throws an exception + eval { + $marker = Champlain::Marker->new_with_image_full( + "does-not-exist.gif", 10, 10, 1, 1 + ); + }; + isa_ok($@, "Glib::File::Error"); +} diff --git a/bindings/perl/Champlain/t/ChamplainNetworkMapSource.t b/bindings/perl/Champlain/t/ChamplainNetworkMapSource.t new file mode 100644 index 0000000..8976778 --- /dev/null +++ b/bindings/perl/Champlain/t/ChamplainNetworkMapSource.t @@ -0,0 +1,48 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Clutter::TestHelper tests => 3; + +use Champlain ':coords'; + + +exit tests(); + + +sub tests { + test_all(); + return 0; +} + + +sub test_all { + my $map_source = Champlain::NetworkMapSource->new_full( + 'fake-map', + 'free', + 'http://www.it-is-free.org/license.txt', + 0, + 10, + 128, + 'mercator', + 'http://www.it-is-free.org/tiles/#Z#/#X#-#Y#.png' + ); + isa_ok($map_source, 'Champlain::NetworkMapSource'); + + + is( + $map_source->get_tile_uri(100, 200, 3), + 'http://www.it-is-free.org/tiles/3/100-200.png', + "get_tile_uri()" + ); + + + # Change the tile URI and check that there's a change + $map_source->set_tile_uri('http://www.it-is-not-free.org/hidden/#X#-#Y#_#Z#.png'); + is( + $map_source->get_tile_uri(50, 75, 6), + 'http://www.it-is-not-free.org/hidden/50-75_6.png', + "get_tile_uri() changed" + ); +} diff --git a/bindings/perl/Champlain/t/ChamplainTile.t b/bindings/perl/Champlain/t/ChamplainTile.t new file mode 100644 index 0000000..96a36be --- /dev/null +++ b/bindings/perl/Champlain/t/ChamplainTile.t @@ -0,0 +1,89 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Clutter::TestHelper tests => 32; + +use Champlain ':coords'; + + +exit tests(); + + +sub tests { + test_new_empty(); + test_new_full(); + return 0; +} + + +sub test_new_full { + my $tile = Champlain::Tile->new_full(50, 75, 514, 1); + isa_ok($tile, 'Champlain::Tile'); + + is($tile->get_x(), 50, "get_x() full tile"); + is($tile->get_y(), 75, "get_y() full tile"); + is($tile->get_zoom_level(), 1, "get_zoom_level() full tile"); + is($tile->get_size(), 514, "get_size() full tile"); + is($tile->get_state(), 'init', "get_state() full tile"); + is($tile->get_uri(), '', "get_uri() full tile"); + is($tile->get_filename(), '', "get_filename() full tile"); + is($tile->get_actor(), undef, "get_actor() full tile"); + + test_all_setters($tile); +} + + +sub test_new_empty { + my $tile = Champlain::Tile->new(); + isa_ok($tile, 'Champlain::Tile'); + + is($tile->get_x(), 0, "get_x() default tile"); + is($tile->get_y(), 0, "get_y() default tile"); + is($tile->get_zoom_level(), 0, "get_zoom_level() default tile"); + is($tile->get_size(), 0, "get_size() default tile"); + is($tile->get_state(), 'init', "get_state() default tile"); + is($tile->get_uri(), '', "get_uri() default tile"); + is($tile->get_filename(), '', "get_filename() default tile"); + is($tile->get_actor(), undef, "get_actor() default tile"); + + test_all_setters($tile); +} + + +sub test_all_setters { + my $tile = Champlain::Tile->new(); + + $tile->set_x(100); + is($tile->get_x(), 100, "set_x()"); + + $tile->set_y(250); + is($tile->get_y(), 250, "set_y()"); + + $tile->set_zoom_level(2); + is($tile->get_zoom_level(), 2, "set_zoom_level()"); + + $tile->set_size(128); + is($tile->get_size(), 128, "set_size()"); + + $tile->set_state('done'); + is($tile->get_state(), 'done', "set_state()"); + + $tile->set_uri('http://localhost/tile/2/100-250.png'); + is($tile->get_uri(), 'http://localhost/tile/2/100-250.png', "set_uri()"); + + my $actor = Clutter::Ex::DeadActor->new(); + $tile->set_actor($actor); + is($tile->get_actor(), $actor, "set_actor()"); +} + + +# +# An empty actor. +# +package Clutter::Ex::DeadActor; +use Glib::Object::Subclass 'Clutter::Actor',; + +# This is a an empty actor. This class is needed because Clutter::Actor is an +# abstract class. diff --git a/bindings/perl/Champlain/t/ChamplainView.t b/bindings/perl/Champlain/t/ChamplainView.t new file mode 100644 index 0000000..214e707 --- /dev/null +++ b/bindings/perl/Champlain/t/ChamplainView.t @@ -0,0 +1,150 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Clutter::TestHelper tests => 25; + +use Champlain ':coords'; + + +exit tests(); + + +sub tests { + test_generic(); + test_zoom(); + test_event(); + return 0; +} + + +# +# Test some default functionality +# +sub test_generic { + my $view = Champlain::View->new(); + isa_ok($view, 'Champlain::View'); + + my $stage = Clutter::Stage->get_default(); + $stage->set_size(800, 600); + $view->set_size(800, 600); + $stage->add($view); + + # center_on() can be tested by checking the properties latitude and longitude. + # And even then, the values set are not the ones returned + my $latitude = $view->get('latitude'); + my $longitude = $view->get('longitude'); + $view->center_on(48.144722, 17.112778); + ok($view->get('latitude') != $latitude, "center_on() changed latitude"); + ok($view->get('longitude') != $longitude, "center_on() changed longitude"); + + # set_size() can be tested by checking the properties width and height + is($view->get('width'), 800, "original width"); + is($view->get('height'), 600, "original height"); + $view->set_size(600, 400); + is($view->get('width'), 600, "set_size() changed width"); + is($view->get('height'), 400, "set_size() changed height"); + + + # Can't be tested but at least we check that it doesn't crash when invoked + my $layer = Champlain::Layer->new(); + $view->add_layer($layer); + + + # Change the map source (get a different map source) + my $source_original = $view->get('map-source'); + my $source_new = Champlain::MapSource->new_osm_mapnik(); + if ($source_original->get_name eq $source_new->get_name) { + # Same kind of map source, take another one + $source_new = Champlain::MapSource->new_oam(); + } + $view->set_map_source($source_new); + is($view->get('map-source'), $source_new, "Change map source"); +} + + +# +# Test the zoom functionality +# +sub test_zoom { + my $view = Champlain::View->new(); + isa_ok($view, 'Champlain::View'); + + + # Zoom in + is($view->get('zoom-level'), 0, "original zoom-level"); + $view->zoom_in(); + is($view->get('zoom-level'), 1, "zoom-in once"); + $view->zoom_in(); + is($view->get('zoom-level'), 2, "zoom-in twice"); + + # Zoom out + $view->zoom_out(); + is($view->get('zoom-level'), 1, "zoom-out once"); + $view->zoom_out(); + is($view->get('zoom-level'), 0, "zoom-out twice"); + + my $map_source = $view->get('map-source'); + + # Zoom out past the min zoom level + my $min = $map_source->get_min_zoom_level; + $view->set_zoom_level($min); + is($view->get('zoom-level'), $min, "zoom-out to the minimal level"); + $view->zoom_out(); + is($view->get('zoom-level'), $min, "zoom-out past minimal level has no effect"); + + + # Zoom in after the max zoom level + my $max = $map_source->get_max_zoom_level; + $view->set_zoom_level($max); + is($view->get('zoom-level'), $max, "zoom-in to the maximal level"); + $view->zoom_in(); + is($view->get('zoom-level'), $max, "zoom-in past maximal level has no effect"); + + + # Try to set directly the zoom level to a value inferior to min level + $view->set_zoom_level($min - 1); + is($view->get('zoom-level'), $min, "set zoom (min - 1)"); + + # Try to set directly the zoom level to a valu superior to max level + $view->set_zoom_level($max + 1); + is($view->get('zoom-level'), $max, "set zoom (max + 1)"); +} + + +# +# Test getting the coordinates from an event. +# +# This tests simulates that the user clicked at the coordinate (0, 0) (where +# Greenwich meets the Equator). In order to simulate this, the test sets the +# view to be as big as the first tile and will simulate a click in the middle of +# the tile. Because the computations are made with a projection a slight error +# threshold will be accepted. +# +sub test_event { + my $view = Champlain::View->new(); + isa_ok($view, 'Champlain::View'); + + my $map_source = $view->get('map-source'); + my $size = $map_source->get_tile_size; + + # NOTE: At the moment this works only if the view is in a stage and if + # show_all() was called + my $stage = Clutter::Stage->get_default(); + $stage->set_size($size, $size); + $view->set_size($size, $size); + $stage->add($view); + $stage->show_all(); + + # Create a fake event in the middle of the tile + my $event = Clutter::Event->new('button_press'); + $event->x($size/2); + $event->y($size/2); + is($event->x, $size/2); + is($event->y, $size/2); + + my ($latitude, $longitude) = $view->get_coords_from_event($event); + ok($latitude >= -2.0 && $latitude <= 2.0, "get_coords_from_event() latitude"); + ok($longitude >= -2.0 && $longitude <= 2.0, "get_coords_from_event() longitude"); +} diff --git a/bindings/perl/Champlain/t/Gtk2ChamplainViewEmbed.t b/bindings/perl/Champlain/t/Gtk2ChamplainViewEmbed.t new file mode 100644 index 0000000..a815ad5 --- /dev/null +++ b/bindings/perl/Champlain/t/Gtk2ChamplainViewEmbed.t @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Clutter::TestHelper tests => 3, sub_module => 'gtk'; +use Gtk2 '-init'; + +use Champlain ':coords'; + + +exit tests(); + + +sub tests { + test_all(); + return 0; +} + +sub test_all { + + my $view = Champlain::View->new(); + isa_ok($view, 'Champlain::View'); + + my $embed = Gtk2::Champlain::ViewEmbed->new($view); + isa_ok($embed, 'Gtk2::Champlain::ViewEmbed'); + + is($embed->get_view, $view, "get_view()"); +} -- 2.39.5