openlp/tests/functional/openlp_core/lib/test_ui.py

319 lines
13 KiB
Python
Raw Normal View History

2014-03-24 13:32:28 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2019 OpenLP Developers #
# ---------------------------------------------------------------------- #
# 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-03-24 13:32:28 +00:00
"""
Package to test the openlp.core.lib.ui package.
"""
from unittest import TestCase
2018-10-02 04:39:42 +00:00
from unittest.mock import MagicMock, call, patch
from PyQt5 import QtCore, QtGui, QtWidgets
2014-03-24 13:32:28 +00:00
2017-10-07 07:05:07 +00:00
from openlp.core.common.i18n import UiStrings, translate
2018-10-02 04:39:42 +00:00
from openlp.core.lib.ui import add_welcome_page, create_action, create_button, create_button_box, \
create_horizontal_adjusting_combo_box, create_valign_selection_widgets, create_widget_action, \
critical_error_message_box, find_and_set_in_combo_box, set_case_insensitive_completer
2014-03-24 13:32:28 +00:00
class TestUi(TestCase):
"""
Test the functions in the ui module
"""
2016-05-31 21:40:13 +00:00
def test_add_welcome_page(self):
2014-03-24 13:32:28 +00:00
"""
Test appending a welcome page to a wizard
"""
# GIVEN: A wizard
2015-11-07 00:49:40 +00:00
wizard = QtWidgets.QWizard()
2014-03-24 13:32:28 +00:00
# WHEN: A welcome page has been added to the wizard
add_welcome_page(wizard, ':/wizards/wizard_firsttime.bmp')
# THEN: The wizard should have one page with a pixmap.
2017-12-17 17:52:17 +00:00
assert 1 == len(wizard.pageIds()), 'The wizard should have one page.'
assert isinstance(wizard.page(0).pixmap(QtWidgets.QWizard.WatermarkPixmap), QtGui.QPixmap)
2014-03-26 12:06:48 +00:00
2016-05-31 21:40:13 +00:00
def test_create_button_box(self):
2014-03-26 12:06:48 +00:00
"""
Test creating a button box for a dialog
"""
# GIVEN: A dialog
2015-11-07 00:49:40 +00:00
dialog = QtWidgets.QDialog()
2014-03-26 12:06:48 +00:00
# WHEN: We create the button box with five buttons
btnbox = create_button_box(dialog, 'my_btns', ['ok', 'save', 'cancel', 'close', 'defaults'])
# THEN: We should get a QDialogButtonBox with five buttons
2017-12-17 17:52:17 +00:00
assert isinstance(btnbox, QtWidgets.QDialogButtonBox)
assert 5 == len(btnbox.buttons())
2014-03-26 12:06:48 +00:00
# WHEN: We create the button box with a custom button
2015-11-07 00:49:40 +00:00
btnbox = create_button_box(dialog, 'my_btns', None, [QtWidgets.QPushButton('Custom')])
2014-03-26 12:06:48 +00:00
# THEN: We should get a QDialogButtonBox with one button
2017-12-17 17:52:17 +00:00
assert isinstance(btnbox, QtWidgets.QDialogButtonBox)
assert 1 == len(btnbox.buttons())
2014-03-26 12:06:48 +00:00
# WHEN: We create the button box with a custom button and a custom role
btnbox = create_button_box(dialog, 'my_btns', None,
2015-11-07 00:49:40 +00:00
[(QtWidgets.QPushButton('Help'), QtWidgets.QDialogButtonBox.HelpRole)])
2014-03-26 12:06:48 +00:00
# THEN: We should get a QDialogButtonBox with one button with a certain role
2017-12-17 17:52:17 +00:00
assert isinstance(btnbox, QtWidgets.QDialogButtonBox)
assert 1 == len(btnbox.buttons())
assert QtWidgets.QDialogButtonBox.HelpRole, btnbox.buttonRole(btnbox.buttons()[0])
2014-04-09 11:10:18 +00:00
@patch('openlp.core.lib.ui.Registry')
def test_critical_error_message_box(self, MockRegistry):
"""
Test the critical_error_message_box() function
"""
# GIVEN: A mocked Registry
# WHEN: critical_error_message_box() is called
critical_error_message_box('Error', 'This is an error')
# THEN: The error_message() method on the main window should be called
MockRegistry.return_value.get.return_value.error_message.assert_called_once_with('Error', 'This is an error')
@patch('openlp.core.lib.ui.QtWidgets.QMessageBox.critical')
def test_critical_error_question(self, mocked_critical):
"""
Test the critical_error_message_box() function
"""
# GIVEN: A mocked critical() method and some other mocks
mocked_parent = MagicMock()
# WHEN: critical_error_message_box() is called
critical_error_message_box(None, 'This is a question', mocked_parent, True)
# THEN: The error_message() method on the main window should be called
mocked_critical.assert_called_once_with(mocked_parent, 'Error', 'This is a question',
QtWidgets.QMessageBox.StandardButtons(QtWidgets.QMessageBox.Yes |
QtWidgets.QMessageBox.No))
2016-05-31 21:40:13 +00:00
def test_create_horizontal_adjusting_combo_box(self):
2014-05-03 15:22:10 +00:00
"""
Test creating a horizontal adjusting combo box
"""
# GIVEN: A dialog
2015-11-07 00:49:40 +00:00
dialog = QtWidgets.QDialog()
2014-05-03 15:22:10 +00:00
# WHEN: We create the combobox
combo = create_horizontal_adjusting_combo_box(dialog, 'combo1')
# THEN: We should get a ComboBox
2017-12-17 17:52:17 +00:00
assert isinstance(combo, QtWidgets.QComboBox)
assert combo.objectName() == 'combo1'
2017-12-17 17:52:17 +00:00
assert QtWidgets.QComboBox.AdjustToMinimumContentsLength == combo.sizeAdjustPolicy()
2014-05-03 15:22:10 +00:00
@patch('openlp.core.lib.ui.log')
def test_create_button(self, mocked_log):
2014-04-08 14:08:07 +00:00
"""
Test creating a button
"""
# GIVEN: A dialog
2015-11-07 00:49:40 +00:00
dialog = QtWidgets.QDialog()
2014-04-13 10:45:53 +00:00
2014-04-08 14:08:07 +00:00
# WHEN: We create a button with some attributes
btn = create_button(dialog, 'my_btn', text='Hello', tooltip='How are you?', enabled=False, role='test', test=1)
2014-04-08 14:08:07 +00:00
# THEN: We should get a button with those attributes
2017-12-17 17:52:17 +00:00
assert isinstance(btn, QtWidgets.QPushButton)
assert btn.objectName() == 'my_btn'
assert btn.text() == 'Hello'
assert btn.toolTip() == 'How are you?'
2017-12-17 17:52:17 +00:00
assert btn.isEnabled() is False
assert mocked_log.warning.call_args_list == [call('The role "test" is not defined in create_push_button().'),
call('Parameter test was not consumed in create_button().')]
def test_create_tool_button(self):
"""
Test creating a toolbutton
"""
# GIVEN: A dialog
dialog = QtWidgets.QDialog()
2014-04-08 14:08:07 +00:00
# WHEN: We create a toolbutton
btn = create_button(dialog, 'my_btn', btn_class='toolbutton')
# THEN: We should get a toolbutton
2017-12-17 17:52:17 +00:00
assert isinstance(btn, QtWidgets.QToolButton)
assert btn.objectName() == 'my_btn'
2017-12-17 17:52:17 +00:00
assert btn.isEnabled() is True
2014-04-13 10:42:57 +00:00
@patch('openlp.core.lib.ui.log')
def test_create_action(self, mocked_log):
2014-04-09 11:10:18 +00:00
"""
Test creating an action
"""
# GIVEN: A dialog
2015-11-07 00:49:40 +00:00
dialog = QtWidgets.QDialog()
2014-04-09 11:10:18 +00:00
# WHEN: We create an action with some properties
action = create_action(dialog, 'my_action', text='my text', icon=':/wizards/wizard_firsttime.bmp',
tooltip='my tooltip', statustip='my statustip', test=1)
2014-04-09 11:10:18 +00:00
# THEN: These properties should be set
2017-12-17 17:52:17 +00:00
assert isinstance(action, QtWidgets.QAction)
assert action.objectName() == 'my_action'
assert action.text() == 'my text'
2017-12-17 17:52:17 +00:00
assert isinstance(action.icon(), QtGui.QIcon)
assert action.toolTip() == 'my tooltip'
assert action.statusTip() == 'my statustip'
mocked_log.warning.assert_called_once_with('Parameter test was not consumed in create_action().')
2014-05-03 15:22:10 +00:00
2016-05-31 21:40:13 +00:00
def test_create_action_on_mac_osx(self):
"""
Test creating an action on OS X calls the correct method
"""
# GIVEN: A dialog and a mocked out is_macosx() method to always return True
with patch('openlp.core.lib.ui.is_macosx') as mocked_is_macosx, \
2015-11-07 00:49:40 +00:00
patch('openlp.core.lib.ui.QtWidgets.QAction') as MockedQAction:
mocked_is_macosx.return_value = True
mocked_action = MagicMock()
MockedQAction.return_value = mocked_action
2015-11-07 00:49:40 +00:00
dialog = QtWidgets.QDialog()
# WHEN: An action is created
create_action(dialog, 'my_action')
# THEN: setIconVisibleInMenu should be called
mocked_action.setIconVisibleInMenu.assert_called_with(False)
2016-05-31 21:40:13 +00:00
def test_create_action_not_on_mac_osx(self):
"""
Test creating an action on something other than OS X doesn't call the method
"""
# GIVEN: A dialog and a mocked out is_macosx() method to always return True
with patch('openlp.core.lib.ui.is_macosx') as mocked_is_macosx, \
2015-11-07 00:49:40 +00:00
patch('openlp.core.lib.ui.QtWidgets.QAction') as MockedQAction:
mocked_is_macosx.return_value = False
mocked_action = MagicMock()
MockedQAction.return_value = mocked_action
2015-11-07 00:49:40 +00:00
dialog = QtWidgets.QDialog()
# WHEN: An action is created
create_action(dialog, 'my_action')
# THEN: setIconVisibleInMenu should not be called
2017-12-17 17:52:17 +00:00
assert 0 == mocked_action.setIconVisibleInMenu.call_count, \
2017-12-17 20:19:19 +00:00
'setIconVisibleInMenu should not have been called'
2016-05-31 21:40:13 +00:00
def test_create_checked_disabled_invisible_action(self):
2014-09-13 22:00:43 +00:00
"""
2014-09-14 13:32:38 +00:00
Test that an invisible, disabled, checked action is created correctly
2014-09-13 22:00:43 +00:00
"""
# GIVEN: A dialog
2015-11-07 00:49:40 +00:00
dialog = QtWidgets.QDialog()
2014-09-13 22:00:43 +00:00
# WHEN: We create an action with some properties
action = create_action(dialog, 'my_action', checked=True, enabled=False, visible=False)
# THEN: These properties should be set
2017-12-17 17:52:17 +00:00
assert action.isChecked() is True, 'The action should be checked'
assert action.isEnabled() is False, 'The action should be disabled'
assert action.isVisible() is False, 'The action should be invisble'
2014-09-13 22:00:43 +00:00
2016-05-31 21:40:13 +00:00
def test_create_action_separator(self):
2014-09-13 22:00:43 +00:00
"""
Test creating an action as separator
"""
# GIVEN: A dialog
2015-11-07 00:49:40 +00:00
dialog = QtWidgets.QDialog()
2014-09-13 22:00:43 +00:00
# WHEN: We create an action as a separator
action = create_action(dialog, 'my_action', separator=True)
# THEN: The action should be a separator
2017-12-17 17:52:17 +00:00
assert action.isSeparator() is True, 'The action should be a separator'
2014-05-22 13:48:57 +00:00
2016-05-31 21:40:13 +00:00
def test_create_valign_selection_widgets(self):
2014-05-03 15:22:10 +00:00
"""
Test creating a combo box for valign selection
"""
# GIVEN: A dialog
2015-11-07 00:49:40 +00:00
dialog = QtWidgets.QDialog()
2014-05-03 15:22:10 +00:00
# WHEN: We create the widgets
label, combo = create_valign_selection_widgets(dialog)
# THEN: We should get a label and a combobox.
2017-12-17 17:52:17 +00:00
assert translate('OpenLP.Ui', '&Vertical Align:') == label.text()
assert isinstance(combo, QtWidgets.QComboBox)
assert combo == label.buddy()
2014-05-03 15:22:10 +00:00
for text in [UiStrings().Top, UiStrings().Middle, UiStrings().Bottom]:
2017-12-17 17:52:17 +00:00
assert combo.findText(text) >= 0
2014-05-03 15:22:20 +00:00
2016-05-31 21:40:13 +00:00
def test_find_and_set_in_combo_box(self):
2014-05-03 15:22:20 +00:00
"""
Test finding a string in a combo box and setting it as the selected item if present
"""
# GIVEN: A ComboBox
2015-11-07 00:49:40 +00:00
combo = QtWidgets.QComboBox()
2014-05-03 15:22:20 +00:00
combo.addItems(['One', 'Two', 'Three'])
combo.setCurrentIndex(1)
# WHEN: We call the method with a non-existing value and set_missing=False
find_and_set_in_combo_box(combo, 'Four', set_missing=False)
# THEN: The index should not have changed
2017-12-17 20:19:19 +00:00
assert 1 == combo.currentIndex()
2014-05-03 15:22:20 +00:00
# WHEN: We call the method with a non-existing value
find_and_set_in_combo_box(combo, 'Four')
# THEN: The index should have been reset
2017-12-17 17:52:17 +00:00
assert 0 == combo.currentIndex()
2014-05-03 15:22:20 +00:00
# WHEN: We call the method with the default behavior
find_and_set_in_combo_box(combo, 'Three')
# THEN: The index should have changed
2017-12-17 17:52:17 +00:00
assert 2 == combo.currentIndex()
2014-05-21 15:31:25 +00:00
2016-05-31 21:40:13 +00:00
def test_create_widget_action(self):
2014-05-21 15:31:25 +00:00
"""
Test creating an action for a widget
"""
# GIVEN: A button
2015-11-07 00:49:40 +00:00
button = QtWidgets.QPushButton()
2014-05-21 15:31:25 +00:00
# WHEN: We call the function
action = create_widget_action(button, 'some action')
# THEN: The action should be returned
2017-12-17 17:52:17 +00:00
assert isinstance(action, QtWidgets.QAction)
assert action.objectName() == 'some action'
2014-06-03 19:23:21 +00:00
2016-05-31 21:40:13 +00:00
def test_set_case_insensitive_completer(self):
2014-05-21 15:17:08 +00:00
"""
Test setting a case insensitive completer on a widget
"""
# GIVEN: A QComboBox and a list of completion items
2015-11-07 00:49:40 +00:00
line_edit = QtWidgets.QLineEdit()
2014-05-21 15:17:08 +00:00
suggestions = ['one', 'Two', 'THRee', 'FOUR']
# WHEN: We call the function
set_case_insensitive_completer(suggestions, line_edit)
# THEN: The Combobox should have a completer which is case insensitive
completer = line_edit.completer()
2017-12-17 17:52:17 +00:00
assert isinstance(completer, QtWidgets.QCompleter)
assert completer.caseSensitivity() == QtCore.Qt.CaseInsensitive