2017-11-07 03:32:40 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-04-13 13:00:22 +00:00
|
|
|
##########################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# ---------------------------------------------------------------------- #
|
2020-12-30 21:42:49 +00:00
|
|
|
# Copyright (c) 2008-2021 OpenLP Developers #
|
2019-04-13 13:00:22 +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, either version 3 of the License, or #
|
|
|
|
# (at your option) any later version. #
|
|
|
|
# #
|
|
|
|
# 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, see <https://www.gnu.org/licenses/>. #
|
|
|
|
##########################################################################
|
2017-11-07 03:32:40 +00:00
|
|
|
"""
|
|
|
|
Test the :mod:`~openlp.core.display.render` package.
|
|
|
|
"""
|
2021-01-03 14:32:00 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2017-11-07 03:32:40 +00:00
|
|
|
|
2018-10-02 04:39:42 +00:00
|
|
|
from openlp.core.display.render import compare_chord_lyric_width, find_formatting_tags, remove_tags, render_chords, \
|
2021-01-03 14:32:00 +00:00
|
|
|
render_chords_for_printing, render_tags, ThemePreviewRenderer
|
2017-11-07 03:32:40 +00:00
|
|
|
from openlp.core.lib.formattingtags import FormattingTags
|
|
|
|
|
|
|
|
|
2019-02-13 21:36:37 +00:00
|
|
|
@patch('openlp.core.display.render.FormattingTags.get_html_tags')
|
2017-11-07 03:32:40 +00:00
|
|
|
def test_remove_tags(mocked_get_tags):
|
|
|
|
"""
|
|
|
|
Test remove_tags() method.
|
|
|
|
"""
|
|
|
|
# GIVEN: Mocked get_html_tags() method.
|
|
|
|
mocked_get_tags.return_value = [{
|
|
|
|
'desc': 'Black',
|
|
|
|
'start tag': '{b}',
|
|
|
|
'start html': '<span style="-webkit-text-fill-color:black">',
|
|
|
|
'end tag': '{/b}', 'end html': '</span>', 'protected': True,
|
|
|
|
'temporary': False
|
|
|
|
}]
|
|
|
|
string_to_pass = 'ASDF<br>foo{br}bar {b}black{/b}'
|
|
|
|
expected_string = 'ASDF\nfoo\nbar black'
|
|
|
|
|
|
|
|
# WHEN: Clean the string.
|
|
|
|
result_string = remove_tags(string_to_pass)
|
|
|
|
|
|
|
|
# THEN: The strings should be identical.
|
|
|
|
assert result_string == expected_string, 'The strings should be identical'
|
|
|
|
|
|
|
|
|
2019-02-13 21:36:37 +00:00
|
|
|
@patch('openlp.core.display.render.FormattingTags.get_html_tags')
|
2020-01-16 04:57:56 +00:00
|
|
|
def test_render_tags(mocked_get_tags, settings):
|
2017-11-07 03:32:40 +00:00
|
|
|
"""
|
|
|
|
Test the render_tags() method.
|
|
|
|
"""
|
|
|
|
# GIVEN: Mocked get_html_tags() method.
|
2020-01-16 04:57:56 +00:00
|
|
|
settings.setValue('songs/chord notation', 'english')
|
2017-11-07 03:32:40 +00:00
|
|
|
mocked_get_tags.return_value = [
|
|
|
|
{
|
|
|
|
'desc': 'Black',
|
|
|
|
'start tag': '{b}',
|
|
|
|
'start html': '<span style="-webkit-text-fill-color:black">',
|
|
|
|
'end tag': '{/b}', 'end html': '</span>', 'protected': True,
|
|
|
|
'temporary': False
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'desc': 'Yellow',
|
|
|
|
'start tag': '{y}',
|
|
|
|
'start html': '<span style="-webkit-text-fill-color:yellow">',
|
|
|
|
'end tag': '{/y}', 'end html': '</span>', 'protected': True,
|
|
|
|
'temporary': False
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'desc': 'Green',
|
|
|
|
'start tag': '{g}',
|
|
|
|
'start html': '<span style="-webkit-text-fill-color:green">',
|
|
|
|
'end tag': '{/g}', 'end html': '</span>', 'protected': True,
|
|
|
|
'temporary': False
|
|
|
|
}
|
|
|
|
]
|
|
|
|
string_to_pass = '{b}black{/b}{y}yellow{/y}'
|
|
|
|
expected_string = '<span style="-webkit-text-fill-color:black">black</span>' + \
|
|
|
|
'<span style="-webkit-text-fill-color:yellow">yellow</span>'
|
|
|
|
|
|
|
|
# WHEN: Replace the tags.
|
|
|
|
result_string = render_tags(string_to_pass)
|
|
|
|
|
|
|
|
# THEN: The strings should be identical.
|
|
|
|
assert result_string == expected_string, 'The strings should be identical.'
|
|
|
|
|
|
|
|
|
2020-01-16 04:57:56 +00:00
|
|
|
def test_render_chords(settings):
|
2017-11-07 03:32:40 +00:00
|
|
|
"""
|
|
|
|
Test that the rendering of chords works as expected.
|
|
|
|
"""
|
|
|
|
# GIVEN: A lyrics-line with chords
|
2020-01-16 04:57:56 +00:00
|
|
|
settings.setValue('songs/chord notation', 'english')
|
|
|
|
text_with_chords = 'H[C]alleluya.[F] [G/B]'
|
2017-11-07 03:32:40 +00:00
|
|
|
|
|
|
|
# WHEN: Expanding the chords
|
|
|
|
text_with_rendered_chords = render_chords(text_with_chords)
|
|
|
|
|
|
|
|
# THEN: We should get html that looks like below
|
2021-07-04 16:33:18 +00:00
|
|
|
expected_html = '<span class="chordline">H<span class="chord"><span><strong>C</strong></span>' \
|
2017-11-07 03:32:40 +00:00
|
|
|
'</span>alleluya.<span class="chord"><span><strong>F</strong></span></span><span class="ws">' \
|
2020-01-16 04:57:56 +00:00
|
|
|
' </span> <span class="chord"><span><strong>G/B</strong></span></span></span>'
|
2017-11-07 03:32:40 +00:00
|
|
|
assert text_with_rendered_chords == expected_html, 'The rendered chords should look as expected'
|
|
|
|
|
|
|
|
|
2020-01-16 04:57:56 +00:00
|
|
|
def test_render_chords_with_special_chars(settings):
|
2017-11-07 03:32:40 +00:00
|
|
|
"""
|
|
|
|
Test that the rendering of chords works as expected when special chars are involved.
|
|
|
|
"""
|
|
|
|
# GIVEN: A lyrics-line with chords
|
2020-01-16 04:57:56 +00:00
|
|
|
settings.setValue('songs/chord notation', 'english')
|
2017-11-07 03:32:40 +00:00
|
|
|
text_with_chords = "I[D]'M NOT MOVED BY WHAT I SEE HALLE[F]LUJA[C]H"
|
|
|
|
|
|
|
|
# WHEN: Expanding the chords
|
|
|
|
text_with_rendered_chords = render_tags(text_with_chords, can_render_chords=True)
|
|
|
|
|
|
|
|
# THEN: We should get html that looks like below
|
2021-07-04 16:18:06 +00:00
|
|
|
expected_html = '<span class="chordline">I<span class="chord"><span><strong>D</strong></span>' \
|
2017-11-07 03:32:40 +00:00
|
|
|
'</span>'M NOT MOVED BY WHAT I SEE HALLE<span class="chord"><span><strong>F</strong>' \
|
|
|
|
'</span></span>LUJA<span class="chord"><span><strong>C</strong></span></span>H</span>'
|
|
|
|
assert text_with_rendered_chords == expected_html, 'The rendered chords should look as expected'
|
|
|
|
|
|
|
|
|
|
|
|
def test_compare_chord_lyric_short_chord():
|
|
|
|
"""
|
|
|
|
Test that the chord/lyric comparing works.
|
|
|
|
"""
|
|
|
|
# GIVEN: A chord and some lyric
|
|
|
|
chord = 'C'
|
|
|
|
lyrics = 'alleluya'
|
|
|
|
|
|
|
|
# WHEN: Comparing the chord and lyrics
|
|
|
|
ret = compare_chord_lyric_width(chord, lyrics)
|
|
|
|
|
|
|
|
# THEN: The returned value should 0 because the lyric is longer than the chord
|
|
|
|
assert ret == 0, 'The returned value should 0 because the lyric is longer than the chord'
|
|
|
|
|
|
|
|
|
|
|
|
def test_compare_chord_lyric_long_chord():
|
|
|
|
"""
|
|
|
|
Test that the chord/lyric comparing works.
|
|
|
|
"""
|
|
|
|
# GIVEN: A chord and some lyric
|
|
|
|
chord = 'Gsus'
|
|
|
|
lyrics = 'me'
|
|
|
|
|
|
|
|
# WHEN: Comparing the chord and lyrics
|
|
|
|
ret = compare_chord_lyric_width(chord, lyrics)
|
|
|
|
|
|
|
|
# THEN: The returned value should 4 because the chord is longer than the lyric
|
|
|
|
assert ret == 4, 'The returned value should 4 because the chord is longer than the lyric'
|
|
|
|
|
|
|
|
|
2020-01-16 04:57:56 +00:00
|
|
|
def test_render_chords_for_printing(settings):
|
2017-11-07 03:32:40 +00:00
|
|
|
"""
|
|
|
|
Test that the rendering of chords for printing works as expected.
|
|
|
|
"""
|
|
|
|
# GIVEN: A lyrics-line with chords
|
2020-01-16 04:57:56 +00:00
|
|
|
settings.setValue('songs/chord notation', 'english')
|
2017-11-07 03:32:40 +00:00
|
|
|
text_with_chords = '{st}[D]Amazing {r}gr[D7]ace{/r} how [G]sweet the [D]sound [F]{/st}'
|
|
|
|
FormattingTags.load_tags()
|
|
|
|
|
|
|
|
# WHEN: Expanding the chords
|
|
|
|
text_with_rendered_chords = render_chords_for_printing(text_with_chords, '{br}')
|
|
|
|
|
|
|
|
# THEN: We should get html that looks like below
|
|
|
|
expected_html = '<table class="line" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td><table ' \
|
|
|
|
'class="segment" cellpadding="0" cellspacing="0" border="0" align="left"><tr class="chordrow">'\
|
|
|
|
'<td class="chord"> </td><td class="chord">D</td></tr><tr><td class="lyrics">{st}{/st}' \
|
|
|
|
'</td><td class="lyrics">{st}Amazing {/st}</td></tr></table><table class="segment" ' \
|
|
|
|
'cellpadding="0" cellspacing="0" border="0" align="left"><tr class="chordrow">' \
|
|
|
|
'<td class="chord"> </td><td class="chord">D7</td></tr><tr><td class="lyrics">{st}{r}gr' \
|
|
|
|
'{/r}{/st}</td><td class="lyrics">{r}{st}ace{/r} {/st}</td></tr></table><table ' \
|
|
|
|
'class="segment" cellpadding="0" cellspacing="0" border="0" align="left"><tr class="chordrow">'\
|
|
|
|
'<td class="chord"> </td></tr><tr><td class="lyrics">{st} {/st}</td></tr></table>' \
|
|
|
|
'<table class="segment" cellpadding="0" cellspacing="0" border="0" align="left"><tr ' \
|
|
|
|
'class="chordrow"><td class="chord"> </td></tr><tr><td class="lyrics">{st}how {/st}' \
|
|
|
|
'</td></tr></table><table class="segment" cellpadding="0" cellspacing="0" border="0" ' \
|
|
|
|
'align="left"><tr class="chordrow"><td class="chord">G</td></tr><tr><td class="lyrics">{st}' \
|
|
|
|
'sweet {/st}</td></tr></table><table class="segment" cellpadding="0" cellspacing="0" ' \
|
|
|
|
'border="0" align="left"><tr class="chordrow"><td class="chord"> </td></tr><tr><td ' \
|
|
|
|
'class="lyrics">{st}the {/st}</td></tr></table><table class="segment" cellpadding="0" ' \
|
|
|
|
'cellspacing="0" border="0" align="left"><tr class="chordrow"><td class="chord">D</td></tr>' \
|
|
|
|
'<tr><td class="lyrics">{st}sound {/st}</td></tr></table><table class="segment" ' \
|
|
|
|
'cellpadding="0" cellspacing="0" border="0" align="left"><tr class="chordrow"><td ' \
|
|
|
|
'class="chord"> </td></tr><tr><td class="lyrics">{st} {/st}</td></tr></table>' \
|
|
|
|
'<table class="segment" cellpadding="0" cellspacing="0" border="0" align="left"><tr ' \
|
|
|
|
'class="chordrow"><td class="chord">F</td></tr><tr><td class="lyrics">{st}{/st} </td>' \
|
|
|
|
'</tr></table></td></tr></table>'
|
|
|
|
assert text_with_rendered_chords == expected_html, 'The rendered chords should look as expected!'
|
|
|
|
|
|
|
|
|
2020-03-04 06:06:47 +00:00
|
|
|
def test_find_formatting_tags(settings):
|
2017-11-07 03:32:40 +00:00
|
|
|
"""
|
|
|
|
Test that find_formatting_tags works as expected
|
|
|
|
"""
|
|
|
|
# GIVEN: Lyrics with formatting tags and a empty list of formatting tags
|
|
|
|
lyrics = '{st}Amazing {r}grace{/r} how sweet the sound'
|
|
|
|
tags = []
|
|
|
|
FormattingTags.load_tags()
|
|
|
|
|
|
|
|
# WHEN: Detecting active formatting tags
|
|
|
|
active_tags = find_formatting_tags(lyrics, tags)
|
|
|
|
|
|
|
|
# THEN: The list of active tags should contain only 'st'
|
|
|
|
assert active_tags == ['st'], 'The list of active tags should contain only "st"'
|
2021-01-03 14:32:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_format_slide(settings):
|
|
|
|
"""
|
|
|
|
Test that the format_slide function works as expected
|
|
|
|
"""
|
|
|
|
# GIVEN: Renderer where no text fits on screen (force one line per slide)
|
|
|
|
with patch('openlp.core.display.render.ThemePreviewRenderer.__init__') as init_fn:
|
|
|
|
init_fn.return_value = None
|
|
|
|
preview_renderer = ThemePreviewRenderer()
|
|
|
|
lyrics = 'hello {st}test{/st}\nline two\n[---]\nline after optional split'
|
|
|
|
preview_renderer._is_initialised = True
|
|
|
|
preview_renderer.log_debug = MagicMock()
|
|
|
|
preview_renderer._text_fits_on_slide = MagicMock(side_effect=lambda a: a == '')
|
|
|
|
preview_renderer.force_page = False
|
|
|
|
|
|
|
|
# WHEN: format_slide is run
|
|
|
|
formatted_slides = preview_renderer.format_slide(lyrics, None)
|
|
|
|
|
|
|
|
# THEN: The formatted slides should have all the text and no blank slides
|
|
|
|
assert formatted_slides == ['hello {st}test{/st}', 'line two', 'line after optional split']
|
|
|
|
|
|
|
|
|
|
|
|
def test_format_slide_no_split(settings):
|
|
|
|
"""
|
|
|
|
Test that the format_slide function doesn't split a slide that fits
|
|
|
|
"""
|
|
|
|
# GIVEN: Renderer where no text fits on screen (force one line per slide)
|
|
|
|
with patch('openlp.core.display.render.ThemePreviewRenderer.__init__') as init_fn:
|
|
|
|
init_fn.return_value = None
|
|
|
|
preview_renderer = ThemePreviewRenderer()
|
|
|
|
lyrics = 'line one\n[---]\nline two'
|
|
|
|
preview_renderer._is_initialised = True
|
|
|
|
preview_renderer.log_debug = MagicMock()
|
|
|
|
preview_renderer._text_fits_on_slide = MagicMock(return_value=True)
|
|
|
|
preview_renderer.force_page = False
|
|
|
|
|
|
|
|
# WHEN: format_slide is run
|
|
|
|
formatted_slides = preview_renderer.format_slide(lyrics, None)
|
|
|
|
|
|
|
|
# THEN: The formatted slides should have all the text and no blank slides
|
|
|
|
assert formatted_slides == ['line one<br>line two']
|