shutter / t /Shutter /App /17_dialogs.t
runitx's picture
Claude Opus 5.5
Extract upload error and profile name dialogs into Shutter::App::Dialogs
b36e93e unverified
Raw History Blame Contribute Delete
5.83 kB
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use Gtk3; # to escape warnings "Too late to run INIT block"
use Test::More;
use Test::MockModule;
use Glib qw/ TRUE FALSE /;
use File::Temp qw/ tempdir /;
require_ok("Shutter::App::Dialogs");
can_ok("Shutter::App::Dialogs", qw/upload_error_message_gnome_vfs upload_error_message profile_name/);
# Minimal fake objects: a class whose methods are the given closures
my $fake_class_counter = 0;
sub fake_object {
my (%methods) = @_;
my $class = "Local::Fake" . ++$fake_class_counter;
no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
*{"${class}::$_"} = $methods{$_} for keys %methods;
return bless {}, $class;
}
# fake gettext: returns the msgid unchanged
my $d = fake_object(get => sub { return $_[1] });
# fake helper functions
sub fake_shf {
return fake_object(
utf8_decode => sub { return "decoded(" . $_[1] . ")" },
file_exists => sub { my ($self, $f) = @_; return -e $f },
);
}
# capture what is handed to SimpleDialogs
my @calls;
my $sd_mock = Test::MockModule->new("Shutter::App::SimpleDialogs");
$sd_mock->mock("dlg_error_message", sub { shift; push @calls, ["error", @_]; return 42 });
$sd_mock->mock("dlg_warning_message", sub { shift; push @calls, ["warning", @_]; return $main::warning_response });
my $gtk_ok = Gtk3::init_check();
subtest "upload_error_message: login error (999)" => sub {
@calls = ();
is(Shutter::App::Dialogs::upload_error_message($d, 999), 42, "returns dialog response");
is(scalar @calls, 1, "one dialog");
is_deeply($calls[0], ["error", "Please check your credentials and try again.", "Error while login"], "login error dialog args");
};
subtest "upload_error_message: max filesize (998)" => sub {
@calls = ();
Shutter::App::Dialogs::upload_error_message($d, 998, "5 MB");
is_deeply($calls[0], ["error", "Maximum filesize reached", "Error while uploading", "Skip all", "Skip", undef, undef, undef, undef, "Maximum filesize: 5 MB"], "filesize dialog args");
};
subtest "upload_error_message: other status" => sub {
@calls = ();
Shutter::App::Dialogs::upload_error_message($d, "Connection refused");
is_deeply($calls[0], ["error", "Connection refused", "Error while connecting", "Skip all", "Skip", "Retry"], "generic dialog args");
};
subtest "upload_error_message_gnome_vfs" => sub {
plan skip_all => "no display" unless $gtk_ok;
my $shf = fake_shf();
my $unescape = sub { return "unescaped(" . $_[0] . ")" };
@calls = ();
my $file = fake_object(
get_path => sub { return undef },
get_uri => sub { return "sftp://host/a%20b.png" },
get_basename => sub { return "a b.png" },
);
is(Shutter::App::Dialogs::upload_error_message_gnome_vfs($d, $shf, $unescape, $file, "details"), 42, "returns dialog response");
my @args = @{$calls[0]};
is($args[1], "Error while copying the image 'a b.png'.", "header uses basename");
is($args[2], "There was an error copying the image into 'decoded(unescaped(sftp://host/a%20b.png))'.", "falls back to uri when no path");
is($args[3], "Skip all", "extra1");
is($args[4], "Skip", "extra2");
isa_ok($args[6], "Gtk3::Button", "retry button");
is($args[6]->get_label, "_Retry", "retry button label");
is($args[9], "details", "detail text passed through");
};
subtest "profile_name" => sub {
plan skip_all => "no display" unless $gtk_ok;
my $home = tempdir(CLEANUP => 1);
local $ENV{HOME} = $home;
mkdir "$home/.shutter";
mkdir "$home/.shutter/profiles";
my $shf = fake_shf();
my $sd = Shutter::App::SimpleDialogs->new;
my ($validated_entry, $validated_hint);
# simulate the user answering the modal dialog
my $response = 'accept';
my $typed_name = undef;
my $validate = sub {
($validated_entry, $validated_hint) = @_;
# once the dialog's modal loop is running, type and answer it
Glib::Idle->add(
sub {
$validated_entry->set_text($typed_name) if defined $typed_name;
$validated_entry->get_toplevel->response($response);
return FALSE;
});
};
my $name = Shutter::App::Dialogs::profile_name(undef, $d, $sd, $shf, $validate, "current");
is($name, "current", "accept returns the prefilled current profile name");
isa_ok($validated_entry, "Gtk3::Entry", "validator got the entry");
isa_ok($validated_hint, "Gtk3::Label", "validator got the hint label");
$typed_name = "fresh";
is(Shutter::App::Dialogs::profile_name(undef, $d, $sd, $shf, $validate, undef), "fresh", "accept returns the typed name");
$response = 'reject';
$typed_name = undef;
is(Shutter::App::Dialogs::profile_name(undef, $d, $sd, $shf, $validate, "current"), FALSE, "reject returns FALSE");
# existing profile -> replace confirmation
$response = 'accept';
open my $fh, '>', "$home/.shutter/profiles/current.xml" or die $!;
close $fh;
@calls = ();
local $main::warning_response = 40;
is(Shutter::App::Dialogs::profile_name(undef, $d, $sd, $shf, $validate, "current"), "current", "replace confirmed returns name");
is($calls[0][0], "warning", "asked before replacing");
is($calls[0][2], "A profile named 'current' already exists. Do you want to replace it?", "warning text");
local $main::warning_response = 20;
is(Shutter::App::Dialogs::profile_name(undef, $d, $sd, $shf, $validate, "current"), FALSE, "replace declined returns FALSE");
};
done_testing();