openlp/tests/functional/openlp_core_lib/test_renderer.py

208 lines
9.0 KiB
Python
Raw Normal View History

2014-01-11 19:31:06 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2016-12-31 11:01:36 +00:00
# Copyright (c) 2008-2017 OpenLP Developers #
2014-01-11 19:31:06 +00:00
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
Package to test the openlp.core.ui.renderer package.
"""
from unittest import TestCase
from unittest.mock import MagicMock, patch
2014-01-11 19:31:06 +00:00
2015-11-07 00:49:40 +00:00
from PyQt5 import QtCore
2014-01-11 19:31:06 +00:00
from openlp.core.common import Registry
from openlp.core.lib import Renderer, ScreenList, ServiceItem, FormattingTags
from openlp.core.lib.renderer import words_split, get_start_tags
2017-05-21 16:02:02 +00:00
from openlp.core.lib.theme import Theme
2014-01-11 19:31:06 +00:00
SCREEN = {
'primary': False,
'number': 1,
'size': QtCore.QRect(0, 0, 1024, 768)
}
# WARNING: Leave formatting alone - this is how it's returned in renderer.py
CSS_TEST_ONE = """<!DOCTYPE html><html><head><script>
function show_text(newtext) {
var main = document.getElementById('main');
main.innerHTML = newtext;
// We need to be sure that the page is loaded, that is why we
// return the element's height (even though we do not use the
// returned value).
return main.offsetHeight;
}
</script>
<style>
*{margin: 0; padding: 0; border: 0;}
#main {position: absolute; top: 0px; FORMAT CSS; OUTLINE CSS; }
</style></head>
<body><div id="main"></div></body></html>'"""
2014-01-11 19:31:06 +00:00
class TestRenderer(TestCase):
def setUp(self):
"""
2014-03-31 15:17:40 +00:00
Set up the components need for all tests
2014-01-11 19:31:06 +00:00
"""
# Mocked out desktop object
self.desktop = MagicMock()
self.desktop.primaryScreen.return_value = SCREEN['primary']
self.desktop.screenCount.return_value = SCREEN['number']
self.desktop.screenGeometry.return_value = SCREEN['size']
self.screens = ScreenList.create(self.desktop)
Registry.create()
def tearDown(self):
"""
Delete QApplication.
"""
del self.screens
2016-05-31 21:40:13 +00:00
def test_default_screen_layout(self):
2014-01-11 19:31:06 +00:00
"""
2014-03-31 15:17:40 +00:00
Test the default layout calculations
2014-01-11 19:31:06 +00:00
"""
# GIVEN: A new renderer instance.
renderer = Renderer()
# WHEN: given the default screen size has been created.
# THEN: The renderer have created a default screen.
self.assertEqual(renderer.width, 1024, 'The base renderer should be a live controller')
self.assertEqual(renderer.height, 768, 'The base renderer should be a live controller')
self.assertEqual(renderer.screen_ratio, 0.75, 'The base renderer should be a live controller')
2014-03-31 15:17:40 +00:00
self.assertEqual(renderer.footer_start, 691, 'The base renderer should be a live controller')
@patch('openlp.core.lib.renderer.FormattingTags.get_html_tags')
2016-05-31 21:40:13 +00:00
def test_get_start_tags(self, mocked_get_html_tags):
2014-03-31 15:17:40 +00:00
"""
Test the get_start_tags() method
2014-03-31 15:17:40 +00:00
"""
2014-03-31 15:20:17 +00:00
# GIVEN: A new renderer instance. Broken raw_text (missing closing tags).
2014-03-31 15:17:40 +00:00
given_raw_text = '{st}{r}Text text text'
expected_tuple = ('{st}{r}Text text text{/r}{/st}', '{st}{r}',
2014-03-31 15:24:31 +00:00
'<strong><span style="-webkit-text-fill-color:red">')
mocked_get_html_tags.return_value = [{'temporary': False, 'end tag': '{/r}', 'desc': 'Red',
'start html': '<span style="-webkit-text-fill-color:red">',
'end html': '</span>', 'start tag': '{r}', 'protected': True},
{'temporary': False, 'end tag': '{/st}', 'desc': 'Bold',
'start html': '<strong>', 'end html': '</strong>', 'start tag': '{st}',
'protected': True}]
2014-03-31 15:17:40 +00:00
2014-11-22 07:07:08 +00:00
# WHEN: The renderer converts the start tags
result = get_start_tags(given_raw_text)
2014-03-31 15:17:40 +00:00
2014-03-31 15:24:31 +00:00
# THEN: Check if the correct tuple is returned.
2014-04-14 18:59:28 +00:00
self.assertEqual(result, expected_tuple), 'A tuple should be returned containing the text with correct ' \
'tags, the opening tags, and the opening html tags.'
2014-03-31 15:31:05 +00:00
2016-05-31 21:40:13 +00:00
def test_word_split(self):
2014-03-31 15:31:05 +00:00
"""
Test the word_split() method
2014-03-31 15:31:05 +00:00
"""
# GIVEN: A line of text
given_line = 'beginning asdf \n end asdf'
expected_words = ['beginning', 'asdf', 'end', 'asdf']
2014-11-22 07:07:08 +00:00
# WHEN: Split the line based on word split rules
result_words = words_split(given_line)
2014-03-31 15:31:05 +00:00
# THEN: The word lists should be the same.
self.assertListEqual(result_words, expected_words)
2014-11-20 06:58:33 +00:00
2016-05-31 21:40:13 +00:00
def test_format_slide_logical_split(self):
2014-11-20 06:58:33 +00:00
"""
Test that a line with text and a logic break does not break the renderer just returns the input
"""
# GIVEN: A line of with a space text and the logical split
renderer = Renderer()
2015-11-20 19:30:46 +00:00
renderer.empty_height = 480
2014-11-20 06:58:33 +00:00
given_line = 'a\n[---]\nb'
expected_words = ['a<br>[---]<br>b']
service_item = ServiceItem(None)
2014-11-22 07:07:08 +00:00
# WHEN: Split the line based on word split rules
2014-11-20 06:58:33 +00:00
result_words = renderer.format_slide(given_line, service_item)
# THEN: The word lists should be the same.
self.assertListEqual(result_words, expected_words)
2016-05-31 21:40:13 +00:00
def test_format_slide_blank_before_split(self):
2014-11-20 06:58:33 +00:00
"""
Test that a line with blanks before the logical split at handled
"""
# GIVEN: A line of with a space before the logical split
renderer = Renderer()
2015-11-20 19:30:46 +00:00
renderer.empty_height = 480
2014-11-20 06:58:33 +00:00
given_line = '\n [---]\n'
expected_words = ['<br> [---]']
service_item = ServiceItem(None)
2014-11-22 07:07:08 +00:00
# WHEN: Split the line based on word split rules
2014-11-20 06:58:33 +00:00
result_words = renderer.format_slide(given_line, service_item)
# THEN: The blanks have been removed.
self.assertListEqual(result_words, expected_words)
2016-05-31 21:40:13 +00:00
def test_format_slide_blank_after_split(self):
2014-11-20 06:58:33 +00:00
"""
Test that a line with blanks before the logical split at handled
"""
# GIVEN: A line of with a space after the logical split
renderer = Renderer()
2015-11-20 19:30:46 +00:00
renderer.empty_height = 480
2014-11-20 06:58:33 +00:00
given_line = '\n[---] \n'
expected_words = ['<br>[---] ']
service_item = ServiceItem(None)
2015-01-16 20:23:56 +00:00
# WHEN: Split the line based on word split rules
2014-11-20 06:58:33 +00:00
result_words = renderer.format_slide(given_line, service_item)
# THEN: The blanks have been removed.
2014-11-22 07:07:08 +00:00
self.assertListEqual(result_words, expected_words)
2016-06-16 15:07:34 +00:00
@patch('openlp.core.lib.renderer.QtWebKitWidgets.QWebView')
@patch('openlp.core.lib.renderer.build_lyrics_format_css')
@patch('openlp.core.lib.renderer.build_lyrics_outline_css')
2017-06-03 22:52:11 +00:00
@patch('openlp.core.lib.renderer.build_chords_css')
def test_set_text_rectangle(self, mock_build_chords_css, mock_outline_css, mock_lyrics_css, mock_webview):
"""
2016-06-16 15:07:34 +00:00
Test set_text_rectangle returns a proper html string
"""
# GIVEN: test object and data
mock_lyrics_css.return_value = ' FORMAT CSS; '
mock_outline_css.return_value = ' OUTLINE CSS; '
2017-06-03 22:52:11 +00:00
mock_build_chords_css.return_value = ' CHORDS CSS; '
2017-05-21 16:02:02 +00:00
theme_data = Theme()
theme_data.font_main_name = 'Arial'
theme_data.font_main_size = 20
theme_data.font_main_color = '#FFFFFF'
theme_data.font_main_outline_color = '#FFFFFF'
main = QtCore.QRect(10, 10, 1280, 900)
foot = QtCore.QRect(10, 1000, 1260, 24)
renderer = Renderer()
2016-06-16 15:07:34 +00:00
# WHEN: Calling method
renderer._set_text_rectangle(theme_data=theme_data, rect_main=main, rect_footer=foot)
# THEN: QtWebKitWidgets should be called with the proper string
mock_webview.setHtml.called_with(CSS_TEST_ONE, 'Should be the same')