Skip to content

Commit

Permalink
Merge pull request #5029 from VladimirSlavik/master-split-l10n-module…
Browse files Browse the repository at this point in the history
…-tests

tests: Split l10n module tests
  • Loading branch information
VladimirSlavik authored Aug 21, 2023
2 parents eb998f3 + bfa6515 commit 9520b6c
Show file tree
Hide file tree
Showing 5 changed files with 992 additions and 926 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#
# Copyright (C) 2023 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties 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, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
import unittest
from unittest.mock import patch

from pyanaconda.modules.localization.live_keyboard import GnomeShellKeyboard, \
get_live_keyboard_instance


class LiveSystemKeyboardTestCase(unittest.TestCase):
@patch("pyanaconda.modules.localization.live_keyboard.conf")
def test_get_live_keyboard_instance(self, mocked_conf):
"""Test get_live_keyboard_instance function."""
mocked_conf.system.provides_liveuser = True
assert isinstance(get_live_keyboard_instance(), GnomeShellKeyboard)

mocked_conf.reset_mock()
mocked_conf.system.provides_liveuser = False
assert get_live_keyboard_instance() is None

def _check_gnome_shell_layouts_conversion(self, mocked_exec_with_capture, system_input, output):
mocked_exec_with_capture.reset_mock()
mocked_exec_with_capture.return_value = system_input

gs = GnomeShellKeyboard()

assert gs.read_keyboard_layouts() == output
mocked_exec_with_capture.assert_called_once_with(
"gsettings",
["get", "org.gnome.desktop.input-sources", "sources"]
)

def _check_gnome_shell_current_layout_conversion(self, mocked_exec_with_capture, system_input,
output):
mocked_exec_with_capture.reset_mock()
mocked_exec_with_capture.return_value = system_input

gs = GnomeShellKeyboard()

assert gs.read_current_keyboard_layout() == output
mocked_exec_with_capture.assert_called_once_with(
"gsettings",
["get", "org.gnome.desktop.input-sources", "mru-sources"]
)

@patch("pyanaconda.modules.localization.live_keyboard.execWithCaptureAsLiveUser")
def test_gnome_shell_keyboard(self, mocked_exec_with_capture):
"""Test GnomeShellKeyboard live instance layouts."""
# test one simple layout set
self._check_gnome_shell_layouts_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('xkb', 'cz')]",
output=["cz"]
)

# test one complex layout is set
self._check_gnome_shell_layouts_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('xkb', 'cz+qwerty')]",
output=["cz (qwerty)"]
)

# test multiple layouts are set
self._check_gnome_shell_layouts_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('xkb', 'cz+qwerty'), ('xkb', 'us'), ('xkb', 'cz+dvorak-ucw')]",
output=["cz (qwerty)", "us", "cz (dvorak-ucw)"]
)

# test layouts with ibus (ibus is ignored)
self._check_gnome_shell_layouts_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('xkb', 'cz'), ('ibus', 'libpinyin')]",
output=["cz"]
)

# test only ibus layout
self._check_gnome_shell_layouts_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('ibus', 'libpinyin')]",
output=[]
)

# test wrong input
self._check_gnome_shell_layouts_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"wrong input",
output=[]
)

@patch("pyanaconda.modules.localization.live_keyboard.execWithCaptureAsLiveUser")
def test_gnome_shell_current_keyboard_layout(self, mocked_exec_with_capture):
"""Test GnomeShellKeyboard live instance current layout."""
# test one simple layout is set
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('xkb', 'us')]",
output="us"
)

# test one complex layout is set
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('xkb', 'cz+qwerty')]",
output="cz (qwerty)"
)

# test multiple layouts are set
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('xkb', 'cz+qwerty'), ('xkb', 'us')]",
output="cz (qwerty)"
)

# test layouts with ibus (ibus is ignored)
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('xkb', 'cz'), ('ibus', 'libpinyin')]",
output="cz"
)

# test layouts with ibus first (ibus should be skipped)
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('ibus', 'libpinyin'), ('xkb', 'us')]",
output="us"
)

# test only ibus layout
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('ibus', 'libpinyin')]",
output=""
)

# test wrong input
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"wrong input",
output=""
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#
# Copyright (C) 2023 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties 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, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
import unittest
from unittest.mock import patch, Mock

from pyanaconda.modules.localization.localed import LocaledWrapper


class LocaledWrapperTestCase(unittest.TestCase):
"""Test LocaledWrapper."""

@patch("pyanaconda.modules.localization.localed.conf")
def test_localed_wrapper_no_systembus_conf(self, mocked_conf):
"""Test LocaledWrapper on environments with nonavailability of systembus configured."""
mocked_conf.system.provides_system_bus = False
localed_wrapper = LocaledWrapper()
self._guarded_localed_wrapper_calls_check(localed_wrapper)

def _guarded_localed_wrapper_calls_check(self, localed_wrapper):
"""Test that calls to LocaledWrapper are guarded not to fail."""
assert localed_wrapper.keymap == ""
assert localed_wrapper.options == []
assert localed_wrapper.layouts_variants == []
localed_wrapper.set_keymap("cz")
localed_wrapper.set_keymap("cz", convert=True)
localed_wrapper.convert_keymap("cz")
localed_wrapper.set_and_convert_keymap("cz")
localed_wrapper.set_layouts(["cz (qwerty)", "us (euro)"],
options="grp:alt_shift_toggle",
convert=True)
localed_wrapper.set_and_convert_layouts(["cz (qwerty)", "us (euro)"])
localed_wrapper.convert_layouts(["cz (qwerty)", "us (euro)"])
localed_wrapper.set_layouts(["us-altgr-intl"])

@patch("pyanaconda.modules.localization.localed.SystemBus")
@patch("pyanaconda.modules.localization.localed.LOCALED")
@patch("pyanaconda.modules.localization.localed.conf")
def test_localed_wrapper_properties(self, mocked_conf, mocked_localed_service,
mocked_system_bus):
"""Test conversion of return values from Localed service to LocaledWraper."""
mocked_system_bus.check_connection.return_value = True
mocked_conf.system.provides_system_bus = True
mocked_localed_proxy = Mock()
mocked_localed_service.get_proxy.return_value = mocked_localed_proxy
localed_wrapper = LocaledWrapper()
mocked_localed_proxy.VConsoleKeymap = "cz"
mocked_localed_proxy.X11Layout = "cz,fi,us,fr"
mocked_localed_proxy.X11Variant = "qwerty,,euro"
mocked_localed_proxy.X11Options = "grp:alt_shift_toggle,grp:ctrl_alt_toggle"
assert localed_wrapper.keymap == \
"cz"
assert localed_wrapper.layouts_variants == \
["cz (qwerty)", "fi", "us (euro)", "fr"]
assert localed_wrapper.options == \
["grp:alt_shift_toggle", "grp:ctrl_alt_toggle"]

mocked_localed_proxy.VConsoleKeymap = ""
mocked_localed_proxy.X11Layout = ""
mocked_localed_proxy.X11Variant = ""
mocked_localed_proxy.X11Options = ""
assert localed_wrapper.keymap == ""
assert localed_wrapper.options == []
assert localed_wrapper.layouts_variants == []

@patch("pyanaconda.modules.localization.localed.SystemBus")
@patch("pyanaconda.modules.localization.localed.LOCALED")
@patch("pyanaconda.modules.localization.localed.conf")
def test_localed_wrapper_safe_calls(self, mocked_conf, mocked_localed_service,
mocked_system_bus):
"""Test calling LocaledWrapper with invalid values does not raise exception."""
mocked_system_bus.check_connection.return_value = True
mocked_conf.system.provides_system_bus = True
mocked_localed_proxy = Mock()
mocked_localed_service.get_proxy.return_value = mocked_localed_proxy
mocked_localed_proxy.VConsoleKeymap = "cz"
mocked_localed_proxy.X11Layout = "cz,fi,us,fr"
mocked_localed_proxy.X11Variant = "qwerty,,euro"
mocked_localed_proxy.X11Options = "grp:alt_shift_toggle,grp:ctrl_alt_toggle"
localed_wrapper = LocaledWrapper()
# valid values
localed_wrapper.set_keymap("cz")
localed_wrapper.set_keymap("cz", convert=True)
localed_wrapper.convert_keymap("cz")
localed_wrapper.set_and_convert_keymap("cz")
# invalid values
localed_wrapper.set_keymap("iinvalid")
localed_wrapper.set_keymap("iinvalid", convert=True)
localed_wrapper.convert_keymap("iinvalid")
localed_wrapper.set_and_convert_keymap("iinvalid")
# valid values
localed_wrapper.set_layouts(["cz (qwerty)", "us (euro)"],
options="grp:alt_shift_toggle",
convert=True)
localed_wrapper.set_and_convert_layouts(["cz (qwerty)", "us (euro)"])
localed_wrapper.convert_layouts(["cz (qwerty)", "us (euro)"])
# invalid values
# rhbz#1843379
localed_wrapper.set_layouts(["us-altgr-intl"])
localed_wrapper.set_and_convert_layouts(["us-altgr-intl"])
localed_wrapper.convert_layouts(["us-altgr-intl"])

@patch("pyanaconda.modules.localization.localed.SystemBus")
def test_localed_wrapper_no_systembus(self, mocked_system_bus):
"""Test LocaledWrapper in environment without system bus.
Which is also the environment of our tests.
"""
# Emulates mock environment
mocked_system_bus.check_connection.return_value = False
localed_wrapper = LocaledWrapper()
self._guarded_localed_wrapper_calls_check(localed_wrapper)
Loading

0 comments on commit 9520b6c

Please sign in to comment.