def create_media_manager_item(self): """ Create the Media Manager List. """ self.media_item = PresentationMediaItem( self.main_window.media_dock_manager.media_dock, self, self.controllers)
def setUp(self): """ Set up the components need for all tests. """ Registry.create() Registry().register('service_manager', MagicMock()) Registry().register('main_window', MagicMock()) with patch('openlp.plugins.presentations.lib.mediaitem.MediaManagerItem._setup'), \ patch('openlp.plugins.presentations.lib.mediaitem.PresentationMediaItem.setup_item'): self.media_item = PresentationMediaItem(None, MagicMock, MagicMock()) self.setup_application()
class TestMediaItem(TestCase): """ Test the mediaitem methods. """ def setUp(self): """ Set up the components need for all tests. """ Registry.create() Registry().register('service_manager', MagicMock()) Registry().register('main_window', MagicMock()) with patch('openlp.plugins.presentations.lib.mediaitem.PresentationMediaItem.__init__') as mocked_init: mocked_init.return_value = None self.media_item = PresentationMediaItem(MagicMock(), MagicMock, MagicMock(), MagicMock()) self.application = QtGui.QApplication.instance() def tearDown(self): """ Delete all the C++ objects at the end so that we don't have a segfault """ del self.application def build_file_mask_string_test(self): """ Test the build_file_mask_string() method """ # GIVEN: Different controllers. impress_controller = MagicMock() impress_controller.enabled.return_value = True impress_controller.supports = ['odp'] impress_controller.also_supports = ['ppt'] presentation_controller = MagicMock() presentation_controller.enabled.return_value = True presentation_controller.supports = ['ppt'] presentation_controller.also_supports = [] presentation_viewer_controller = MagicMock() presentation_viewer_controller.enabled.return_value = False # Mock the controllers. self.media_item.controllers = { 'Impress': impress_controller, 'Powerpoint': presentation_controller, 'Powerpoint Viewer': presentation_viewer_controller } # WHEN: Build the file mask. with patch('openlp.plugins.presentations.lib.mediaitem.translate') as mocked_translate: mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate self.media_item.build_file_mask_string() # THEN: The file mask should be generated. assert self.media_item.on_new_file_masks == 'Presentations (*.odp *.ppt )', \ 'The file mask should contain the odp and ppt extensions'
def test_search(self, mocked_settings, *unreferenced_mocks): """ Test that the search method finds the correct results """ # GIVEN: A mocked Settings class which returns a list of Path objects, # and an instance of the PresentationMediaItem path_1 = Path('some_dir', 'Impress_file_1') path_2 = Path('some_other_dir', 'impress_file_2') path_3 = Path('another_dir', 'ppt_file') mocked_returned_settings = MagicMock() mocked_returned_settings.value.return_value = [path_1, path_2, path_3] mocked_settings.return_value = mocked_returned_settings media_item = PresentationMediaItem(None, MagicMock(), None) media_item.settings_section = '' # WHEN: Calling search results = media_item.search('IMPRE', False) # THEN: The first two results should have been returned assert results == [[str(path_1), 'Impress_file_1'], [str(path_2), 'impress_file_2']]
def setUp(self): """ Set up the components need for all tests. """ Registry.create() Registry().register('service_manager', MagicMock()) Registry().register('main_window', MagicMock()) with patch('openlp.plugins.presentations.lib.mediaitem.PresentationMediaItem.__init__') as mocked_init: mocked_init.return_value = None self.media_item = PresentationMediaItem(MagicMock(), MagicMock, MagicMock(), MagicMock()) self.application = QtGui.QApplication.instance()
class TestMediaItem(TestCase, TestMixin): """ Test the mediaitem methods. """ def setUp(self): """ Set up the components need for all tests. """ Registry.create() Registry().register('service_manager', MagicMock()) Registry().register('main_window', MagicMock()) with patch('openlp.plugins.presentations.lib.mediaitem.MediaManagerItem._setup'), \ patch('openlp.plugins.presentations.lib.mediaitem.PresentationMediaItem.setup_item'): self.media_item = PresentationMediaItem(None, MagicMock, MagicMock()) self.setup_application() def test_build_file_mask_string(self): """ Test the build_file_mask_string() method """ # GIVEN: Different controllers. impress_controller = MagicMock() impress_controller.enabled.return_value = True impress_controller.supports = ['odp'] impress_controller.also_supports = ['ppt'] presentation_controller = MagicMock() presentation_controller.enabled.return_value = True presentation_controller.supports = ['ppt'] presentation_controller.also_supports = [] presentation_viewer_controller = MagicMock() presentation_viewer_controller.enabled.return_value = False pdf_controller = MagicMock() pdf_controller.enabled.return_value = True pdf_controller.supports = ['pdf'] pdf_controller.also_supports = ['xps', 'oxps', 'epub', 'cbz', 'fb2'] # Mock the controllers. self.media_item.controllers = { 'Impress': impress_controller, 'Powerpoint': presentation_controller, 'Powerpoint Viewer': presentation_viewer_controller, 'Pdf': pdf_controller } # WHEN: Build the file mask. with patch('openlp.plugins.presentations.lib.mediaitem.translate' ) as mocked_translate: mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate self.media_item.build_file_mask_string() # THEN: The file mask should be generated correctly assert '*.odp' in self.media_item.on_new_file_masks, 'The file mask should contain the odp extension' assert '*.ppt' in self.media_item.on_new_file_masks, 'The file mask should contain the ppt extension' assert '*.pdf' in self.media_item.on_new_file_masks, 'The file mask should contain the pdf extension' assert '*.xps' in self.media_item.on_new_file_masks, 'The file mask should contain the xps extension' assert '*.oxps' in self.media_item.on_new_file_masks, 'The file mask should contain the oxps extension' assert '*.epub' in self.media_item.on_new_file_masks, 'The file mask should contain the epub extension' assert '*.cbz' in self.media_item.on_new_file_masks, 'The file mask should contain the cbz extension' assert '*.fb2' in self.media_item.on_new_file_masks, 'The file mask should contain the fb2 extension' def test_clean_up_thumbnails(self): """ Test that the clean_up_thumbnails method works as expected when files exists. """ # GIVEN: A mocked controller, and mocked os.path.getmtime mocked_disabled_controller = MagicMock() mocked_disabled_controller.enabled.return_value = False mocked_disabled_supports = PropertyMock() type(mocked_disabled_controller).supports = mocked_disabled_supports mocked_enabled_controller = MagicMock() mocked_enabled_controller.enabled.return_value = True mocked_doc = MagicMock(**{'get_thumbnail_path.return_value': Path()}) mocked_enabled_controller.add_document.return_value = mocked_doc mocked_enabled_controller.supports = ['tmp'] self.media_item.controllers = { 'Enabled': mocked_enabled_controller, 'Disabled': mocked_disabled_controller } thumb_path = MagicMock(st_mtime=100) file_path = MagicMock(st_mtime=400) with patch.object(Path, 'stat', side_effect=[thumb_path, file_path]), \ patch.object(Path, 'exists', return_value=True): presentation_file = Path('file.tmp') # WHEN: calling clean_up_thumbnails self.media_item.clean_up_thumbnails(presentation_file, True) # THEN: doc.presentation_deleted should have been called since the thumbnails mtime will be greater than # the presentation_file's mtime. mocked_doc.assert_has_calls( [call.get_thumbnail_path(1, True), call.presentation_deleted()], True) assert mocked_disabled_supports.call_count == 0 def test_clean_up_thumbnails_missing_file(self): """ Test that the clean_up_thumbnails method works as expected when file is missing. """ # GIVEN: A mocked controller, and mocked os.path.exists mocked_controller = MagicMock() mocked_doc = MagicMock() mocked_controller.add_document.return_value = mocked_doc mocked_controller.supports = ['tmp'] self.media_item.controllers = {'Mocked': mocked_controller} presentation_file = Path('file.tmp') with patch.object(Path, 'exists', return_value=False): # WHEN: calling clean_up_thumbnails self.media_item.clean_up_thumbnails(presentation_file, True) # THEN: doc.presentation_deleted should have been called since the presentation file did not exists. mocked_doc.assert_has_calls( [call.get_thumbnail_path(1, True), call.presentation_deleted()], True) @patch('openlp.plugins.presentations.lib.mediaitem.MediaManagerItem._setup' ) @patch( 'openlp.plugins.presentations.lib.mediaitem.PresentationMediaItem.setup_item' ) def test_search(self, *unreferenced_mocks): """ Test that the search method finds the correct results """ # GIVEN: A mocked Settings class which returns a list of Path objects, # and an instance of the PresentationMediaItem path_1 = Path('some_dir', 'Impress_file_1') path_2 = Path('some_other_dir', 'impress_file_2') path_3 = Path('another_dir', 'ppt_file') mocked_returned_settings = MagicMock() mocked_returned_settings.value.return_value = [path_1, path_2, path_3] Registry().register('settings', mocked_returned_settings) media_item = PresentationMediaItem(None, MagicMock(), None) media_item.settings_section = '' # WHEN: Calling search results = media_item.search('IMPRE', False) # THEN: The first two results should have been returned assert results == [[str(path_1), 'Impress_file_1'], [str(path_2), 'impress_file_2']]
class TestMediaItem(TestCase, TestMixin): """ Test the mediaitem methods. """ def setUp(self): """ Set up the components need for all tests. """ Registry.create() Registry().register('service_manager', MagicMock()) Registry().register('main_window', MagicMock()) with patch('openlp.plugins.presentations.lib.mediaitem.MediaManagerItem._setup'), \ patch('openlp.plugins.presentations.lib.mediaitem.PresentationMediaItem.setup_item'): self.media_item = PresentationMediaItem(None, MagicMock, MagicMock()) self.setup_application() def build_file_mask_string_test(self): """ Test the build_file_mask_string() method """ # GIVEN: Different controllers. impress_controller = MagicMock() impress_controller.enabled.return_value = True impress_controller.supports = ['odp'] impress_controller.also_supports = ['ppt'] presentation_controller = MagicMock() presentation_controller.enabled.return_value = True presentation_controller.supports = ['ppt'] presentation_controller.also_supports = [] presentation_viewer_controller = MagicMock() presentation_viewer_controller.enabled.return_value = False pdf_controller = MagicMock() pdf_controller.enabled.return_value = True pdf_controller.supports = ['pdf'] pdf_controller.also_supports = ['xps', 'oxps'] # Mock the controllers. self.media_item.controllers = { 'Impress': impress_controller, 'Powerpoint': presentation_controller, 'Powerpoint Viewer': presentation_viewer_controller, 'Pdf': pdf_controller } # WHEN: Build the file mask. with patch('openlp.plugins.presentations.lib.mediaitem.translate' ) as mocked_translate: mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate self.media_item.build_file_mask_string() # THEN: The file mask should be generated correctly self.assertIn('*.odp', self.media_item.on_new_file_masks, 'The file mask should contain the odp extension') self.assertIn('*.ppt', self.media_item.on_new_file_masks, 'The file mask should contain the ppt extension') self.assertIn('*.pdf', self.media_item.on_new_file_masks, 'The file mask should contain the pdf extension') self.assertIn('*.xps', self.media_item.on_new_file_masks, 'The file mask should contain the xps extension') self.assertIn('*.oxps', self.media_item.on_new_file_masks, 'The file mask should contain the oxps extension') def clean_up_thumbnails_test(self): """ Test that the clean_up_thumbnails method works as expected when files exists. """ # GIVEN: A mocked controller, and mocked os.path.getmtime mocked_controller = MagicMock() mocked_doc = MagicMock() mocked_controller.add_document.return_value = mocked_doc mocked_controller.supports = ['tmp'] self.media_item.controllers = {'Mocked': mocked_controller} presentation_file = 'file.tmp' with patch('openlp.plugins.presentations.lib.mediaitem.os.path.getmtime') as mocked_getmtime, \ patch('openlp.plugins.presentations.lib.mediaitem.os.path.exists') as mocked_exists: mocked_getmtime.side_effect = [100, 200] mocked_exists.return_value = True # WHEN: calling clean_up_thumbnails self.media_item.clean_up_thumbnails(presentation_file, True) # THEN: doc.presentation_deleted should have been called since the thumbnails mtime will be greater than # the presentation_file's mtime. mocked_doc.assert_has_calls( [call.get_thumbnail_path(1, True), call.presentation_deleted()], True) def clean_up_thumbnails_missing_file_test(self): """ Test that the clean_up_thumbnails method works as expected when file is missing. """ # GIVEN: A mocked controller, and mocked os.path.exists mocked_controller = MagicMock() mocked_doc = MagicMock() mocked_controller.add_document.return_value = mocked_doc mocked_controller.supports = ['tmp'] self.media_item.controllers = {'Mocked': mocked_controller} presentation_file = 'file.tmp' with patch('openlp.plugins.presentations.lib.mediaitem.os.path.exists' ) as mocked_exists: mocked_exists.return_value = False # WHEN: calling clean_up_thumbnails self.media_item.clean_up_thumbnails(presentation_file, True) # THEN: doc.presentation_deleted should have been called since the presentation file did not exists. mocked_doc.assert_has_calls( [call.get_thumbnail_path(1, True), call.presentation_deleted()], True)
class TestMediaItem(TestCase, TestMixin): """ Test the mediaitem methods. """ def setUp(self): """ Set up the components need for all tests. """ Registry.create() Registry().register('service_manager', MagicMock()) Registry().register('main_window', MagicMock()) with patch('openlp.plugins.presentations.lib.mediaitem.MediaManagerItem._setup'), \ patch('openlp.plugins.presentations.lib.mediaitem.PresentationMediaItem.setup_item'): self.media_item = PresentationMediaItem(None, MagicMock, MagicMock()) self.setup_application() def build_file_mask_string_test(self): """ Test the build_file_mask_string() method """ # GIVEN: Different controllers. impress_controller = MagicMock() impress_controller.enabled.return_value = True impress_controller.supports = ['odp'] impress_controller.also_supports = ['ppt'] presentation_controller = MagicMock() presentation_controller.enabled.return_value = True presentation_controller.supports = ['ppt'] presentation_controller.also_supports = [] presentation_viewer_controller = MagicMock() presentation_viewer_controller.enabled.return_value = False pdf_controller = MagicMock() pdf_controller.enabled.return_value = True pdf_controller.supports = ['pdf'] pdf_controller.also_supports = ['xps', 'oxps'] # Mock the controllers. self.media_item.controllers = { 'Impress': impress_controller, 'Powerpoint': presentation_controller, 'Powerpoint Viewer': presentation_viewer_controller, 'Pdf': pdf_controller } # WHEN: Build the file mask. with patch('openlp.plugins.presentations.lib.mediaitem.translate') as mocked_translate: mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate self.media_item.build_file_mask_string() # THEN: The file mask should be generated correctly self.assertIn('*.odp', self.media_item.on_new_file_masks, 'The file mask should contain the odp extension') self.assertIn('*.ppt', self.media_item.on_new_file_masks, 'The file mask should contain the ppt extension') self.assertIn('*.pdf', self.media_item.on_new_file_masks, 'The file mask should contain the pdf extension') self.assertIn('*.xps', self.media_item.on_new_file_masks, 'The file mask should contain the xps extension') self.assertIn('*.oxps', self.media_item.on_new_file_masks, 'The file mask should contain the oxps extension') def clean_up_thumbnails_test(self): """ Test that the clean_up_thumbnails method works as expected when files exists. """ # GIVEN: A mocked controller, and mocked os.path.getmtime mocked_controller = MagicMock() mocked_doc = MagicMock() mocked_controller.add_document.return_value = mocked_doc mocked_controller.supports = ['tmp'] self.media_item.controllers = { 'Mocked': mocked_controller } presentation_file = 'file.tmp' with patch('openlp.plugins.presentations.lib.mediaitem.os.path.getmtime') as mocked_getmtime, \ patch('openlp.plugins.presentations.lib.mediaitem.os.path.exists') as mocked_exists: mocked_getmtime.side_effect = [100, 200] mocked_exists.return_value = True # WHEN: calling clean_up_thumbnails self.media_item.clean_up_thumbnails(presentation_file, True) # THEN: doc.presentation_deleted should have been called since the thumbnails mtime will be greater than # the presentation_file's mtime. mocked_doc.assert_has_calls([call.get_thumbnail_path(1, True), call.presentation_deleted()], True) def clean_up_thumbnails_missing_file_test(self): """ Test that the clean_up_thumbnails method works as expected when file is missing. """ # GIVEN: A mocked controller, and mocked os.path.exists mocked_controller = MagicMock() mocked_doc = MagicMock() mocked_controller.add_document.return_value = mocked_doc mocked_controller.supports = ['tmp'] self.media_item.controllers = { 'Mocked': mocked_controller } presentation_file = 'file.tmp' with patch('openlp.plugins.presentations.lib.mediaitem.os.path.exists') as mocked_exists: mocked_exists.return_value = False # WHEN: calling clean_up_thumbnails self.media_item.clean_up_thumbnails(presentation_file, True) # THEN: doc.presentation_deleted should have been called since the presentation file did not exists. mocked_doc.assert_has_calls([call.get_thumbnail_path(1, True), call.presentation_deleted()], True)
class PresentationPlugin(Plugin): """ This plugin allowed a Presentation to be opened, controlled and displayed on the output display. The plugin controls third party applications such as OpenOffice.org Impress, and Microsoft PowerPoint. """ log = logging.getLogger('PresentationPlugin') 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 create_settings_tab(self, parent): """ Create the settings Tab. :param parent: parent UI Element """ visible_name = self.get_string(StringContent.VisibleName) self.settings_tab = PresentationTab(parent, self.name, visible_name['title'], self.controllers, self.icon_path) def initialise(self): """ Initialise the plugin. Determine which controllers are enabled are start their processes. """ log.info('Presentations Initialising') super(PresentationPlugin, self).initialise() for controller in self.controllers: if self.controllers[controller].enabled(): try: self.controllers[controller].start_process() except Exception: log.exception('Failed to start controller process') self.controllers[controller].available = False self.media_item.build_file_mask_string() def finalise(self): """ Finalise the plugin. Ask all the enabled presentation applications to close down their applications and release resources. """ log.info('Plugin Finalise') # Ask each controller to tidy up. for key in self.controllers: controller = self.controllers[key] if controller.enabled(): controller.kill() super(PresentationPlugin, self).finalise() def create_media_manager_item(self): """ Create the Media Manager List. """ self.media_item = PresentationMediaItem( self.main_window.media_dock_manager.media_dock, self, self.controllers) def register_controllers(self, controller): """ Register each presentation controller (Impress, PPT etc) and store for later use. :param controller: controller to register """ self.controllers[controller.name] = controller def check_pre_conditions(self): """ Check to see if we have any presentation software available. If not do not install the plugin. """ log.debug('check_pre_conditions') controller_dir = os.path.join('plugins', 'presentations', 'lib') # Find all files that do not begin with '.' (lp:#1738047) and end with controller.py glob_pattern = os.path.join(controller_dir, '[!.]*controller.py') extension_loader(glob_pattern, ['presentationcontroller.py']) controller_classes = PresentationController.__subclasses__() for controller_class in controller_classes: controller = controller_class(self) self.register_controllers(controller) return bool(self.controllers) def app_startup(self): """ Perform tasks on application startup. """ # TODO: Can be removed when the upgrade path to OpenLP 3.0 is no longer needed, also ensure code in # PresentationDocument.get_thumbnail_folder and PresentationDocument.get_temp_folder is removed super().app_startup() presentation_paths = Settings().value( 'presentations/presentations files') for path in presentation_paths: self.media_item.clean_up_thumbnails(path, clean_for_update=True) self.media_item.list_view.clear() Settings().setValue('presentations/thumbnail_scheme', 'md5') self.media_item.validate_and_load(presentation_paths) @staticmethod def about(): """ Return information about this plugin. """ about_text = translate( 'PresentationPlugin', '<strong>Presentation ' 'Plugin</strong><br />The presentation plugin provides the ' 'ability to show presentations using a number of different ' 'programs. The choice of available presentation programs is ' 'available to the user in a drop down box.') return about_text def set_plugin_text_strings(self): """ Called to define all translatable texts of the plugin. """ # Name PluginList self.text_strings[StringContent.Name] = { 'singular': translate('PresentationPlugin', 'Presentation', 'name singular'), 'plural': translate('PresentationPlugin', 'Presentations', 'name plural') } # Name for MediaDockManager, SettingsManager self.text_strings[StringContent.VisibleName] = { 'title': translate('PresentationPlugin', 'Presentations', 'container title') } # Middle Header Bar tooltips = { 'load': translate('PresentationPlugin', 'Load a new presentation.'), 'import': '', 'new': '', 'edit': '', 'delete': translate('PresentationPlugin', 'Delete the selected presentation.'), 'preview': translate('PresentationPlugin', 'Preview the selected presentation.'), 'live': translate('PresentationPlugin', 'Send the selected presentation live.'), 'service': translate('PresentationPlugin', 'Add the selected presentation to the service.') } self.set_plugin_ui_text_strings(tooltips)