Best Python code snippet using avocado_python
test_servicemanager.py
Source:test_servicemanager.py
...32from openlp.core.lib.serviceitem import ItemCapabilities, ServiceItem33from openlp.core.ui.servicemanager import ServiceManager, ServiceManagerList34from openlp.core.widgets.toolbar import OpenLPToolbar35from tests.helpers.testmixin import TestMixin36def test_initial_service_manager(settings):37 """38 Test the initial of service manager.39 """40 # GIVEN: A new service manager instance.41 ServiceManager(None)42 # WHEN: the default service manager is built.43 # THEN: The the controller should be registered in the registry.44 assert Registry().get('service_manager') is not None, 'The base service manager should be registered'45def test_new_file(registry):46 """Test the new_file() method"""47 # GIVEN: A service manager48 mocked_settings = MagicMock()49 mocked_plugin_manager = MagicMock()50 mocked_main_window = MagicMock()51 mocked_slide_controller = MagicMock()52 registry.register('settings', mocked_settings)53 registry.register('plugin_manager', mocked_plugin_manager)54 registry.register('main_window', mocked_main_window)55 registry.register('live_controller', mocked_slide_controller)56 service_manager = ServiceManager(None)57 service_manager.service_items = [MagicMock(), MagicMock()]58 service_manager.service_id = 559 service_manager._service_path = 'service.osz'60 service_manager._modified = True61 service_manager.service_manager_list = MagicMock()62 service_manager.plugin_manager.new_service_created = MagicMock()63 # WHEN: new_file() is called64 service_manager.new_file()65 # THEN: Everything should be correct66 service_manager.service_manager_list.clear.assert_called_once_with()67 assert service_manager.service_items == [], 'The list of service items should be blank'68 assert service_manager._service_path is None, 'The file_name should be None'69 assert service_manager.service_id == 7, 'The service id should be 6'70 mocked_settings.setValue.assert_called_with('servicemanager/last file', None)71 mocked_plugin_manager.new_service_created.assert_called_once_with()72 assert mocked_slide_controller.slide_count == 0, 'Slide count should be zero'73def test_create_basic_service(settings):74 """75 Test the create basic service array76 """77 # GIVEN: A new service manager instance.78 service_manager = ServiceManager(None)79 # WHEN: when the basic service array is created.80 service_manager._save_lite = False81 service_manager.service_theme = 'test_theme'82 service = service_manager.create_basic_service()[0]83 # THEN: The controller should be registered in the registry.84 assert service is not None, 'The base service should be created'85 assert service['openlp_core']['service-theme'] == 'test_theme', 'The test theme should be saved'86 assert service['openlp_core']['lite-service'] is False, 'The lite service should be saved'87def test_is_modified(settings):88 """89 Test the is_modified() method90 """91 # GIVEN: A service manager with the self._modified set92 service_manager = ServiceManager(None)93 service_manager._modified = True94 # WHEN: is_modified is called95 result = service_manager.is_modified()96 # THEN: The result should be True97 assert result is True, 'is_modified should return True'98def test_supported_suffixes(settings):99 """100 Test the create basic service array101 """102 # GIVEN: A new service manager instance.103 service_manager = ServiceManager(None)104 # WHEN: a suffix is added as an individual or a list.105 service_manager.supported_suffixes('txt')106 service_manager.supported_suffixes(['pptx', 'ppt'])107 # THEN: The suffixes should be available to test.108 assert 'txt' in service_manager.suffixes, 'The suffix txt should be in the list'109 assert 'ppt' in service_manager.suffixes, 'The suffix ppt should be in the list'110 assert 'pptx' in service_manager.suffixes, 'The suffix pptx should be in the list'111def test_reset_supported_suffixes(settings):112 """113 Test the create basic service array114 """115 # GIVEN: A new service manager instance with some supported suffixes116 service_manager = ServiceManager(None)117 service_manager.suffixes = ['mp3', 'aac', 'wav', 'flac']118 # WHEN: reset_supported_suffixes() is called119 service_manager.reset_supported_suffixes()120 # THEN: The suffixes should be cleared out121 assert service_manager.suffixes == [], 'There should not be any suffixes'122def test_build_context_menu(settings):123 """124 Test the creation of a context menu from a null service item.125 """126 # GIVEN: A new service manager instance and a default service item.127 service_manager = ServiceManager(None)128 item = MagicMock()129 item.parent.return_value = False130 item.data.return_value = 0131 service_manager.service_manager_list = MagicMock()132 service_manager.service_manager_list.itemAt.return_value = item133 service_item = ServiceItem(None)134 service_manager.service_items.insert(1, {'service_item': service_item})135 service_manager.edit_action = MagicMock()136 service_manager.rename_action = MagicMock()137 service_manager.create_custom_action = MagicMock()138 service_manager.maintain_action = MagicMock()139 service_manager.notes_action = MagicMock()140 service_manager.time_action = MagicMock()141 service_manager.auto_start_action = MagicMock()142 service_manager.auto_play_slides_menu = MagicMock()143 service_manager.auto_play_slides_once = MagicMock()144 service_manager.auto_play_slides_loop = MagicMock()145 service_manager.timed_slide_interval = MagicMock()146 service_manager.theme_menu = MagicMock()147 service_manager.menu = MagicMock()148 # WHEN I define a context menu149 service_manager.context_menu(1)150 # THEN the following calls should have occurred.151 assert service_manager.edit_action.setVisible.call_count == 1, 'Should have been called once'152 assert service_manager.rename_action.setVisible.call_count == 1, 'Should have been called once'153 assert service_manager.create_custom_action.setVisible.call_count == 1, 'Should have been called once'154 assert service_manager.maintain_action.setVisible.call_count == 1, 'Should have been called once'155 assert service_manager.notes_action.setVisible.call_count == 1, 'Should have been called once'156 assert service_manager.time_action.setVisible.call_count == 1, 'Should have been called once'157 assert service_manager.auto_start_action.setVisible.call_count == 1, 'Should have been called once'158 assert service_manager.auto_play_slides_menu.menuAction().setVisible.call_count == 1, \159 'Should have been called once'160 assert service_manager.auto_play_slides_once.setChecked.call_count == 0, 'Should not be called'161 assert service_manager.auto_play_slides_loop.setChecked.call_count == 0, 'Should not be called'162 assert service_manager.timed_slide_interval.setChecked.call_count == 0, 'Should not be called'163 assert service_manager.theme_menu.menuAction().setVisible.call_count == 1, \164 'Should have been called once'165def test_build_song_context_menu(settings, state):166 """167 Test the creation of a context menu from service item of type text from Songs.168 """169 # GIVEN: A new service manager instance and a default service item.170 mocked_renderer = MagicMock()171 mocked_renderer.theme_level = ThemeLevel.Song172 Registry().register('plugin_manager', MagicMock())173 Registry().register('renderer', mocked_renderer)174 service_manager = ServiceManager(None)175 item = MagicMock()176 item.parent.return_value = False177 item.data.return_value = 0178 service_manager.service_manager_list = MagicMock()179 service_manager.service_manager_list.itemAt.return_value = item180 service_item = ServiceItem(None)181 for capability in [ItemCapabilities.CanEdit, ItemCapabilities.CanPreview, ItemCapabilities.CanLoop,182 ItemCapabilities.OnLoadUpdate, ItemCapabilities.AddIfNewItem,183 ItemCapabilities.CanSoftBreak]:184 service_item.add_capability(capability)185 service_item.service_item_type = ServiceItemType.Text186 service_item.edit_id = 1187 service_item._display_slides = []188 service_item._display_slides.append(MagicMock())189 service_manager.service_items.insert(1, {'service_item': service_item})190 service_manager.edit_action = MagicMock()191 service_manager.rename_action = MagicMock()192 service_manager.create_custom_action = MagicMock()193 service_manager.maintain_action = MagicMock()194 service_manager.notes_action = MagicMock()195 service_manager.time_action = MagicMock()196 service_manager.auto_start_action = MagicMock()197 service_manager.auto_play_slides_menu = MagicMock()198 service_manager.auto_play_slides_once = MagicMock()199 service_manager.auto_play_slides_loop = MagicMock()200 service_manager.timed_slide_interval = MagicMock()201 service_manager.theme_menu = MagicMock()202 service_manager.menu = MagicMock()203 # WHEN I define a context menu204 service_manager.context_menu(1)205 # THEN the following calls should have occurred.206 assert service_manager.edit_action.setVisible.call_count == 2, 'Should have be called twice'207 assert service_manager.rename_action.setVisible.call_count == 1, 'Should have be called once'208 assert service_manager.create_custom_action.setVisible.call_count == 1, 'Should have be called once'209 assert service_manager.maintain_action.setVisible.call_count == 1, 'Should have be called once'210 assert service_manager.notes_action.setVisible.call_count == 1, 'Should have be called once'211 assert service_manager.time_action.setVisible.call_count == 1, 'Should have be called once'212 assert service_manager.auto_start_action.setVisible.call_count == 1, 'Should have be called once'213 assert service_manager.auto_play_slides_menu.menuAction().setVisible.call_count == 1, \214 'Should have be called once'215 assert service_manager.auto_play_slides_once.setChecked.call_count == 0, 'Should not be called'216 assert service_manager.auto_play_slides_loop.setChecked.call_count == 0, 'Should not be called'217 assert service_manager.timed_slide_interval.setChecked.call_count == 0, 'Should not be called'218 assert service_manager.theme_menu.menuAction().setVisible.call_count == 2, \219 'Should have be called twice'220 # THEN we add a 2nd display frame221 service_item._display_slides.append(MagicMock())222 service_manager.context_menu(1)223 # THEN the following additional calls should have occurred.224 assert service_manager.auto_play_slides_menu.menuAction().setVisible.call_count == 2, \225 'Should have be called twice'226 assert service_manager.auto_play_slides_once.setChecked.call_count == 1, 'Should have be called once'227 assert service_manager.auto_play_slides_loop.setChecked.call_count == 1, 'Should have be called once'228 assert service_manager.timed_slide_interval.setChecked.call_count == 1, 'Should have be called once'229def test_build_bible_context_menu(settings, state):230 """231 Test the creation of a context menu from service item of type text from Bibles.232 """233 # GIVEN: A new service manager instance and a default service item.234 mocked_renderer = MagicMock()235 mocked_renderer.theme_level = ThemeLevel.Song236 Registry().register('plugin_manager', MagicMock())237 Registry().register('renderer', mocked_renderer)238 service_manager = ServiceManager(None)239 item = MagicMock()240 item.parent.return_value = False241 item.data.return_value = 0242 service_manager.service_manager_list = MagicMock()243 service_manager.service_manager_list.itemAt.return_value = item244 service_item = ServiceItem(None)245 for capability in [ItemCapabilities.NoLineBreaks, ItemCapabilities.CanPreview,246 ItemCapabilities.CanLoop, ItemCapabilities.CanWordSplit,247 ItemCapabilities.CanEditTitle]:248 service_item.add_capability(capability)249 service_item.service_item_type = ServiceItemType.Text250 service_item.edit_id = 1251 service_item._display_slides = []252 service_item._display_slides.append(MagicMock())253 service_manager.service_items.insert(1, {'service_item': service_item})254 service_manager.edit_action = MagicMock()255 service_manager.rename_action = MagicMock()256 service_manager.create_custom_action = MagicMock()257 service_manager.maintain_action = MagicMock()258 service_manager.notes_action = MagicMock()259 service_manager.time_action = MagicMock()260 service_manager.auto_start_action = MagicMock()261 service_manager.auto_play_slides_menu = MagicMock()262 service_manager.auto_play_slides_once = MagicMock()263 service_manager.auto_play_slides_loop = MagicMock()264 service_manager.timed_slide_interval = MagicMock()265 service_manager.theme_menu = MagicMock()266 service_manager.menu = MagicMock()267 # WHEN I define a context menu268 service_manager.context_menu(1)269 # THEN the following calls should have occurred.270 assert service_manager.edit_action.setVisible.call_count == 1, 'Should have be called once'271 assert service_manager.rename_action.setVisible.call_count == 2, 'Should have be called twice'272 assert service_manager.create_custom_action.setVisible.call_count == 1, 'Should have be called once'273 assert service_manager.maintain_action.setVisible.call_count == 1, 'Should have be called once'274 assert service_manager.notes_action.setVisible.call_count == 1, 'Should have be called once'275 assert service_manager.time_action.setVisible.call_count == 1, 'Should have be called once'276 assert service_manager.auto_start_action.setVisible.call_count == 1, 'Should have be called once'277 assert service_manager.auto_play_slides_menu.menuAction().setVisible.call_count == 1, \278 'Should have be called once'279 assert service_manager.auto_play_slides_once.setChecked.call_count == 0, 'Should not be called'280 assert service_manager.auto_play_slides_loop.setChecked.call_count == 0, 'Should not be called'281 assert service_manager.timed_slide_interval.setChecked.call_count == 0, 'Should not be called'282 assert service_manager.theme_menu.menuAction().setVisible.call_count == 2, \283 'Should have be called twice'284 # THEN we add a 2nd display frame285 service_item._display_slides.append(MagicMock())286 service_manager.context_menu(1)287 # THEN the following additional calls should have occurred.288 assert service_manager.auto_play_slides_menu.menuAction().setVisible.call_count == 2, \289 'Should have be called twice'290 assert service_manager.auto_play_slides_once.setChecked.call_count == 1, 'Should have be called once'291 assert service_manager.auto_play_slides_loop.setChecked.call_count == 1, 'Should have be called once'292 assert service_manager.timed_slide_interval.setChecked.call_count == 1, 'Should have be called once'293def test_build_custom_context_menu(settings, state):294 """295 Test the creation of a context menu from service item of type text from Custom.296 """297 # GIVEN: A new service manager instance and a default service item.298 mocked_renderer = MagicMock()299 mocked_renderer.theme_level = ThemeLevel.Song300 Registry().register('plugin_manager', MagicMock())301 Registry().register('renderer', mocked_renderer)302 service_manager = ServiceManager(None)303 item = MagicMock()304 item.parent.return_value = False305 item.data.return_value = 0306 service_manager.service_manager_list = MagicMock()307 service_manager.service_manager_list.itemAt.return_value = item308 service_item = ServiceItem(None)309 service_item.add_capability(ItemCapabilities.CanEdit)310 service_item.add_capability(ItemCapabilities.CanPreview)311 service_item.add_capability(ItemCapabilities.CanLoop)312 service_item.add_capability(ItemCapabilities.CanSoftBreak)313 service_item.add_capability(ItemCapabilities.OnLoadUpdate)314 service_item.service_item_type = ServiceItemType.Text315 service_item.edit_id = 1316 service_item._display_slides = []317 service_item._display_slides.append(MagicMock())318 service_manager.service_items.insert(1, {'service_item': service_item})319 service_manager.edit_action = MagicMock()320 service_manager.rename_action = MagicMock()321 service_manager.create_custom_action = MagicMock()322 service_manager.maintain_action = MagicMock()323 service_manager.notes_action = MagicMock()324 service_manager.time_action = MagicMock()325 service_manager.auto_start_action = MagicMock()326 service_manager.auto_play_slides_menu = MagicMock()327 service_manager.auto_play_slides_once = MagicMock()328 service_manager.auto_play_slides_loop = MagicMock()329 service_manager.timed_slide_interval = MagicMock()330 service_manager.theme_menu = MagicMock()331 service_manager.menu = MagicMock()332 # WHEN I define a context menu333 service_manager.context_menu(1)334 # THEN the following calls should have occurred.335 assert service_manager.edit_action.setVisible.call_count == 2, 'Should have be called twice'336 assert service_manager.rename_action.setVisible.call_count == 1, 'Should have be called once'337 assert service_manager.create_custom_action.setVisible.call_count == 1, 'Should have be called once'338 assert service_manager.maintain_action.setVisible.call_count == 1, 'Should have be called once'339 assert service_manager.notes_action.setVisible.call_count == 1, 'Should have be called once'340 assert service_manager.time_action.setVisible.call_count == 1, 'Should have be called once'341 assert service_manager.auto_start_action.setVisible.call_count == 1, 'Should have be called once'342 assert service_manager.auto_play_slides_menu.menuAction().setVisible.call_count == 1, \343 'Should have be called once'344 assert service_manager.auto_play_slides_once.setChecked.call_count == 0, 'Should not be called'345 assert service_manager.auto_play_slides_loop.setChecked.call_count == 0, 'Should not be called'346 assert service_manager.timed_slide_interval.setChecked.call_count == 0, 'Should not be called'347 assert service_manager.theme_menu.menuAction().setVisible.call_count == 2, \348 'Should have be called twice'349 # THEN we add a 2nd display frame350 service_item._display_slides.append(MagicMock())351 service_manager.context_menu(1)352 # THEN the following additional calls should have occurred.353 assert service_manager.auto_play_slides_menu.menuAction().setVisible.call_count == 2, \354 'Should have be called twice'355 assert service_manager.auto_play_slides_once.setChecked.call_count == 1, 'Should have be called once'356 assert service_manager.auto_play_slides_loop.setChecked.call_count == 1, 'Should have be called once'357 assert service_manager.timed_slide_interval.setChecked.call_count == 1, 'Should have be called once'358def test_build_image_context_menu(settings):359 """360 Test the creation of a context menu from service item of type Image from Image.361 """362 # GIVEN: A new service manager instance and a default service item.363 Registry().register('plugin_manager', MagicMock())364 Registry().register('renderer', MagicMock())365 service_manager = ServiceManager(None)366 item = MagicMock()367 item.parent.return_value = False368 item.data.return_value = 0369 service_manager.service_manager_list = MagicMock()370 service_manager.service_manager_list.itemAt.return_value = item371 service_item = ServiceItem(None)372 service_item.add_capability(ItemCapabilities.CanMaintain)373 service_item.add_capability(ItemCapabilities.CanPreview)374 service_item.add_capability(ItemCapabilities.CanLoop)375 service_item.add_capability(ItemCapabilities.CanAppend)376 service_item.add_capability(ItemCapabilities.CanEditTitle)377 service_item.service_item_type = ServiceItemType.Image378 service_item.edit_id = 1379 service_item.slides.append(MagicMock())380 service_manager.service_items.insert(1, {'service_item': service_item})381 service_manager.edit_action = MagicMock()382 service_manager.rename_action = MagicMock()383 service_manager.create_custom_action = MagicMock()384 service_manager.maintain_action = MagicMock()385 service_manager.notes_action = MagicMock()386 service_manager.time_action = MagicMock()387 service_manager.auto_start_action = MagicMock()388 service_manager.auto_play_slides_menu = MagicMock()389 service_manager.auto_play_slides_once = MagicMock()390 service_manager.auto_play_slides_loop = MagicMock()391 service_manager.timed_slide_interval = MagicMock()392 service_manager.theme_menu = MagicMock()393 service_manager.menu = MagicMock()394 # WHEN I define a context menu395 service_manager.context_menu(1)396 # THEN the following calls should have occurred.397 assert service_manager.edit_action.setVisible.call_count == 1, 'Should have be called once'398 assert service_manager.rename_action.setVisible.call_count == 2, 'Should have be called twice'399 assert service_manager.create_custom_action.setVisible.call_count == 1, 'Should have be called once'400 assert service_manager.maintain_action.setVisible.call_count == 2, 'Should have be called twice'401 assert service_manager.notes_action.setVisible.call_count == 1, 'Should have be called once'402 assert service_manager.time_action.setVisible.call_count == 1, 'Should have be called once'403 assert service_manager.auto_start_action.setVisible.call_count == 1, 'Should have be called once'404 assert service_manager.auto_play_slides_menu.menuAction().setVisible.call_count == 1, \405 'Should have be called once'406 assert service_manager.auto_play_slides_once.setChecked.call_count == 0, 'Should not be called'407 assert service_manager.auto_play_slides_loop.setChecked.call_count == 0, 'Should not be called'408 assert service_manager.timed_slide_interval.setChecked.call_count == 0, 'Should not be called'409 assert service_manager.theme_menu.menuAction().setVisible.call_count == 1, \410 'Should have be called once'411 # THEN we add a 2nd display frame and regenerate the menu.412 service_item.slides.append(MagicMock())413 service_manager.context_menu(1)414 # THEN the following additional calls should have occurred.415 assert service_manager.auto_play_slides_menu.menuAction().setVisible.call_count == 2, \416 'Should have be called twice'417 assert service_manager.auto_play_slides_once.setChecked.call_count == 1, 'Should have be called once'418 assert service_manager.auto_play_slides_loop.setChecked.call_count == 1, 'Should have be called once'419 assert service_manager.timed_slide_interval.setChecked.call_count == 1, 'Should have be called once'420def test_build_media_context_menu(settings):421 """422 Test the creation of a context menu from service item of type Command from Media.423 """424 # GIVEN: A new service manager instance and a default service item.425 Registry().register('plugin_manager', MagicMock())426 Registry().register('renderer', MagicMock())427 service_manager = ServiceManager(None)428 item = MagicMock()429 item.parent.return_value = False430 item.data.return_value = 0431 service_manager.service_manager_list = MagicMock()432 service_manager.service_manager_list.itemAt.return_value = item433 service_item = ServiceItem(None)434 service_item.add_capability(ItemCapabilities.CanAutoStartForLive)435 service_item.add_capability(ItemCapabilities.CanEditTitle)436 service_item.add_capability(ItemCapabilities.RequiresMedia)437 service_item.service_item_type = ServiceItemType.Command438 service_item.edit_id = 1439 service_item.slides.append(MagicMock())440 service_manager.service_items.insert(1, {'service_item': service_item})441 service_manager.edit_action = MagicMock()442 service_manager.rename_action = MagicMock()443 service_manager.create_custom_action = MagicMock()444 service_manager.maintain_action = MagicMock()445 service_manager.notes_action = MagicMock()446 service_manager.time_action = MagicMock()447 service_manager.auto_start_action = MagicMock()448 service_manager.auto_play_slides_menu = MagicMock()449 service_manager.auto_play_slides_once = MagicMock()450 service_manager.auto_play_slides_loop = MagicMock()451 service_manager.timed_slide_interval = MagicMock()452 service_manager.theme_menu = MagicMock()453 service_manager.menu = MagicMock()454 # WHEN I define a context menu455 service_manager.context_menu(1)456 # THEN the following calls should have occurred.457 assert service_manager.edit_action.setVisible.call_count == 1, 'Should have be called once'458 assert service_manager.rename_action.setVisible.call_count == 2, 'Should have be called twice'459 assert service_manager.create_custom_action.setVisible.call_count == 1, 'Should have be called once'460 assert service_manager.maintain_action.setVisible.call_count == 1, 'Should have be called once'461 assert service_manager.notes_action.setVisible.call_count == 1, 'Should have be called once'462 assert service_manager.time_action.setVisible.call_count == 1, 'Should have be called once'463 assert service_manager.auto_start_action.setVisible.call_count == 2, 'Should have be called twice'464 assert service_manager.auto_play_slides_menu.menuAction().setVisible.call_count == 1, \465 'Should have be called once'466 assert service_manager.auto_play_slides_once.setChecked.call_count == 0, 'Should not be called'467 assert service_manager.auto_play_slides_loop.setChecked.call_count == 0, 'Should not be called'468 assert service_manager.timed_slide_interval.setChecked.call_count == 0, 'Should not be called'469 assert service_manager.theme_menu.menuAction().setVisible.call_count == 1, \470 'Should have be called once'471 # THEN I change the length of the media and regenerate the menu.472 service_item.set_media_length(5)473 service_manager.context_menu(1)474 # THEN the following additional calls should have occurred.475 assert service_manager.time_action.setVisible.call_count == 3, 'Should have be called three times'476def test_build_presentation_pdf_context_menu(settings):477 """478 Test the creation of a context menu from service item of type Command with PDF from Presentation.479 """480 # GIVEN: A new service manager instance and a default service item.481 Registry().register('plugin_manager', MagicMock())482 Registry().register('renderer', MagicMock())483 service_manager = ServiceManager(None)484 item = MagicMock()485 item.parent.return_value = False486 item.data.return_value = 0487 service_manager.service_manager_list = MagicMock()488 service_manager.service_manager_list.itemAt.return_value = item489 service_item = ServiceItem(None)490 service_item.add_capability(ItemCapabilities.CanMaintain)491 service_item.add_capability(ItemCapabilities.CanPreview)492 service_item.add_capability(ItemCapabilities.CanLoop)493 service_item.add_capability(ItemCapabilities.CanAppend)494 service_item.service_item_type = ServiceItemType.Command495 service_item.edit_id = 1496 service_item.slides.append(MagicMock())497 service_manager.service_items.insert(1, {'service_item': service_item})498 service_manager.edit_action = MagicMock()499 service_manager.rename_action = MagicMock()500 service_manager.create_custom_action = MagicMock()501 service_manager.maintain_action = MagicMock()502 service_manager.notes_action = MagicMock()503 service_manager.time_action = MagicMock()504 service_manager.auto_start_action = MagicMock()505 service_manager.auto_play_slides_menu = MagicMock()506 service_manager.auto_play_slides_once = MagicMock()507 service_manager.auto_play_slides_loop = MagicMock()508 service_manager.timed_slide_interval = MagicMock()509 service_manager.theme_menu = MagicMock()510 service_manager.menu = MagicMock()511 # WHEN I define a context menu512 service_manager.context_menu(1)513 # THEN the following calls should have occurred.514 assert service_manager.edit_action.setVisible.call_count == 1, 'Should have be called once'515 assert service_manager.rename_action.setVisible.call_count == 1, 'Should have be called once'516 assert service_manager.create_custom_action.setVisible.call_count == 1, 'Should have be called once'517 assert service_manager.maintain_action.setVisible.call_count == 2, 'Should have be called twice'518 assert service_manager.notes_action.setVisible.call_count == 1, 'Should have be called once'519 assert service_manager.time_action.setVisible.call_count == 1, 'Should have be called once'520 assert service_manager.auto_start_action.setVisible.call_count == 1, 'Should have be called once'521 assert service_manager.auto_play_slides_menu.menuAction().setVisible.call_count == 1, \522 'Should have be called once'523 assert service_manager.auto_play_slides_once.setChecked.call_count == 0, 'Should not be called'524 assert service_manager.auto_play_slides_loop.setChecked.call_count == 0, 'Should not be called'525 assert service_manager.timed_slide_interval.setChecked.call_count == 0, 'Should not be called'526 assert service_manager.theme_menu.menuAction().setVisible.call_count == 1, \527 'Should have be called once'528def test_build_presentation_non_pdf_context_menu(settings):529 """530 Test the creation of a context menu from service item of type Command with Impress from Presentation.531 """532 # GIVEN: A new service manager instance and a default service item.533 Registry().register('plugin_manager', MagicMock())534 Registry().register('renderer', MagicMock())535 service_manager = ServiceManager(None)536 item = MagicMock()537 item.parent.return_value = False538 item.data.return_value = 0539 service_manager.service_manager_list = MagicMock()540 service_manager.service_manager_list.itemAt.return_value = item541 service_item = ServiceItem(None)542 service_item.add_capability(ItemCapabilities.ProvidesOwnDisplay)543 service_item.service_item_type = ServiceItemType.Command544 service_item.edit_id = 1545 service_item.slides.append(MagicMock())546 service_manager.service_items.insert(1, {'service_item': service_item})547 service_manager.edit_action = MagicMock()548 service_manager.rename_action = MagicMock()549 service_manager.create_custom_action = MagicMock()550 service_manager.maintain_action = MagicMock()551 service_manager.notes_action = MagicMock()552 service_manager.time_action = MagicMock()553 service_manager.auto_start_action = MagicMock()554 service_manager.auto_play_slides_menu = MagicMock()555 service_manager.auto_play_slides_once = MagicMock()556 service_manager.auto_play_slides_loop = MagicMock()557 service_manager.timed_slide_interval = MagicMock()558 service_manager.theme_menu = MagicMock()559 service_manager.menu = MagicMock()560 # WHEN I define a context menu561 service_manager.context_menu(1)562 # THEN the following calls should have occurred.563 assert service_manager.edit_action.setVisible.call_count == 1, 'Should have be called once'564 assert service_manager.rename_action.setVisible.call_count == 1, 'Should have be called once'565 assert service_manager.create_custom_action.setVisible.call_count == 1, 'Should have be called once'566 assert service_manager.maintain_action.setVisible.call_count == 1, 'Should have be called once'567 assert service_manager.notes_action.setVisible.call_count == 1, 'Should have be called once'568 assert service_manager.time_action.setVisible.call_count == 1, 'Should have be called once'569 assert service_manager.auto_start_action.setVisible.call_count == 1, 'Should have be called once'570 assert service_manager.auto_play_slides_menu.menuAction().setVisible.call_count == 1, \571 'Should have be called once'572 assert service_manager.auto_play_slides_once.setChecked.call_count == 0, 'Should not be called'573 assert service_manager.auto_play_slides_loop.setChecked.call_count == 0, 'Should not be called'574 assert service_manager.timed_slide_interval.setChecked.call_count == 0, 'Should not be called'575 assert service_manager.theme_menu.menuAction().setVisible.call_count == 1, \576 'Should have be called once'577@patch('openlp.core.ui.servicemanager.QtCore.QTimer.singleShot')578def test_single_click_preview_true(mocked_singleShot, settings):579 """580 Test that when "Preview items when clicked in Service Manager" enabled the preview timer starts581 """582 # GIVEN: A setting to enable "Preview items when clicked in Service Manager" and a service manager.583 settings.setValue('advanced/single click service preview', True)584 service_manager = ServiceManager(None)585 # WHEN: on_single_click_preview() is called586 service_manager.on_single_click_preview()587 # THEN: timer should have been started588 mocked_singleShot.assert_called_with(QtWidgets.QApplication.instance().doubleClickInterval(),589 service_manager.on_single_click_preview_timeout)590@patch('openlp.core.ui.servicemanager.QtCore.QTimer.singleShot')591def test_single_click_preview_false(mocked_singleShot, settings):592 """593 Test that when "Preview items when clicked in Service Manager" disabled the preview timer doesn't start594 """595 # GIVEN: A setting to enable "Preview items when clicked in Service Manager" and a service manager.596 settings.setValue('advanced/single click service preview', False)597 service_manager = ServiceManager(None)598 # WHEN: on_single_click_preview() is called599 service_manager.on_single_click_preview()600 # THEN: timer should not be started601 assert mocked_singleShot.call_count == 0, 'Should not be called'602@patch('openlp.core.ui.servicemanager.QtCore.QTimer.singleShot')603@patch('openlp.core.ui.servicemanager.ServiceManager.make_live')604def test_single_click_preview_double(mocked_make_live, mocked_singleShot, settings):605 """606 Test that when a double click has registered the preview timer doesn't start607 """608 # GIVEN: A setting to enable "Preview items when clicked in Service Manager" and a service manager.609 settings.setValue('advanced/single click service preview', True)610 service_manager = ServiceManager(None)611 # WHEN: on_single_click_preview() is called following a double click612 service_manager.on_double_click_live()613 service_manager.on_single_click_preview()614 # THEN: timer should not be started615 mocked_make_live.assert_called_with()616 assert mocked_singleShot.call_count == 0, 'Should not be called'617@patch('openlp.core.ui.servicemanager.ServiceManager.make_preview')618def test_single_click_timeout_single(mocked_make_preview, settings):619 """620 Test that when a single click has been registered, the item is sent to preview621 """622 # GIVEN: A service manager.623 service_manager = ServiceManager(None)624 # WHEN: on_single_click_preview() is called625 service_manager.on_single_click_preview_timeout()626 # THEN: make_preview() should have been called627 assert mocked_make_preview.call_count == 1, 'ServiceManager.make_preview() should have been called once'628@patch('openlp.core.ui.servicemanager.ServiceManager.make_preview')629@patch('openlp.core.ui.servicemanager.ServiceManager.make_live')630def test_single_click_timeout_double(mocked_make_live, mocked_make_preview, settings):631 """632 Test that when a double click has been registered, the item does not goes to preview633 """634 # GIVEN: A service manager.635 service_manager = ServiceManager(None)636 # WHEN: on_single_click_preview() is called after a double click637 service_manager.on_double_click_live()638 service_manager.on_single_click_preview_timeout()639 # THEN: make_preview() should not have been called640 assert mocked_make_preview.call_count == 0, 'ServiceManager.make_preview() should not be called'641@patch('openlp.core.ui.servicemanager.zipfile')642@patch('openlp.core.ui.servicemanager.ServiceManager.save_file_as')643@patch('openlp.core.ui.servicemanager.os')644@patch('openlp.core.ui.servicemanager.shutil')645@patch('openlp.core.ui.servicemanager.NamedTemporaryFile')646def test_save_file_raises_permission_error(mocked_temp_file, mocked_shutil, mocked_os, mocked_save_file_as,647 mocked_zipfile, settings):648 """649 Test that when a PermissionError is raised when trying to save a file, it is handled correctly650 """651 # GIVEN: A service manager, a service to save652 mocked_main_window = MagicMock()653 Registry().register('main_window', mocked_main_window)654 service_manager = ServiceManager(None)655 service_manager._service_path = MagicMock()656 service_manager._save_lite = False657 service_manager.service_items = []658 service_manager.service_theme = 'Default'659 service_manager.service_manager_list = MagicMock()660 mocked_save_file_as.return_value = False661 mocked_zipfile.ZipFile.return_value = MagicMock()662 mocked_os.link.side_effect = PermissionError663 mocked_shutil.copyfile.side_effect = PermissionError664 # WHEN: The service is saved and a PermissionError is raised665 result = service_manager.save_file()666 # THEN: The "save_as" method is called to save the service667 assert result is False668 mocked_save_file_as.assert_called_with()669@patch('openlp.core.ui.servicemanager.zipfile')670@patch('openlp.core.ui.servicemanager.ServiceManager.save_file_as')671@patch('openlp.core.ui.servicemanager.os')672@patch('openlp.core.ui.servicemanager.shutil')673@patch('openlp.core.ui.servicemanager.len')674@patch('openlp.core.ui.servicemanager.NamedTemporaryFile')675def test_save_file_large_file(mocked_temp_file, mocked_len, mocked_shutil, mocked_os, mocked_save_file_as,676 mocked_zipfile, registry):677 """678 Test that when a file size size larger than a 32bit signed int is attempted to save, the progress bar679 should be provided a value that fits in a 32bit int (because it's passed to C++ as a 32bit unsigned int)680 """681 # GIVEN: A service manager, a service to save, and len() returns a huge value (file size)682 def check_for_i32_overflow(val):683 if val > 2147483647:684 raise OverflowError685 mocked_main_window = MagicMock()686 mocked_main_window.display_progress_bar.side_effect = check_for_i32_overflow687 Registry().register('main_window', mocked_main_window)688 Registry().register('application', MagicMock())689 Registry().register('settings', MagicMock())690 service_manager = ServiceManager(None)691 service_manager._service_path = MagicMock()692 service_manager._save_lite = False693 service_manager.service_items = []694 service_manager.service_theme = 'Default'695 service_manager.service_manager_list = MagicMock()696 mocked_save_file_as.return_value = False697 mocked_zipfile.ZipFile.return_value = MagicMock()698 mocked_len.return_value = 10000000000000699 # WHEN: The service is saved and no error is raised700 result = service_manager.save_file()701 # THEN: The "save_as" method is called to save the service702 assert result is True703 mocked_save_file_as.assert_not_called()704@patch('openlp.core.ui.servicemanager.zipfile')705@patch('openlp.core.ui.servicemanager.ServiceManager.save_file_as')706@patch('openlp.core.ui.servicemanager.os')707@patch('openlp.core.ui.servicemanager.shutil')708@patch('openlp.core.ui.servicemanager.NamedTemporaryFile')709def test_save_file_falls_back_to_shutil(mocked_temp_file, mocked_shutil, mocked_os, mocked_save_file_as, mocked_zipfile,710 registry):711 """712 Test that when a PermissionError is raised when trying to save a file, it is handled correctly713 """714 # GIVEN: A service manager, a service to save715 mocked_main_window = MagicMock()716 Registry().register('main_window', mocked_main_window)717 Registry().register('application', MagicMock())718 Registry().register('settings', MagicMock())719 service_manager = ServiceManager(None)720 service_manager._service_path = MagicMock()721 service_manager._save_lite = False722 service_manager.service_items = []723 service_manager.service_theme = 'Default'724 service_manager.service_manager_list = MagicMock()725 mocked_save_file_as.return_value = False726 mocked_zipfile.ZipFile.return_value = MagicMock()727 mocked_os.link.side_effect = OSError728 # WHEN: The service is saved and a PermissionError is raised729 result = service_manager.save_file()730 # THEN: The result is true731 assert result is True732 mocked_shutil.copyfile.assert_called_once()733@patch('openlp.core.ui.servicemanager.ServiceManager.regenerate_service_items')734def test_theme_change_global(mocked_regenerate_service_items, settings):735 """736 Test that when a Toolbar theme combobox displays correctly when the theme is set to Global737 """738 # GIVEN: A service manager, settings set to Global theme739 service_manager = ServiceManager(None)740 service_manager.toolbar = OpenLPToolbar(None)741 service_manager.toolbar.add_toolbar_action('theme_combo_box', triggers=MagicMock())742 service_manager.toolbar.add_toolbar_action('theme_label', triggers=MagicMock())743 settings.setValue('themes/theme level', ThemeLevel.Global)744 # WHEN: theme_change is called745 service_manager.on_theme_level_changed()746 # THEN: The the theme toolbar should not be visible747 assert service_manager.toolbar.actions['theme_combo_box'].isVisible() is False, \748 'The visibility should be False'749@patch('openlp.core.ui.servicemanager.ServiceManager.regenerate_service_items')750def test_theme_change_service(mocked_regenerate_service_items, settings):751 """752 Test that when a Toolbar theme combobox displays correctly when the theme is set to Theme753 """754 # GIVEN: A service manager, settings set to Service theme755 service_manager = ServiceManager(None)756 service_manager.toolbar = OpenLPToolbar(None)757 service_manager.toolbar.add_toolbar_action('theme_combo_box', triggers=MagicMock())758 service_manager.toolbar.add_toolbar_action('theme_label', triggers=MagicMock())759 settings.setValue('themes/theme level', ThemeLevel.Service)760 # WHEN: theme_change is called761 service_manager.on_theme_level_changed()762 # THEN: The the theme toolbar should be visible763 assert service_manager.toolbar.actions['theme_combo_box'].isVisible() is True, \764 'The visibility should be True'765@patch('openlp.core.ui.servicemanager.ServiceManager.regenerate_service_items')766def test_theme_change_song(mocked_regenerate_service_items, settings):767 """768 Test that when a Toolbar theme combobox displays correctly when the theme is set to Song769 """770 # GIVEN: A service manager, settings set to Song theme771 service_manager = ServiceManager(None)772 service_manager.toolbar = OpenLPToolbar(None)773 service_manager.toolbar.add_toolbar_action('theme_combo_box', triggers=MagicMock())774 service_manager.toolbar.add_toolbar_action('theme_label', triggers=MagicMock())775 settings.setValue('themes/theme level', ThemeLevel.Song)776 # WHEN: theme_change is called777 service_manager.on_theme_level_changed()778 # THEN: The the theme toolbar should be visible779 assert service_manager.toolbar.actions['theme_combo_box'].isVisible() is True, \780 'The visibility should be True'781@patch('PyQt5.QtWidgets.QTreeWidgetItemIterator')782def test_regenerate_service_items(mocked_tree, settings):783 """784 test that an unmodified service item that is regenerated is still unmodified785 """786 # GIVEN: A service manager and a service item787 mocked_main_window = MagicMock()788 Registry().register('main_window', mocked_main_window)789 service_manager = ServiceManager(None)790 service_item = ServiceItem(None)791 service_item.service_item_type = ServiceItemType.Command792 service_item.edit_id = 1793 service_item.icon = MagicMock(pixmap=MagicMock())794 service_item.slides.append(MagicMock())795 service_manager.service_items.insert(1, {'service_item': service_item, 'expanded': False})796 service_manager._modified = False797 service_manager.service_manager_list = MagicMock()798 service_manager.repaint_service_list = MagicMock()799 mocked_tree.return_value = MagicMock(value=MagicMock(return_value=None))800 # WHEN: regenerate_service_items is called801 service_manager.regenerate_service_items()802 # THEN: The the service should be repainted and not be marked as modified803 assert service_manager.is_modified() is False804 service_manager.repaint_service_list.assert_called_once()805@patch('PyQt5.QtWidgets.QTreeWidgetItemIterator')806def test_regenerate_service_items_modified(mocked_tree, settings):807 """808 test that an unmodified service item that is regenerated is still unmodified809 """810 # GIVEN: A service manager and a service item811 mocked_main_window = MagicMock()812 Registry().register('main_window', mocked_main_window)813 service_manager = ServiceManager(None)814 service_item = ServiceItem(None)815 service_item.service_item_type = ServiceItemType.Command816 service_item.edit_id = 1817 service_item.icon = MagicMock(pixmap=MagicMock())818 service_item.slides.append(MagicMock())819 service_manager.service_items.insert(1, {'service_item': service_item, 'expanded': False})820 service_manager._modified = True821 service_manager.service_manager_list = MagicMock()822 service_manager.repaint_service_list = MagicMock()823 mocked_tree.return_value = MagicMock(value=MagicMock(return_value=None))824 # WHEN: regenerate_service_items is called825 service_manager.regenerate_service_items()826 # THEN: The the service should be repainted and still be marked as modified827 assert service_manager.is_modified() is True828 service_manager.repaint_service_list.assert_called_once()829@patch('PyQt5.QtWidgets.QTreeWidgetItemIterator')830def test_regenerate_service_items_set_modified(mocked_tree, settings):831 """832 test that a service item that is regenerated with the modified argument becomes modified833 """834 # GIVEN: A service manager and a service item835 mocked_main_window = MagicMock()836 Registry().register('main_window', mocked_main_window)837 service_manager = ServiceManager(None)838 service_item = ServiceItem(None)839 service_item.service_item_type = ServiceItemType.Command840 service_item.edit_id = 1841 service_item.icon = MagicMock(pixmap=MagicMock())842 service_item.slides.append(MagicMock())843 service_manager.service_items.insert(1, {'service_item': service_item, 'expanded': False})844 service_manager._modified = False845 service_manager.service_manager_list = MagicMock()846 service_manager.repaint_service_list = MagicMock()847 mocked_tree.return_value = MagicMock(value=MagicMock(return_value=None))848 # WHEN: regenerate_service_items is called849 service_manager.regenerate_service_items(True)850 # THEN: The the service should be repainted and now be marked as modified851 assert service_manager.is_modified() is True852 service_manager.repaint_service_list.assert_called_once()853def test_service_manager_list_drag_enter_event():854 """855 Test that the ServiceManagerList.dragEnterEvent slot accepts the event856 """857 # GIVEN: A service manager and a mocked event858 service_manager_list = ServiceManagerList(None)859 mocked_event = MagicMock()860 # WHEN: The dragEnterEvent method is called861 service_manager_list.dragEnterEvent(mocked_event)862 # THEN: The accept() method on the event is called863 mocked_event.accept.assert_called_once_with()864def test_service_manager_list_drag_move_event():865 """866 Test that the ServiceManagerList.dragMoveEvent slot accepts the event867 """868 # GIVEN: A service manager and a mocked event869 service_manager_list = ServiceManagerList(None)870 mocked_event = MagicMock()871 # WHEN: The dragMoveEvent method is called872 service_manager_list.dragMoveEvent(mocked_event)873 # THEN: The accept() method on the event is called874 mocked_event.accept.assert_called_once_with()875def test_service_manager_list_key_press_event():876 """877 Test that the ServiceManagerList.keyPressEvent slot ignores the event878 """879 # GIVEN: A service manager and a mocked event880 service_manager_list = ServiceManagerList(None)881 mocked_event = MagicMock()882 # WHEN: The keyPressEvent method is called883 service_manager_list.keyPressEvent(mocked_event)884 # THEN: The ignore() method on the event is called885 mocked_event.ignore.assert_called_once_with()886def test_service_manager_list_mouse_move_event_not_left_button():887 """888 Test that the ServiceManagerList.mouseMoveEvent slot ignores the event if it's not a left-click889 """890 # GIVEN: A service manager and a mocked event891 service_manager_list = ServiceManagerList(None)892 mocked_event = MagicMock()893 mocked_event.buttons.return_value = QtCore.Qt.RightButton894 # WHEN: The mouseMoveEvent method is called895 service_manager_list.mouseMoveEvent(mocked_event)896 # THEN: The ignore() method on the event is called897 mocked_event.ignore.assert_called_once_with()898@patch('openlp.core.ui.servicemanager.QtGui.QCursor')899def test_service_manager_list_mouse_move_event_no_item(MockQCursor):900 """901 Test that the ServiceManagerList.mouseMoveEvent slot ignores the event if it's not over an item902 """903 # GIVEN: A service manager and a mocked event904 service_manager_list = ServiceManagerList(None)905 service_manager_list.itemAt = MagicMock(return_value=None)906 service_manager_list.mapFromGlobal = MagicMock()907 mocked_event = MagicMock()908 mocked_event.buttons.return_value = QtCore.Qt.LeftButton909 # WHEN: The mouseMoveEvent method is called910 service_manager_list.mouseMoveEvent(mocked_event)911 # THEN: The ignore() method on the event is called912 mocked_event.ignore.assert_called_once_with()913@patch('openlp.core.ui.servicemanager.QtGui.QCursor')914@patch('openlp.core.ui.servicemanager.QtGui.QDrag')915@patch('openlp.core.ui.servicemanager.QtCore.QMimeData')916def test_service_manager_list_mouse_move_event(MockQMimeData, MockQDrag, MockQCursor):917 """918 Test that the ServiceManagerList.mouseMoveEvent slot ignores the event if it's not over an item919 """920 # GIVEN: A service manager and a mocked event921 service_manager_list = ServiceManagerList(None)922 service_manager_list.itemAt = MagicMock(return_value=True)923 service_manager_list.mapFromGlobal = MagicMock()924 mocked_event = MagicMock()925 mocked_event.buttons.return_value = QtCore.Qt.LeftButton926 mocked_drag = MagicMock()927 MockQDrag.return_value = mocked_drag928 mocked_mime_data = MagicMock()929 MockQMimeData.return_value = mocked_mime_data930 # WHEN: The mouseMoveEvent method is called931 service_manager_list.mouseMoveEvent(mocked_event)932 # THEN: The ignore() method on the event is called933 mocked_drag.setMimeData.assert_called_once_with(mocked_mime_data)934 mocked_mime_data.setText.assert_called_once_with('ServiceManager')935 mocked_drag.exec.assert_called_once_with(QtCore.Qt.CopyAction)936def test_on_new_service_clicked_not_saved_cancel(registry):937 """Test that when you click on the new service button, you're asked to save any modified documents"""938 # GIVEN: A Service manager and some mocks939 service_manager = ServiceManager(None)940 service_manager.is_modified = MagicMock(return_value=True)941 service_manager.save_modified_service = MagicMock(return_value=QtWidgets.QMessageBox.Cancel)942 # WHEN: on_new_service_clicked() is called943 result = service_manager.on_new_service_clicked()944 # THEN: The method should have exited early (hence the False return)945 assert result is False, 'The method should have returned early'946def test_on_new_service_clicked_not_saved_false_save(registry):947 """Test that when you click on the new service button, you're asked to save any modified documents"""948 # GIVEN: A Service manager and some mocks949 service_manager = ServiceManager(None)950 service_manager.is_modified = MagicMock(return_value=True)951 service_manager.save_modified_service = MagicMock(return_value=QtWidgets.QMessageBox.Save)952 service_manager.decide_save_method = MagicMock(return_value=False)953 # WHEN: on_new_service_clicked() is called954 result = service_manager.on_new_service_clicked()955 # THEN: The method should have exited early (hence the False return)956 assert result is False, 'The method should have returned early'957@patch('openlp.core.ui.servicemanager.QtWidgets.QMessageBox')958def test_on_new_service_clicked_unmodified_blank_service(MockQMessageBox, registry):959 """Test that when the click the new button with an unmodified service, it shows you a message"""960 # GIVEN: A service manager with no service items and a bunch of mocks961 mocked_message_box = MagicMock()962 mocked_message_box.checkbox.return_value.isChecked.return_value = True963 MockQMessageBox.return_value = mocked_message_box964 mocked_settings = MagicMock()965 mocked_settings.value.return_value = True966 registry.register('settings', mocked_settings)967 service_manager = ServiceManager(None)968 service_manager.is_modified = MagicMock(return_value=False)969 service_manager.service_items = []970 service_manager.new_file = MagicMock()971 # WHEN: on_new_service_clicked() is called972 service_manager.on_new_service_clicked()973 # THEN: The message box should have been shown, and the message supressed974 mocked_settings.value.assert_called_once_with('advanced/new service message')975 assert mocked_message_box.setCheckBox.call_count == 1, 'setCheckBox was called to place the checkbox'976 mocked_message_box.exec.assert_called_once_with()977 mocked_settings.setValue.assert_called_once_with('advanced/new service message', False)978 service_manager.new_file.assert_called_once_with()979def _create_mock_action(parent, name, **kwargs):980 """981 Create a fake action with some "real" attributes982 """983 action = QtWidgets.QAction(parent)984 action.setObjectName(name)985 if kwargs.get('triggers'):986 action.triggered.connect(kwargs.pop('triggers'))987 return action988def _add_service_item(s_manager):989 "adds a mocked service item to the passed service manager"990 mocked_plugin = MagicMock()991 mocked_plugin.name = 'songs'992 service_item = ServiceItem(mocked_plugin)993 service_item.add_icon()994 slide = "Test slide"995 service_item.add_from_text(slide)996 service_item.title = "Test item"997 s_manager.add_service_item(service_item, rebuild=True, selected=True)998@pytest.fixture()999def service_manager(registry, settings):1000 """Setup a service manager with a service item and a few mocked registry entries"""1001 # Mocked registry entries1002 Registry().register('main_window', MagicMock())1003 Registry().register('live_controller', MagicMock())1004 Registry().register('renderer', MagicMock())1005 # Service manager1006 service_manager = ServiceManager()1007 add_toolbar_action_patcher = patch('openlp.core.ui.servicemanager.OpenLPToolbar.add_toolbar_action')1008 mocked_add_toolbar_action = add_toolbar_action_patcher.start()1009 mocked_add_toolbar_action.side_effect = \1010 lambda name, **kwargs: _create_mock_action(service_manager.toolbar, name, **kwargs)1011 service_manager.setup_ui(service_manager)1012 yield service_manager1013 del service_manager1014 add_toolbar_action_patcher.stop()1015def test_on_delete_from_service_confirmation_disabled(settings, service_manager):1016 """1017 Test that when the on_delete_from_service function is called and1018 confirmation for deleting items is disabled, the currently selected1019 item is removed.1020 """1021 # GIVEN delete item confirmation is disabled and a mock service item1022 settings.setValue('advanced/delete service item confirmation', False)1023 _add_service_item(service_manager)1024 # WHEN the on_delete_from_service function is called1025 service_manager.on_delete_from_service()1026 # THEN the service_items list should be empty1027 assert len(service_manager.service_items) == 01028def test_on_delete_from_service_confirmation_enabled_choose_delete(settings, service_manager):1029 """1030 Test that when the on_delete_from_service function is called and1031 confirmation for deleting items is enabled, and the user confirms1032 deletion, the currently selected item is removed from the service.1033 """1034 # GIVEN delete item confirmation is enabled and a mock service item1035 settings.setValue('advanced/delete service item confirmation', True)1036 _add_service_item(service_manager)1037 # WHEN the on_delete_from_service function is called and the user chooses to delete1038 service_manager._delete_confirmation_dialog = MagicMock(return_value=QtWidgets.QMessageBox.Close)1039 service_manager.on_delete_from_service()1040 # THEN the service_items list should be empty1041 assert len(service_manager.service_items) == 01042def test_on_delete_from_service_confirmation_enabled_choose_cancel(settings, service_manager):1043 """1044 Test that when the on_delete_from_service function is called and1045 confirmation for deleting items is enabled, and the user confirms1046 deletion, the service remains unchanged.1047 """1048 # GIVEN delete item confirmation is enabled a mock service item1049 settings.setValue('advanced/delete service item confirmation', True)1050 _add_service_item(service_manager)1051 service_items_copy = service_manager.service_items.copy()1052 # WHEN the on_delete_from_service function is called and the user cancels1053 service_manager._delete_confirmation_dialog = MagicMock(return_value=QtWidgets.QMessageBox.Cancel)1054 service_manager.on_delete_from_service()1055 # THEN the service_items list should be unchanged1056 assert service_manager.service_items == service_items_copy1057def test_on_load_service_clicked(registry):1058 """Test that a service is loaded when you click the button"""1059 # GIVEN: A service manager1060 service_manager = ServiceManager(None)1061 # THEN: Check that we have a load_service method first, before mocking it1062 assert hasattr(service_manager, 'load_service'), 'ServiceManager.load_service() should exist'1063 # GIVEN: A mocked out load_service() method1064 service_manager.load_service = MagicMock()1065 # WHEN: The load button is clicked1066 service_manager.on_load_service_clicked(False)1067 # THEN: load_service() should have been called1068 service_manager.load_service.assert_called_once_with()1069def test_load_service_modified_cancel_save(registry):1070 """Test that the load_service() method exits early when the service is modified, but the save is canceled"""1071 # GIVEN: A modified ServiceManager1072 service_manager = ServiceManager(None)1073 service_manager.is_modified = MagicMock(return_value=True)1074 service_manager.save_modified_service = MagicMock(return_value=QtWidgets.QMessageBox.Cancel)1075 # WHEN: A service is loaded1076 result = service_manager.load_service()1077 # THEN: The result should be False because of an early exit1078 assert result is False, 'The method did not exit early'1079def test_load_service_modified_saved_with_file_path(registry):1080 """Test that the load_service() method saves the file and loads the specified file"""1081 # GIVEN: A modified ServiceManager1082 mocked_settings = MagicMock()1083 registry.register('settings', mocked_settings)1084 service_manager = ServiceManager(None)1085 service_manager.is_modified = MagicMock(return_value=True)1086 service_manager.save_modified_service = MagicMock(return_value=QtWidgets.QMessageBox.Save)1087 service_manager.decide_save_method = MagicMock()1088 service_manager.load_file = MagicMock()1089 # WHEN: A service is loaded1090 service_manager.load_service(Path.home() / 'service.osz')1091 # THEN: The service should be loaded1092 service_manager.decide_save_method.assert_called_once_with()1093 mocked_settings.setValue.assert_called_once_with('servicemanager/last directory', Path.home())1094 service_manager.load_file.assert_called_once_with(Path.home() / 'service.osz')1095@patch('openlp.core.ui.servicemanager.Path', autospec=True)1096def test_service_manager_load_file_str(MockPath, registry):1097 """Test the service manager's load_file method when it is given a str"""1098 # GIVEN: A service manager and a mocked path object1099 service_manager = ServiceManager(None)1100 MockPath.__class__ = Path.__class__1101 MockPath.return_value.exists.return_value = False1102 # WHEN: A str filename is passed to load_file1103 result = service_manager.load_file('service.osz')1104 # THEN: False should be returned, but a valid Path object created1105 assert result is False, 'ServiceManager.load_file should return false for a non-existant file'1106 MockPath.assert_called_once_with('service.osz')1107 MockPath.return_value.exists.assert_called_once()1108class TestServiceManager(TestCase, TestMixin):1109 """1110 Test the service manager1111 """1112 def _create_mock_action(self, name, **kwargs):1113 """1114 Create a fake action with some "real" attributes1115 """1116 action = QtWidgets.QAction(self.service_manager)1117 action.setObjectName(name)1118 if kwargs.get('triggers'):1119 action.triggered.connect(kwargs.pop('triggers'))1120 self.service_manager.toolbar.actions[name] = action1121 return action1122 def setUp(self):1123 """1124 Create the UI1125 """1126 Registry.create()1127 self.setup_application()1128 Registry().register('application', MagicMock())1129 Registry().register('main_window', MagicMock())1130 Registry().register('settings', Settings())1131 self.service_manager = ServiceManager()1132 self.add_toolbar_action_patcher = patch('openlp.core.ui.servicemanager.OpenLPToolbar.add_toolbar_action')1133 self.mocked_add_toolbar_action = self.add_toolbar_action_patcher.start()1134 self.mocked_add_toolbar_action.side_effect = self._create_mock_action1135 def tearDown(self):1136 """1137 Delete all the C++ objects at the end so that we don't have a segfault1138 """1139 self.add_toolbar_action_patcher.stop()1140 del self.service_manager1141 def test_bootstrap_initialise(self):1142 """1143 Test the bootstrap_initialise method1144 """1145 # GIVEN: Mocked out stuff1146 self.service_manager.setup_ui = MagicMock()1147 self.service_manager.servicemanager_set_item = MagicMock()1148 self.service_manager.servicemanager_set_item_by_uuid = MagicMock()1149 self.service_manager.servicemanager_next_item = MagicMock()1150 self.service_manager.servicemanager_previous_item = MagicMock()1151 self.service_manager.servicemanager_new_file = MagicMock()1152 self.service_manager.theme_update_service = MagicMock()1153 # WHEN: bootstrap_initialise is called1154 self.service_manager.bootstrap_initialise()1155 # THEN: The correct calls should have been made1156 self.service_manager.setup_ui.assert_called_once_with(self.service_manager)1157 self.service_manager.servicemanager_set_item.connect.assert_called_once_with(1158 self.service_manager.on_set_item)1159 self.service_manager.servicemanager_set_item_by_uuid.connect.assert_called_once_with(1160 self.service_manager.set_item_by_uuid)1161 self.service_manager.servicemanager_next_item.connect.assert_called_once_with(1162 self.service_manager.next_item)1163 self.service_manager.servicemanager_previous_item.connect.assert_called_once_with(1164 self.service_manager.previous_item)1165 self.service_manager.servicemanager_new_file.connect.assert_called_once_with(1166 self.service_manager.new_file)1167 self.service_manager.theme_update_service.connect.assert_called_once_with(1168 self.service_manager.on_service_theme_change)1169 @patch('openlp.core.ui.servicemanager.ServiceNoteForm')1170 @patch('openlp.core.ui.servicemanager.ServiceItemEditForm')1171 @patch('openlp.core.ui.servicemanager.StartTimeForm')1172 def test_bootstrap_post_set_up(self, MockStartTimeForm, MockServiceItemEditForm, MockServiceNoteForm):1173 """1174 Test the post bootstrap setup1175 """1176 # GIVEN: Mocked forms and a ServiceManager object1177 mocked_service_note_form = MagicMock()1178 MockServiceNoteForm.return_value = mocked_service_note_form1179 mocked_service_item_edit_form = MagicMock()1180 MockServiceItemEditForm.return_value = mocked_service_item_edit_form1181 mocked_start_time_form = MagicMock()1182 MockStartTimeForm.return_value = mocked_start_time_form1183 # WHEN: bootstrap_post_set_up is run1184 self.service_manager.bootstrap_post_set_up()1185 # THEN: The forms should have been created1186 assert self.service_manager.service_note_form == mocked_service_note_form1187 assert self.service_manager.service_item_edit_form == mocked_service_item_edit_form1188 assert self.service_manager.start_time_form == mocked_start_time_form1189 def test_set_file_name_osz(self):1190 """1191 Test setting the file name1192 """1193 # GIVEN: A service manager, some mocks and a file name1194 self.service_manager.set_modified = MagicMock()1195 self.service_manager.settings.setValue = MagicMock()1196 file_path = Path('servicefile.osz')1197 # WHEN: The filename is set1198 self.service_manager.set_file_name(file_path)1199 # THEN: Various things should have been called and set1200 assert self.service_manager._service_path == file_path1201 self.service_manager.set_modified.assert_called_once_with(False)1202 self.service_manager.settings.setValue.assert_called_once_with('servicemanager/last file', file_path)1203 assert self.service_manager._save_lite is False1204 def test_set_file_name_oszl(self):1205 """1206 Test setting the file name1207 """1208 # GIVEN: A service manager, some mocks and a file name1209 self.service_manager.set_modified = MagicMock()1210 self.service_manager.settings.setValue = MagicMock()1211 file_path = Path('servicefile.oszl')1212 # WHEN: The filename is set1213 self.service_manager.set_file_name(file_path)1214 # THEN: Various things should have been called and set1215 assert self.service_manager._service_path == file_path1216 self.service_manager.set_modified.assert_called_once_with(False)1217 self.service_manager.settings.setValue.assert_called_once_with('servicemanager/last file', file_path)1218 assert self.service_manager._save_lite is True1219 def test_short_file_name(self):1220 """1221 Test the short_file_name method1222 """1223 # GIVEN: A service manager and a file name1224 self.service_manager._service_path = Path('/home/user/service.osz')1225 # WHEN: short_file_name is called1226 result = self.service_manager.short_file_name()1227 # THEN: The result should be correct1228 assert result == 'service.osz'1229 def test_basic_service_manager(self):1230 """1231 Test the Service Manager UI Functionality1232 """1233 # GIVEN: A New Service Manager instance1234 # WHEN I have set up the display1235 self.service_manager.setup_ui(self.service_manager)1236 # THEN the count of items should be zero1237 assert self.service_manager.service_manager_list.topLevelItemCount() == 0, \1238 'The service manager list should be empty '1239 @patch('openlp.core.ui.servicemanager.QtWidgets.QTreeWidget.itemAt')1240 @patch('openlp.core.ui.servicemanager.QtWidgets.QWidget.mapToGlobal')1241 @patch('openlp.core.ui.servicemanager.QtWidgets.QMenu.exec')1242 def test_default_context_menu(self, mocked_exec, mocked_mapToGlobal, mocked_item_at_method):1243 """...
cls_api_client.py
Source:cls_api_client.py
1"""2* *******************************************************3* Copyright VMware, Inc. 2016-2018. All Rights Reserved.4* SPDX-License-Identifier: MIT5* *******************************************************6*7* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT8* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,9* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED10* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,11* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.12"""13__author__ = 'VMware, Inc.'14__vcenter_version__ = '6.0+'15from com.vmware.content_client import (Library,16 LocalLibrary,17 SubscribedLibrary)18from com.vmware.content.library_client import Item, SubscribedItem19from com.vmware.content.library.item_client import DownloadSession20from com.vmware.content.library.item_client import UpdateSession21from com.vmware.content.library.item.downloadsession_client import File as DownloadSessionFile22from com.vmware.content.library.item.updatesession_client import File as UpdateSessionFile23from com.vmware.vcenter_client import VM24from com.vmware.vcenter.iso_client import Image25from com.vmware.vcenter.ovf_client import LibraryItem26from com.vmware.vcenter.vm_template_client import LibraryItems as VmtxLibraryItem27class ClsApiClient(object):28 """29 This is a simplified wrapper around the Content Library APIs.30 It is used to access services exposed by Content Library Service.31 """32 def __init__(self, service_manager):33 # Client for all the services on a management node.34 self.service_manager = service_manager35 # Returns the service which provides support for generic functionality36 # which can be applied equally to all types of libraries37 self.library_service = Library(self.service_manager.stub_config)38 # Returns the service for managing local libraries39 self.local_library_service = LocalLibrary(self.service_manager.stub_config)40 # Returns the service for managing subscribed libraries41 self.subscribed_library_service = SubscribedLibrary(self.service_manager.stub_config)42 # Returns the service for managing library items43 self.library_item_service = Item(self.service_manager.stub_config)44 # Returns the service for managing sessions to update or delete content45 self.upload_service = UpdateSession(self.service_manager.stub_config)46 # Returns the service for managing files within an update session47 self.upload_file_service = UpdateSessionFile(self.service_manager.stub_config)48 # Returns the service for managing sessions to download content49 self.download_service = DownloadSession(self.service_manager.stub_config)50 # Returns the service for managing files within a download session51 self.download_file_service = DownloadSessionFile(self.service_manager.stub_config)52 # Returns the service for deploying virtual machines from OVF library items53 self.ovf_lib_item_service = LibraryItem(self.service_manager.stub_config)54 # Returns the service for mount and unmount of an iso file on a VM55 self.iso_service = Image(self.service_manager.stub_config)56 # Returns the service for managing subscribed library items57 self.subscribed_item_service = SubscribedItem(self.service_manager.stub_config)58 # Returns the service for managing library items containing virtual59 # machine templates60 self.vmtx_service = VmtxLibraryItem(self.service_manager.stub_config)61 # Creates the service that communicates with virtual machines62 self.vm_service = VM(self.service_manager.stub_config)...
service_manager_factory.py
Source:service_manager_factory.py
...18 Factory class for getting service manager for a management node.19 """20 service_manager = None21 @classmethod22 def get_service_manager(cls, server, username, password, skip_verification):23 cls.service_manager = ServiceManager(server,24 username,25 password,26 skip_verification)27 cls.service_manager.connect()28 return cls.service_manager29 @classmethod30 def disconnect(cls):31 if cls.service_manager:32 cls.service_manager.disconnect()33import atexit...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!