Beispiel #1
0
 def setUp(self):
     """
     Create the UI
     """
     self.build_settings()
     self.setup_application()
     Registry.create()
     self.theme_manager = ThemeManager()
 def setUp(self):
     """
     Create the UI
     """
     Registry.create()
     self.setup_application()
     self.main_window = QtWidgets.QMainWindow()
     Registry().register('main_window', self.main_window)
     self.form = SelectPlanForm()
     self.form.planning_center_api.airplane_mode = True
     self.form.planning_center_api.airplane_mode_directory = TEST_PATH
     theme_manager = ThemeManager(None)
     theme_manager.get_themes = MagicMock()
     theme_manager.get_themes.return_value = ['themeA','themeB']
Beispiel #3
0
    def write_theme_same_image_test(self):
        """
        Test that we don't try to overwrite a theme background image with itself
        """
        # GIVEN: A new theme manager instance, with mocked builtins.open, shutil.copyfile,
        #        theme, check_directory_exists and thememanager-attributes.
        with patch('builtins.open') as mocked_open, \
                patch('openlp.core.ui.thememanager.shutil.copyfile') as mocked_copyfile, \
                patch('openlp.core.ui.thememanager.check_directory_exists') as mocked_check_directory_exists:
            mocked_open.return_value = MagicMock()
            theme_manager = ThemeManager(None)
            theme_manager.old_background_image = None
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.path = ''
            mocked_theme = MagicMock()
            mocked_theme.theme_name = 'themename'
            mocked_theme.extract_formatted_xml = MagicMock()
            mocked_theme.extract_formatted_xml.return_value = 'fake_theme_xml'.encode(
            )

            # WHEN: Calling _write_theme with path to the same image, but the path written slightly different
            file_name1 = os.path.join(TEST_RESOURCES_PATH, 'church.jpg')
            # Do replacement from end of string to avoid problems with path start
            file_name2 = file_name1[::-1].replace(os.sep, os.sep + os.sep,
                                                  2)[::-1]
            theme_manager._write_theme(mocked_theme, file_name1, file_name2)

            # THEN: The mocked_copyfile should not have been called
            self.assertFalse(mocked_copyfile.called,
                             'shutil.copyfile should not be called')
Beispiel #4
0
    def unzip_theme_test(self):
        """
        Test that unzipping of themes works
        """
        # GIVEN: A theme file, a output folder and some mocked out internal functions
        with patch('openlp.core.ui.thememanager.critical_error_message_box') \
                as mocked_critical_error_message_box:
            theme_manager = ThemeManager(None)
            theme_manager._create_theme_from_xml = MagicMock()
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.path = ''
            folder = mkdtemp()
            theme_file = os.path.join(TEST_RESOURCES_PATH, 'themes',
                                      'Moss_on_tree.otz')

            # WHEN: We try to unzip it
            theme_manager.unzip_theme(theme_file, folder)

            # THEN: Files should be unpacked
            self.assertTrue(
                os.path.exists(
                    os.path.join(folder, 'Moss on tree', 'Moss on tree.xml')))
            self.assertEqual(mocked_critical_error_message_box.call_count, 0,
                             'No errors should have happened')
            shutil.rmtree(folder)
Beispiel #5
0
    def write_theme_diff_images_test(self):
        """
        Test that we do overwrite a theme background image when a new is submitted
        """
        # GIVEN: A new theme manager instance, with mocked builtins.open, shutil.copyfile,
        #        theme, check_directory_exists and thememanager-attributes.
        with patch('builtins.open') as mocked_open, \
                patch('openlp.core.ui.thememanager.shutil.copyfile') as mocked_copyfile, \
                patch('openlp.core.ui.thememanager.check_directory_exists') as mocked_check_directory_exists:
            mocked_open.return_value = MagicMock()
            theme_manager = ThemeManager(None)
            theme_manager.old_background_image = None
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.path = ''
            mocked_theme = MagicMock()
            mocked_theme.theme_name = 'themename'
            mocked_theme.extract_formatted_xml = MagicMock()
            mocked_theme.extract_formatted_xml.return_value = 'fake_theme_xml'.encode(
            )

            # WHEN: Calling _write_theme with path to different images
            file_name1 = os.path.join(TEST_RESOURCES_PATH, 'church.jpg')
            file_name2 = os.path.join(TEST_RESOURCES_PATH, 'church2.jpg')
            theme_manager._write_theme(mocked_theme, file_name1, file_name2)

            # THEN: The mocked_copyfile should not have been called
            self.assertTrue(mocked_copyfile.called,
                            'shutil.copyfile should be called')
Beispiel #6
0
    def test_export_theme(self, mocked_zipfile_write, mocked_zipfile_init):
        """
        Test exporting a theme .
        """
        # GIVEN: A new ThemeManager instance.
        theme_manager = ThemeManager()
        theme_manager.theme_path = RESOURCE_PATH / 'themes'
        mocked_zipfile_init.return_value = None

        # WHEN: The theme is exported
        theme_manager._export_theme(Path('some', 'path', 'Default.otz'),
                                    'Default')

        # THEN: The zipfile should be created at the given path
        mocked_zipfile_init.assert_called_with(
            os.path.join('some', 'path', 'Default.otz'), 'w')
        mocked_zipfile_write.assert_called_with(
            str(RESOURCE_PATH / 'themes' / 'Default' / 'Default.xml'),
            os.path.join('Default', 'Default.xml'))
Beispiel #7
0
    def test_unzip_theme_invalid_version(self):
        """
        Test that themes with invalid (< 2.0) or with no version attributes are rejected
        """
        # GIVEN: An instance of ThemeManager whilst mocking a theme that returns a theme with no version attribute
        with patch('openlp.core.ui.thememanager.zipfile.ZipFile') as mocked_zip_file,\
                patch('openlp.core.ui.thememanager.ElementTree.getroot') as mocked_getroot,\
                patch('openlp.core.ui.thememanager.XML'),\
                patch('openlp.core.ui.thememanager.critical_error_message_box') as mocked_critical_error_message_box:

            mocked_zip_file.return_value = MagicMock(**{'namelist.return_value': [os.path.join('theme', 'theme.xml')]})
            mocked_getroot.return_value = MagicMock(**{'get.return_value': None})
            theme_manager = ThemeManager(None)

            # WHEN: unzip_theme is called
            theme_manager.unzip_theme('theme.file', 'folder')

            # THEN: The critical_error_message_box should have been called
            self.assertEqual(mocked_critical_error_message_box.call_count, 1, 'Should have been called once')
Beispiel #8
0
    def test_over_write_message_box_no(self):
        """
        Test that theme_manager.over_write_message_box returns False when the user clicks no.
        """
        # GIVEN: A patched QMessageBox.question and an instance of ThemeManager
        with patch('openlp.core.ui.thememanager.QtWidgets.QMessageBox.question', return_value=QtWidgets.QMessageBox.No)\
                as mocked_qmessagebox_question,\
                patch('openlp.core.ui.thememanager.translate') as mocked_translate:
            mocked_translate.side_effect = lambda context, text: text
            theme_manager = ThemeManager(None)

            # WHEN: Calling over_write_message_box with 'Theme Name'
            result = theme_manager.over_write_message_box('Theme Name')

            # THEN: over_write_message_box should return False and the message box should contain the theme name
            self.assertFalse(result)
            mocked_qmessagebox_question.assert_called_once_with(
                theme_manager, 'Theme Already Exists', 'Theme Theme Name already exists. Do you want to replace it?',
                ANY, ANY)
Beispiel #9
0
    def test_export_theme(self):
        """
        Test exporting a theme .
        """
        # GIVEN: A new ThemeManager instance.
        theme_manager = ThemeManager()
        theme_manager.path = os.path.join(TEST_RESOURCES_PATH, 'themes')
        with patch('zipfile.ZipFile.__init__') as mocked_zipfile_init, \
                patch('zipfile.ZipFile.write') as mocked_zipfile_write:
            mocked_zipfile_init.return_value = None

            # WHEN: The theme is exported
            theme_manager._export_theme(os.path.join('some', 'path'), 'Default')

            # THEN: The zipfile should be created at the given path
            mocked_zipfile_init.assert_called_with(os.path.join('some', 'path', 'Default.otz'), 'w')
            mocked_zipfile_write.assert_called_with(os.path.join(TEST_RESOURCES_PATH, 'themes',
                                                                 'Default', 'Default.xml'),
                                                    os.path.join('Default', 'Default.xml'))
Beispiel #10
0
    def test_over_write_message_box_no(self, mocked_translate,
                                       mocked_qmessagebox_question):
        """
        Test that theme_manager.over_write_message_box returns False when the user clicks no.
        """
        # GIVEN: A patched QMessageBox.question and an instance of ThemeManager
        mocked_translate.side_effect = lambda context, text: text
        theme_manager = ThemeManager(None)

        # WHEN: Calling over_write_message_box with 'Theme Name'
        result = theme_manager.over_write_message_box('Theme Name')

        # THEN: over_write_message_box should return False and the message box should contain the theme name
        assert result is False
        mocked_qmessagebox_question.assert_called_once_with(
            theme_manager,
            'Theme Already Exists',
            'Theme Theme Name already exists. Do you want to replace it?',
            defaultButton=ANY)
Beispiel #11
0
    def over_write_message_box_no_test(self):
        """
        Test that theme_manager.over_write_message_box returns False when the user clicks no.
        """
        # GIVEN: A patched QMessageBox.question and an instance of ThemeManager
        with patch('openlp.core.ui.thememanager.QtGui.QMessageBox.question', return_value=QtGui.QMessageBox.No)\
                as mocked_qmessagebox_question,\
                patch('openlp.core.ui.thememanager.translate') as mocked_translate:
            mocked_translate.side_effect = lambda context, text: text
            theme_manager = ThemeManager(None)

            # WHEN: Calling over_write_message_box with 'Theme Name'
            result = theme_manager.over_write_message_box('Theme Name')

            # THEN: over_write_message_box should return False and the message box should contain the theme name
            self.assertFalse(result)
            mocked_qmessagebox_question.assert_called_once_with(
                theme_manager, 'Theme Already Exists',
                'Theme Theme Name already exists. Do you want to replace it?',
                ANY, ANY)
Beispiel #12
0
    def initial_theme_manager_test(self):
        """
        Test the instantiation of theme manager.
        """
        # GIVEN: A new service manager instance.
        ThemeManager(None)

        # WHEN: the default theme manager is built.
        # THEN: The the controller should be registered in the registry.
        self.assertIsNotNone(Registry().get('theme_manager'),
                             'The base theme manager should be registered')
Beispiel #13
0
    def test_write_theme_diff_images(self):
        """
        Test that we do overwrite a theme background image when a new is submitted
        """
        # GIVEN: A new theme manager instance, with mocked builtins.open, shutil.copyfile,
        #        theme, check_directory_exists and thememanager-attributes.
        with patch('builtins.open') as mocked_open, \
                patch('openlp.core.ui.thememanager.shutil.copyfile') as mocked_copyfile, \
                patch('openlp.core.ui.thememanager.check_directory_exists') as mocked_check_directory_exists:
            mocked_open.return_value = MagicMock()
            theme_manager = ThemeManager(None)
            theme_manager.old_background_image = None
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.path = ''
            mocked_theme = MagicMock()
            mocked_theme.theme_name = 'themename'
            mocked_theme.extract_formatted_xml = MagicMock()
            mocked_theme.extract_formatted_xml.return_value = 'fake_theme_xml'.encode()

            # WHEN: Calling _write_theme with path to different images
            file_name1 = os.path.join(TEST_RESOURCES_PATH, 'church.jpg')
            file_name2 = os.path.join(TEST_RESOURCES_PATH, 'church2.jpg')
            theme_manager._write_theme(mocked_theme, file_name1, file_name2)

            # THEN: The mocked_copyfile should not have been called
            self.assertTrue(mocked_copyfile.called, 'shutil.copyfile should be called')
Beispiel #14
0
    def test_write_theme_same_image(self):
        """
        Test that we don't try to overwrite a theme background image with itself
        """
        # GIVEN: A new theme manager instance, with mocked builtins.open, shutil.copyfile,
        #        theme, check_directory_exists and thememanager-attributes.
        with patch('builtins.open') as mocked_open, \
                patch('openlp.core.ui.thememanager.shutil.copyfile') as mocked_copyfile, \
                patch('openlp.core.ui.thememanager.check_directory_exists') as mocked_check_directory_exists:
            mocked_open.return_value = MagicMock()
            theme_manager = ThemeManager(None)
            theme_manager.old_background_image = None
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.path = ''
            mocked_theme = MagicMock()
            mocked_theme.theme_name = 'themename'
            mocked_theme.extract_formatted_xml = MagicMock()
            mocked_theme.extract_formatted_xml.return_value = 'fake_theme_xml'.encode()

            # WHEN: Calling _write_theme with path to the same image, but the path written slightly different
            file_name1 = os.path.join(TEST_RESOURCES_PATH, 'church.jpg')
            # Do replacement from end of string to avoid problems with path start
            file_name2 = file_name1[::-1].replace(os.sep, os.sep + os.sep, 2)[::-1]
            theme_manager._write_theme(mocked_theme, file_name1, file_name2)

            # THEN: The mocked_copyfile should not have been called
            self.assertFalse(mocked_copyfile.called, 'shutil.copyfile should not be called')
Beispiel #15
0
    def unzip_theme_invalid_version_test(self):
        """
        Test that themes with invalid (< 2.0) or with no version attributes are rejected
        """
        # GIVEN: An instance of ThemeManager whilst mocking a theme that returns a theme with no version attribute
        with patch('openlp.core.ui.thememanager.zipfile.ZipFile') as mocked_zip_file,\
                patch('openlp.core.ui.thememanager.ElementTree.getroot') as mocked_getroot,\
                patch('openlp.core.ui.thememanager.XML'),\
                patch('openlp.core.ui.thememanager.critical_error_message_box') as mocked_critical_error_message_box:

            mocked_zip_file.return_value = MagicMock(**{
                'namelist.return_value': [os.path.join('theme', 'theme.xml')]
            })
            mocked_getroot.return_value = MagicMock(
                **{'get.return_value': None})
            theme_manager = ThemeManager(None)

            # WHEN: unzip_theme is called
            theme_manager.unzip_theme('theme.file', 'folder')

            # THEN: The critical_error_message_box should have been called
            mocked_critical_error_message_box.assert_called_once(ANY, ANY)
Beispiel #16
0
    def export_theme_test(self):
        """
        Test exporting a theme .
        """
        # GIVEN: A new ThemeManager instance.
        theme_manager = ThemeManager()
        theme_manager.path = os.path.join(TEST_RESOURCES_PATH, 'themes')
        with patch('zipfile.ZipFile.__init__') as mocked_zipfile_init, \
                patch('zipfile.ZipFile.write') as mocked_zipfile_write:
            mocked_zipfile_init.return_value = None

            # WHEN: The theme is exported
            theme_manager._export_theme(os.path.join('some', 'path'),
                                        'Default')

            # THEN: The zipfile should be created at the given path
            mocked_zipfile_init.assert_called_with(
                os.path.join('some', 'path', 'Default.otz'), 'w')
            mocked_zipfile_write.assert_called_with(
                os.path.join(TEST_RESOURCES_PATH, 'themes', 'Default',
                             'Default.xml'),
                os.path.join('Default', 'Default.xml'))
Beispiel #17
0
    def test_write_theme_special_char_name(self):
        """
        Test that we can save themes with special characters in the name
        """
        # GIVEN: A new theme manager instance, with mocked theme and thememanager-attributes.
        theme_manager = ThemeManager(None)
        theme_manager.old_background_image = None
        theme_manager.generate_and_save_image = MagicMock()
        theme_manager.theme_path = Path(self.temp_folder)
        mocked_theme = MagicMock()
        mocked_theme.theme_name = 'theme 愛 name'
        mocked_theme.export_theme.return_value = "{}"

        # WHEN: Calling _write_theme with a theme with a name with special characters in it
        theme_manager._write_theme(mocked_theme, None, None)

        # THEN: It should have been created
        assert os.path.exists(os.path.join(self.temp_folder, 'theme 愛 name', 'theme 愛 name.json')) is True, \
            'Theme with special characters should have been created!'
Beispiel #18
0
    def test_write_theme_diff_images(self, mocked_create_paths,
                                     mocked_copyfile):
        """
        Test that we do overwrite a theme background image when a new is submitted
        """
        # GIVEN: A new theme manager instance, with mocked builtins.open, copyfile,
        #        theme, create_paths and thememanager-attributes.
        theme_manager = ThemeManager(None)
        theme_manager.old_background_image = None
        theme_manager.generate_and_save_image = MagicMock()
        theme_manager.theme_path = MagicMock()
        mocked_theme = MagicMock()
        mocked_theme.theme_name = 'themename'
        mocked_theme.filename = "filename"

        # WHEN: Calling _write_theme with path to different images
        file_path_1 = RESOURCE_PATH / 'church.jpg'
        file_path_2 = RESOURCE_PATH / 'church2.jpg'
        theme_manager._write_theme(mocked_theme, file_path_1, file_path_2)

        # THEN: The mocked_copyfile should not have been called
        assert mocked_copyfile.called is True, 'copyfile should be called'
Beispiel #19
0
    def test_unzip_theme(self):
        """
        Test that unzipping of themes works
        """
        # GIVEN: A theme file, a output folder and some mocked out internal functions
        with patch('openlp.core.ui.thememanager.critical_error_message_box') \
                as mocked_critical_error_message_box:
            theme_manager = ThemeManager(None)
            theme_manager._create_theme_from_xml = MagicMock()
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.theme_path = None
            folder_path = Path(mkdtemp())
            theme_file_path = RESOURCE_PATH / 'themes' / 'Moss_on_tree.otz'

            # WHEN: We try to unzip it
            theme_manager.unzip_theme(theme_file_path, folder_path)

            # THEN: Files should be unpacked
            assert (folder_path / 'Moss on tree' /
                    'Moss on tree.xml').exists() is True
            assert mocked_critical_error_message_box.call_count == 0, 'No errors should have happened'
            folder_path.rmtree()
Beispiel #20
0
    def test_write_theme_same_image(self, mocked_create_paths,
                                    mocked_copyfile):
        """
        Test that we don't try to overwrite a theme background image with itself
        """
        # GIVEN: A new theme manager instance, with mocked builtins.open, copyfile,
        #        theme, create_paths and thememanager-attributes.
        theme_manager = ThemeManager(None)
        theme_manager.old_background_image = None
        theme_manager.generate_and_save_image = MagicMock()
        theme_manager.theme_path = MagicMock()
        mocked_theme = MagicMock()
        mocked_theme.theme_name = 'themename'
        mocked_theme.extract_formatted_xml = MagicMock()
        mocked_theme.extract_formatted_xml.return_value = 'fake_theme_xml'.encode(
        )

        # WHEN: Calling _write_theme with path to the same image, but the path written slightly different
        file_path_1 = RESOURCE_PATH / 'church.jpg'
        theme_manager._write_theme(mocked_theme, file_path_1, file_path_1)

        # THEN: The mocked_copyfile should not have been called
        assert mocked_copyfile.called is False, 'copyfile should not be called'
Beispiel #21
0
    def test_write_theme_special_char_name(self):
        """
        Test that we can save themes with special characters in the name
        """
        # GIVEN: A new theme manager instance, with mocked theme and thememanager-attributes.
        theme_manager = ThemeManager(None)
        theme_manager.old_background_image = None
        theme_manager.generate_and_save_image = MagicMock()
        theme_manager.path = self.temp_folder
        mocked_theme = MagicMock()
        mocked_theme.theme_name = 'theme 愛 name'
        mocked_theme.extract_formatted_xml = MagicMock()
        mocked_theme.extract_formatted_xml.return_value = 'fake theme 愛 XML'.encode()

        # WHEN: Calling _write_theme with a theme with a name with special characters in it
        theme_manager._write_theme(mocked_theme, None, None)

        # THEN: It should have been created
        self.assertTrue(os.path.exists(os.path.join(self.temp_folder, 'theme 愛 name', 'theme 愛 name.xml')),
                        'Theme with special characters should have been created!')
Beispiel #22
0
    def write_theme_special_char_name_test(self):
        """
        Test that we can save themes with special characters in the name
        """
        # GIVEN: A new theme manager instance, with mocked theme and thememanager-attributes.
        theme_manager = ThemeManager(None)
        theme_manager.old_background_image = None
        theme_manager.generate_and_save_image = MagicMock()
        theme_manager.path = self.temp_folder
        mocked_theme = MagicMock()
        mocked_theme.theme_name = 'theme 愛 name'
        mocked_theme.extract_formatted_xml = MagicMock()
        mocked_theme.extract_formatted_xml.return_value = 'fake theme 愛 XML'.encode(
        )

        # WHEN: Calling _write_theme with a theme with a name with special characters in it
        theme_manager._write_theme(mocked_theme, None, None)

        # THEN: It should have been created
        self.assertTrue(
            os.path.exists(
                os.path.join(self.temp_folder, 'theme 愛 name',
                             'theme 愛 name.xml')),
            'Theme with special characters should have been created!')
Beispiel #23
0
    def test_unzip_theme(self):
        """
        Test that unzipping of themes works
        """
        # GIVEN: A theme file, a output folder and some mocked out internal functions
        with patch('openlp.core.ui.thememanager.critical_error_message_box') \
                as mocked_critical_error_message_box:
            theme_manager = ThemeManager(None)
            theme_manager._create_theme_from_xml = MagicMock()
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.path = ''
            folder = mkdtemp()
            theme_file = os.path.join(TEST_RESOURCES_PATH, 'themes', 'Moss_on_tree.otz')

            # WHEN: We try to unzip it
            theme_manager.unzip_theme(theme_file, folder)

            # THEN: Files should be unpacked
            self.assertTrue(os.path.exists(os.path.join(folder, 'Moss on tree', 'Moss on tree.xml')))
            self.assertEqual(mocked_critical_error_message_box.call_count, 0, 'No errors should have happened')
            shutil.rmtree(folder)
Beispiel #24
0
class TestThemeManager(TestCase, TestMixin):
    """
    Test the functions in the ThemeManager module
    """
    def setUp(self):
        """
        Create the UI
        """
        self.build_settings()
        self.setup_application()
        Registry.create()
        self.theme_manager = ThemeManager()

    def tearDown(self):
        """
        Delete all the C++ objects at the end so that we don't have a segfault
        """
        self.destroy_settings()
        del self.theme_manager

    def initialise_test(self):
        """
        Test the thememanager initialise - basic test
        """
        # GIVEN: A new a call to initialise
        self.theme_manager.build_theme_path = MagicMock()
        self.theme_manager.load_first_time_themes = MagicMock()
        Settings().setValue('themes/global theme', 'my_theme')

        # WHEN: the initialisation is run
        self.theme_manager.bootstrap_initialise()

        # THEN:
        self.assertEqual(1, self.theme_manager.build_theme_path.call_count,
                         'The function build_theme_path should have been called')
        self.assertEqual(1, self.theme_manager.load_first_time_themes.call_count,
                         'The function load_first_time_themes should have been called only once')
        self.assertEqual(self.theme_manager.global_theme, 'my_theme',
                         'The global theme should have been set to my_theme')

    def build_theme_path_test(self):
        """
        Test the thememanager build_theme_path - basic test
        """
        # GIVEN: A new a call to initialise
        with patch('openlp.core.common.applocation.check_directory_exists') as mocked_check_directory_exists:
            # GIVEN: A mocked out Settings class and a mocked out AppLocation.get_directory()
            mocked_check_directory_exists.return_value = True
        Settings().setValue('themes/global theme', 'my_theme')

        self.theme_manager.theme_form = MagicMock()
        self.theme_manager.load_first_time_themes = MagicMock()

        # WHEN: the build_theme_path is run
        self.theme_manager.build_theme_path()

        #  THEN:
        assert self.theme_manager.thumb_path.startswith(self.theme_manager.path) is True, \
            'The thumb path and the main path should start with the same value'

    def click_on_new_theme_test(self):
        """
        Test the on_add_theme event handler is called by the UI
        """
        # GIVEN: An initial form
        Settings().setValue('themes/global theme', 'my_theme')
        mocked_event = MagicMock()
        self.theme_manager.on_add_theme = mocked_event
        self.theme_manager.setup_ui(self.theme_manager)

        # WHEN displaying the UI and pressing cancel
        new_theme = self.theme_manager.toolbar.actions['newTheme']
        new_theme.trigger()

        assert mocked_event.call_count == 1, 'The on_add_theme method should have been called once'

    @patch('openlp.core.ui.themeform.ThemeForm._setup')
    @patch('openlp.core.ui.filerenameform.FileRenameForm._setup')
    def bootstrap_post_test(self, mocked_theme_form, mocked_rename_form):
        """
        Test the functions of bootstrap_post_setup are called.
        """
        # GIVEN:
        self.theme_manager.load_themes = MagicMock()
        self.theme_manager.path = MagicMock()

        # WHEN:
        self.theme_manager.bootstrap_post_set_up()

        # THEN:
        self.assertEqual(self.theme_manager.path, self.theme_manager.theme_form.path)
        self.assertEqual(1, self.theme_manager.load_themes.call_count, "load_themes should have been called once")
Beispiel #25
0
class TestThemeManager(TestCase, TestMixin):
    """
    Test the functions in the ThemeManager module
    """
    def setUp(self):
        """
        Create the UI
        """
        self.setup_application()
        self.build_settings()
        Registry.create()
        self.theme_manager = ThemeManager()

    def tearDown(self):
        """
        Delete all the C++ objects at the end so that we don't have a segfault
        """
        self.destroy_settings()
        del self.theme_manager

    def test_initialise(self):
        """
        Test the thememanager initialise - basic test
        """
        # GIVEN: A new a call to initialise
        self.theme_manager.setup_ui = MagicMock()
        self.theme_manager.build_theme_path = MagicMock()
        self.theme_manager.load_first_time_themes = MagicMock()
        self.theme_manager.upgrade_themes = MagicMock()
        Settings().setValue('themes/global theme', 'my_theme')

        # WHEN: the initialisation is run
        self.theme_manager.bootstrap_initialise()

        # THEN:
        self.theme_manager.setup_ui.assert_called_once_with(self.theme_manager)
        assert self.theme_manager.global_theme == 'my_theme'
        self.theme_manager.build_theme_path.assert_called_once_with()
        self.theme_manager.load_first_time_themes.assert_called_once_with()
        self.theme_manager.upgrade_themes.assert_called_once_with()

    @patch('openlp.core.ui.thememanager.create_paths')
    @patch('openlp.core.ui.thememanager.AppLocation.get_section_data_path')
    def test_build_theme_path(self, mocked_get_section_data_path,
                              mocked_create_paths):
        """
        Test the thememanager build_theme_path
        """
        # GIVEN: A mocked out AppLocation.get_directory() and mocked create_paths
        mocked_get_section_data_path.return_value = Path('tests/my_theme')

        # WHEN: the build_theme_path is run
        self.theme_manager.build_theme_path()

        #  THEN: The theme path and the thumb path should be correct
        assert self.theme_manager.theme_path == Path('tests/my_theme')
        assert self.theme_manager.thumb_path == Path(
            'tests/my_theme/thumbnails')
        mocked_create_paths.assert_called_once_with(
            Path('tests/my_theme'), Path('tests/my_theme/thumbnails'))

    def test_click_on_new_theme(self):
        """
        Test the on_add_theme event handler is called by the UI
        """
        # GIVEN: An initial form
        Settings().setValue('themes/global theme', 'my_theme')
        mocked_event = MagicMock()
        self.theme_manager.on_add_theme = mocked_event
        self.theme_manager.setup_ui(self.theme_manager)

        # WHEN displaying the UI and pressing cancel
        new_theme = self.theme_manager.toolbar.actions['newTheme']
        new_theme.trigger()

        assert mocked_event.call_count == 1, 'The on_add_theme method should have been called once'

    @patch('openlp.core.ui.themeform.ThemeForm._setup')
    @patch('openlp.core.ui.filerenameform.FileRenameForm._setup')
    def test_bootstrap_post(self, mocked_rename_form, mocked_theme_form):
        """
        Test the functions of bootstrap_post_setup are called.
        """
        # GIVEN:
        self.theme_manager.load_themes = MagicMock()
        self.theme_manager.theme_path = MagicMock()

        # WHEN:
        self.theme_manager.bootstrap_post_set_up()

        # THEN:
        assert 1 == self.theme_manager.load_themes.call_count, "load_themes should have been called once"