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 a532078 commit 3a9e084
Show file tree
Hide file tree
Showing 4 changed files with 149 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
46 changes: 46 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,46 @@
#!/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"

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

let(:startup_page) { double("Startup").as_null_object }

before do
allow(Y2Firewall::Widgets::Pages::Startup).to receive(:new)
.and_return(startup_page)
end

describe "#switch_page" do
let(:page) { Y2Firewall::Widgets::Pages::Zones.new(double("pager")) }

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
end

0 comments on commit 3a9e084

Please sign in to comment.