def click_save_credentials_button_test(self): """ Test that we try and save credentials, but they are rejected because they are bad. I do not want to store valid credentials in here because this source is openly available. """ # GIVEN: An instance of the PlanningCenterAuthForm with patch('PyQt5.QtWidgets.QDialog.exec'): # WHEN: The form is shown self.form.exec() # WHEN: The "delete credentials" button is clicked with new credentials set application_id = 'def' secret = '456' self.form.application_id_line_edit.setText(application_id) self.form.secret_line_edit.setText(secret) with patch('PyQt5.QtWidgets.QMessageBox'): QtTest.QTest.mouseClick(self.form.save_credentials_button, QtCore.Qt.LeftButton) # THEN: We should test credentials and validate that they did not get saved self.assertNotEqual( Settings().value('planningcenter/application_id'), application_id, 'The application_id should not have been saved') self.assertNotEqual(Settings().value('planningcenter/secret'), secret, 'The secret code should not have been saved')
def setUp(self): """ Set up the environment for testing bible parse reference """ self.build_settings() Registry.create() Registry().register('service_list', MagicMock()) Registry().register('application', MagicMock()) bible_settings = { 'bibles/proxy name': '', 'bibles/db type': 'sqlite', 'bibles/book name language': LanguageSelection.Bible, 'bibles/verse separator': '', 'bibles/range separator': '', 'bibles/list separator': '', 'bibles/end separator': '', } Settings().extend_default_settings(bible_settings) with patch('openlp.core.common.applocation.Settings') as mocked_class, \ patch('openlp.core.common.AppLocation.get_section_data_path') as mocked_get_section_data_path, \ patch('openlp.core.common.AppLocation.get_files') as mocked_get_files: # GIVEN: A mocked out Settings class and a mocked out AppLocation.get_files() mocked_settings = mocked_class.return_value mocked_settings.contains.return_value = False mocked_get_files.return_value = ["tests.sqlite"] mocked_get_section_data_path.return_value = TEST_RESOURCES_PATH + "/bibles" self.manager = BibleManager(MagicMock())
def initial_defaults_test(self): """ Test that the SelectPlanForm displays with correct defaults """ # GIVEN: An SelectPlanForm instance with airplane mode enabled, resources available, # a theme manager with mocked themes, and a fake date = Sunday (7/29/2018) with patch('PyQt5.QtWidgets.QDialog.exec'), \ patch('openlp.plugins.planningcenter.forms.selectplanform.date') as mock_date: # need to always return 7/29/2018 for date.today() mock_date.today.return_value = date(2018,7,29) mock_date.side_effect = lambda *args, **kw: date(*args, **kw) # WHEN: The form is shown self.form.exec() # THEN: The correct count of service types show up in the combo box self.assertEqual(self.form.service_type_combo_box.count(), 3, 'The service_type_combo_box contains 3 items') # The first service type is selected self.assertEqual(self.form.service_type_combo_box.currentText(), '01 Grace Bible Fellowship', 'The service_type_combo_box defaults to "01 Grace Bible Fellowship"') # the selected plan is today (the mocked date is a Sunday) self.assertEqual(self.form.plan_selection_combo_box.currentText(), date.strftime(mock_date.today.return_value,'%B %d, %Y'), 'Incorrect default date selected for Plan Date') # count the number of themes listed and make sure it matches expected value self.assertEqual(self.form.song_theme_selection_combo_box.count(), 2, 'Count of song themes is incorrect') self.assertEqual(self.form.slide_theme_selection_combo_box.count(), 2, 'Count of custom slide themes is incorrect')
def auto_start_context_menu_test(self): """ Test the context_menu() method with can auto start """ # GIVEN: A service item added self.service_manager.setup_ui(self.service_manager) with patch("PyQt4.QtGui.QTreeWidget.itemAt") as mocked_item_at_method, patch( "PyQt4.QtGui.QWidget.mapToGlobal" ), patch("PyQt4.QtGui.QMenu.exec_"): mocked_item = MagicMock() mocked_item.parent.return_value = None mocked_item_at_method.return_value = mocked_item # We want 1 to be returned for the position mocked_item.data.return_value = 1 # A service item without capabilities. service_item = ServiceItem() service_item.add_capability(ItemCapabilities.CanAutoStartForLive) self.service_manager.service_items = [{"service_item": service_item}] q_point = None # Mocked actions. self.service_manager.edit_action.setVisible = MagicMock() self.service_manager.create_custom_action.setVisible = MagicMock() self.service_manager.maintain_action.setVisible = MagicMock() self.service_manager.notes_action.setVisible = MagicMock() self.service_manager.time_action.setVisible = MagicMock() self.service_manager.auto_start_action.setVisible = MagicMock() self.service_manager.rename_action.setVisible = MagicMock() # WHEN: Show the context menu. self.service_manager.context_menu(q_point) # THEN: The following actions should be not visible. self.service_manager.edit_action.setVisible.assert_called_once_with( False ), "The action should be set invisible." self.service_manager.create_custom_action.setVisible.assert_called_once_with( False ), "The action should be set invisible." self.service_manager.maintain_action.setVisible.assert_called_once_with( False ), "The action should be set invisible." self.service_manager.notes_action.setVisible.assert_called_with(True), "The action should be set visible." self.service_manager.time_action.setVisible.assert_called_once_with( False ), "The action should be set invisible." self.service_manager.auto_start_action.setVisible.assert_called_with( True ), "The action should be set visible." self.service_manager.rename_action.setVisible.assert_called_once_with( False ), "The action should be set invisible."
def setUp(self): """ Create the UI """ Registry.create() self.setup_application() ScreenList.create(self.app.desktop()) Registry().register("application", MagicMock()) # Mock classes and methods used by mainwindow. with patch("openlp.core.ui.mainwindow.SettingsForm") as mocked_settings_form, patch( "openlp.core.ui.mainwindow.ImageManager" ) as mocked_image_manager, patch("openlp.core.ui.mainwindow.LiveController") as mocked_live_controller, patch( "openlp.core.ui.mainwindow.PreviewController" ) as mocked_preview_controller, patch( "openlp.core.ui.mainwindow.OpenLPDockWidget" ) as mocked_dock_widget, patch( "openlp.core.ui.mainwindow.QtGui.QToolBox" ) as mocked_q_tool_box_class, patch( "openlp.core.ui.mainwindow.QtGui.QMainWindow.addDockWidget" ) as mocked_add_dock_method, patch( "openlp.core.ui.mainwindow.ThemeManager" ) as mocked_theme_manager, patch( "openlp.core.ui.mainwindow.ProjectorManager" ) as mocked_projector_manager, patch( "openlp.core.ui.mainwindow.Renderer" ) as mocked_renderer: self.main_window = MainWindow() self.service_manager = Registry().get("service_manager")
def click_load_button_test(self): """ Test that the correct function is called when load is clicked, and that it behaves as expected. """ # GIVEN: Mocked methods. with patch('openlp.plugins.media.forms.mediaclipselectorform.critical_error_message_box') as \ mocked_critical_error_message_box,\ patch('openlp.plugins.media.forms.mediaclipselectorform.os.path.exists') as mocked_os_path_exists,\ patch('PyQt4.QtGui.QDialog.exec_') as mocked_exec: self.form.exec_() # WHEN: The load button is clicked with no path set QtTest.QTest.mouseClick(self.form.load_disc_button, QtCore.Qt.LeftButton) # THEN: we should get an error mocked_critical_error_message_box.assert_called_with( message='No path was given') # WHEN: The load button is clicked with a non-existing path mocked_os_path_exists.return_value = False self.form.media_path_combobox.insertItem( 0, '/non-existing/test-path.test') self.form.media_path_combobox.setCurrentIndex(0) QtTest.QTest.mouseClick(self.form.load_disc_button, QtCore.Qt.LeftButton) # THEN: we should get an error assert self.form.media_path_combobox.currentText() == '/non-existing/test-path.test',\ 'The media path should be the given one.' mocked_critical_error_message_box.assert_called_with( message='Given path does not exists') # WHEN: The load button is clicked with a mocked existing path mocked_os_path_exists.return_value = True self.form.vlc_media_player = MagicMock() self.form.vlc_media_player.play.return_value = -1 self.form.media_path_combobox.insertItem( 0, '/existing/test-path.test') self.form.media_path_combobox.setCurrentIndex(0) QtTest.QTest.mouseClick(self.form.load_disc_button, QtCore.Qt.LeftButton) # THEN: we should get an error assert self.form.media_path_combobox.currentText() == '/existing/test-path.test',\ 'The media path should be the given one.' mocked_critical_error_message_box.assert_called_with( message='VLC player failed playing the media')
def title_combobox_test(self): """ Test the behavior when the title combobox is updated """ # GIVEN: Mocked methods and some entries in the title combobox. with patch('PyQt4.QtGui.QDialog.exec_') as mocked_exec: self.form.exec_() self.form.vlc_media_player.get_length.return_value = 1000 self.form.audio_tracks_combobox.itemData = MagicMock() self.form.subtitle_tracks_combobox.itemData = MagicMock() self.form.audio_tracks_combobox.itemData.return_value = None self.form.subtitle_tracks_combobox.itemData.return_value = None self.form.titles_combo_box.insertItem(0, 'Test Title 0') self.form.titles_combo_box.insertItem(1, 'Test Title 1') # WHEN: There exists audio and subtitle tracks and the index is updated. self.form.vlc_media_player.audio_get_track_description.return_value = [(-1, b'Disabled'), (0, b'Audio Track 1')] self.form.vlc_media_player.video_get_spu_description.return_value = [(-1, b'Disabled'), (0, b'Subtitle Track 1')] self.form.titles_combo_box.setCurrentIndex(1) # THEN: The subtitle and audio track comboboxes should be updated and get signals and call itemData. self.form.audio_tracks_combobox.itemData.assert_any_call(0) self.form.audio_tracks_combobox.itemData.assert_any_call(1) self.form.subtitle_tracks_combobox.itemData.assert_any_call(0)
def title_combobox_test(self): """ Test the behavior when the title combobox is updated """ # GIVEN: Mocked methods and some entries in the title combobox. with patch('PyQt4.QtGui.QDialog.exec_') as mocked_exec: self.form.exec_() self.form.vlc_media_player.get_length.return_value = 1000 self.form.audio_tracks_combobox.itemData = MagicMock() self.form.subtitle_tracks_combobox.itemData = MagicMock() self.form.audio_tracks_combobox.itemData.return_value = None self.form.subtitle_tracks_combobox.itemData.return_value = None self.form.titles_combo_box.insertItem(0, 'Test Title 0') self.form.titles_combo_box.insertItem(1, 'Test Title 1') # WHEN: There exists audio and subtitle tracks and the index is updated. self.form.vlc_media_player.audio_get_track_description.return_value = [ (-1, b'Disabled'), (0, b'Audio Track 1') ] self.form.vlc_media_player.video_get_spu_description.return_value = [ (-1, b'Disabled'), (0, b'Subtitle Track 1') ] self.form.titles_combo_box.setCurrentIndex(1) # THEN: The subtitle and audio track comboboxes should be updated and get signals and call itemData. self.form.audio_tracks_combobox.itemData.assert_any_call(0) self.form.audio_tracks_combobox.itemData.assert_any_call(1) self.form.subtitle_tracks_combobox.itemData.assert_any_call(0)
def verify_default_plan_date_is_next_sunday_test(self): """ Test that the SelectPlanForm displays Next Sunday's Date by Default """ # GIVEN: An SelectPlanForm instance with airplane mode enabled, resources available, # a theme manager with mocked themes, and a fake date = Monday (7/30/2018) with patch('PyQt5.QtWidgets.QDialog.exec'), \ patch('openlp.plugins.planningcenter.forms.selectplanform.date') as mock_date: # need to always return 7/29/2018 for date.today() mock_date.today.return_value = date(2018,7,30) mock_date.side_effect = lambda *args, **kw: date(*args, **kw) # WHEN: The form is shown self.form.exec() # THEN: The plan selection date is 8/5 (the following Sunday) self.assertEqual(self.form.plan_selection_combo_box.currentText(), 'August 5, 2018', 'The next Sunday\'s Date is not selected in the plan_selection_combo_box')
def window_title_test(self): """ Test the windowTitle of the FileRenameDialog """ # GIVEN: A mocked QDialog.exec_() method with patch('PyQt4.QtGui.QDialog.exec_') as mocked_exec: # WHEN: The form is executed with no args self.form.exec_() # THEN: the window title is set correctly self.assertEqual(self.form.windowTitle(), 'File Rename', 'The window title should be "File Rename"') # WHEN: The form is executed with False arg self.form.exec_(False) # THEN: the window title is set correctly self.assertEqual(self.form.windowTitle(), 'File Rename', 'The window title should be "File Rename"') # WHEN: The form is executed with True arg self.form.exec_(True) # THEN: the window title is set correctly self.assertEqual(self.form.windowTitle(), 'File Copy', 'The window title should be "File Copy"')
def click_save_button_test(self): """ Test that the correct function is called when save is clicked, and that it behaves as expected. """ # GIVEN: Mocked methods. with patch('openlp.plugins.media.forms.mediaclipselectorform.critical_error_message_box') as \ mocked_critical_error_message_box,\ patch('PyQt4.QtGui.QDialog.exec_') as mocked_exec: self.form.exec_() # WHEN: The save button is clicked with a NoneType in start_time_ms or end_time_ms self.form.accept() # THEN: we should get an error message mocked_critical_error_message_box.assert_called_with('DVD not loaded correctly', 'The DVD was not loaded correctly, ' 'please re-load and try again.')
def test_auto_start_context_menu(self): """ Test the context_menu() method with can auto start """ # GIVEN: A service item added self.service_manager.setup_ui(self.service_manager) with patch('PyQt5.QtWidgets.QTreeWidget.itemAt') as mocked_item_at_method, \ patch('PyQt5.QtWidgets.QWidget.mapToGlobal'), \ patch('PyQt5.QtWidgets.QMenu.exec'): mocked_item = MagicMock() mocked_item.parent.return_value = None mocked_item_at_method.return_value = mocked_item # We want 1 to be returned for the position mocked_item.data.return_value = 1 # A service item without capabilities. service_item = ServiceItem() service_item.add_capability(ItemCapabilities.CanAutoStartForLive) self.service_manager.service_items = [{'service_item': service_item}] q_point = None # Mocked actions. self.service_manager.edit_action.setVisible = MagicMock() self.service_manager.create_custom_action.setVisible = MagicMock() self.service_manager.maintain_action.setVisible = MagicMock() self.service_manager.notes_action.setVisible = MagicMock() self.service_manager.time_action.setVisible = MagicMock() self.service_manager.auto_start_action.setVisible = MagicMock() self.service_manager.rename_action.setVisible = MagicMock() # WHEN: Show the context menu. self.service_manager.context_menu(q_point) # THEN: The following actions should be not visible. self.service_manager.edit_action.setVisible.assert_called_once_with(False), \ 'The action should be set invisible.' self.service_manager.create_custom_action.setVisible.assert_called_once_with(False), \ 'The action should be set invisible.' self.service_manager.maintain_action.setVisible.assert_called_once_with(False), \ 'The action should be set invisible.' self.service_manager.notes_action.setVisible.assert_called_with(True), 'The action should be set visible.' self.service_manager.time_action.setVisible.assert_called_once_with(False), \ 'The action should be set invisible.' self.service_manager.auto_start_action.setVisible.assert_called_with(True), \ 'The action should be set visible.' self.service_manager.rename_action.setVisible.assert_called_once_with(False), \ 'The action should be set invisible.'
def click_save_button_test(self): """ Test that the correct function is called when save is clicked, and that it behaves as expected. """ # GIVEN: Mocked methods. with patch('openlp.plugins.media.forms.mediaclipselectorform.critical_error_message_box') as \ mocked_critical_error_message_box,\ patch('PyQt4.QtGui.QDialog.exec_') as mocked_exec: self.form.exec_() # WHEN: The save button is clicked with a NoneType in start_time_ms or end_time_ms self.form.accept() # THEN: we should get an error message mocked_critical_error_message_box.assert_called_with( 'DVD not loaded correctly', 'The DVD was not loaded correctly, ' 'please re-load and try again.')
def test_loopy_context_menu(self): """ Test the context_menu() method with a loop """ # GIVEN: A service item added self.service_manager.setup_ui(self.service_manager) with patch('PyQt5.QtWidgets.QTreeWidget.itemAt') as mocked_item_at_method, \ patch('PyQt5.QtWidgets.QWidget.mapToGlobal'), \ patch('PyQt5.QtWidgets.QMenu.exec'): mocked_item = MagicMock() mocked_item.parent.return_value = None mocked_item_at_method.return_value = mocked_item # We want 1 to be returned for the position mocked_item.data.return_value = 1 # A service item without capabilities. service_item = ServiceItem() service_item.add_capability(ItemCapabilities.CanLoop) service_item._raw_frames.append("One") service_item._raw_frames.append("Two") self.service_manager.service_items = [{'service_item': service_item}] q_point = None # Mocked actions. self.service_manager.edit_action.setVisible = MagicMock() self.service_manager.create_custom_action.setVisible = MagicMock() self.service_manager.maintain_action.setVisible = MagicMock() self.service_manager.notes_action.setVisible = MagicMock() self.service_manager.time_action.setVisible = MagicMock() self.service_manager.auto_start_action.setVisible = MagicMock() # WHEN: Show the context menu. self.service_manager.context_menu(q_point) # THEN: The following actions should be not visible. self.service_manager.edit_action.setVisible.assert_called_once_with(False), \ 'The action should be set invisible.' self.service_manager.create_custom_action.setVisible.assert_called_once_with(False), \ 'The action should be set invisible.' self.service_manager.maintain_action.setVisible.assert_called_once_with(False), \ 'The action should be set invisible.' self.service_manager.notes_action.setVisible.assert_called_with(True), 'The action should be set visible.' self.service_manager.time_action.setVisible.assert_called_once_with(False), \ 'The action should be set invisible.' self.service_manager.auto_start_action.setVisible.assert_called_once_with(False), \ 'The action should be set invisible.'
def test_on_add_button_clicked(self): """ Test the on_add_button_clicked_test method / add_button button. """ # GIVEN: A mocked QDialog.exec() method with patch('PyQt5.QtWidgets.QDialog.exec') as mocked_exec: # WHEN: Add a new slide. QtTest.QTest.mouseClick(self.form.add_button, QtCore.Qt.LeftButton) # THEN: One slide should be added. assert self.form.slide_list_view.count() == 1, 'There should be one slide added.'
def basic_test(self): """ Test if the dialog is correctly set up. """ # GIVEN: A mocked QDialog.exec_() method with patch("PyQt4.QtGui.QDialog.exec_") as mocked_exec: # WHEN: Show the dialog. self.form.exec_() # THEN: The dialog should be empty. assert self.form.slide_text_edit.toPlainText() == "", "There should not be any text in the text editor."
def test_basic(self): """ Test if the dialog is correctly set up. """ # GIVEN: A mocked QDialog.exec() method with patch('PyQt5.QtWidgets.QDialog.exec') as mocked_exec: # WHEN: Show the dialog. self.form.exec() # THEN: The dialog should be empty. assert self.form.slide_text_edit.toPlainText() == '', 'There should not be any text in the text editor.'
def basic_test(self): """ Test if the dialog is correctly set up. """ # GIVEN: A mocked QDialog.exec_() method with patch('PyQt4.QtGui.QDialog.exec_') as mocked_exec: # WHEN: Show the dialog. self.form.exec_() # THEN: The media path should be empty. assert self.form.media_path_combobox.currentText() == '', 'There should not be any text in the media path.'
def on_add_button_clicked_test(self): """ Test the on_add_button_clicked_test method / add_button button. """ # GIVEN: A mocked QDialog.exec_() method with patch('PyQt4.QtGui.QDialog.exec_') as mocked_exec: # WHEN: Add a new slide. QtTest.QTest.mouseClick(self.form.add_button, QtCore.Qt.LeftButton) # THEN: One slide should be added. assert self.form.slide_list_view.count( ) == 1, 'There should be one slide added.'
def verify_service_type_changed_called_when_service_type_combo_changed_test(self): """ Test that the "on_service_type_combobox_changed" function is executed when the service_type_combobox is changed """ # GIVEN: An SelectPlanForm instance with airplane mode enabled, resources available, with patch('PyQt5.QtWidgets.QDialog.exec'): self.form.on_service_type_combobox_changed = MagicMock() self.form.exec() # WHEN: The Service Type combo is set to index 1 self.form.service_type_combo_box.setCurrentIndex(1) # THEN: The on_import_as_new_button_cliced function is called self.form.on_service_type_combobox_changed.assert_called_with(1)
def basic_test(self): """ Test if the dialog is correctly set up. """ # GIVEN: A mocked QDialog.exec_() method with patch('PyQt4.QtGui.QDialog.exec_') as mocked_exec: # WHEN: Show the dialog. self.form.exec_() # THEN: The media path should be empty. assert self.form.media_path_combobox.currentText( ) == '', 'There should not be any text in the media path.'
def verify_service_refreshed_when_refresh_button_clicked_test(self): """ Test that a service is refreshed when the "Refresh Service" button is clicked """ # GIVEN: An SelectPlanForm instance with airplane mode enabled, resources available, many mocked functions with patch('PyQt5.QtWidgets.QDialog.exec'), \ patch('openlp.core.lib.db.Manager.__init__'), \ patch('openlp.plugins.planningcenter.lib.songimport.PlanningCenterSongImport.add_song') as mock_add_song, \ patch('openlp.plugins.planningcenter.lib.customimport.PlanningCenterCustomImport.add_slide') as mock_add_slide: Registry().register('application', MagicMock()) Registry().register('service_manager', MagicMock()) Registry().register('songs', MagicMock()) Registry().register('custom', MagicMock()) self.form.exec() # WHEN: The Service Type combo is set to index 1 and the Select Plan combo box is set to index 3 and the "Import New" button is clicked self.form.service_type_combo_box.setCurrentIndex(1) self.form.plan_selection_combo_box.setCurrentIndex(3) QtTest.QTest.mouseClick(self.form.update_existing_button, QtCore.Qt.LeftButton) # THEN: The add_song function was called 2 times and add_slide once mock_add_slide.assert_called_once() self.assertEqual(mock_add_song.call_count, 2, 'Add_song should be called 2 times')
def basic_test(self): """ Test if the dialog is correctly set up. """ # GIVEN: A mocked QDialog.exec_() method with patch('PyQt4.QtGui.QDialog.exec_') as mocked_exec: # WHEN: Show the dialog. self.form.exec_() # THEN: The dialog should be empty. assert self.form.slide_text_edit.toPlainText( ) == '', 'There should not be any text in the text editor.'
def setUp(self): """ Create the UI """ Registry.create() self.setup_application() ScreenList.create(self.app.desktop()) Registry().register('application', MagicMock()) # Mock classes and methods used by mainwindow. with patch('openlp.core.ui.mainwindow.SettingsForm') as mocked_settings_form, \ patch('openlp.core.ui.mainwindow.ImageManager') as mocked_image_manager, \ patch('openlp.core.ui.mainwindow.LiveController') as mocked_live_controller, \ patch('openlp.core.ui.mainwindow.PreviewController') as mocked_preview_controller, \ patch('openlp.core.ui.mainwindow.OpenLPDockWidget') as mocked_dock_widget, \ patch('openlp.core.ui.mainwindow.QtWidgets.QToolBox') as mocked_q_tool_box_class, \ patch('openlp.core.ui.mainwindow.QtWidgets.QMainWindow.addDockWidget') as mocked_add_dock_method, \ patch('openlp.core.ui.mainwindow.ThemeManager') as mocked_theme_manager, \ patch('openlp.core.ui.mainwindow.ProjectorManager') as mocked_projector_manager, \ patch('openlp.core.ui.mainwindow.Renderer') as mocked_renderer: self.main_window = MainWindow() self.service_manager = Registry().get('service_manager')
def click_load_button_test(self): """ Test that the correct function is called when load is clicked, and that it behaves as expected. """ # GIVEN: Mocked methods. with patch('openlp.plugins.media.forms.mediaclipselectorform.critical_error_message_box') as \ mocked_critical_error_message_box,\ patch('openlp.plugins.media.forms.mediaclipselectorform.os.path.exists') as mocked_os_path_exists,\ patch('PyQt4.QtGui.QDialog.exec_') as mocked_exec: self.form.exec_() # WHEN: The load button is clicked with no path set QtTest.QTest.mouseClick(self.form.load_disc_button, QtCore.Qt.LeftButton) # THEN: we should get an error mocked_critical_error_message_box.assert_called_with(message='No path was given') # WHEN: The load button is clicked with a non-existing path mocked_os_path_exists.return_value = False self.form.media_path_combobox.insertItem(0, '/non-existing/test-path.test') self.form.media_path_combobox.setCurrentIndex(0) QtTest.QTest.mouseClick(self.form.load_disc_button, QtCore.Qt.LeftButton) # THEN: we should get an error assert self.form.media_path_combobox.currentText() == '/non-existing/test-path.test',\ 'The media path should be the given one.' mocked_critical_error_message_box.assert_called_with(message='Given path does not exists') # WHEN: The load button is clicked with a mocked existing path mocked_os_path_exists.return_value = True self.form.vlc_media_player = MagicMock() self.form.vlc_media_player.play.return_value = -1 self.form.media_path_combobox.insertItem(0, '/existing/test-path.test') self.form.media_path_combobox.setCurrentIndex(0) QtTest.QTest.mouseClick(self.form.load_disc_button, QtCore.Qt.LeftButton) # THEN: we should get an error assert self.form.media_path_combobox.currentText() == '/existing/test-path.test',\ 'The media path should be the given one.' mocked_critical_error_message_box.assert_called_with(message='VLC player failed playing the media')
def test_basic_accept(self): """ Test running the settings form and pressing Ok """ # GIVEN: An initial form # WHEN displaying the UI and pressing Ok with patch('PyQt5.QtWidgets.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, 'The QDialog.accept should have been called'
def time_display_test(self): """ Test StartTimeDialog display functionality """ # GIVEN: A service item with with time mocked_serviceitem = MagicMock() mocked_serviceitem.start_time = 61 mocked_serviceitem.end_time = 3701 mocked_serviceitem.media_length = 3701 # WHEN displaying the UI and pressing enter self.form.item = {'service_item': mocked_serviceitem} with patch('PyQt4.QtGui.QDialog.exec_'): self.form.exec_() ok_widget = self.form.button_box.button(self.form.button_box.Ok) QtTest.QTest.mouseClick(ok_widget, QtCore.Qt.LeftButton) # THEN the following input values are returned self.assertEqual(self.form.hour_spin_box.value(), 0) self.assertEqual(self.form.minute_spin_box.value(), 1) self.assertEqual(self.form.second_spin_box.value(), 1) self.assertEqual(self.form.item['service_item'].start_time, 61, 'The start time should stay the same') # WHEN displaying the UI, changing the time to 2min 3secs and pressing enter self.form.item = {'service_item': mocked_serviceitem} with patch('PyQt4.QtGui.QDialog.exec_'): self.form.exec_() self.form.minute_spin_box.setValue(2) self.form.second_spin_box.setValue(3) ok_widget = self.form.button_box.button(self.form.button_box.Ok) QtTest.QTest.mouseClick(ok_widget, QtCore.Qt.LeftButton) # THEN the following values are returned self.assertEqual(self.form.hour_spin_box.value(), 0) self.assertEqual(self.form.minute_spin_box.value(), 2) self.assertEqual(self.form.second_spin_box.value(), 3) self.assertEqual(self.form.item['service_item'].start_time, 123, 'The start time should have changed')
def disable_import_buttons_test(self): """ Test that the import buttons are disabled when the "Select Plan Date" element in the Plan Selection List is selected. """ # GIVEN: An SelectPlanForm instance with airplane mode enabled, resources available, and the form with patch('PyQt5.QtWidgets.QDialog.exec'): self.form.exec() # WHEN: The Select Plan combo box is set to "Select Plan Date" index = self.form.plan_selection_combo_box.findText('Select Plan Date') self.form.plan_selection_combo_box.setCurrentIndex(index) # THEN: "Import New" and "Refresh Service" buttons become inactive self.assertEqual(self.form.import_as_new_button.isEnabled(), False, '"Import as New" button should be disabled') self.assertEqual(self.form.update_existing_button.isEnabled(), False, '"Refresh Service" button should be disabled')
def test_basic_cancel(self): """ Test running the settings form and pressing Cancel """ # GIVEN: An initial form # WHEN displaying the UI and pressing cancel with patch('PyQt5.QtWidgets.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, 'The QDialog.reject should have been called'
def test_basic_cancel(self): """ Test running the settings form and pressing Cancel """ # GIVEN: An initial form # WHEN displaying the UI and pressing cancel with patch('PyQt5.QtWidgets.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, 'The QDialog.reject should have been called'
def test_basic_register(self): """ Test running the settings form and adding a single function """ # GIVEN: An initial form add a register function self.form.register_post_process('function1') # WHEN displaying the UI and pressing Ok with patch('PyQt5.QtWidgets.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, 'The one requested process should have been removed from the stack'
def test_change_slide(self): """ Test the change_slide method. """ # GIVEN: A ServiceItem with two frames content. service_item = ServiceItem(None) service = read_service_from_file('serviceitem_image_3.osj') with patch('os.path.exists'): service_item.set_from_service(service[0]) # WHEN: Added to the preview widget and switched to the second frame. self.preview_widget.replace_service_item(service_item, 1, 0) self.preview_widget.change_slide(1) # THEN: The current_slide_number should reflect the change. self.assertEqual(self.preview_widget.current_slide_number(), 1, 'The current slide number should be 1.')
def verify_import_function_called_when_import_button_clicked_test(self): """ Test that the "on_import_as_new_button_clicked" function is executed when the "Import New" button is clicked """ # GIVEN: An SelectPlanForm instance with airplane mode enabled, resources available, with patch('PyQt5.QtWidgets.QDialog.exec'): self.form.on_import_as_new_button_clicked = MagicMock() self.form.exec() # WHEN: The Service Type combo is set to index 1 and the Select Plan combo box is set to index 3 and the "Import New" button is clicked self.form.service_type_combo_box.setCurrentIndex(1) self.form.plan_selection_combo_box.setCurrentIndex(3) QtTest.QTest.mouseClick(self.form.import_as_new_button, QtCore.Qt.LeftButton) # THEN: The on_import_as_new_button_cliced function is called self.form.on_import_as_new_button_clicked.assert_called_with(False)
def test_replace_service_item(self): """ Test item counts and current number with a service item. """ # GIVEN: A ServiceItem with two frames. service_item = ServiceItem(None) service = read_service_from_file('serviceitem_image_3.osj') with patch('os.path.exists'): service_item.set_from_service(service[0]) # WHEN: Added to the preview widget. self.preview_widget.replace_service_item(service_item, 1, 1) # THEN: The slide count and number should fit. self.assertEqual(self.preview_widget.slide_count(), 2, 'The slide count should be 2.') self.assertEqual(self.preview_widget.current_slide_number(), 1, 'The current slide number should be 1.')
def restore_current_media_manager_item_test(self): """ Regression test for bug #1152509. """ # GIVEN: Mocked Settings().value method. with patch('openlp.core.ui.mainwindow.Settings.value') as mocked_value: # save current plugin: True; current media plugin: 2 mocked_value.side_effect = [True, 2] # WHEN: Call the restore method. self.main_window.restore_current_media_manager_item() # THEN: The current widget should have been set. self.main_window.media_tool_box.setCurrentIndex.assert_called_with(2)
def basic_display_test(self): """ Test Service Note form functionality """ # GIVEN: A dialog with an empty text box self.form.text_edit.setPlainText('') # WHEN displaying the UI and pressing enter with patch('PyQt4.QtGui.QDialog.exec_'): self.form.exec_() ok_widget = self.form.button_box.button(self.form.button_box.Save) QtTest.QTest.mouseClick(ok_widget, QtCore.Qt.LeftButton) # THEN the following input text is returned self.assertEqual(self.form.text_edit.toPlainText(), '', 'The returned text should be empty') # WHEN displaying the UI, having set the text and pressing enter text = 'OpenLP is the best worship software' self.form.text_edit.setPlainText(text) with patch('PyQt4.QtGui.QDialog.exec_'): self.form.exec_() ok_widget = self.form.button_box.button(self.form.button_box.Save) QtTest.QTest.mouseClick(ok_widget, QtCore.Qt.LeftButton) # THEN the following text is returned self.assertEqual(self.form.text_edit.toPlainText(), text, 'The text originally entered should still be there') # WHEN displaying the UI, having set the text and pressing enter self.form.text_edit.setPlainText('') with patch('PyQt4.QtGui.QDialog.exec_'): self.form.exec_() self.form.text_edit.setPlainText(text) ok_widget = self.form.button_box.button(self.form.button_box.Save) QtTest.QTest.mouseClick(ok_widget, QtCore.Qt.LeftButton) # THEN the following text is returned self.assertEqual(self.form.text_edit.toPlainText(), text, 'The new text should be returned')
def test_time_display(self): """ Test StartTimeDialog display functionality """ # GIVEN: A service item with with time mocked_serviceitem = MagicMock() mocked_serviceitem.start_time = 61 mocked_serviceitem.end_time = 3701 mocked_serviceitem.media_length = 3701 # WHEN displaying the UI and pressing enter self.form.item = {'service_item': mocked_serviceitem} with patch('PyQt5.QtWidgets.QDialog.exec'): self.form.exec() ok_widget = self.form.button_box.button(self.form.button_box.Ok) QtTest.QTest.mouseClick(ok_widget, QtCore.Qt.LeftButton) # THEN the following input values are returned self.assertEqual(self.form.hour_spin_box.value(), 0) self.assertEqual(self.form.minute_spin_box.value(), 1) self.assertEqual(self.form.second_spin_box.value(), 1) self.assertEqual(self.form.item['service_item'].start_time, 61, 'The start time should stay the same') # WHEN displaying the UI, changing the time to 2min 3secs and pressing enter self.form.item = {'service_item': mocked_serviceitem} with patch('PyQt5.QtWidgets.QDialog.exec'): self.form.exec() self.form.minute_spin_box.setValue(2) self.form.second_spin_box.setValue(3) ok_widget = self.form.button_box.button(self.form.button_box.Ok) QtTest.QTest.mouseClick(ok_widget, QtCore.Qt.LeftButton) # THEN the following values are returned self.assertEqual(self.form.hour_spin_box.value(), 0) self.assertEqual(self.form.minute_spin_box.value(), 2) self.assertEqual(self.form.second_spin_box.value(), 3) self.assertEqual(self.form.item['service_item'].start_time, 123, 'The start time should have changed')
def line_edit_focus_test(self): """ Regression test for bug1067251 Test that the file_name_edit setFocus has called with True when executed """ # GIVEN: A mocked QDialog.exec_() method and mocked file_name_edit.setFocus() method. with patch('PyQt4.QtGui.QDialog.exec_'): mocked_set_focus = MagicMock() self.form.file_name_edit.setFocus = mocked_set_focus # WHEN: The form is executed self.form.exec_() # THEN: the setFocus method of the file_name_edit has been called with True mocked_set_focus.assert_called_with()
def change_slide_test(self): """ Test the change_slide method. """ # GIVEN: A ServiceItem with two frames content. service_item = ServiceItem(None) service = read_service_from_file('serviceitem_image_3.osj') with patch('os.path.exists'): service_item.set_from_service(service[0]) # WHEN: Added to the preview widget and switched to the second frame. self.preview_widget.replace_service_item(service_item, 1, 0) self.preview_widget.change_slide(1) # THEN: The current_slide_number should reflect the change. self.assertEqual(self.preview_widget.current_slide_number(), 1, 'The current slide number should be 1.')
def test_restore_current_media_manager_item(self): """ Regression test for bug #1152509. """ # GIVEN: Mocked Settings().value method. with patch('openlp.core.ui.mainwindow.Settings.value') as mocked_value: # save current plugin: True; current media plugin: 2 mocked_value.side_effect = [True, 2] # WHEN: Call the restore method. self.main_window.restore_current_media_manager_item() # THEN: The current widget should have been set. self.main_window.media_tool_box.setCurrentIndex.assert_called_with( 2)
def test_validate_not_valid_part2(self): """ Test the _validate() method. """ # GIVEN: Mocked methods. with patch('openlp.plugins.custom.forms.editcustomform.critical_error_message_box') as \ mocked_critical_error_message_box: self.form.title_edit.displayText = MagicMock(return_value='something') self.form.slide_list_view.count = MagicMock(return_value=0) # WHEN: Call the method. result = self.form._validate() # THEN: The validate method should have returned False. assert not result, 'The _validate() method should have retured False' mocked_critical_error_message_box.assert_called_with(message='You need to add at least one slide.')
def test_basic_register(self): """ Test running the settings form and adding a single function """ # GIVEN: An initial form add a register function self.form.register_post_process('function1') # WHEN displaying the UI and pressing Ok with patch('PyQt5.QtWidgets.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, 'The one requested process should have been removed from the stack'
def basic_display_test(self): """ Test the EditAuthForm displays the default values from Settings """ # GIVEN: An PlanningCenterAuthForm instance with patch('PyQt5.QtWidgets.QDialog.exec'): # WHEN: The form is shown self.form.exec() # THEN: The default values match what is saved in the config self.assertEqual( self.form.application_id_line_edit.text(), self.application_id, 'The application_id edit box defaults to the value in settings.' ) self.assertEqual( self.form.secret_line_edit.text(), self.secret, 'The secret edit box defaults to the value in settings.')
def replace_service_item_test(self): """ Test item counts and current number with a service item. """ # GIVEN: A ServiceItem with two frames. service_item = ServiceItem(None) service = read_service_from_file('serviceitem_image_3.osj') with patch('os.path.exists'): service_item.set_from_service(service[0]) # WHEN: Added to the preview widget. self.preview_widget.replace_service_item(service_item, 1, 1) # THEN: The slide count and number should fit. self.assertEqual(self.preview_widget.slide_count(), 2, 'The slide count should be 2.') self.assertEqual(self.preview_widget.current_slide_number(), 1, 'The current slide number should be 1.')
def click_delete_credentials_button_test(self): """ Test that clicking the delete credentials button deletes the credentials from Settings """ # GIVEN: An instance of the PlanningCenterAuthForm with patch('PyQt5.QtWidgets.QDialog.exec'): # WHEN: The form is shown self.form.exec() # WHEN: The "delete credentials" button is clicked QtTest.QTest.mouseClick(self.form.delete_credentials_button, QtCore.Qt.LeftButton) # THEN: The application_id and secret should be set to '' in Settings self.assertEqual( Settings().value('planningcenter/application_id'), '', 'The application_id setting should have been reset.') self.assertEqual(Settings().value('planningcenter/secret'), '', 'The secret setting should have been reset')
def validate_not_valid_part2_test(self): """ Test the _validate() method. """ # GIVEN: Mocked methods. with patch('openlp.plugins.custom.forms.editcustomform.critical_error_message_box') as \ mocked_critical_error_message_box: self.form.title_edit.displayText = MagicMock( return_value='something') self.form.slide_list_view.count = MagicMock(return_value=0) # WHEN: Call the method. result = self.form._validate() # THEN: The validate method should have returned False. assert not result, 'The _validate() method should have retured False' mocked_critical_error_message_box.assert_called_with( message='You need to add at least one slide.')
def test_validate_not_valid_part1(self): """ Test the _validate() method. """ # GIVEN: Mocked methods. with patch('openlp.plugins.custom.forms.editcustomform.critical_error_message_box') as \ mocked_critical_error_message_box: self.form.title_edit.displayText = MagicMock(return_value='') mocked_setFocus = MagicMock() self.form.title_edit.setFocus = mocked_setFocus # WHEN: Call the method. result = self.form._validate() # THEN: The validate method should have returned False. assert not result, 'The _validate() method should have retured False' mocked_setFocus.assert_called_with() mocked_critical_error_message_box.assert_called_with(message='You need to type in a title.')
def adjust_button_test(self): """ Test the _adjust_button() method """ # GIVEN: A button. button = QtGui.QPushButton() checked = True enabled = True text = "new!" # WHEN: Call the method. with patch("PyQt4.QtGui.QPushButton.setChecked") as mocked_check_method: self.form._adjust_button(button, checked, enabled, text) # THEN: The button should be changed. self.assertEqual(button.text(), text, "The text should match.") mocked_check_method.assert_called_once_with(True) self.assertEqual(button.isEnabled(), enabled, "The button should be disabled.")
def test_adjust_button(self): """ Test the _adjust_button() method """ # GIVEN: A button. button = QtWidgets.QPushButton() checked = True enabled = True text = 'new!' # WHEN: Call the method. with patch('PyQt5.QtWidgets.QPushButton.setChecked') as mocked_check_method: self.form._adjust_button(button, checked, enabled, text) # THEN: The button should be changed. self.assertEqual(button.text(), text, 'The text should match.') mocked_check_method.assert_called_once_with(True) self.assertEqual(button.isEnabled(), enabled, 'The button should be disabled.')
def validate_not_valid_part1_test(self): """ Test the _validate() method. """ # GIVEN: Mocked methods. with patch('openlp.plugins.custom.forms.editcustomform.critical_error_message_box') as \ mocked_critical_error_message_box: self.form.title_edit.displayText = MagicMock(return_value='') mocked_setFocus = MagicMock() self.form.title_edit.setFocus = mocked_setFocus # WHEN: Call the method. result = self.form._validate() # THEN: The validate method should have returned False. assert not result, 'The _validate() method should have retured False' mocked_setFocus.assert_called_with() mocked_critical_error_message_box.assert_called_with( message='You need to type in a title.')
def test_set_text(self): """ Test the set_text() method. """ # GIVEN: A mocked QDialog.exec() method with patch('PyQt5.QtWidgets.QDialog.exec') as mocked_exec: mocked_set_focus = MagicMock() self.form.slide_text_edit.setFocus = mocked_set_focus wanted_text = 'THIS TEXT SHOULD BE SHOWN.' # WHEN: Show the dialog and set the text. self.form.exec() self.form.set_text(wanted_text) # THEN: The dialog should show the text. assert self.form.slide_text_edit.toPlainText() == wanted_text, \ 'The text editor should show the correct text.' # THEN: The dialog should have focus. mocked_set_focus.assert_called_with()
def set_text_test(self): """ Test the set_text() method. """ # GIVEN: A mocked QDialog.exec_() method with patch("PyQt4.QtGui.QDialog.exec_") as mocked_exec: mocked_set_focus = MagicMock() self.form.slide_text_edit.setFocus = mocked_set_focus wanted_text = "THIS TEXT SHOULD BE SHOWN." # WHEN: Show the dialog and set the text. self.form.exec_() self.form.set_text(wanted_text) # THEN: The dialog should show the text. assert ( self.form.slide_text_edit.toPlainText() == wanted_text ), "The text editor should show the correct text." # THEN: The dialog should have focus. mocked_set_focus.assert_called_with()
def setUp(self): """ Create the UI """ Registry.create() self.setup_application() self.main_window = QtGui.QMainWindow() Registry().register('main_window', self.main_window) # Mock VLC so we don't actually use it self.vlc_patcher = patch('openlp.plugins.media.forms.mediaclipselectorform.vlc') self.vlc_patcher.start() Registry().register('application', self.app) # Mock the media item self.mock_media_item = MagicMock() # create form to test self.form = MediaClipSelectorForm(self.mock_media_item, self.main_window, None) mock_media_state_wait = MagicMock() mock_media_state_wait.return_value = True self.form.media_state_wait = mock_media_state_wait self.form.application.set_busy_cursor = MagicMock() self.form.application.set_normal_cursor = MagicMock() self.form.find_optical_devices = MagicMock()
def setUp(self): """ Create the UI """ Registry.create() self.registry = Registry() self.setup_application() # Mock cursor busy/normal methods. self.app.set_busy_cursor = MagicMock() self.app.set_normal_cursor = MagicMock() self.app.args = [] Registry().register('application', self.app) # Mock classes and methods used by mainwindow. with patch('openlp.core.ui.mainwindow.SettingsForm') as mocked_settings_form, \ patch('openlp.core.ui.mainwindow.ImageManager') as mocked_image_manager, \ patch('openlp.core.ui.mainwindow.LiveController') as mocked_live_controller, \ patch('openlp.core.ui.mainwindow.PreviewController') as mocked_preview_controller, \ patch('openlp.core.ui.mainwindow.OpenLPDockWidget') as mocked_dock_widget, \ patch('openlp.core.ui.mainwindow.QtGui.QToolBox') as mocked_q_tool_box_class, \ patch('openlp.core.ui.mainwindow.QtGui.QMainWindow.addDockWidget') as mocked_add_dock_method, \ patch('openlp.core.ui.mainwindow.ServiceManager') as mocked_service_manager, \ patch('openlp.core.ui.mainwindow.ThemeManager') as mocked_theme_manager, \ patch('openlp.core.ui.mainwindow.ProjectorManager') as mocked_projector_manager, \ patch('openlp.core.ui.mainwindow.Renderer') as mocked_renderer: self.main_window = MainWindow()