2014-03-24 13:32:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2016-12-31 11:01:36 +00:00
|
|
|
# Copyright (c) 2008-2017 OpenLP Developers #
|
2014-03-24 13:32:28 +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; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# 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, write to the Free Software Foundation, Inc., 59 #
|
|
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
|
|
|
Package to test the openlp.core.lib.ui package.
|
|
|
|
"""
|
|
|
|
from unittest import TestCase
|
2017-04-24 05:17:55 +00:00
|
|
|
from unittest.mock import MagicMock, 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
|
2014-09-07 22:17:20 +00:00
|
|
|
from openlp.core.lib.ui import add_welcome_page, create_button_box, create_horizontal_adjusting_combo_box, \
|
|
|
|
create_button, create_action, create_valign_selection_widgets, find_and_set_in_combo_box, create_widget_action, \
|
|
|
|
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
|
|
|
|
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 'combo1' == combo.objectName()
|
|
|
|
assert QtWidgets.QComboBox.AdjustToMinimumContentsLength == combo.sizeAdjustPolicy()
|
2014-05-03 15:22:10 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_create_button(self):
|
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 the button
|
|
|
|
btn = create_button(dialog, 'my_btn')
|
|
|
|
|
|
|
|
# THEN: We should get a button with a name
|
2017-12-17 17:52:17 +00:00
|
|
|
assert isinstance(btn, QtWidgets.QPushButton)
|
|
|
|
assert 'my_btn' == btn.objectName()
|
|
|
|
assert btn.isEnabled() is True
|
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)
|
|
|
|
|
|
|
|
# THEN: We should get a button with those attributes
|
2017-12-17 17:52:17 +00:00
|
|
|
assert isinstance(btn, QtWidgets.QPushButton)
|
|
|
|
assert 'Hello' == btn.text()
|
|
|
|
assert 'How are you?' == btn.toolTip()
|
|
|
|
assert btn.isEnabled() is False
|
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 'my_btn' == btn.objectName()
|
|
|
|
assert btn.isEnabled() is True
|
2014-04-13 10:42:57 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_create_action(self):
|
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
|
|
|
|
action = create_action(dialog, 'my_action')
|
|
|
|
|
|
|
|
# THEN: We should get a QAction
|
2017-12-17 17:52:17 +00:00
|
|
|
assert isinstance(action, QtWidgets.QAction)
|
|
|
|
assert 'my_action' == action.objectName()
|
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',
|
2014-04-16 19:56:54 +00:00
|
|
|
tooltip='my tooltip', statustip='my statustip')
|
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 'my text' == action.text()
|
|
|
|
assert isinstance(action.icon(), QtGui.QIcon)
|
2017-12-17 20:19:19 +00:00
|
|
|
assert 'my tooltip' == action.toolTip()
|
2017-12-17 17:52:17 +00:00
|
|
|
assert 'my statustip' == action.statusTip()
|
2014-05-03 15:22:10 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_create_action_on_mac_osx(self):
|
2014-09-07 22:17:20 +00:00
|
|
|
"""
|
|
|
|
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:
|
2014-09-07 22:17:20 +00:00
|
|
|
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()
|
2014-09-07 22:17:20 +00:00
|
|
|
|
|
|
|
# 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):
|
2014-09-07 22:17:20 +00:00
|
|
|
"""
|
|
|
|
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:
|
2014-09-07 22:17:20 +00:00
|
|
|
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()
|
2014-09-07 22:17:20 +00:00
|
|
|
|
|
|
|
# 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'
|
2014-09-07 22:17:20 +00:00
|
|
|
|
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
|