""" Package to test the openlp.core.lib.settingsform package. """ from unittest import TestCase from mock import MagicMock, patch from PyQt4 import QtCore, QtTest, QtGui from openlp.core.ui import settingsform from openlp.core.lib import Registry, ScreenList SCREEN = { u'primary': False, u'number': 1, u'size': QtCore.QRect(0, 0, 1024, 768) } class TestSettingsForm(TestCase): """ Test the PluginManager class """ def setUp(self): """ Some pre-test setup required. """ self.dummy1 = MagicMock() self.dummy2 = MagicMock() self.dummy3 = MagicMock() self.desktop = MagicMock() self.app = QtGui.QApplication([]) self.desktop.primaryScreen.return_value = SCREEN[u'primary'] self.desktop.screenCount.return_value = SCREEN[u'number'] self.desktop.screenGeometry.return_value = SCREEN[u'size'] self.screens = ScreenList.create(self.desktop) Registry.create() self.form = settingsform.SettingsForm() def tearDown(self): """ Delete all the C++ objects at the end so that we don't have a segfault """ del self.form del self.app def basic_cancel_test(self): """ Test running the settings form and pressing Cancel """ # GIVEN: An initial form # WHEN displaying the UI and pressing cancel with patch(u'PyQt4.QtGui.QDialog.reject') as mocked_reject: cancel_widget = self.form.button_box.button(self.form.button_box.Cancel) QtTest.QTest.mouseClick(cancel_widget, QtCore.Qt.LeftButton) # THEN the dialog reject should have been called assert mocked_reject.call_count == 1, u'The QDialog.reject should have been called' def basic_accept_test(self): """ Test running the settings form and pressing Ok """ # GIVEN: An initial form # WHEN displaying the UI and pressing Ok with patch(u'PyQt4.QtGui.QDialog.accept') as mocked_accept: ok_widget = self.form.button_box.button(self.form.button_box.Ok) QtTest.QTest.mouseClick(ok_widget, QtCore.Qt.LeftButton) # THEN the dialog reject should have been called assert mocked_accept.call_count == 1, u'The QDialog.accept should have been called' def basic_register_test(self): """ Test running the settings form and adding a single function """ # GIVEN: An initial form add a register function self.form.register_post_process(u'function1') # WHEN displaying the UI and pressing Ok with patch(u'PyQt4.QtGui.QDialog.accept'): ok_widget = self.form.button_box.button(self.form.button_box.Ok) QtTest.QTest.mouseClick(ok_widget, QtCore.Qt.LeftButton) # THEN the processing stack should be empty assert len(self.form.processes) == 0, u'The one requested process should have been removed from the stack' def register_multiple_functions_test(self): """ Test running the settings form and adding multiple functions """ # GIVEN: Registering a single function self.form.register_post_process(u'function1') # WHEN testing the processing stack # THEN the processing stack should have one item assert len(self.form.processes) == 1, u'The one requested process should have been added to the stack' # GIVEN: Registering a new function self.form.register_post_process(u'function2') # WHEN testing the processing stack # THEN the processing stack should have two items assert len(self.form.processes) == 2, u'The two requested processes should have been added to the stack' # GIVEN: Registering a process for the second time self.form.register_post_process(u'function1') # WHEN testing the processing stack # THEN the processing stack should still have two items assert len(self.form.processes) == 2, u'No new processes should have been added to the stack' def register_image_manager_trigger_test_one(self): """ Test the triggering of the image manager rebuild event from image background change """ # GIVEN: Three functions registered to be call Registry().register_function(u'images_config_updated', self.dummy1) Registry().register_function(u'config_screen_changed', self.dummy2) Registry().register_function(u'images_regenerate', self.dummy3) # WHEN: The Images have been changed and the form submitted self.form.register_post_process(u'images_config_updated') self.form.accept() # THEN: images_regenerate should have been added. assert self.dummy1.call_count == 1, u'dummy1 should have been called once' assert self.dummy2.call_count == 0, u'dummy2 should not have been called at all' assert self.dummy3.call_count == 1, u'dummy3 should have been called once' def register_image_manager_trigger_test_two(self): """ Test the triggering of the image manager rebuild event from screen dimension change """ # GIVEN: Three functions registered to be call Registry().register_function(u'images_config_updated', self.dummy1) Registry().register_function(u'config_screen_changed', self.dummy2) Registry().register_function(u'images_regenerate', self.dummy3) # WHEN: The Images have been changed and the form submitted self.form.register_post_process(u'config_screen_changed') self.form.accept() # THEN: images_regenerate should have been added. assert self.dummy1.call_count == 0, u'dummy1 should not have been called at all' assert self.dummy2.call_count == 1, u'dummy2 should have been called once' assert self.dummy3.call_count == 1, u'dummy3 should have been called once' def register_image_manager_trigger_test_three(self): """ Test the triggering of the image manager rebuild event from image background change and a change to the screen dimension. """ # GIVEN: Three functions registered to be call Registry().register_function(u'images_config_updated', self.dummy1) Registry().register_function(u'config_screen_changed', self.dummy2) Registry().register_function(u'images_regenerate', self.dummy3) # WHEN: The Images have been changed and the form submitted self.form.register_post_process(u'config_screen_changed') self.form.register_post_process(u'images_config_updated') self.form.accept() # THEN: Images_regenerate should have been added. assert self.dummy1.call_count == 1, u'dummy1 should have been called once' assert self.dummy2.call_count == 1, u'dummy2 should have been called once' assert self.dummy3.call_count == 1, u'dummy3 should have been called once'