From f676cd943c9fade0268be5d64c8549985ed7a5c9 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Sun, 7 Jul 2024 05:22:08 +0200 Subject: [PATCH] Fixing regression and adding test to ensure it is not going to happen again. (Invalid app passed) --- lib/JapaChar/View/HiraganaKatakanaLesson.pm | 45 +++++++++++-------- t/02-view-lesson.t | 48 +++++++++++++++++++++ 2 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 t/02-view-lesson.t diff --git a/lib/JapaChar/View/HiraganaKatakanaLesson.pm b/lib/JapaChar/View/HiraganaKatakanaLesson.pm index d458f4d..451974f 100644 --- a/lib/JapaChar/View/HiraganaKatakanaLesson.pm +++ b/lib/JapaChar/View/HiraganaKatakanaLesson.pm @@ -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; diff --git a/t/02-view-lesson.t b/t/02-view-lesson.t new file mode 100644 index 0000000..6609205 --- /dev/null +++ b/t/02-view-lesson.t @@ -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'; +}