Skip to content

Commit

Permalink
Extract OverviewTreePager to its own file and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Sep 20, 2018
1 parent 06176ff commit 74e8417
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/y2firewall/dialogs/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

require "yast"
require "cwm/dialog"
require "y2firewall/widgets/overview"
require "y2firewall/widgets/overview_tree_pager"

Yast.import "Label"

Expand Down
1 change: 0 additions & 1 deletion src/lib/y2firewall/widgets/overview_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
# ------------------------------------------------------------------------------

require "yast"
require "cwm/widget"
require "cwm/tree"

module Y2Firewall
Expand Down
102 changes: 102 additions & 0 deletions src/lib/y2firewall/widgets/overview_tree_pager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# 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 "yast"
require "cwm/tree_pager"
require "y2firewall/firewalld"
require "y2firewall/widgets/overview_tree"
require "y2firewall/widgets/pages"
require "y2firewall/helpers/interfaces"

module Y2Firewall
module Widgets
# Widget representing firewall overview pager with tree on left side and rest on right side.
#
# It has replace point where it displays more details about selected element in firewall.
class OverviewTreePager < CWM::TreePager
include Y2Firewall::Helpers::Interfaces

# Constructor
def initialize
textdomain "firewall"

@fw = Y2Firewall::Firewalld.instance
super(OverviewTree.new(items))
end

# @see http://www.rubydoc.info/github/yast/yast-yast2/CWM%2FTree:items
def items
[
startup_item,
interfaces_item,
zones_item,
# masquerade_item,
# broadcast_item,
# logging_item,
# custom_rules_item
]
end

# Overrides default behavior of TreePager to register the new state with
# {UIState} before jumping to the tree node
#
# @param page [CWM::Page] Page to switch to
def switch_page(page)
UIState.instance.go_to_tree_node(page)
super
end

# Ensures the tree is properly initialized according to {UIState} after
# a redraw.
def initial_page
UIState.instance.find_tree_node(@pages) || super
end

private

# @return [CWM::PagerTreeItem]
def startup_item
page = Pages::Startup.new(self)
CWM::PagerTreeItem.new(page)
end

# @return [CWM::PagerTreeItem]
def interfaces_item
page = Pages::Interfaces.new(self)
CWM::PagerTreeItem.new(page)
end

# @return [CWM::PagerTreeItem]
def zones_item
zones = @fw.zones
children = zones.map { |z| zone_item(z) }
page = Pages::Zones.new(self)
CWM::PagerTreeItem.new(page, children: children)
end

# @return [CWM::PagerTreeItem]
def zone_item(z)
page = Pages::Zone.new(z, self)
CWM::PagerTreeItem.new(page)
end
end
end
end
72 changes: 72 additions & 0 deletions test/lib/y2firewall/widgets/overview_tree_pager_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/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.rb"
require "cwm/rspec"
require "y2firewall/widgets/overview_tree_pager"
require "y2firewall/widgets/pages"

RSpec.shared_examples "CWM::TreePager" do
include_examples "CWM::Pager"
end

describe Y2Firewall::Widgets::OverviewTreePager do
let(:widget) { described_class.new }

before do
fw = double("fake firewall")
z1 = double(name: "air")
allow(Y2Firewall::Firewalld).to receive(:instance).and_return(fw)
allow(fw).to receive(:zones).and_return([z1])

if1 = {
"id" => "eth0",
"name" => "Intel Ethernet Connection I217-LM",
"zone" => "external"
}
allow_any_instance_of(Y2Firewall::Helpers::Interfaces)
.to receive(:known_interfaces).and_return([if1])

startup_page = double("fake page", widget_id: "fake", label: "f", initial: false)
allow(Y2Firewall::Widgets::Pages::Startup)
.to receive(:new)
.and_return startup_page
end

describe "#initial_page" do
it "navigates to the page stored in the UIState instance" do
expect(Y2Firewall::UIState.instance).to receive(:find_tree_node)
widget.initial_page
end
end

describe "#switch_page" do
let(:page) { double("zones page", widget_id: "zones", label: "z", initial: false) }

it "registers the page in the UIState instance" do
expect(Y2Firewall::UIState.instance).to receive(:go_to_tree_node).with(page)
widget.switch_page(page)
end
end

include_examples "CWM::TreePager"
end

0 comments on commit 74e8417

Please sign in to comment.