openlp/tests/functional/openlp_core_utils/test_actions.py

213 lines
9.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2013-12-24 08:56:50 +00:00
# Copyright (c) 2008-2014 Raoul Snyman #
# Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
# --------------------------------------------------------------------------- #
# 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.utils.actions package.
"""
from unittest import TestCase
from PyQt4 import QtGui, QtCore
2013-10-13 20:36:42 +00:00
from openlp.core.common import Settings
from openlp.core.utils import ActionList
2014-04-22 10:29:15 +00:00
from openlp.core.utils.actions import CategoryActionList
2014-04-22 10:32:02 +00:00
from tests.functional import MagicMock
2014-03-14 22:08:44 +00:00
from tests.helpers.testmixin import TestMixin
2014-03-14 22:08:44 +00:00
class TestActionList(TestCase, TestMixin):
"""
Test the ActionList class
"""
def setUp(self):
"""
Prepare the tests
"""
self.action_list = ActionList.get_instance()
2014-03-14 22:08:44 +00:00
self.build_settings()
self.settings = Settings()
2013-08-31 18:17:38 +00:00
self.settings.beginGroup('shortcuts')
def tearDown(self):
"""
Clean up
"""
self.settings.endGroup()
2014-03-14 22:08:44 +00:00
self.destroy_settings()
def test_add_action_same_parent(self):
"""
ActionList test - Tests the add_action method. The actions have the same parent, the same shortcuts and both
have the QtCore.Qt.WindowShortcut shortcut context set.
"""
# GIVEN: Two actions with the same shortcuts.
parent = QtCore.QObject()
2013-02-20 07:35:55 +00:00
action1 = QtGui.QAction(parent)
2013-08-31 18:17:38 +00:00
action1.setObjectName('action1')
2013-02-20 07:35:55 +00:00
action_with_same_shortcuts1 = QtGui.QAction(parent)
2013-08-31 18:17:38 +00:00
action_with_same_shortcuts1.setObjectName('action_with_same_shortcuts1')
# Add default shortcuts to Settings class.
default_shortcuts = {
2013-08-31 18:17:38 +00:00
'shortcuts/action1': [QtGui.QKeySequence('a'), QtGui.QKeySequence('b')],
'shortcuts/action_with_same_shortcuts1': [QtGui.QKeySequence('b'), QtGui.QKeySequence('a')]
}
Settings.extend_default_settings(default_shortcuts)
# WHEN: Add the two actions to the action list.
2013-08-31 18:17:38 +00:00
self.action_list.add_action(action1, 'example_category')
self.action_list.add_action(action_with_same_shortcuts1, 'example_category')
# Remove the actions again.
2013-08-31 18:17:38 +00:00
self.action_list.remove_action(action1, 'example_category')
self.action_list.remove_action(action_with_same_shortcuts1, 'example_category')
# THEN: As both actions have the same shortcuts, they should be removed from one action.
2013-08-31 18:17:38 +00:00
assert len(action1.shortcuts()) == 2, 'The action should have two shortcut assigned.'
assert len(action_with_same_shortcuts1.shortcuts()) == 0, 'The action should not have a shortcut assigned.'
def test_add_action_different_parent(self):
"""
ActionList test - Tests the add_action method. The actions have the different parent, the same shortcuts and
both have the QtCore.Qt.WindowShortcut shortcut context set.
"""
# GIVEN: Two actions with the same shortcuts.
parent = QtCore.QObject()
2013-02-20 07:35:55 +00:00
action2 = QtGui.QAction(parent)
2013-08-31 18:17:38 +00:00
action2.setObjectName('action2')
second_parent = QtCore.QObject()
2013-02-20 07:35:55 +00:00
action_with_same_shortcuts2 = QtGui.QAction(second_parent)
2013-08-31 18:17:38 +00:00
action_with_same_shortcuts2.setObjectName('action_with_same_shortcuts2')
# Add default shortcuts to Settings class.
default_shortcuts = {
2013-08-31 18:17:38 +00:00
'shortcuts/action2': [QtGui.QKeySequence('c'), QtGui.QKeySequence('d')],
'shortcuts/action_with_same_shortcuts2': [QtGui.QKeySequence('d'), QtGui.QKeySequence('c')]
}
Settings.extend_default_settings(default_shortcuts)
# WHEN: Add the two actions to the action list.
2013-08-31 18:17:38 +00:00
self.action_list.add_action(action2, 'example_category')
self.action_list.add_action(action_with_same_shortcuts2, 'example_category')
# Remove the actions again.
2013-08-31 18:17:38 +00:00
self.action_list.remove_action(action2, 'example_category')
self.action_list.remove_action(action_with_same_shortcuts2, 'example_category')
# THEN: As both actions have the same shortcuts, they should be removed from one action.
2013-08-31 18:17:38 +00:00
assert len(action2.shortcuts()) == 2, 'The action should have two shortcut assigned.'
assert len(action_with_same_shortcuts2.shortcuts()) == 0, 'The action should not have a shortcut assigned.'
def test_add_action_different_context(self):
"""
ActionList test - Tests the add_action method. The actions have the different parent, the same shortcuts and
both have the QtCore.Qt.WidgetShortcut shortcut context set.
"""
# GIVEN: Two actions with the same shortcuts.
parent = QtCore.QObject()
2013-02-20 07:35:55 +00:00
action3 = QtGui.QAction(parent)
2013-08-31 18:17:38 +00:00
action3.setObjectName('action3')
2013-02-20 07:35:55 +00:00
action3.setShortcutContext(QtCore.Qt.WidgetShortcut)
second_parent = QtCore.QObject()
2013-02-20 07:35:55 +00:00
action_with_same_shortcuts3 = QtGui.QAction(second_parent)
2013-08-31 18:17:38 +00:00
action_with_same_shortcuts3.setObjectName('action_with_same_shortcuts3')
2013-02-20 07:35:55 +00:00
action_with_same_shortcuts3.setShortcutContext(QtCore.Qt.WidgetShortcut)
# Add default shortcuts to Settings class.
default_shortcuts = {
2013-08-31 18:17:38 +00:00
'shortcuts/action3': [QtGui.QKeySequence('e'), QtGui.QKeySequence('f')],
'shortcuts/action_with_same_shortcuts3': [QtGui.QKeySequence('e'), QtGui.QKeySequence('f')]
}
Settings.extend_default_settings(default_shortcuts)
# WHEN: Add the two actions to the action list.
2013-08-31 18:17:38 +00:00
self.action_list.add_action(action3, 'example_category2')
self.action_list.add_action(action_with_same_shortcuts3, 'example_category2')
# Remove the actions again.
2013-08-31 18:17:38 +00:00
self.action_list.remove_action(action3, 'example_category2')
self.action_list.remove_action(action_with_same_shortcuts3, 'example_category2')
# THEN: Both action should keep their shortcuts.
2013-08-31 18:17:38 +00:00
assert len(action3.shortcuts()) == 2, 'The action should have two shortcut assigned.'
assert len(action_with_same_shortcuts3.shortcuts()) == 2, 'The action should have two shortcuts assigned.'
2014-04-22 10:29:15 +00:00
class TestCategoryActionList(TestCase):
def setUp(self):
"""
"""
self.added_action = MagicMock()
self.added_action.text = MagicMock('first')
self.not_added_action = MagicMock('second')
self.not_added_action.text = MagicMock()
self.list = CategoryActionList()
self.list.add(self.added_action, 10)
def tearDown(self):
"""
"""
del self.list
def len_test(self):
"""
Test the __len__ method
"""
# GIVEN: The list.
# WHEN: Check the length
length = len(self.list)
# THEN:
self.assertEqual(length, 1, "The length should be 1.")
# GIVEN: A list with an item.
self.list.append(self.not_added_action)
# WHEN: Check the length.
length = len(self.list)
# THEN:
self.assertEqual(length, 2, "The length should be 2.")
def remove_test(self):
"""
Test the remove() method
"""
# GIVEN: The list
# WHEN: Delete an item from the list.
self.list.remove(self.added_action)
# THEN: Now the element should not be in the list anymore.
self.assertFalse(self.added_action in self.list)
def contains_test(self):
"""
Test the __contains__() method
"""
# GIVEN: The list.
# WHEN: Do nothing.
# THEN: A not added item should not be in the list.
self.assertFalse(self.not_added_action in self.list)