openlp/tests/functional/openlp_core_lib/test_renderer.py

162 lines
6.9 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 #
# --------------------------------------------------------------------------- #
2015-12-31 22:46:06 +00:00
# Copyright (c) 2008-2016 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
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
2014-01-11 19:31:06 +00:00
from tests.functional import MagicMock, patch
2014-01-11 19:31:06 +00:00
SCREEN = {
'primary': False,
'number': 1,
'size': QtCore.QRect(0, 0, 1024, 768)
}
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
def default_screen_layout_test(self):
"""
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')
def get_start_tags_test(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
def word_split_test(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
def format_slide_logical_split_test(self):
"""
Test that a line with text and a logic break does not break the renderer just returns the input
"""
# GIVEN: A line of with a space text and the logical split
renderer = Renderer()
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)
def format_slide_blank_before_split_test(self):
"""
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)
def format_slide_blank_after_split_test(self):
"""
Test that a line with blanks before the logical split at handled
"""
# GIVEN: A line of with a space after the logical split
renderer = Renderer()
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)