diff --git a/WWW-WebKit/Changes b/WWW-WebKit/Changes index a713f9d..7ab261b 100644 --- a/WWW-WebKit/Changes +++ b/WWW-WebKit/Changes @@ -1,3 +1,7 @@ +0.11 2017-07-25 11:11:00 CET + * Make window size configurable by using attributes + * Croak if an object is dragged out of the window/screen + 0.10 2016-12-13 16:45:00 CET * Fix compatibility with libwebkitgtk-3_0-0 version > 2.4.8 diff --git a/WWW-WebKit/lib/WWW/WebKit.pm b/WWW-WebKit/lib/WWW/WebKit.pm index 951c995..9544664 100644 --- a/WWW-WebKit/lib/WWW/WebKit.pm +++ b/WWW-WebKit/lib/WWW/WebKit.pm @@ -40,7 +40,7 @@ use XSLoader; use English '-no_match_vars'; use POSIX qw; -our $VERSION = '0.10'; +our $VERSION = '0.11'; use constant DOM_TYPE_ELEMENT => 1; use constant ORDERED_NODE_SNAPSHOT_TYPE => 7; @@ -78,6 +78,18 @@ has scrolled_view => ( } ); +has window_width => ( + is => 'ro', + isa => 'Int', + default => 1600, +); + +has window_height => ( + is => 'ro', + isa => 'Int', + default => 1200, +); + has window => ( is => 'ro', isa => 'Gtk3::Window', @@ -91,7 +103,7 @@ has window => ( my $win = Gtk3::Window->new; $win->set_title($self->window_title); - $win->set_default_size(1600, 1200); + $win->set_default_size($self->window_width, $self->window_height); $win->add($sw); return $win; @@ -315,7 +327,8 @@ sub setup_xvfb { open STDERR, '>', '/dev/null' or die "Cant' open STDERR: $!"; - system ("Xvfb -nolisten tcp -terminate -screen 0 1600x1200x24 -displayfd $writefd &"); + my $screen_dimensions = join 'x', $self->window_width, $self->window_height, 24; + system ("Xvfb -nolisten tcp -terminate -screen 0 $screen_dimensions -displayfd $writefd &"); open STDERR, '>&', $stderr or die "Can't open STDERR: $!"; @@ -1065,21 +1078,23 @@ sub wait_for_condition { return $result; } -=head3 native_drag_and_drop_to_position($source, $target_x, $target_y, $options) +=head3 native_drag_and_drop_to_position($source_locator, $target_x, $target_y, $options) -Drag and drop $source to position ($target_x and $target_y). +Drag source element and drop it to position $target_x, $target_y. =cut sub native_drag_and_drop_to_position { - my ($self, $source, $target_x, $target_y, $options) = @_; + my ($self, $source_locator, $target_x, $target_y, $options) = @_; + $self->check_window_bounds($target_x, $target_y, "target"); my $steps = $options->{steps} // 5; my $step_delay = $options->{step_delay} // 150; # ms $self->event_send_delay($options->{event_send_delay}) if $options->{event_send_delay}; - $source = $self->resolve_locator($source); + my $source = $self->resolve_locator($source_locator); my ($source_x, $source_y) = $self->get_center_screen_position($source); + $self->check_window_bounds($source_x, $source_y, "source '$source_locator'"); my ($delta_x, $delta_y) = ($target_x - $source_x, $target_y - $source_y); my ($step_x, $step_y) = (int($delta_x / $steps), int($delta_y / $steps)); @@ -1105,9 +1120,9 @@ sub native_drag_and_drop_to_position { $self->process_page_load; } -=head3 native_drag_and_drop_to_object($source, $target, $options) +=head3 native_drag_and_drop_to_object($source_locator, $target_locator, $options) -Drag and drop $source to $target. +Drag source element and drop it into target element. =cut @@ -1124,6 +1139,7 @@ sub native_drag_and_drop_to_object { my $source = $self->resolve_locator($source_locator) or croak "did not find element $source_locator"; my ($x, $y) = $self->get_center_screen_position($source); + $self->check_window_bounds($x, $y, "source '$source_locator'"); $self->pause($step_delay); $self->move_mouse_abs($x, $y); @@ -1131,9 +1147,10 @@ sub native_drag_and_drop_to_object { $self->press_mouse_button(1); $self->pause($step_delay); - foreach (0 .. $steps - 1) { - my ($target_x, $target_y) = $self->get_center_screen_position($target); + my ($target_x, $target_y) = $self->get_center_screen_position($target); + $self->check_window_bounds($target_x, $target_y, "target '$target_locator'"); + foreach (0 .. $steps - 1) { my $delta_x = $target_x - $x; my $delta_y = $target_y - $y; my $step_x = int($delta_x / ($steps - $_)); @@ -1157,6 +1174,19 @@ sub native_drag_and_drop_to_object { $self->process_page_load; } +sub check_window_bounds { + my ($self, $x, $y, $obj_description) = @_; + + my ($max_x, $max_y) = ($self->window_width, $self->window_height); + if ($x > $max_x or $y > $max_y) { + croak + "$obj_description out of bounds (position: $x, $y - window bounds: $max_x x $max_y). " + . "Raise window_width/window_height!" + } + + return 1; +} + sub move_mouse_abs { my ($self, $x, $y) = @_;