Fixing regression and adding test to ensure it is not going to happen again.

(Invalid app passed)
This commit is contained in:
sergiotarxz 2024-07-07 05:22:08 +02:00
parent 72998fa761
commit f676cd943c
2 changed files with 74 additions and 19 deletions

View File

@ -20,6 +20,7 @@ use JapaChar::Score;
use Glib::IO;
use constant PANGO_SCALE => 1024;
my $exit_the_lesson_id = 'exit-the-lesson';
Glib::Object::Introspection->setup(
basename => 'Gtk',
@ -135,30 +136,36 @@ sub create_exit_lesson_back_button( $self, $on_exit ) {
'clicked',
sub {
$back_button->set_sensitive(0);
my $dialog = Adw::AlertDialog->new( 'Exit the lessson',
'On exit you will lose your progress' );
$dialog->add_response( 'close', 'Continue' );
my $exit_the_lesson_id = 'exit-the-lesson';
$dialog->add_response( $exit_the_lesson_id, 'Exit' );
$dialog->set_response_appearance( $exit_the_lesson_id,
'destructive' );
$self->app->present_dialog($dialog);
$dialog->signal_connect(
'response',
sub( $obj, $response ) {
if ( $response eq $exit_the_lesson_id ) {
$on_exit->();
require JapaChar::View::MainMenu;
JapaChar::View::MainMenu->new( app => $self->app )->run;
return;
}
}
);
$self->_create_dialog_exit_lesson($on_exit);
}
);
return $back_button;
}
sub _create_dialog_exit_lesson( $self, $on_exit ) {
my $dialog = Adw::AlertDialog->new( 'Exit the lessson',
'On exit you will lose your progress' );
$dialog->add_response( 'close', 'Continue' );
$dialog->add_response( $exit_the_lesson_id, 'Exit' );
$dialog->set_response_appearance( $exit_the_lesson_id, 'destructive' );
$dialog->signal_connect(
'response',
sub( $obj, $response ) {
$self->_on_dialog_exit_lesson_response( $response, $on_exit );
}
);
$self->app->present_dialog($dialog);
return $dialog;
}
sub _on_dialog_exit_lesson_response( $self, $response, $on_exit ) {
if ( $response eq $exit_the_lesson_id ) {
$on_exit->();
require JapaChar::View::MainMenu;
JapaChar::View::MainMenu->new( app => $self->app )->run;
}
}
sub finish_lesson_screen($self) {
my $notable_lesson = $self->_successes >= 7;
my $feedback_label;

48
t/02-view-lesson.t Normal file
View File

@ -0,0 +1,48 @@
#!/usr/bin/env perl
use v5.38.2;
use strict;
use warnings;
use Test::Most tests => 3;
use Test::MockModule;
use Test::MockObject;
use Path::Tiny;
use File::Basename;
use lib dirname( dirname(__FILE__) ) . '/lib';
use JapaChar;
BEGIN {
use_ok 'JapaChar::View::HiraganaKatakanaLesson';
}
my $home = Path::Tiny->tempdir;
$ENV{HOME} = $home;
{
my $app = JapaChar->new;
my $lesson = JapaChar::View::HiraganaKatakanaLesson->new( app => $app );
my $called_on_exit = 0;
my $id = 'exit-the-lesson';
my $mock_main_menu = Test::MockObject->new;
$mock_main_menu->mock(run => sub {
});
my $mock_module_main_menu = Test::MockModule->new('JapaChar::View::MainMenu');
my $received_app;
$mock_module_main_menu->mock('new' => sub($self, %args) {
$received_app = $args{app};
return $mock_main_menu;
});
$lesson->_on_dialog_exit_lesson_response(
$id,
sub {
$called_on_exit = 1;
}
);
ok $called_on_exit, 'On exit dialog cleanup';
ok $received_app->can('window_set_child'), 'On exit main menu is called with a valid app as parameter';
}