added actionList tests; removed useless test

This commit is contained in:
Andreas Preikschat 2013-02-19 19:29:32 +01:00
parent d30cc33f86
commit 269e5f5901
3 changed files with 110 additions and 33 deletions

View File

@ -322,6 +322,7 @@ class ActionList(object):
ActionList.shortcut_map[shortcuts[0]] = actions ActionList.shortcut_map[shortcuts[0]] = actions
else: else:
shortcuts.remove(shortcuts[0]) shortcuts.remove(shortcuts[0])
action.setShortcuts([QtGui.QKeySequence(shortcut) for shortcut in shortcuts]) action.setShortcuts([QtGui.QKeySequence(shortcut) for shortcut in shortcuts])
def remove_action(self, action, category=None): def remove_action(self, action, category=None):
@ -350,7 +351,7 @@ class ActionList(object):
def add_category(self, name, weight): def add_category(self, name, weight):
""" """
Add an empty category to the list of categories. This is ony convenient for categories with a given weight. Add an empty category to the list of categories. This is only convenient for categories with a given weight.
``name`` ``name``
The category's name. The category's name.
@ -403,15 +404,15 @@ class ActionList(object):
``action`` ``action``
The action which wants to use a particular shortcut. The action which wants to use a particular shortcut.
""" """
local = action.shortcutContext() in [QtCore.Qt.WindowShortcut, QtCore.Qt.ApplicationShortcut] local_context = action.shortcutContext() not in [QtCore.Qt.WindowShortcut, QtCore.Qt.ApplicationShortcut]
affected_actions = [] affected_actions = []
if local: if local_context:
affected_actions = filter( affected_actions = filter(
lambda a: isinstance(a, QtGui.QAction), self.get_all_child_objects(action.parent())) lambda a: isinstance(a, QtGui.QAction), self.get_all_child_objects(action.parent()))
for existing_action in existing_actions: for existing_action in existing_actions:
if action is existing_action: if action is existing_action:
continue continue
if not local or existing_action in affected_actions: if not local_context or existing_action in affected_actions:
return False return False
if existing_action.shortcutContext() in [QtCore.Qt.WindowShortcut, QtCore.Qt.ApplicationShortcut]: if existing_action.shortcutContext() in [QtCore.Qt.WindowShortcut, QtCore.Qt.ApplicationShortcut]:
return False return False

View File

@ -1,10 +1,13 @@
""" """
Package to test the openlp.core.utils.actions package. Package to test the openlp.core.utils.actions package.
""" """
import os
from tempfile import mkstemp
from unittest import TestCase from unittest import TestCase
from mock import patch from PyQt4 import QtGui, QtCore
from openlp.core.lib import Settings
from openlp.core.utils import ActionList from openlp.core.utils import ActionList
@ -15,8 +18,107 @@ class TestActionList(TestCase):
Prepare the tests Prepare the tests
""" """
self.action_list = ActionList.get_instance() self.action_list = ActionList.get_instance()
self.settings = Settings()
fd, self.ini_file = mkstemp(u'.ini')
self.settings.set_filename(self.ini_file)
self.settings.beginGroup(u'shortcuts')
def test_(self): def tearDown(self):
""" """
Clean up
""" """
pass self.settings.endGroup()
os.unlink(self.ini_file)
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()
action = QtGui.QAction(parent)
action.setObjectName(u'action')
action_with_same_shortcuts = QtGui.QAction(parent)
action_with_same_shortcuts.setObjectName(u'action_with_same_shortcuts')
# Add default shortcuts to Settings class.
default_shortcuts = {
u'shortcuts/action': [QtGui.QKeySequence(u'v'), QtGui.QKeySequence(u'c')],
u'shortcuts/action_with_same_shortcuts': [QtGui.QKeySequence(u'v'), QtGui.QKeySequence(u'c')]
}
Settings.extend_default_settings(default_shortcuts)
# WHEN: Add the two actions to the action list.
self.action_list.add_action(action, u'example_category')
self.action_list.add_action(action_with_same_shortcuts, u'example_category')
# Remove the actions again.
self.action_list.remove_action(action, u'example_category')
self.action_list.remove_action(action_with_same_shortcuts, u'example_category')
# THEN: As both actions have the same shortcuts, they should be removed from one action.
assert len(action.shortcuts()) == 2, u'The action should have two shortcut assigned.'
assert len(action_with_same_shortcuts.shortcuts()) == 0, u'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()
action = QtGui.QAction(parent)
action.setObjectName(u'action2')
second_parent = QtCore.QObject()
action_with_same_shortcuts = QtGui.QAction(second_parent)
action_with_same_shortcuts.setObjectName(u'action_with_same_shortcuts2')
# Add default shortcuts to Settings class.
default_shortcuts = {
u'shortcuts/action2': [QtGui.QKeySequence(u'v'), QtGui.QKeySequence(u'c')],
u'shortcuts/action_with_same_shortcuts2': [QtGui.QKeySequence(u'v'), QtGui.QKeySequence(u'c')]
}
Settings.extend_default_settings(default_shortcuts)
# WHEN: Add the two actions to the action list.
self.action_list.add_action(action, u'example_category')
self.action_list.add_action(action_with_same_shortcuts, u'example_category')
# Remove the actions again.
self.action_list.remove_action(action, u'example_category')
self.action_list.remove_action(action_with_same_shortcuts, u'example_category')
# THEN: As both actions have the same shortcuts, they should be removed from one action.
assert len(action.shortcuts()) == 2, u'The action should have two shortcut assigned.'
assert len(action_with_same_shortcuts.shortcuts()) == 0, u'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()
action = QtGui.QAction(parent)
action.setObjectName(u'action3')
action.setShortcutContext(QtCore.Qt.WidgetShortcut)
second_parent = QtCore.QObject()
action_with_same_shortcuts = QtGui.QAction(second_parent)
action_with_same_shortcuts.setObjectName(u'action_with_same_shortcuts3')
action_with_same_shortcuts.setShortcutContext(QtCore.Qt.WidgetShortcut)
# Add default shortcuts to Settings class.
default_shortcuts = {
u'shortcuts/action3': [QtGui.QKeySequence(u'1'), QtGui.QKeySequence(u'2')],
u'shortcuts/action_with_same_shortcuts3': [QtGui.QKeySequence(u'1'), QtGui.QKeySequence(u'2')]
}
Settings.extend_default_settings(default_shortcuts)
# WHEN: Add the two actions to the action list.
self.action_list.add_action(action, u'example_category2')
self.action_list.add_action(action_with_same_shortcuts, u'example_category2')
# Remove the actions again.
self.action_list.remove_action(action, u'example_category2')
self.action_list.remove_action(action_with_same_shortcuts, u'example_category2')
# THEN: Both action should keep their shortcuts.
assert len(action.shortcuts()) == 2, u'The action should have two shortcut assigned.'
assert len(action_with_same_shortcuts.shortcuts()) == 2, u'The action should have two shortcuts assigned.'

View File

@ -1,26 +0,0 @@
"""
Functional tests for the Language Manager.
"""
from unittest import TestCase
from mock import patch
from openlp.core.utils import LanguageManager
class TestLanguageManager(TestCase):
"""
A test suite to test out various methods around the LanguageManager class.
"""
def get_translator_linux_test(self):
"""
"""
with patch(u'openlp.core.utils.sys.platform') as mocked_platform:
# GIVEN: We are on linux.
mocked_platform.return_value = u'linux2'
app_translator, default_translator = LanguageManager.get_translator('en')
assert not app_translator.isEmpty(), u'The application translator should not be empty'
assert not default_translator.isEmpty(), u'The default translator should not be empty'