openlp/tests/interfaces/openlp_core/ui/test_shortcutlistform.py

228 lines
10 KiB
Python
Raw Normal View History

2014-04-20 14:03:07 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2020 OpenLP Developers #
2019-04-13 13:00:22 +00:00
# ---------------------------------------------------------------------- #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# 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, see <https://www.gnu.org/licenses/>. #
##########################################################################
2014-04-20 14:03:07 +00:00
"""
Package to test the openlp.core.ui.shortcutform package.
"""
from unittest import TestCase
from unittest.mock import MagicMock, patch
2014-04-20 14:03:07 +00:00
2017-10-07 07:05:07 +00:00
from PyQt5 import QtCore, QtWidgets
2014-04-20 14:03:07 +00:00
2017-10-07 07:05:07 +00:00
from openlp.core.common.registry import Registry
2014-04-20 14:03:07 +00:00
from openlp.core.ui.shortcutlistform import ShortcutListForm
from tests.helpers.testmixin import TestMixin
class TestShortcutform(TestCase, TestMixin):
def setUp(self):
"""
Create the UI
"""
Registry.create()
self.setup_application()
2015-11-07 00:49:40 +00:00
self.main_window = QtWidgets.QMainWindow()
2014-04-20 14:03:07 +00:00
Registry().register('main_window', self.main_window)
self.form = ShortcutListForm()
def tearDown(self):
"""
Delete all the C++ objects at the end so that we don't have a segfault
"""
del self.form
del self.main_window
2016-05-31 21:40:13 +00:00
def test_adjust_button(self):
2014-04-20 14:03:07 +00:00
"""
Test the _adjust_button() method
"""
# GIVEN: A button.
2015-11-07 00:49:40 +00:00
button = QtWidgets.QPushButton()
2014-04-20 14:13:09 +00:00
checked = True
2014-04-20 14:03:07 +00:00
enabled = True
text = 'new!'
2014-04-20 14:03:07 +00:00
# WHEN: Call the method.
2015-11-07 00:49:40 +00:00
with patch('PyQt5.QtWidgets.QPushButton.setChecked') as mocked_check_method:
2014-04-20 14:03:07 +00:00
self.form._adjust_button(button, checked, enabled, text)
# THEN: The button should be changed.
2017-12-23 09:09:45 +00:00
assert button.text() == text, 'The text should match.'
2014-04-20 14:03:07 +00:00
mocked_check_method.assert_called_once_with(True)
2017-12-23 09:09:45 +00:00
assert button.isEnabled() == enabled, 'The button should be disabled.'
2016-05-31 21:40:13 +00:00
def test_space_key_press_event(self):
"""
Test the keyPressEvent when the spacebar was pressed
"""
# GIVEN: A key event that is a space
mocked_event = MagicMock()
mocked_event.key.return_value = QtCore.Qt.Key_Space
2016-02-05 19:01:21 +00:00
# WHEN: The event is handled
with patch.object(self.form, 'keyReleaseEvent') as mocked_key_release_event:
self.form.keyPressEvent(mocked_event)
# THEN: The key should be released
mocked_key_release_event.assert_called_with(mocked_event)
2017-12-23 09:09:45 +00:00
assert 0 == mocked_event.accept.call_count
2016-05-31 21:40:13 +00:00
def test_primary_push_button_checked_key_press_event(self):
"""
Test the keyPressEvent when the primary push button is checked
"""
# GIVEN: The primary push button is checked
with patch.object(self.form, 'keyReleaseEvent') as mocked_key_release_event, \
patch.object(self.form.primary_push_button, 'isChecked') as mocked_is_checked:
mocked_is_checked.return_value = True
mocked_event = MagicMock()
2016-02-05 19:01:21 +00:00
# WHEN: The event is handled
self.form.keyPressEvent(mocked_event)
# THEN: The key should be released
mocked_key_release_event.assert_called_with(mocked_event)
2017-12-23 09:09:45 +00:00
assert 0 == mocked_event.accept.call_count
2016-05-31 21:40:13 +00:00
def test_alternate_push_button_checked_key_press_event(self):
"""
Test the keyPressEvent when the alternate push button is checked
"""
# GIVEN: The primary push button is checked
with patch.object(self.form, 'keyReleaseEvent') as mocked_key_release_event, \
patch.object(self.form.alternate_push_button, 'isChecked') as mocked_is_checked:
mocked_is_checked.return_value = True
mocked_event = MagicMock()
2016-02-05 19:01:21 +00:00
# WHEN: The event is handled
self.form.keyPressEvent(mocked_event)
# THEN: The key should be released
mocked_key_release_event.assert_called_with(mocked_event)
2017-12-23 09:09:45 +00:00
assert 0 == mocked_event.accept.call_count
2016-05-31 21:40:13 +00:00
def test_escape_key_press_event(self):
"""
Test the keyPressEvent when the escape key was pressed
"""
# GIVEN: A key event that is an escape
mocked_event = MagicMock()
mocked_event.key.return_value = QtCore.Qt.Key_Escape
2016-02-05 19:01:21 +00:00
# WHEN: The event is handled
with patch.object(self.form, 'close') as mocked_close:
self.form.keyPressEvent(mocked_event)
# THEN: The key should be released
mocked_event.accept.assert_called_with()
mocked_close.assert_called_with()
2016-05-31 21:40:13 +00:00
def test_on_default_radio_button_not_toggled(self):
2016-02-04 21:25:06 +00:00
"""
Test that the default radio button method exits early when the button is not toggled
"""
# GIVEN: A not-toggled custom radio button
with patch.object(self.form, '_current_item_action') as mocked_current_item_action:
# WHEN: The clicked method is called
self.form.on_default_radio_button_clicked(False)
# THEN: The method should exit early (i.e. the rest of the methods are not called)
2017-12-23 09:09:45 +00:00
assert 0 == mocked_current_item_action.call_count
2016-02-04 21:25:06 +00:00
2016-05-31 21:40:13 +00:00
def test_on_default_radio_button_clicked_no_action(self):
2016-02-04 21:25:06 +00:00
"""
Test that nothing happens when an action hasn't been selected and you click the default radio button
"""
# GIVEN: Some mocked out methods, a current action, and some shortcuts
with patch.object(self.form, '_current_item_action') as mocked_current_item_action, \
patch.object(self.form, '_action_shortcuts') as mocked_action_shortcuts:
mocked_current_item_action.return_value = None
2016-02-05 19:01:21 +00:00
# WHEN: The default radio button is clicked
2016-02-04 21:25:06 +00:00
self.form.on_default_radio_button_clicked(True)
2016-02-05 19:01:21 +00:00
# THEN: The method should exit early (i.e. the rest of the methods are not called)
2016-02-04 21:25:06 +00:00
mocked_current_item_action.assert_called_with()
2017-12-23 09:09:45 +00:00
assert 0 == mocked_action_shortcuts.call_count
2016-02-04 21:25:06 +00:00
2016-05-31 21:40:13 +00:00
def test_on_default_radio_button_clicked(self):
2016-02-04 21:25:06 +00:00
"""
Test that the values are copied across correctly when the default radio button is selected
"""
# GIVEN: Some mocked out methods, a current action, and some shortcuts
with patch.object(self.form, '_current_item_action') as mocked_current_item_action, \
patch.object(self.form, '_action_shortcuts') as mocked_action_shortcuts, \
patch.object(self.form, 'refresh_shortcut_list') as mocked_refresh_shortcut_list, \
patch.object(self.form, 'get_shortcut_string') as mocked_get_shortcut_string, \
patch.object(self.form.primary_push_button, 'setText') as mocked_set_text:
mocked_action = MagicMock()
mocked_action.default_shortcuts = [QtCore.Qt.Key_Escape]
mocked_current_item_action.return_value = mocked_action
mocked_action_shortcuts.return_value = [QtCore.Qt.Key_Escape]
mocked_get_shortcut_string.return_value = 'Esc'
2016-02-05 19:01:21 +00:00
# WHEN: The default radio button is clicked
2016-02-04 21:25:06 +00:00
self.form.on_default_radio_button_clicked(True)
2016-02-05 19:01:21 +00:00
# THEN: The shorcuts should be copied across
2016-02-04 21:25:06 +00:00
mocked_current_item_action.assert_called_with()
mocked_action_shortcuts.assert_called_with(mocked_action)
mocked_refresh_shortcut_list.assert_called_with()
mocked_set_text.assert_called_with('Esc')
2016-05-31 21:40:13 +00:00
def test_on_custom_radio_button_not_toggled(self):
"""
2016-02-04 21:25:06 +00:00
Test that the custom radio button method exits early when the button is not toggled
"""
# GIVEN: A not-toggled custom radio button
with patch.object(self.form, '_current_item_action') as mocked_current_item_action:
# WHEN: The clicked method is called
self.form.on_custom_radio_button_clicked(False)
# THEN: The method should exit early (i.e. the rest of the methods are not called)
2017-12-23 09:09:45 +00:00
assert 0 == mocked_current_item_action.call_count
2016-05-31 21:40:13 +00:00
def test_on_custom_radio_button_clicked(self):
"""
Test that the values are copied across correctly when the custom radio button is selected
"""
# GIVEN: Some mocked out methods, a current action, and some shortcuts
with patch.object(self.form, '_current_item_action') as mocked_current_item_action, \
patch.object(self.form, '_action_shortcuts') as mocked_action_shortcuts, \
patch.object(self.form, 'refresh_shortcut_list') as mocked_refresh_shortcut_list, \
patch.object(self.form, 'get_shortcut_string') as mocked_get_shortcut_string, \
patch.object(self.form.primary_push_button, 'setText') as mocked_set_text:
mocked_action = MagicMock()
mocked_current_item_action.return_value = mocked_action
mocked_action_shortcuts.return_value = [QtCore.Qt.Key_Escape]
mocked_get_shortcut_string.return_value = 'Esc'
2016-02-05 19:01:21 +00:00
# WHEN: The custom radio button is clicked
self.form.on_custom_radio_button_clicked(True)
2016-02-05 19:01:21 +00:00
# THEN: The shorcuts should be copied across
mocked_current_item_action.assert_called_with()
mocked_action_shortcuts.assert_called_with(mocked_action)
mocked_refresh_shortcut_list.assert_called_with()
mocked_set_text.assert_called_with('Esc')