def test_service_item_load_custom_from_service(self): """ Test the Service Item - adding a custom slide from a saved service """ # GIVEN: A new service item and a mocked add icon function service_item = ServiceItem(None) service_item.add_icon = MagicMock() FormattingTags.load_tags() # WHEN: We add a custom from a saved serviceand set the media state line = convert_file_service_item(TEST_PATH, 'serviceitem_custom_1.osj') State().add_service("media", 0) State().update_pre_conditions("media", True) State().flush_preconditions() service_item.set_from_service(line) # THEN: We should get back a valid service item assert service_item.is_valid is True, 'The new service item should be valid' assert len(service_item.get_frames() ) == 2, 'The service item should have 2 display frames' assert len(service_item.capabilities ) == 5, 'There should be 5 default custom item capabilities' # THEN: The frames should also be valid assert 'Test Custom' == service_item.get_display_title( ), 'The title should be "Test Custom"' assert 'Slide 1' == service_item.get_frames()[0]['text'] assert 'Slide 2' == service_item.get_rendered_frame(1) assert 'Slide 1' == service_item.get_frame_title( 0), '"Slide 1" has been returned as the title' assert 'Slide 2' == service_item.get_frame_title( 1), '"Slide 2" has been returned as the title' assert '' == service_item.get_frame_title( 2), 'Blank has been returned as the title of slide 3'
def test_hook_settings_tabs_with_active_plugin_and_mocked_form(self): """ Test running the hook_settings_tabs() method with an active plugin and a mocked settings form """ # GIVEN: A PluginManager instance and a list with a mocked up plugin whose status is set to Active mocked_plugin = MagicMock() mocked_plugin.status = PluginStatus.Active plugin_manager = PluginManager() Registry().register('mock_plugin', mocked_plugin) mocked_settings_form = MagicMock() # Replace the autoloaded plugin with the version for testing in real code this would error mocked_settings_form.plugin_manager = plugin_manager State().add_service("mock", 1, is_plugin=True, status=PluginStatus.Active) State().flush_preconditions() # WHEN: We run hook_settings_tabs() plugin_manager.hook_settings_tabs() # THEN: The create_media_manager_item() method should have been called with the mocked settings form assert 1 == mocked_plugin.create_settings_tab.call_count, \ 'The create_media_manager_item() method should have been called once.' assert plugin_manager.plugins == mocked_settings_form.plugin_manager.plugins, \ 'The plugins on the settings form should be the same as the plugins in the plugin manager'
def __init__(self): super(SongUsagePlugin, self).__init__('songusage') self.manager = Manager('songusage', init_schema, upgrade_mod=upgrade) self.weight = -4 self.icon = UiIcons().song_usage self.song_usage_active = False State().add_service('song_usage', self.weight, is_plugin=True) State().update_pre_conditions('song_usage', self.check_pre_conditions())
def test_active_service(self): # GIVEN a new state State().load_settings() # WHEN I add a new service which is Active State().add_service("test", 1, 1, PluginStatus.Active) # THEN I have a single saved service assert State().is_module_active('test') is True
def test_add_service(self): # GIVEN a new state State().load_settings() # WHEN I add a new service State().add_service("test", 1, PluginStatus.Active) # THEN I have a saved service assert len(State().modules) == 1
def __init__(self): super(ImagePlugin, self).__init__('images', ImageMediaItem, ImageTab) self.manager = Manager('images', init_schema, upgrade_mod=upgrade) self.weight = -7 self.icon_path = UiIcons().picture self.icon = build_icon(self.icon_path) register_endpoint(images_endpoint) register_endpoint(api_images_endpoint) State().add_service('image', self.weight, is_plugin=True) State().update_pre_conditions('image', self.check_pre_conditions())
def __init__(self): super(BiblePlugin, self).__init__('bibles', BibleMediaItem, BiblesTab) self.weight = -9 self.icon_path = UiIcons().bible self.icon = UiIcons().bible self.manager = BibleManager(self) register_endpoint(bibles_endpoint) register_endpoint(api_bibles_endpoint) State().add_service('bible', self.weight, is_plugin=True) State().update_pre_conditions('bible', self.check_pre_conditions())
def __init__(self): super(CustomPlugin, self).__init__('custom', CustomMediaItem, CustomTab) self.weight = -5 self.db_manager = Manager('custom', init_schema) self.icon_path = UiIcons().clone self.icon = build_icon(self.icon_path) register_endpoint(custom_endpoint) register_endpoint(api_custom_endpoint) State().add_service(self.name, self.weight, is_plugin=True) State().update_pre_conditions(self.name, self.check_pre_conditions())
def __init__(self): super(MediaPlugin, self).__init__('media', MediaMediaItem) self.weight = -6 self.icon_path = UiIcons().video self.icon = build_icon(self.icon_path) # passed with drag and drop messages self.dnd_id = 'Media' register_endpoint(media_endpoint) register_endpoint(api_media_endpoint) State().add_service(self.name, self.weight, requires='mediacontroller', is_plugin=True) State().update_pre_conditions(self.name, self.check_pre_conditions())
def required_icons(self): """ Set which icons the media manager tab should show """ MediaManagerItem.required_icons(self) self.has_file_icon = True self.has_new_icon = False self.has_edit_icon = False if not State().check_preconditions('media'): self.can_preview = False self.can_make_live = False self.can_add_to_service = False if State().check_preconditions('media_live'): self.can_make_live = True
def __init__(self): """ Create and set up the PlanningCenter plugin. """ super(PlanningCenterPlugin, self).__init__('planningcenter', settings_tab_class=PlanningCenterTab) self.planningcenter_form = None self.icon = UiIcons().planning_center self.icon_path = self.icon self.weight = -1 State().add_service('planning_center', self.weight, is_plugin=True) State().update_pre_conditions('planning_center', self.check_pre_conditions())
def __init__(self): """ PluginPresentation constructor. """ log.debug('Initialised') self.controllers = {} Plugin.__init__(self, 'presentations', None) self.weight = -8 self.icon_path = UiIcons().presentation self.icon = build_icon(self.icon_path) register_endpoint(presentations_endpoint) register_endpoint(api_presentations_endpoint) State().add_service('presentation', self.weight, is_plugin=True) State().update_pre_conditions('presentation', self.check_pre_conditions())
def hook_media_manager(): """ Create the plugins' media manager items. """ for plugin in State().list_plugins(): if plugin and plugin.status is not PluginStatus.Disabled: plugin.create_media_manager_item()
def validate_item(self, suffixes=None): """ Validates a service item to make sure it is valid :param set[str] suffixes: A set of valid suffixes """ self.is_valid = True for slide in self.slides: if self.is_image() and not os.path.exists(slide['path']): self.is_valid = False break elif self.is_command(): if self.is_capable(ItemCapabilities.IsOptical) and State().check_preconditions('media'): if not os.path.exists(slide['title']): self.is_valid = False break else: file_name = os.path.join(slide['path'], slide['title']) if not os.path.exists(file_name): self.is_valid = False break if suffixes and not self.is_text(): file_suffix = "*.{suffx}".format(suffx=slide['title'].split('.')[-1]) if file_suffix.lower() not in suffixes: self.is_valid = False break
def setUp(self): """ Set up the objects we need for all of the tests """ 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.process_events = MagicMock() self.app.args = [] Registry().register('application', self.app) Registry().set_flag('no_web_server', True) self.add_toolbar_action_patcher = patch( 'openlp.core.ui.mainwindow.create_action') self.mocked_add_toolbar_action = self.add_toolbar_action_patcher.start( ) self.mocked_add_toolbar_action.side_effect = self._create_mock_action self.renderer_patcher = patch('openlp.core.display.render.Renderer') self.mocked_renderer = self.renderer_patcher.start() mocked_desktop = MagicMock() mocked_desktop.screenCount.return_value = 1 mocked_desktop.screenGeometry.return_value = QtCore.QRect( 0, 0, 1024, 768) mocked_desktop.primaryScreen.return_value = 1 ScreenList.create(mocked_desktop) State().load_settings() self.main_window = MainWindow()
def _validate_theme_action(self, select_text, confirm_title, confirm_text, test_plugin=True, confirm=True): """ Check to see if theme has been selected and the destructive action is allowed. :param select_text: Text for message box if no item selected. :param confirm_title: Confirm message title to be displayed. :param confirm_text: Confirm message text to be displayed. :param test_plugin: Do we check the plugins for theme usage. :param confirm: Do we display a confirm box before run checks. :return: True or False depending on the validity. """ self.global_theme = Settings().value(self.settings_section + '/global theme') if check_item_selected(self.theme_list_widget, select_text): item = self.theme_list_widget.currentItem() theme = item.text() # confirm deletion if confirm: answer = QtWidgets.QMessageBox.question( self, confirm_title, confirm_text.format(theme_name=theme), defaultButton=QtWidgets.QMessageBox.No) if answer == QtWidgets.QMessageBox.No: return False # should be the same unless default if theme != item.data(QtCore.Qt.UserRole): critical_error_message_box(message=translate( 'OpenLP.ThemeManager', 'You are unable to delete the default theme.')) return False # check for use in the system else where. if test_plugin: plugin_usage = "" for plugin in State().list_plugins(): used_count = plugin.uses_theme(theme) if used_count: plugin_usage = "{plug}{text}".format( plug=plugin_usage, text=(translate( 'OpenLP.ThemeManager', '{count} time(s) by {plugin}').format( name=used_count, plugin=plugin.name))) plugin_usage = "{text}\n".format(text=plugin_usage) if plugin_usage: critical_error_message_box( translate('OpenLP.ThemeManager', 'Unable to delete theme'), translate('OpenLP.ThemeManager', 'Theme is currently used \n\n{text}').format( text=plugin_usage)) return False return True return False
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) Registry().set_flag('no_web_server', True) mocked_plugin = MagicMock() mocked_plugin.status = PluginStatus.Active mocked_plugin.icon = QtGui.QIcon() Registry().register('mock_plugin', mocked_plugin) State().add_service("mock", 1, is_plugin=True, status=PluginStatus.Active) # Mock classes and methods used by mainwindow. with patch('openlp.core.ui.mainwindow.SettingsForm'), \ patch('openlp.core.ui.mainwindow.OpenLPDockWidget'), \ patch('openlp.core.ui.mainwindow.QtWidgets.QToolBox'), \ patch('openlp.core.ui.mainwindow.QtWidgets.QMainWindow.addDockWidget'), \ patch('openlp.core.ui.mainwindow.ServiceManager'), \ patch('openlp.core.ui.mainwindow.ThemeManager'), \ patch('openlp.core.ui.mainwindow.ProjectorManager'), \ patch('openlp.core.ui.mainwindow.HttpServer'), \ patch('openlp.core.ui.mainwindow.WebSocketServer'), \ patch('openlp.core.ui.mainwindow.start_zeroconf'), \ patch('openlp.core.ui.mainwindow.PluginForm'): self.main_window = MainWindow()
def setUp(self): """ Create the UI """ self.registry = Registry() Registry.create() self.setup_application() self.build_settings() State().load_settings() Registry().register('settings', Settings()) Registry().register( 'main_window', MagicMock(service_manager_settings_section='servicemanager')) self.application_id = 'abc' self.secret = '123' Settings().setValue('planningcenter/application_id', self.application_id) Settings().setValue('planningcenter/secret', self.secret) # init the planning center plugin so we have default values defined for Settings() self.planning_center_plugin = PlanningCenterPlugin() # setup our form self.form = SelectPlanForm() self.form.planning_center_api.airplane_mode = True self.form.planning_center_api.airplane_mode_directory = TEST_PATH self.theme_manager = ThemeManager(None) self.theme_manager.get_theme_names = MagicMock() self.theme_manager.get_theme_names.return_value = ['themeA', 'themeB']
def on_rename_theme(self, field=None): """ Renames an existing theme to a new name :param field: """ if self._validate_theme_action( translate('OpenLP.ThemeManager', 'You must select a theme to rename.'), translate('OpenLP.ThemeManager', 'Rename Confirmation'), translate('OpenLP.ThemeManager', 'Rename {theme_name} theme?'), False, False): item = self.theme_list_widget.currentItem() old_theme_name = item.data(QtCore.Qt.UserRole) self.file_rename_form.file_name_edit.setText(old_theme_name) if self.file_rename_form.exec(): new_theme_name = self.file_rename_form.file_name_edit.text() if old_theme_name == new_theme_name: return if self.check_if_theme_exists(new_theme_name): old_theme_data = self.get_theme_data(old_theme_name) self.clone_theme_data(old_theme_data, new_theme_name) self.delete_theme(old_theme_name) for plugin in State().list_plugins(): if plugin.uses_theme(old_theme_name): plugin.rename_theme(old_theme_name, new_theme_name) self.renderer.set_theme( self.get_theme_data(new_theme_name)) self.load_themes()
def new_service_created(): """ Loop through all the plugins and give them an opportunity to handle a new service """ for plugin in State().list_plugins(): if plugin.is_active(): plugin.new_service_created()
def exec(self, starting_tab_name=None): """ Execute the form """ # load all the widgets self.setting_list_widget.blockSignals(True) self.setting_list_widget.clear() while self.stacked_layout.count(): # take at 0 and the rest shuffle up. self.stacked_layout.takeAt(0) self.insert_tab(self.general_tab) self.insert_tab(self.advanced_tab) self.insert_tab(self.screens_tab) self.insert_tab(self.themes_tab) self.insert_tab(self.player_tab) self.insert_tab(self.projector_tab) self.insert_tab(self.api_tab) for plugin in State().list_plugins(): if plugin.settings_tab: self.insert_tab(plugin.settings_tab, plugin.is_active()) self.setting_list_widget.blockSignals(False) starting_tab_row = 0 for index in range(self.setting_list_widget.count()): item = self.setting_list_widget.item(index) if item.text() == starting_tab_name: starting_tab_row = index break self.setting_list_widget.setCurrentRow(starting_tab_row) return QtWidgets.QDialog.exec(self)
def __init__(self): """ Class __init__ method """ super(AlertsPlugin, self).__init__('alerts', settings_tab_class=AlertsTab) self.weight = -3 self.icon_path = UiIcons().alert self.icon = self.icon_path AlertsManager(self) self.manager = Manager('alerts', init_schema) self.alert_form = AlertForm(self) register_endpoint(alerts_endpoint) register_endpoint(api_alerts_endpoint) State().add_service(self.name, self.weight, is_plugin=True) State().update_pre_conditions(self.name, self.check_pre_conditions())
def get_plugin_by_name(name): """ Return the plugin which has a name with value ``name``. """ for plugin in State().list_plugins(): if plugin and plugin.name == name: return plugin return None
def setUp(self): """ Create the UI. """ Registry.create() self.setup_application() State().load_settings() State().add_service("media", 0) State().update_pre_conditions("media", True) State().flush_preconditions() self.main_window = QtWidgets.QMainWindow() self.image = QtGui.QImage(1, 1, QtGui.QImage.Format_RGB32) self.image_manager = MagicMock() self.image_manager.get_image.return_value = self.image Registry().register('image_manager', self.image_manager) self.preview_widget = ListPreviewWidget(self.main_window, 2) Registry().register('settings', Settings())
def hook_tools_menu(self): """ Loop through all the plugins and give them an opportunity to add an item to the tools menu. """ for plugin in State().list_plugins(): if plugin and plugin.status is not PluginStatus.Disabled: plugin.add_tools_menu_item(self.main_window.tools_menu)
def bootstrap_post_set_up(self): """ Set up the controllers. :return: """ if State().check_preconditions('mediacontroller'): try: self.setup_display(self.live_controller, False) except AttributeError: State().update_pre_conditions('media_live', False) State().missing_text( 'media_live', translate( 'OpenLP.MediaController', 'No Displays have been configured, so Live Media has been disabled' )) self.setup_display(self.preview_controller, True)
def bootstrap_completion(self): """ Give all the plugins a chance to perform some tasks at startup """ self.application.process_events() for plugin in State().list_plugins(): if plugin and plugin.is_active(): plugin.app_startup() self.application.process_events()
def __init__(self): """ Create and set up the Songs plugin. """ super(SongsPlugin, self).__init__('songs', SongMediaItem, SongsTab) self.manager = Manager('songs', init_schema, upgrade_mod=upgrade) self.weight = -10 self.icon_path = UiIcons().music self.icon = build_icon(self.icon_path) self.songselect_form = None self.settings.extend_default_settings(song_footer) register_endpoint(songs_endpoint) register_endpoint(api_songs_endpoint) State().add_service(self.name, self.weight, is_plugin=True) State().update_pre_conditions(self.name, self.check_pre_conditions()) if not self.settings.value('songs/last import type'): self.settings.setValue('songs/last import type', SongFormat.OpenLyrics)
def hook_upgrade_plugin_settings(settings): """ Loop through all the plugins and give them an opportunity to upgrade their settings. :param settings: The Settings object containing the old settings. """ for plugin in State().list_plugins(): if plugin and plugin.status is not PluginStatus.Disabled: plugin.upgrade_settings(settings)
def finalise_plugins(self): """ Loop through all the plugins and give them an opportunity to clean themselves up """ for plugin in State().list_plugins(): if plugin and plugin.is_active(): plugin.finalise() self.log_info('Finalisation Complete for {plugin}'.format( plugin=plugin.name))