Skip to content

Commit

Permalink
Ask before unselecting an addon
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Jan 27, 2020
1 parent 44ca353 commit 988727a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/lib/registration/widgets/package_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def search_package(text)
@search = ::Registration::PackageSearch.new(text: text)
# TRANSLATORS: searching for packages
Yast::Popup.Feedback(_("Searching..."), _("Searching for packages")) do
selected_package_names = @selected_packages.map(&:name)
selected_package_names = selected_packages.map(&:name)
@search.packages.each do |pkg|
pkg.select! if selected_package_names.include?(pkg.name)
end
Expand Down Expand Up @@ -180,17 +180,22 @@ def toggle_package
# If required, it selects the addon for registration.
def select_package(package)
addon = package.addon
# FIXME: it will crash if addon.nil?
return unless addon.registered? || addon.selected? || enable_addon?(addon)

addon.selected unless addon.registered? || addon.selected?
package.select!
@selected_packages << package
selected_packages << package
end

# Unselects the current package for installation
def unselect_package(package)
package.unselect!
@selected_packages.delete(package)
selected_packages.delete(package)
addon = package.addon
return unless addon

addon.unselected unless needed_addon?(package.addon) || !disable_addon?(addon)
end

# Updates the package details widget
Expand All @@ -201,8 +206,6 @@ def update_details

# Asks the user to enable the addon
#
# It omits the question if the addon is already registered or selected for registration.
#
# @param addon [Addon] Addon to ask about
def enable_addon?(addon)
message = format(
Expand All @@ -212,6 +215,23 @@ def enable_addon?(addon)
)
Yast::Popup.YesNo(message)
end

# Asks the user to disable the addon
#
# @param addon [Addon] Addon to ask about
def disable_addon?(addon)
message = format(
_("'%{name}' module/extension is not needed anymore.\n" \
"Do you want to unselect it?"),
name: addon.name
)
Yast::Popup.YesNo(message)
end

# Determines whether the addon is still needed
def needed_addon?(addon)
selected_packages.any? { |pkg| pkg.addon == addon }
end
end
end
end
55 changes: 55 additions & 0 deletions test/registration/widgets/package_search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,61 @@
context "when a package is selected for installation" do
let(:event) { { "WidgetID" => "remote_packages_table", "EventReason" => "Activated" } }

context "and the package is already selected" do
let(:package) do
instance_double(
Registration::RemotePackage, name: "gnome-desktop", addon: addon,
selected?: true, unselect!: nil, installed?: false
)
end

it "unselects the package" do
expect(package).to receive(:unselect!)
subject.handle(event)
end

context "and the addon is still needed" do
let(:another_package) do
instance_double(Registration::RemotePackage, name: "eog", addon: addon)
end

before do
allow(subject).to receive(:selected_packages).and_return([package, another_package])
subject.handle(event)
end

it "does not unselect the addon" do
expect(Yast::Popup).to_not receive(:YesNo)
expect(addon).to_not receive(:unselected)
subject.handle(event)
end
end

context "and the addon is not needed anymore" do
before do
allow(Yast::Popup).to receive(:YesNo).and_return(unselect?)
end

context "and the user agrees to unselect it" do
let(:unselect?) { true }

it "unselects the addon" do
expect(addon).to receive(:unselected)
subject.handle(event)
end
end

context "and the user wants to keep the addon" do
let(:unselect?) { false }

it "does not unselect the addon" do
expect(addon).to_not receive(:unselected)
subject.handle(event)
end
end
end
end

context "and the addon is already registered" do
before do
allow(addon).to receive(:registered?).and_return(true)
Expand Down

0 comments on commit 988727a

Please sign in to comment.