From 6a39fcaffa22b33be09628f508bdb35654f3f457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Tue, 18 Sep 2018 09:27:02 +0100 Subject: [PATCH] Add a CWM::Popup widget class * Extracted from yast2-storage-ng. --- library/cwm/src/Makefile.am | 1 + library/cwm/src/lib/cwm/popup.rb | 104 +++++++++++++++++++++++++++++++ library/cwm/test/Makefile.am | 1 + library/cwm/test/popup_test.rb | 63 +++++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 library/cwm/src/lib/cwm/popup.rb create mode 100644 library/cwm/test/popup_test.rb diff --git a/library/cwm/src/Makefile.am b/library/cwm/src/Makefile.am index 93dfc0972..6a612701f 100644 --- a/library/cwm/src/Makefile.am +++ b/library/cwm/src/Makefile.am @@ -18,6 +18,7 @@ ycwm_DATA = \ lib/cwm/dialog.rb \ lib/cwm/page.rb \ lib/cwm/pager.rb \ + lib/cwm/popup.rb \ lib/cwm/replace_point.rb \ lib/cwm/rspec.rb \ lib/cwm/table.rb \ diff --git a/library/cwm/src/lib/cwm/popup.rb b/library/cwm/src/lib/cwm/popup.rb new file mode 100644 index 000000000..47ed6b657 --- /dev/null +++ b/library/cwm/src/lib/cwm/popup.rb @@ -0,0 +1,104 @@ +# encoding: utf-8 + +# Copyright (c) [2018] SUSE LLC +# +# All Rights Reserved. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of version 2 of the GNU General Public License as published +# by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, contact SUSE LLC. +# +# To contact SUSE LLC about this file by physical or electronic mail, you may +# find current contact information at www.suse.com. +require "cwm/dialog" + +module CWM + # CWM pop-up dialog + # + # This class offers a CWM dialog which behaves as a pop-up. + class Popup < Dialog + # Determines that a dialog should always be open + # + # @return [true] + # + # @see CWM::Dialog#wizard_create_dialog + def should_open_dialog? + true + end + + private + + # Redefines the mechanism to open the dialog to use the adapted layout + # + # @param block [Proc] + # @see CWM::Dialog#wizard_create_dialog + def wizard_create_dialog(&block) + Yast::UI.OpenDialog(layout) + block.call + ensure + Yast::UI.CloseDialog + end + + # Defines the dialog's layout + # + # @return [Yast::Term] + def layout + VBox( + HSpacing(50), + Left(Heading(Id(:title), title)), + VStretch(), + VSpacing(1), + MinSize(min_width, min_height, ReplacePoint(Id(:contents), Empty())), + VSpacing(1), + VStretch(), + ButtonBox(*buttons) + ) + end + + # Popup min width + # + # @return [Integer] + def min_width + 1 + end + + # Popup min height + # + # @return [Integer] + def min_height + 1 + end + + def ok_button_label + Yast::Label.OKButton + end + + def cancel_button_label + Yast::Label.CancelButton + end + + def buttons + [help_button, ok_button, cancel_button] + end + + def help_button + PushButton(Id(:help), Opt(:helpButton), Yast::Label.HelpButton) + end + + def ok_button + PushButton(Id(:ok), Opt(:default), ok_button_label) + end + + def cancel_button + PushButton(Id(:cancel), cancel_button_label) + end + end +end diff --git a/library/cwm/test/Makefile.am b/library/cwm/test/Makefile.am index 438fd6924..680850100 100644 --- a/library/cwm/test/Makefile.am +++ b/library/cwm/test/Makefile.am @@ -4,6 +4,7 @@ TESTS = \ custom_widget_test.rb \ dialog_test.rb \ pager_test.rb \ + popup_test.rb \ replace_point_test.rb \ table_test.rb \ tree_test.rb \ diff --git a/library/cwm/test/popup_test.rb b/library/cwm/test/popup_test.rb new file mode 100644 index 000000000..bbfbe1ba8 --- /dev/null +++ b/library/cwm/test/popup_test.rb @@ -0,0 +1,63 @@ +#!/usr/bin/env rspec +# encoding: utf-8 + +# Copyright (c) [2018] SUSE LLC +# +# All Rights Reserved. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of version 2 of the GNU General Public License as published +# by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, contact SUSE LLC. +# +# To contact SUSE LLC about this file by physical or electronic mail, you may +# find current contact information at www.suse.com. + +require_relative "test_helper" + +require "cwm/popup" +require "cwm/rspec" + +describe CWM::Popup do + class TestCWMPopup < CWM::Popup + def contents + VBox() + end + end + + subject { TestCWMPopup.new } + + include_examples "CWM::Dialog" + + describe ".run" do + before do + allow(Yast::Wizard).to receive(:IsWizardDialog).and_return(wizard_dialog?) + allow(Yast::CWM).to receive(:show).and_return(:launch) + end + + context "when running on a wizard" do + let(:wizard_dialog?) { true } + + it "always opens a dialog" do + expect(Yast::UI).to receive(:OpenDialog).with(Yast::Term) + TestCWMPopup.run + end + end + + context "when not running on a wizard" do + let(:wizard_dialog?) { false } + + it "always opens a dialog" do + expect(Yast::UI).to receive(:OpenDialog).with(Yast::Term) + TestCWMPopup.run + end + end + end +end