def on_replace_click(self): """ Called to replace Live background with the media selected. """ if check_item_selected(self.list_view, translate('MediaPlugin.MediaItem', 'You must select a media file to replace the background with.')): item = self.list_view.currentItem() filename = item.data(QtCore.Qt.UserRole) if os.path.exists(filename): service_item = ServiceItem() service_item.title = 'webkit' service_item.processor = 'webkit' (path, name) = os.path.split(filename) service_item.add_from_command(path, name, CLAPPERBOARD) if self.media_controller.video(DisplayControllerType.Live, service_item, video_behind_text=True): self.reset_action.setVisible(True) else: critical_error_message_box(UiStrings().LiveBGError, translate('MediaPlugin.MediaItem', 'There was no display item to amend.')) else: critical_error_message_box(UiStrings().LiveBGError, translate('MediaPlugin.MediaItem', 'There was a problem replacing your background, ' 'the media file "%s" no longer exists.') % filename)
def setup(self): """ Set up and build the output screen """ self.log_debug('Start MainDisplay setup (live = %s)' % self.is_live) self.screen = self.screens.current self.setVisible(False) Display.setup(self) if self.is_live: # Build the initial frame. background_color = QtGui.QColor() background_color.setNamedColor(Settings().value('advanced/default color')) if not background_color.isValid(): background_color = QtCore.Qt.white image_file = Settings().value('advanced/default image') splash_image = QtGui.QImage(image_file) self.initial_fame = QtGui.QImage( self.screen['size'].width(), self.screen['size'].height(), QtGui.QImage.Format_ARGB32_Premultiplied) painter_image = QtGui.QPainter() painter_image.begin(self.initial_fame) painter_image.fillRect(self.initial_fame.rect(), background_color) painter_image.drawImage( (self.screen['size'].width() - splash_image.width()) // 2, (self.screen['size'].height() - splash_image.height()) // 2, splash_image) service_item = ServiceItem() service_item.bg_image_bytes = image_to_byte(self.initial_fame) self.web_view.setHtml(build_html(service_item, self.screen, self.is_live, None, plugins=self.plugin_manager.plugins)) self._hide_mouse()
def test_add_from_command_for_a_presentation(self): """ Test the Service Item - adding a presentation """ # GIVEN: A service item, a mocked icon and presentation data service_item = ServiceItem(None) presentation_name = 'test.pptx' image = MagicMock() display_title = 'DisplayTitle' notes = 'Note1\nNote2\n' frame = { 'title': presentation_name, 'image': image, 'path': TEST_PATH, 'display_title': display_title, 'notes': notes } # WHEN: adding presentation to service_item service_item.add_from_command(TEST_PATH, presentation_name, image, display_title, notes) # THEN: verify that it is setup as a Command and that the frame data matches assert service_item.service_item_type == ServiceItemType.Command, 'It should be a Command' assert service_item.get_frames()[0] == frame, 'Frames should match'
def build_html(self, service_item, image_path=''): """ Store the service_item and build the new HTML from it. Add the HTML to the display :param service_item: The Service item to be used :param image_path: Where the image resides. """ self.web_loaded = False self.initial_fame = None self.service_item = service_item background = None # We have an image override so keep the image till the theme changes. if self.override: # We have an video override so allow it to be stopped. if 'video' in self.override: Registry().execute('video_background_replaced') self.override = {} # We have a different theme. elif self.override['theme'] != service_item.theme_data.background_filename: Registry().execute('live_theme_changed') self.override = {} else: # replace the background background = self.image_manager.get_image_bytes(self.override['image'], ImageSource.ImagePlugin) self.set_transparency(self.service_item.theme_data.background_type == BackgroundType.to_string(BackgroundType.Transparent)) image_bytes = None if self.service_item.theme_data.background_type == 'image': if self.service_item.theme_data.background_filename: self.service_item.bg_image_bytes = self.image_manager.get_image_bytes( self.service_item.theme_data.background_filename, ImageSource.Theme) if image_path: image_bytes = self.image_manager.get_image_bytes(image_path, ImageSource.ImagePlugin) created_html = build_html(self.service_item, self.screen, self.is_live, background, image_bytes, plugins=self.plugin_manager.plugins) self.web_view.setHtml(created_html) if service_item.foot_text: self.footer(service_item.foot_text) # if was hidden keep it hidden if self.hide_mode and self.is_live and not service_item.is_media(): if Settings().value('core/auto unblank'): Registry().execute('slidecontroller_live_unblank') else: self.hide_display(self.hide_mode) if self.service_item.theme_data.background_type == 'video' and self.is_live: if self.service_item.theme_data.background_filename: service_item = ServiceItem() service_item.title = 'webkit' service_item.processor = 'webkit' path = os.path.join(AppLocation.get_section_data_path('themes'), self.service_item.theme_data.theme_name) service_item.add_from_command(path, self.service_item.theme_data.background_filename, ':/media/slidecontroller_multimedia.png') self.media_controller.video(DisplayControllerType.Live, service_item, video_behind_text=True) self._hide_mouse()
def build_service_item(self, item=None, xml_version=False, remote=False, context=ServiceItemContext.Live): """ Common method for generating a service item """ service_item = ServiceItem(self.plugin) service_item.add_icon(self.plugin.icon_path) if self.generate_slide_data(service_item, item, xml_version, remote, context): return service_item else: return None
def test_add_from_command_for_a_presentation_thumb(self, mocked_get_section_data_path, mocked_image_manager): """ Test the Service Item - adding a presentation, updating the thumb path & adding the thumb to image_manager """ # GIVEN: A service item, a mocked AppLocation and presentation data mocked_get_section_data_path.return_value = os.path.join('mocked', 'section', 'path') service_item = ServiceItem(None) service_item.add_capability(ItemCapabilities.HasThumbnails) service_item.has_original_files = False service_item.name = 'presentations' presentation_name = 'test.pptx' thumb = os.path.join('tmp', 'test', 'thumb.png') display_title = 'DisplayTitle' notes = 'Note1\nNote2\n' expected_thumb_path = os.path.join('mocked', 'section', 'path', 'thumbnails', md5_hash(os.path.join(TEST_PATH, presentation_name).encode('utf-8')), 'thumb.png') frame = {'title': presentation_name, 'image': expected_thumb_path, 'path': TEST_PATH, 'display_title': display_title, 'notes': notes} # WHEN: adding presentation to service_item service_item.add_from_command(TEST_PATH, presentation_name, thumb, display_title, notes) # THEN: verify that it is setup as a Command and that the frame data matches self.assertEqual(service_item.service_item_type, ServiceItemType.Command, 'It should be a Command') self.assertEqual(service_item.get_frames()[0], frame, 'Frames should match') self.assertEqual(1, mocked_image_manager.add_image.call_count, 'image_manager should be used')
def service_item_basic_test(self): """ Test the Service Item - basic test """ # GIVEN: A new service item # WHEN: A service item is created (without a plugin) service_item = ServiceItem(None) # THEN: We should get back a valid service item self.assertTrue(service_item.is_valid, 'The new service item should be valid') self.assertTrue(service_item.missing_frames(), 'There should not be any frames in the service item')
def serviceitem_load_image_from_service_test(self): """ Test the Service Item - adding an image from a saved service """ # GIVEN: A new service item and a mocked add icon function service_item = ServiceItem(None) mocked_add_icon = MagicMock() service_item.add_icon = mocked_add_icon # WHEN: adding a custom from a saved Service # THEN: We should get back a valid service item assert service_item.is_valid is True, u'The new service item should be valid'
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 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 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 build_service_item(self, item=None, xml_version=False, remote=False, context=ServiceItemContext.Live): """ Common method for generating a service item :param item: Service Item to be built. :param xml_version: version of XML (False) :param remote: Remote triggered (False) :param context: The context on which this is called """ service_item = ServiceItem(self.plugin) service_item.add_icon(self.plugin.icon_path) if self.generate_slide_data(service_item, item, xml_version, remote, context): return service_item else: return None
def build_presentation_non_pdf_context_menu_test(self): """ Test the creation of a context menu from service item of type Command with Impress from Presentation. """ # GIVEN: A new service manager instance and a default service item. Registry().register('plugin_manager', MagicMock()) Registry().register('renderer', MagicMock()) service_manager = ServiceManager(None) item = MagicMock() item.parent.return_value = False item.data.return_value = 0 service_manager.service_manager_list = MagicMock() service_manager.service_manager_list.itemAt.return_value = item service_item = ServiceItem(None) service_item.add_capability(ItemCapabilities.ProvidesOwnDisplay) service_item.service_item_type = ServiceItemType.Command service_item.edit_id = 1 service_item._raw_frames.append(MagicMock()) service_manager.service_items.insert(1, {'service_item': service_item}) service_manager.edit_action = MagicMock() service_manager.rename_action = MagicMock() service_manager.create_custom_action = MagicMock() service_manager.maintain_action = MagicMock() service_manager.notes_action = MagicMock() service_manager.time_action = MagicMock() service_manager.auto_start_action = MagicMock() service_manager.auto_play_slides_menu = MagicMock() service_manager.auto_play_slides_once = MagicMock() service_manager.auto_play_slides_loop = MagicMock() service_manager.timed_slide_interval = MagicMock() service_manager.theme_menu = MagicMock() service_manager.menu = MagicMock() # WHEN I define a context menu service_manager.context_menu(1) # THEN the following calls should have occurred. self.assertEquals(service_manager.edit_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.rename_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.create_custom_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.maintain_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.notes_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.time_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.auto_start_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.auto_play_slides_menu.menuAction().setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.auto_play_slides_once.setChecked.call_count, 0, 'Should not be called') self.assertEquals(service_manager.auto_play_slides_loop.setChecked.call_count, 0, 'Should not be called') self.assertEquals(service_manager.timed_slide_interval.setChecked.call_count, 0, 'Should not be called') self.assertEquals(service_manager.theme_menu.menuAction().setVisible.call_count, 1, 'Should have be called once')
def _setup(self, screen_ratio): """ Set up the widget """ self.setColumnCount(1) self.horizontalHeader().setVisible(False) self.setColumnWidth(0, self.parent().width()) self.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) self.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection) self.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers) self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setAlternatingRowColors(True) # Initialize variables. self.service_item = ServiceItem() self.screen_ratio = screen_ratio
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_build_context_menu(self): """ Test the creation of a context menu from a null service item. """ # GIVEN: A new service manager instance and a default service item. service_manager = ServiceManager(None) item = MagicMock() item.parent.return_value = False item.data.return_value = 0 service_manager.service_manager_list = MagicMock() service_manager.service_manager_list.itemAt.return_value = item service_item = ServiceItem(None) service_manager.service_items.insert(1, {'service_item': service_item}) service_manager.edit_action = MagicMock() service_manager.rename_action = MagicMock() service_manager.create_custom_action = MagicMock() service_manager.maintain_action = MagicMock() service_manager.notes_action = MagicMock() service_manager.time_action = MagicMock() service_manager.auto_start_action = MagicMock() service_manager.auto_play_slides_menu = MagicMock() service_manager.auto_play_slides_once = MagicMock() service_manager.auto_play_slides_loop = MagicMock() service_manager.timed_slide_interval = MagicMock() service_manager.theme_menu = MagicMock() service_manager.menu = MagicMock() # WHEN I define a context menu service_manager.context_menu(1) # THEN the following calls should have occurred. self.assertEqual(service_manager.edit_action.setVisible.call_count, 1, 'Should have been called once') self.assertEqual(service_manager.rename_action.setVisible.call_count, 1, 'Should have been called once') self.assertEqual( service_manager.create_custom_action.setVisible.call_count, 1, 'Should have been called once') self.assertEqual(service_manager.maintain_action.setVisible.call_count, 1, 'Should have been called once') self.assertEqual(service_manager.notes_action.setVisible.call_count, 1, 'Should have been called once') self.assertEqual(service_manager.time_action.setVisible.call_count, 1, 'Should have been called once') self.assertEqual( service_manager.auto_start_action.setVisible.call_count, 1, 'Should have been called once') self.assertEqual( service_manager.auto_play_slides_menu.menuAction().setVisible. call_count, 1, 'Should have been called once') self.assertEqual( service_manager.auto_play_slides_once.setChecked.call_count, 0, 'Should not be called') self.assertEqual( service_manager.auto_play_slides_loop.setChecked.call_count, 0, 'Should not be called') self.assertEqual( service_manager.timed_slide_interval.setChecked.call_count, 0, 'Should not be called') self.assertEqual( service_manager.theme_menu.menuAction().setVisible.call_count, 1, 'Should have been called once')
def test_build_song_footer_base_songbook(self): """ Test build songs footer with basic song and multiple songbooks """ # GIVEN: A Song and a Service Item song = Song() song.title = 'My Song' song.copyright = 'My copyright' song.authors_songs = [] song.songbook_entries = [] song.ccli_number = '' book1 = MagicMock() book1.name = "My songbook" book2 = MagicMock() book2.name = "Thy songbook" song.songbookentries = [] song.add_songbook_entry(book1, '12') song.add_songbook_entry(book2, '502A') service_item = ServiceItem(None) # WHEN: I generate the Footer with default settings self.media_item.generate_footer(service_item, song) # THEN: The songbook should not be in the footer self.assertEqual(service_item.raw_footer, ['My Song', 'My copyright']) # WHEN: I activate the "display songbook" option self.media_item.display_songbook = True self.media_item.generate_footer(service_item, song) # THEN: The songbook should be in the footer self.assertEqual(service_item.raw_footer, ['My Song', 'My copyright', 'My songbook #12, Thy songbook #502A'])
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 build_song_footer_base_songbook_test(self): """ Test build songs footer with basic song and a songbook """ # GIVEN: A Song and a Service Item mock_song = MagicMock() mock_song.title = 'My Song' mock_song.copyright = 'My copyright' mock_song.book = MagicMock() mock_song.book.name = "My songbook" mock_song.song_number = 12 service_item = ServiceItem(None) # WHEN: I generate the Footer with default settings self.media_item.generate_footer(service_item, mock_song) # THEN: The songbook should not be in the footer self.assertEqual(service_item.raw_footer, ['My Song', 'My copyright']) # WHEN: I activate the "display songbook" option self.media_item.display_songbook = True self.media_item.generate_footer(service_item, mock_song) # THEN: The songbook should be in the footer self.assertEqual(service_item.raw_footer, ['My Song', 'My copyright', 'My songbook #12'])
def test_build_song_footer_base_ccli(self): """ Test build songs footer with basic song and a CCLI number """ # GIVEN: A Song and a Service Item and a configured CCLI license mock_song = MagicMock() mock_song.title = 'My Song' mock_song.copyright = 'My copyright' service_item = ServiceItem(None) Settings().setValue('core/ccli number', '1234') # WHEN: I generate the Footer with default settings self.media_item.generate_footer(service_item, mock_song) # THEN: I get the following Array returned assert service_item.raw_footer == ['My Song', 'My copyright', 'CCLI License: 1234'], \ 'The array should be returned correctly with a song, an author, copyright and ccli' # WHEN: I amend the CCLI value Settings().setValue('core/ccli number', '4321') self.media_item.generate_footer(service_item, mock_song) # THEN: I would get an amended footer string assert service_item.raw_footer == ['My Song', 'My copyright', 'CCLI License: 4321'], \ 'The array should be returned correctly with a song, an author, copyright and amended ccli'
def test_build_song_footer_one_author_hide_written_by( self, MockedSettings): """ Test build songs footer with basic song and one author """ # GIVEN: A Song and a Service Item, mocked settings: False for 'songs/display written by' # and False for 'core/ccli number' (ccli will cause traceback if true) mocked_settings = MagicMock() mocked_settings.value.side_effect = [False, False] MockedSettings.return_value = mocked_settings mock_song = MagicMock() mock_song.title = 'My Song' mock_song.authors_songs = [] mock_author = MagicMock() mock_author.display_name = 'my author' mock_author_song = MagicMock() mock_author_song.author = mock_author mock_song.authors_songs.append(mock_author_song) mock_song.copyright = 'My copyright' service_item = ServiceItem(None) # WHEN: I generate the Footer with default settings author_list = self.media_item.generate_footer(service_item, mock_song) # THEN: I get the following Array returned assert service_item.raw_footer == ['My Song', 'my author', 'My copyright'], \ 'The array should be returned correctly with a song, one author and copyright, ' \ 'text Written by should not be part of the text.' assert author_list == [ 'my author' ], 'The author list should be returned correctly with one author'
def add_from_comamnd_without_display_title_and_notes_test(self): """ Test the Service Item - add from command, but not presentation """ # GIVEN: A new service item, a mocked icon and image data service_item = ServiceItem(None) image_name = 'test.img' image = MagicMock() frame = {'title': image_name, 'image': image, 'path': TEST_PATH, 'display_title': None, 'notes': None} # WHEN: adding image to service_item service_item.add_from_command(TEST_PATH, image_name, image) # THEN: verify that it is setup as a Command and that the frame data matches self.assertEqual(service_item.service_item_type, ServiceItemType.Command, 'It should be a Command') self.assertEqual(service_item.get_frames()[0], frame, 'Frames should match')
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 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 add_from_command_for_a_presentation_test(self): """ Test the Service Item - adding a presentation """ # GIVEN: A service item, a mocked icon and presentation data service_item = ServiceItem(None) presentation_name = 'test.pptx' image = MagicMock() display_title = 'DisplayTitle' notes = 'Note1\nNote2\n' frame = {'title': presentation_name, 'image': image, 'path': TEST_PATH, 'display_title': display_title, 'notes': notes} # WHEN: adding presentation to service_item service_item.add_from_command(TEST_PATH, presentation_name, image, display_title, notes) # THEN: verify that it is setup as a Command and that the frame data matches self.assertEqual(service_item.service_item_type, ServiceItemType.Command, 'It should be a Command') self.assertEqual(service_item.get_frames()[0], frame, 'Frames should match')
def serviceitem_add_command_test(self): """ Test the Service Item - add command test """ # GIVEN: A new service item and a mocked renderer service_item = ServiceItem(None) service_item.name = u'test' # WHEN: adding image to a service item test_file = os.path.join(TESTPATH, u'church.jpg') service_item.add_from_command(TESTPATH, u'church.jpg', test_file) # THEN: We should get back a valid service item assert service_item.is_valid is True, u'The new service item should be valid' assert len(service_item._display_frames) == 0, u'The service item has no display frames ' # THEN: We should have a page of output. assert len(service_item._raw_frames) == 1, u'A valid rendered Service Item has one raw frame' assert service_item.get_rendered_frame(0) == test_file, u'The image matches the input' # WHEN requesting a saved service item service = service_item.get_service_repr(True) # THEN: We should have two parts of the service. assert len(service) == 2, u'A saved service has two parts' assert service[u'header'][u'name'] == u'test' , u'A test plugin' assert service[u'data'][0][u'title'] == u'church.jpg' , u'The first title name ' assert service[u'data'][0][u'path'] == TESTPATH , u'The first image name' assert service[u'data'][0][u'image'] == test_file , u'The first image name' # WHEN validating a service item service_item.validate_item([u'jpg']) # THEN the service item should be valid assert service_item.is_valid is True, u'The service item should be valid' # WHEN validating a service item with a different suffix service_item.validate_item([u'png']) # THEN the service item should not be valid assert service_item.is_valid is False, u'The service item is not valid'
def service_load_inactive_test(self): """ Test the service load in custom with a default service item """ # GIVEN: An empty Service Item service_item = ServiceItem(None) # WHEN: I search for the custom in the database item = self.media_item.service_load(service_item) # THEN: the processing should be ignored self.assertEqual(item, None, 'The Service item is inactive so processing should be bypassed')
def on_replace_click(self): """ Called to replace Live background with the media selected. """ if check_item_selected( self.list_view, translate( 'MediaPlugin.MediaItem', 'You must select a media file to replace the background with.' )): item = self.list_view.currentItem() filename = item.data(QtCore.Qt.UserRole) if os.path.exists(filename): service_item = ServiceItem() service_item.title = 'webkit' service_item.processor = 'webkit' (path, name) = os.path.split(filename) service_item.add_from_command(path, name, CLAPPERBOARD) if self.media_controller.video(DisplayControllerType.Live, service_item, video_behind_text=True): self.reset_action.setVisible(True) else: critical_error_message_box( UiStrings().LiveBGError, translate('MediaPlugin.MediaItem', 'There was no display item to amend.')) else: critical_error_message_box( UiStrings().LiveBGError, translate( 'MediaPlugin.MediaItem', 'There was a problem replacing your background, ' 'the media file "%s" no longer exists.') % filename)
def service_load_basic_custom_true_test(self): """ Test the service load in custom with a default service item and a requirement to add to the database """ # GIVEN: An empty Service Item and an active plugin service_item = ServiceItem(None) service_item.raw_footer = FOOTER self.media_item.plugin = MagicMock() self.media_item.plugin.status = PluginStatus.Active self.media_item.plugin.db_manager = MagicMock() self.media_item.plugin.db_manager.get_object_filtered = MagicMock() self.media_item.plugin.db_manager.get_object_filtered.return_value = None with patch('openlp.plugins.custom.lib.mediaitem.CustomSlide'): # WHEN: I search for the custom in the database self.media_item.add_custom_from_service = True self.media_item.create_from_service_item = MagicMock() self.media_item.service_load(service_item) # THEN: the item should not be added to the database. self.assertEqual(self.media_item.create_from_service_item.call_count, 1, 'The item should have been added to the database')
def test_build_song_footer_copyright_disabled(self): """ Test building song footer without displaying the copyright symbol """ # GIVEN: A Song and a Service Item; displaying the copyright symbol should be disabled by default mock_song = MagicMock() mock_song.title = 'My Song' mock_song.copyright = 'My copyright' service_item = ServiceItem(None) # WHEN: I generate the Footer with default settings self.media_item.generate_footer(service_item, mock_song) # THEN: The copyright symbol should not be in the footer assert service_item.raw_footer == ['My Song', 'My copyright']
def _setup(self, screen_ratio): """ Set up the widget """ self.setColumnCount(1) self.horizontalHeader().setVisible(False) self.setColumnWidth(0, self.parent().width()) self.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) self.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) self.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers) self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setAlternatingRowColors(True) # Initialize variables. self.service_item = ServiceItem() self.screen_ratio = screen_ratio
def test_controller_text(self): """ Remote API Tests : test the controller text method can be called with a real service item """ # GIVEN: A mocked service with a dummy service item line = convert_file_service_item(TEST_PATH, 'serviceitem_custom_1.osj') self.mocked_live_controller.service_item = ServiceItem(None) self.mocked_live_controller.service_item.set_from_service(line) self.mocked_live_controller.service_item.render(True) # WHEN: I trigger the method ret = controller_text("SomeText") # THEN: I get a basic set of results results = ret['results'] assert isinstance(ret, dict) assert len(results['slides']) == 2
def test_build_song_footer_copyright_enabled(self): """ Test building song footer with displaying the copyright symbol """ # GIVEN: A Song and a Service Item; displaying the copyright symbol is enabled self.media_item.display_copyright_symbol = True mock_song = MagicMock() mock_song.title = 'My Song' mock_song.copyright = 'My copyright' service_item = ServiceItem(None) # WHEN: I generate the Footer with default settings self.media_item.generate_footer(service_item, mock_song) # THEN: The copyright symbol should be in the footer self.assertEqual(service_item.raw_footer, ['My Song', '© My copyright'])
def test_format_slide_blank_after_split(self): """ Test that a line with blanks before the logical split at handled """ # GIVEN: A line of with a space after the logical split renderer = Renderer() renderer.empty_height = 480 given_line = '\n[---] \n' expected_words = ['<br>[---] '] service_item = ServiceItem(None) # WHEN: Split the line based on word split rules result_words = renderer.format_slide(given_line, service_item) # THEN: The blanks have been removed. self.assertListEqual(result_words, expected_words)
def test_format_slide_logical_split(self): """ Test that a line with text and a logic break does not break the renderer just returns the input """ # GIVEN: A line of with a space text and the logical split renderer = Renderer() renderer.empty_height = 480 given_line = 'a\n[---]\nb' expected_words = ['a<br>[---]<br>b'] service_item = ServiceItem(None) # WHEN: Split the line based on word split rules result_words = renderer.format_slide(given_line, service_item) # THEN: The word lists should be the same. self.assertListEqual(result_words, expected_words)
def __init__(self, parent, screen_ratio): """ Initializes the widget to default state. An empty ServiceItem is used per default. One needs to call replace_service_manager_item() to make this widget display something. """ super(QtGui.QTableWidget, self).__init__(parent) # Set up the widget. self.setColumnCount(1) self.horizontalHeader().setVisible(False) self.setColumnWidth(0, parent.width()) self.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) self.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) self.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers) self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setAlternatingRowColors(True) # Initialize variables. self.service_item = ServiceItem() self.screen_ratio = screen_ratio
def serviceitem_add_text_test(self): """ Test the Service Item - add text test """ # GIVEN: A new service item service_item = ServiceItem(None) # WHEN: adding text to a service item service_item.add_from_text(VERSE) service_item.raw_footer = FOOTER # THEN: We should get back a valid service item assert service_item.is_valid is True, u'The new service item should be valid' assert service_item.missing_frames() is False, u'check frames loaded ' # WHEN: Render called assert len(service_item._display_frames) == 0, u'A blank Service Item with no display frames' service_item.render(True) # THEN: We should have a page of output. assert len(service_item._display_frames) == 1, u'A valid rendered Service Item has 1 display frame' assert service_item.get_rendered_frame(0) == VERSE.split(u'\n')[0], u'A output has rendered correctly.'
def test_build_presentation_non_pdf_context_menu(self): """ Test the creation of a context menu from service item of type Command with Impress from Presentation. """ # GIVEN: A new service manager instance and a default service item. Registry().register('plugin_manager', MagicMock()) Registry().register('renderer', MagicMock()) service_manager = ServiceManager(None) item = MagicMock() item.parent.return_value = False item.data.return_value = 0 service_manager.service_manager_list = MagicMock() service_manager.service_manager_list.itemAt.return_value = item service_item = ServiceItem(None) service_item.add_capability(ItemCapabilities.ProvidesOwnDisplay) service_item.service_item_type = ServiceItemType.Command service_item.edit_id = 1 service_item._raw_frames.append(MagicMock()) service_manager.service_items.insert(1, {'service_item': service_item}) service_manager.edit_action = MagicMock() service_manager.rename_action = MagicMock() service_manager.create_custom_action = MagicMock() service_manager.maintain_action = MagicMock() service_manager.notes_action = MagicMock() service_manager.time_action = MagicMock() service_manager.auto_start_action = MagicMock() service_manager.auto_play_slides_menu = MagicMock() service_manager.auto_play_slides_once = MagicMock() service_manager.auto_play_slides_loop = MagicMock() service_manager.timed_slide_interval = MagicMock() service_manager.theme_menu = MagicMock() service_manager.menu = MagicMock() # WHEN I define a context menu service_manager.context_menu(1) # THEN the following calls should have occurred. assert service_manager.edit_action.setVisible.call_count == 1, 'Should have be called once' assert service_manager.rename_action.setVisible.call_count == 1, 'Should have be called once' assert service_manager.create_custom_action.setVisible.call_count == 1, 'Should have be called once' assert service_manager.maintain_action.setVisible.call_count == 1, 'Should have be called once' assert service_manager.notes_action.setVisible.call_count == 1, 'Should have be called once' assert service_manager.time_action.setVisible.call_count == 1, 'Should have be called once' assert service_manager.auto_start_action.setVisible.call_count == 1, 'Should have be called once' assert service_manager.auto_play_slides_menu.menuAction().setVisible.call_count == 1, \ 'Should have be called once' assert service_manager.auto_play_slides_once.setChecked.call_count == 0, 'Should not be called' assert service_manager.auto_play_slides_loop.setChecked.call_count == 0, 'Should not be called' assert service_manager.timed_slide_interval.setChecked.call_count == 0, 'Should not be called' assert service_manager.theme_menu.menuAction().setVisible.call_count == 1, \ 'Should have be called once'
def serviceitem_load_custom_from_service_test(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) mocked_add_icon = MagicMock() service_item.add_icon = mocked_add_icon # WHEN: adding a custom from a saved Service line = self.convert_file_service_item(u'serviceitem_custom1.osd') service_item.set_from_service(line) # THEN: We should get back a valid service item assert service_item.is_valid is True, u'The new service item should be valid' assert len(service_item._display_frames) == 0, u'The service item has no display frames' assert len(service_item.capabilities) == 5, u'There are 5 default custom item capabilities' service_item.render(True) assert (service_item.get_display_title()) == u'Test Custom', u'The custom title should be correct'
def test_build_song_footer_two_authors(self): """ Test build songs footer with basic song and two authors """ # GIVEN: A Song and a Service Item mock_song = MagicMock() mock_song.title = 'My Song' mock_song.authors_songs = [] mock_author = MagicMock() mock_author.display_name = 'my author' mock_author_song = MagicMock() mock_author_song.author = mock_author mock_author_song.author_type = AuthorType.Music mock_song.authors_songs.append(mock_author_song) mock_author = MagicMock() mock_author.display_name = 'another author' mock_author_song = MagicMock() mock_author_song.author = mock_author mock_author_song.author_type = AuthorType.Words mock_song.authors_songs.append(mock_author_song) mock_author = MagicMock() mock_author.display_name = 'translator' mock_author_song = MagicMock() mock_author_song.author = mock_author mock_author_song.author_type = AuthorType.Translation mock_song.authors_songs.append(mock_author_song) mock_song.copyright = 'My copyright' service_item = ServiceItem(None) # WHEN: I generate the Footer with default settings author_list = self.media_item.generate_footer(service_item, mock_song) # THEN: I get the following Array returned assert service_item.raw_footer == ['My Song', 'Words: another author', 'Music: my author', 'Translation: translator', 'My copyright'], \ 'The array should be returned correctly with a song, two authors and copyright' assert author_list == ['another author', 'my author', 'translator'], \ 'The author list should be returned correctly with two authors'
def test_default_context_menu(self, mocked_exec, mocked_mapToGlobal, mocked_item_at_method): """ Test the context_menu() method with a default service item """ # GIVEN: A service item added mocked_item = MagicMock() mocked_item.parent.return_value = None mocked_item_at_method.return_value = mocked_item mocked_item.data.return_value = 1 self.service_manager.setup_ui(self.service_manager) # A service item without capabilities. service_item = ServiceItem() 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_build_song_footer_one_author(self): """ Test build songs footer with basic song and one author """ # GIVEN: A Song and a Service Item mock_song = MagicMock() mock_song.title = 'My Song' mock_song.authors_songs = [] mock_author = MagicMock() mock_author.display_name = 'my author' mock_author_song = MagicMock() mock_author_song.author = mock_author mock_song.authors_songs.append(mock_author_song) mock_song.copyright = 'My copyright' service_item = ServiceItem(None) # WHEN: I generate the Footer with default settings author_list = self.media_item.generate_footer(service_item, mock_song) # THEN: I get the following Array returned self.assertEqual(service_item.raw_footer, ['My Song', 'Written by: my author', 'My copyright'], 'The array should be returned correctly with a song, one author and copyright') self.assertEqual(author_list, ['my author'], 'The author list should be returned correctly with one author')
def service_item_load_optical_media_from_service_test(self): """ Test the Service Item - load an optical media item """ # GIVEN: A new service item and a mocked add icon function service_item = ServiceItem(None) service_item.add_icon = MagicMock() # WHEN: We load a serviceitem with optical media line = convert_file_service_item(TEST_PATH, 'serviceitem-dvd.osj') with patch('openlp.core.ui.servicemanager.os.path.exists') as mocked_exists: mocked_exists.return_value = True service_item.set_from_service(line) # THEN: We should get back a valid service item with optical media info self.assertTrue(service_item.is_valid, 'The service item should be valid') self.assertTrue(service_item.is_capable(ItemCapabilities.IsOptical), 'The item should be Optical') self.assertEqual(service_item.start_time, 654.375, 'Start time should be 654.375') self.assertEqual(service_item.end_time, 672.069, 'End time should be 672.069') self.assertEqual(service_item.media_length, 17.694, 'Media length should be 17.694')
def generate_preview(self, theme_data, force_page=False): """ Generate a preview of a theme. :param theme_data: The theme to generated a preview for. :param force_page: Flag to tell message lines per page need to be generated. """ # save value for use in format_slide self.force_page = force_page # build a service item to generate preview service_item = ServiceItem() if self.force_page: # make big page for theme edit dialog to get line count service_item.add_from_text(VERSE_FOR_LINE_COUNT) else: service_item.add_from_text(VERSE) service_item.raw_footer = FOOTER # if No file do not update cache if theme_data.background_filename: self.image_manager.add_image( theme_data.background_filename, ImageSource.Theme, QtGui.QColor(theme_data.background_border_color)) theme_data, main, footer = self.pre_render(theme_data) service_item.theme_data = theme_data service_item.main = main service_item.footer = footer service_item.render(True) if not self.force_page: self.display.build_html(service_item) raw_html = service_item.get_rendered_frame(0) self.display.text(raw_html, False) preview = self.display.preview() return preview self.force_page = False
def test_service_item_load_song_and_audio_from_service(self): """ Test the Service Item - adding a song 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 service line = convert_file_service_item(TEST_PATH, 'serviceitem-song-linked-audio.osj') service_item.set_from_service(line, '/test/') # THEN: We should get back a valid service item assert service_item.is_valid is True, 'The new service item should be valid' assert 0 == len(service_item._display_frames ), 'The service item should have no display frames' assert 7 == len(service_item.capabilities ), 'There should be 7 default custom item capabilities' # WHEN: We render the frames of the service item service_item.render(True) # THEN: The frames should also be valid assert 'Amazing Grace' == service_item.get_display_title( ), 'The title should be "Amazing Grace"' assert CLEANED_VERSE[:-1] == service_item.get_frames()[0]['text'], \ 'The returned text matches the input, except the last line feed' assert RENDERED_VERSE.split('\n', 1)[0] == service_item.get_rendered_frame(1), \ 'The first line has been returned' assert 'Amazing Grace! how sweet the s' == service_item.get_frame_title(0), \ '"Amazing Grace! how sweet the s" has been returned as the title' assert '’Twas grace that taught my hea' == service_item.get_frame_title(1), \ '"’Twas grace that taught my hea" has been returned as the title' assert Path('/test/amazing_grace.mp3') == service_item.background_audio[0], \ '"/test/amazing_grace.mp3" should be in the background_audio list'
def build_media_context_menu_test(self): """ Test the creation of a context menu from service item of type Command from Media. """ # GIVEN: A new service manager instance and a default service item. Registry().register('plugin_manager', MagicMock()) Registry().register('renderer', MagicMock()) service_manager = ServiceManager(None) item = MagicMock() item.parent.return_value = False item.data.return_value = 0 service_manager.service_manager_list = MagicMock() service_manager.service_manager_list.itemAt.return_value = item service_item = ServiceItem(None) service_item.add_capability(ItemCapabilities.CanAutoStartForLive) service_item.add_capability(ItemCapabilities.CanEditTitle) service_item.add_capability(ItemCapabilities.RequiresMedia) service_item.service_item_type = ServiceItemType.Command service_item.edit_id = 1 service_item._raw_frames.append(MagicMock()) service_manager.service_items.insert(1, {'service_item': service_item}) service_manager.edit_action = MagicMock() service_manager.rename_action = MagicMock() service_manager.create_custom_action = MagicMock() service_manager.maintain_action = MagicMock() service_manager.notes_action = MagicMock() service_manager.time_action = MagicMock() service_manager.auto_start_action = MagicMock() service_manager.auto_play_slides_menu = MagicMock() service_manager.auto_play_slides_once = MagicMock() service_manager.auto_play_slides_loop = MagicMock() service_manager.timed_slide_interval = MagicMock() service_manager.theme_menu = MagicMock() service_manager.menu = MagicMock() # WHEN I define a context menu service_manager.context_menu(1) # THEN the following calls should have occurred. self.assertEquals(service_manager.edit_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.rename_action.setVisible.call_count, 2, 'Should have be called twice') self.assertEquals(service_manager.create_custom_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.maintain_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.notes_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.time_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.auto_start_action.setVisible.call_count, 2, 'Should have be called twice') self.assertEquals(service_manager.auto_play_slides_menu.menuAction().setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.auto_play_slides_once.setChecked.call_count, 0, 'Should not be called') self.assertEquals(service_manager.auto_play_slides_loop.setChecked.call_count, 0, 'Should not be called') self.assertEquals(service_manager.timed_slide_interval.setChecked.call_count, 0, 'Should not be called') self.assertEquals(service_manager.theme_menu.menuAction().setVisible.call_count, 1, 'Should have be called once') # THEN I change the length of the media and regenerate the menu. service_item.set_media_length(5) service_manager.context_menu(1) # THEN the following additional calls should have occurred. self.assertEquals(service_manager.time_action.setVisible.call_count, 3, 'Should have be called three times')
def service_item_load_custom_from_service_test(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() # WHEN: We add a custom from a saved service line = convert_file_service_item(TEST_PATH, 'serviceitem_custom_1.osj') service_item.set_from_service(line) # THEN: We should get back a valid service item self.assertTrue(service_item.is_valid, 'The new service item should be valid') assert_length(0, service_item._display_frames, 'The service item should have no display frames') assert_length(5, service_item.capabilities, 'There should be 5 default custom item capabilities') # WHEN: We render the frames of the service item service_item.render(True) # THEN: The frames should also be valid self.assertEqual('Test Custom', service_item.get_display_title(), 'The title should be "Test Custom"') self.assertEqual(VERSE[:-1], service_item.get_frames()[0]['text'], 'The returned text matches the input, except the last line feed') self.assertEqual(VERSE.split('\n', 1)[0], service_item.get_rendered_frame(1), 'The first line has been returned') self.assertEqual('Slide 1', service_item.get_frame_title(0), '"Slide 1" has been returned as the title') self.assertEqual('Slide 2', service_item.get_frame_title(1), '"Slide 2" has been returned as the title') self.assertEqual('', service_item.get_frame_title(2), 'Blank has been returned as the title of slide 3')
def service_item_load_image_from_service_test(self): """ Test the Service Item - adding an image from a saved service """ # GIVEN: A new service item and a mocked add icon function image_name = 'image_1.jpg' test_file = os.path.join(TEST_PATH, image_name) frame_array = {'path': test_file, 'title': image_name} service_item = ServiceItem(None) service_item.add_icon = MagicMock() # WHEN: adding an image from a saved Service and mocked exists line = convert_file_service_item(TEST_PATH, 'serviceitem_image_1.osj') with patch('openlp.core.ui.servicemanager.os.path.exists') as mocked_exists,\ patch('openlp.core.lib.serviceitem.create_thumb') as mocked_create_thumb,\ patch('openlp.core.lib.serviceitem.AppLocation.get_section_data_path') as \ mocked_get_section_data_path: mocked_exists.return_value = True mocked_get_section_data_path.return_value = os.path.normpath('/path/') service_item.set_from_service(line, TEST_PATH) # THEN: We should get back a valid service item self.assertTrue(service_item.is_valid, 'The new service item should be valid') self.assertEqual(os.path.normpath(test_file), os.path.normpath(service_item.get_rendered_frame(0)), 'The first frame should match the path to the image') self.assertEqual(frame_array, service_item.get_frames()[0], 'The return should match frame array1') self.assertEqual(test_file, service_item.get_frame_path(0), 'The frame path should match the full path to the image') self.assertEqual(image_name, service_item.get_frame_title(0), 'The frame title should match the image name') self.assertEqual(image_name, service_item.get_display_title(), 'The display title should match the first image name') self.assertTrue(service_item.is_image(), 'This service item should be of an "image" type') self.assertTrue(service_item.is_capable(ItemCapabilities.CanMaintain), 'This service item should be able to be Maintained') self.assertTrue(service_item.is_capable(ItemCapabilities.CanPreview), 'This service item should be able to be be Previewed') self.assertTrue(service_item.is_capable(ItemCapabilities.CanLoop), 'This service item should be able to be run in a can be made to Loop') self.assertTrue(service_item.is_capable(ItemCapabilities.CanAppend), 'This service item should be able to have new items added to it')
class ListPreviewWidget(QtGui.QTableWidget): def __init__(self, parent, screen_ratio): """ Initializes the widget to default state. An empty ServiceItem is used per default. One needs to call replace_service_manager_item() to make this widget display something. """ super(QtGui.QTableWidget, self).__init__(parent) # Set up the widget. self.setColumnCount(1) self.horizontalHeader().setVisible(False) self.setColumnWidth(0, parent.width()) self.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) self.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) self.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers) self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setAlternatingRowColors(True) # Initialize variables. self.service_item = ServiceItem() self.screen_ratio = screen_ratio def resizeEvent(self, QResizeEvent): """ Overloaded method from QTableWidget. Will recalculate the layout. """ self.__recalculate_layout() def __recalculate_layout(self): """ Recalculates the layout of the table widget. It will set height and width of the table cells. QTableWidget does not adapt the cells to the widget size on its own. """ self.setColumnWidth(0, self.viewport().width()) if self.service_item: # Sort out songs, bibles, etc. if self.service_item.is_text(): self.resizeRowsToContents() else: # Sort out image heights. for framenumber in range(len(self.service_item.get_frames())): height = self.viewport().width() // self.screen_ratio self.setRowHeight(framenumber, height) def screen_size_changed(self, screen_ratio): """ To be called whenever the live screen size changes. Because this makes a layout recalculation necessary. """ self.screen_ratio = screen_ratio self.__recalculate_layout() def replace_service_item(self, service_item, width, slideNumber): """ Replaces the current preview items with the ones in service_item. Displays the given slide. """ self.service_item = service_item self.clear() self.setRowCount(0) self.setColumnWidth(0, width) row = 0 text = [] for framenumber, frame in enumerate(self.service_item.get_frames()): self.setRowCount(self.slide_count() + 1) item = QtGui.QTableWidgetItem() slide_height = 0 if self.service_item.is_text(): if frame['verseTag']: # These tags are already translated. verse_def = frame['verseTag'] verse_def = '%s%s' % (verse_def[0], verse_def[1:]) two_line_def = '%s\n%s' % (verse_def[0], verse_def[1:]) row = two_line_def else: row += 1 item.setText(frame['text']) else: label = QtGui.QLabel() label.setMargin(4) if self.service_item.is_media(): label.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter) else: label.setScaledContents(True) if self.service_item.is_command(): label.setPixmap(QtGui.QPixmap(frame['image'])) else: image = self.image_manager.get_image(frame['path'], ImageSource.ImagePlugin) label.setPixmap(QtGui.QPixmap.fromImage(image)) self.setCellWidget(framenumber, 0, label) slide_height = width // self.screen_ratio row += 1 text.append(str(row)) self.setItem(framenumber, 0, item) if slide_height: self.setRowHeight(framenumber, slide_height) self.setVerticalHeaderLabels(text) if self.service_item.is_text(): self.resizeRowsToContents() self.setColumnWidth(0, self.viewport().width()) self.setFocus() self.change_slide(slideNumber) def change_slide(self, slide): """ Switches to the given row. """ if slide >= self.slide_count(): slide = self.slide_count() - 1 # Scroll to next item if possible. if slide + 1 < self.slide_count(): self.scrollToItem(self.item(slide + 1, 0)) self.selectRow(slide) def current_slide_number(self): """ Returns the position of the currently active item. Will return -1 if the widget is empty. """ return super(ListPreviewWidget, self).currentRow() def slide_count(self): """ Returns the number of slides this widget holds. """ return super(ListPreviewWidget, self).rowCount() def _get_image_manager(self): """ Adds the image manager to the class dynamically. """ if not hasattr(self, '_image_manager'): self._image_manager = Registry().get('image_manager') return self._image_manager image_manager = property(_get_image_manager)
def service_item_load_image_from_local_service_test(self): """ Test the Service Item - adding an image from a saved local service """ # GIVEN: A new service item and a mocked add icon function image_name1 = 'image_1.jpg' image_name2 = 'image_2.jpg' test_file1 = os.path.normpath(os.path.join('/home/openlp', image_name1)) test_file2 = os.path.normpath(os.path.join('/home/openlp', image_name2)) frame_array1 = {'path': test_file1, 'title': image_name1} frame_array2 = {'path': test_file2, 'title': image_name2} service_item = ServiceItem(None) service_item.add_icon = MagicMock() service_item2 = ServiceItem(None) service_item2.add_icon = MagicMock() # WHEN: adding an image from a saved Service and mocked exists line = convert_file_service_item(TEST_PATH, 'serviceitem_image_2.osj') line2 = convert_file_service_item(TEST_PATH, 'serviceitem_image_2.osj', 1) with patch('openlp.core.ui.servicemanager.os.path.exists') as mocked_exists, \ patch('openlp.core.lib.serviceitem.create_thumb') as mocked_create_thumb, \ patch('openlp.core.lib.serviceitem.AppLocation.get_section_data_path') as \ mocked_get_section_data_path: mocked_exists.return_value = True mocked_get_section_data_path.return_value = os.path.normpath('/path/') service_item2.set_from_service(line2) service_item.set_from_service(line) # THEN: We should get back a valid service item # This test is copied from service_item.py, but is changed since to conform to # new layout of service item. The layout use in serviceitem_image_2.osd is actually invalid now. self.assertTrue(service_item.is_valid, 'The first service item should be valid') self.assertTrue(service_item2.is_valid, 'The second service item should be valid') # These test will fail on windows due to the difference in folder seperators if os.name != 'nt': self.assertEqual(test_file1, service_item.get_rendered_frame(0), 'The first frame should match the path to the image') self.assertEqual(test_file2, service_item2.get_rendered_frame(0), 'The Second frame should match the path to the image') self.assertEqual(frame_array1, service_item.get_frames()[0], 'The return should match the frame array1') self.assertEqual(frame_array2, service_item2.get_frames()[0], 'The return should match the frame array2') self.assertEqual(test_file1, service_item.get_frame_path(0), 'The frame path should match the full path to the image') self.assertEqual(test_file2, service_item2.get_frame_path(0), 'The frame path should match the full path to the image') self.assertEqual(image_name1, service_item.get_frame_title(0), 'The 1st frame title should match the image name') self.assertEqual(image_name2, service_item2.get_frame_title(0), 'The 2nd frame title should match the image name') self.assertEqual(service_item.name, service_item.title.lower(), 'The plugin name should match the display title, as there are > 1 Images') self.assertTrue(service_item.is_image(), 'This service item should be of an "image" type') self.assertTrue(service_item.is_capable(ItemCapabilities.CanMaintain), 'This service item should be able to be Maintained') self.assertTrue(service_item.is_capable(ItemCapabilities.CanPreview), 'This service item should be able to be be Previewed') self.assertTrue(service_item.is_capable(ItemCapabilities.CanLoop), 'This service item should be able to be run in a can be made to Loop') self.assertTrue(service_item.is_capable(ItemCapabilities.CanAppend), 'This service item should be able to have new items added to it')
def service_item_load_song_and_audio_from_service_test(self): """ Test the Service Item - adding a song 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() # WHEN: We add a custom from a saved service line = convert_file_service_item(TEST_PATH, 'serviceitem-song-linked-audio.osj') service_item.set_from_service(line, '/test/') # THEN: We should get back a valid service item self.assertTrue(service_item.is_valid, 'The new service item should be valid') assert_length(0, service_item._display_frames, 'The service item should have no display frames') assert_length(7, service_item.capabilities, 'There should be 7 default custom item capabilities') # WHEN: We render the frames of the service item service_item.render(True) # THEN: The frames should also be valid self.assertEqual('Amazing Grace', service_item.get_display_title(), 'The title should be "Amazing Grace"') self.assertEqual(VERSE[:-1], service_item.get_frames()[0]['text'], 'The returned text matches the input, except the last line feed') self.assertEqual(VERSE.split('\n', 1)[0], service_item.get_rendered_frame(1), 'The first line has been returned') self.assertEqual('Amazing Grace! how sweet the s', service_item.get_frame_title(0), '"Amazing Grace! how sweet the s" has been returned as the title') self.assertEqual('’Twas grace that taught my hea', service_item.get_frame_title(1), '"’Twas grace that taught my hea" has been returned as the title') self.assertEqual('/test/amazing_grace.mp3', service_item.background_audio[0], '"/test/amazing_grace.mp3" should be in the background_audio list')
def build_custom_context_menu_test(self): """ Test the creation of a context menu from service item of type text from Custom. """ # GIVEN: A new service manager instance and a default service item. mocked_renderer = MagicMock() mocked_renderer.theme_level = ThemeLevel.Song Registry().register('plugin_manager', MagicMock()) Registry().register('renderer', mocked_renderer) service_manager = ServiceManager(None) item = MagicMock() item.parent.return_value = False item.data.return_value = 0 service_manager.service_manager_list = MagicMock() service_manager.service_manager_list.itemAt.return_value = item service_item = ServiceItem(None) service_item.add_capability(ItemCapabilities.CanEdit) service_item.add_capability(ItemCapabilities.CanPreview) service_item.add_capability(ItemCapabilities.CanLoop) service_item.add_capability(ItemCapabilities.CanSoftBreak) service_item.add_capability(ItemCapabilities.OnLoadUpdate) service_item.service_item_type = ServiceItemType.Text service_item.edit_id = 1 service_item._display_frames.append(MagicMock()) service_manager.service_items.insert(1, {'service_item': service_item}) service_manager.edit_action = MagicMock() service_manager.rename_action = MagicMock() service_manager.create_custom_action = MagicMock() service_manager.maintain_action = MagicMock() service_manager.notes_action = MagicMock() service_manager.time_action = MagicMock() service_manager.auto_start_action = MagicMock() service_manager.auto_play_slides_menu = MagicMock() service_manager.auto_play_slides_once = MagicMock() service_manager.auto_play_slides_loop = MagicMock() service_manager.timed_slide_interval = MagicMock() service_manager.theme_menu = MagicMock() service_manager.menu = MagicMock() # WHEN I define a context menu service_manager.context_menu(1) # THEN the following calls should have occurred. self.assertEquals(service_manager.edit_action.setVisible.call_count, 2, 'Should have be called twice') self.assertEquals(service_manager.rename_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.create_custom_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.maintain_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.notes_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.time_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.auto_start_action.setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.auto_play_slides_menu.menuAction().setVisible.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.auto_play_slides_once.setChecked.call_count, 0, 'Should not be called') self.assertEquals(service_manager.auto_play_slides_loop.setChecked.call_count, 0, 'Should not be called') self.assertEquals(service_manager.timed_slide_interval.setChecked.call_count, 0, 'Should not be called') self.assertEquals(service_manager.theme_menu.menuAction().setVisible.call_count, 2, 'Should have be called twice') # THEN we add a 2nd display frame service_item._display_frames.append(MagicMock()) service_manager.context_menu(1) # THEN the following additional calls should have occurred. self.assertEquals(service_manager.auto_play_slides_menu.menuAction().setVisible.call_count, 2, 'Should have be called twice') self.assertEquals(service_manager.auto_play_slides_once.setChecked.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.auto_play_slides_loop.setChecked.call_count, 1, 'Should have be called once') self.assertEquals(service_manager.timed_slide_interval.setChecked.call_count, 1, 'Should have be called once')