~tomasgroth/openlp/portable-path

« back to all changes in this revision

Viewing changes to tests/functional/openlp_core/lib/test_lib.py

  • Committer: Tomas Groth
  • Date: 2019-04-30 19:02:42 UTC
  • mfrom: (2829.2.32 openlp)
  • Revision ID: tomasgroth@yahoo.dk-20190430190242-6zwjk8724tyux70m
trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
###############################################################################
5
5
# OpenLP - Open Source Lyrics Projection                                      #
6
6
# --------------------------------------------------------------------------- #
7
 
# Copyright (c) 2008-2017 OpenLP Developers                                   #
 
7
# Copyright (c) 2008-2019 OpenLP Developers                                   #
8
8
# --------------------------------------------------------------------------- #
9
9
# This program is free software; you can redistribute it and/or modify it     #
10
10
# under the terms of the GNU General Public License as published by the Free  #
28
28
from PyQt5 import QtCore, QtGui
29
29
 
30
30
from openlp.core.common.path import Path
31
 
from openlp.core.lib import FormattingTags, build_icon, check_item_selected, clean_tags, compare_chord_lyric, \
32
 
    create_separated_list, create_thumb, expand_chords, expand_chords_for_printing, expand_tags, find_formatting_tags, \
 
31
from openlp.core.lib import build_icon, check_item_selected, create_separated_list, create_thumb, \
33
32
    get_text_file_string, image_to_byte, resize_image, str_to_bool, validate_thumb
34
33
from tests.utils.constants import RESOURCE_PATH
35
34
 
279
278
        # last test.
280
279
        try:
281
280
            thumb_path.unlink()
282
 
        except:
 
281
        except Exception:
283
282
            pass
284
283
 
285
284
        # Only continue when the thumb does not exist.
297
296
        # Remove the thumb so that the test actually tests if the thumb will be created.
298
297
        try:
299
298
            thumb_path.unlink()
300
 
        except:
 
299
        except Exception:
301
300
            pass
302
301
 
303
302
    def test_create_thumb_no_size(self):
313
312
        # last test.
314
313
        try:
315
314
            thumb_path.unlink()
316
 
        except:
 
315
        except Exception:
317
316
            pass
318
317
 
319
318
        # Only continue when the thumb does not exist.
331
330
        # Remove the thumb so that the test actually tests if the thumb will be created.
332
331
        try:
333
332
            thumb_path.unlink()
334
 
        except:
 
333
        except Exception:
335
334
            pass
336
335
 
337
336
    def test_create_thumb_invalid_size(self):
348
347
        # last test.
349
348
        try:
350
349
            thumb_path.unlink()
351
 
        except:
 
350
        except Exception:
352
351
            pass
353
352
 
354
353
        # Only continue when the thumb does not exist.
366
365
        # Remove the thumb so that the test actually tests if the thumb will be created.
367
366
        try:
368
367
            thumb_path.unlink()
369
 
        except:
 
368
        except Exception:
370
369
            pass
371
370
 
372
371
    def test_create_thumb_width_only(self):
383
382
        # last test.
384
383
        try:
385
384
            thumb_path.unlink()
386
 
        except:
 
385
        except Exception:
387
386
            pass
388
387
 
389
388
        # Only continue when the thumb does not exist.
401
400
        # Remove the thumb so that the test actually tests if the thumb will be created.
402
401
        try:
403
402
            thumb_path.unlink()
404
 
        except:
 
403
        except Exception:
405
404
            pass
406
405
 
407
406
    def test_create_thumb_height_only(self):
418
417
        # last test.
419
418
        try:
420
419
            thumb_path.unlink()
421
 
        except:
 
420
        except Exception:
422
421
            pass
423
422
 
424
423
        # Only continue when the thumb does not exist.
436
435
        # Remove the thumb so that the test actually tests if the thumb will be created.
437
436
        try:
438
437
            thumb_path.unlink()
439
 
        except:
 
438
        except Exception:
440
439
            pass
441
440
 
442
441
    def test_create_thumb_empty_img(self):
454
453
        # last test.
455
454
        try:
456
455
            thumb_path.unlink()
457
 
        except:
 
456
        except Exception:
458
457
            pass
459
458
 
460
459
        # Only continue when the thumb does not exist.
484
483
        # Remove the thumb so that the test actually tests if the thumb will be created.
485
484
        try:
486
485
            thumb_path.unlink()
487
 
        except:
 
486
        except Exception:
488
487
            pass
489
488
 
 
489
    @patch('openlp.core.lib.QtWidgets', MagicMock())
490
490
    def test_check_item_selected_true(self):
491
491
        """
492
492
        Test that the check_item_selected() function returns True when there are selected indexes
493
493
        """
494
494
        # GIVEN: A mocked out QtWidgets module and a list widget with selected indexes
495
 
        # mocked_QtWidgets = patch('openlp.core.lib.QtWidgets')
496
495
        mocked_list_widget = MagicMock()
497
496
        mocked_list_widget.selectedIndexes.return_value = True
498
497
        message = 'message'
525
524
            MockedQtWidgets.QMessageBox.information.assert_called_with('parent', 'mocked translate', 'message')
526
525
            assert result is False, 'The result should be False'
527
526
 
528
 
    def test_clean_tags(self):
529
 
        """
530
 
        Test clean_tags() method.
531
 
        """
532
 
        with patch('openlp.core.lib.FormattingTags.get_html_tags') as mocked_get_tags:
533
 
            # GIVEN: Mocked get_html_tags() method.
534
 
            mocked_get_tags.return_value = [{
535
 
                'desc': 'Black',
536
 
                'start tag': '{b}',
537
 
                'start html': '<span style="-webkit-text-fill-color:black">',
538
 
                'end tag': '{/b}', 'end html': '</span>', 'protected': True,
539
 
                'temporary': False
540
 
            }]
541
 
            string_to_pass = 'ASDF<br>foo{br}bar&nbsp;{b}black{/b}'
542
 
            wanted_string = 'ASDF\nfoo\nbar black'
543
 
 
544
 
            # WHEN: Clean the string.
545
 
            result_string = clean_tags(string_to_pass)
546
 
 
547
 
            # THEN: The strings should be identical.
548
 
            assert wanted_string == result_string, 'The strings should be identical'
549
 
 
550
 
    def test_expand_tags(self):
551
 
        """
552
 
        Test the expand_tags() method.
553
 
        """
554
 
        with patch('openlp.core.lib.FormattingTags.get_html_tags') as mocked_get_tags:
555
 
            # GIVEN: Mocked get_html_tags() method.
556
 
            mocked_get_tags.return_value = [
557
 
                {
558
 
                    'desc': 'Black',
559
 
                    'start tag': '{b}',
560
 
                    'start html': '<span style="-webkit-text-fill-color:black">',
561
 
                    'end tag': '{/b}', 'end html': '</span>', 'protected': True,
562
 
                    'temporary': False
563
 
                },
564
 
                {
565
 
                    'desc': 'Yellow',
566
 
                    'start tag': '{y}',
567
 
                    'start html': '<span style="-webkit-text-fill-color:yellow">',
568
 
                    'end tag': '{/y}', 'end html': '</span>', 'protected': True,
569
 
                    'temporary': False
570
 
                },
571
 
                {
572
 
                    'desc': 'Green',
573
 
                    'start tag': '{g}',
574
 
                    'start html': '<span style="-webkit-text-fill-color:green">',
575
 
                    'end tag': '{/g}', 'end html': '</span>', 'protected': True,
576
 
                    'temporary': False
577
 
                }
578
 
            ]
579
 
            string_to_pass = '{b}black{/b}{y}yellow{/y}'
580
 
            wanted_string = '<span style="-webkit-text-fill-color:black">black</span>' + \
581
 
                '<span style="-webkit-text-fill-color:yellow">yellow</span>'
582
 
 
583
 
            # WHEN: Replace the tags.
584
 
            result_string = expand_tags(string_to_pass)
585
 
 
586
 
            # THEN: The strings should be identical.
587
 
            assert wanted_string == result_string, 'The strings should be identical.'
588
 
 
589
527
    def test_validate_thumb_file_does_not_exist(self):
590
528
        """
591
529
        Test the validate_thumb() function when the thumbnail does not exist
694
632
        """
695
633
        Test the create_separated_list function with an empty list
696
634
        """
697
 
        # GIVEN: An empty list and the mocked Qt module.
 
635
        # GIVEN: An empty list
698
636
        string_list = []
699
637
 
700
638
        # WHEN: We get a string build from the entries it the list and a separator.
742
680
        # THEN: We should have "Author 1, Author 2 and Author 3"
743
681
        assert string_result == 'Author 1, Author 2 and Author 3', \
744
682
            'The string should be "Author 1, Author 2, and Author 3".'
745
 
 
746
 
    def test_expand_chords(self):
747
 
        """
748
 
        Test that the expanding of chords works as expected.
749
 
        """
750
 
        # GIVEN: A lyrics-line with chords
751
 
        text_with_chords = 'H[C]alleluya.[F] [G]'
752
 
 
753
 
        # WHEN: Expanding the chords
754
 
        text_with_expanded_chords = expand_chords(text_with_chords)
755
 
 
756
 
        # THEN: We should get html that looks like below
757
 
        expected_html = '<span class="chordline firstchordline">H<span class="chord"><span><strong>C</strong></span>' \
758
 
                        '</span>alleluya.<span class="chord"><span><strong>F</strong></span></span><span class="ws">' \
759
 
                        '&nbsp;&nbsp;</span> <span class="chord"><span><strong>G</strong></span></span></span>'
760
 
        assert expected_html == text_with_expanded_chords, 'The expanded chords should look as expected!'
761
 
 
762
 
    def test_expand_chords2(self):
763
 
        """
764
 
        Test that the expanding of chords works as expected when special chars are involved.
765
 
        """
766
 
        # GIVEN: A lyrics-line with chords
767
 
        text_with_chords = "I[D]'M NOT MOVED BY WHAT I SEE HALLE[F]LUJA[C]H"
768
 
 
769
 
        # WHEN: Expanding the chords
770
 
        text_with_expanded_chords = expand_tags(text_with_chords, True)
771
 
 
772
 
        # THEN: We should get html that looks like below
773
 
        expected_html = '<span class="chordline firstchordline">I<span class="chord"><span><strong>D</strong></span>' \
774
 
                        '</span>&#x27;M NOT MOVED BY WHAT I SEE HALLE<span class="chord"><span><strong>F</strong>' \
775
 
                        '</span></span>LUJA<span class="chord"><span><strong>C</strong></span></span>H</span>'
776
 
        assert expected_html == text_with_expanded_chords, 'The expanded chords should look as expected!'
777
 
 
778
 
    def test_compare_chord_lyric_short_chord(self):
779
 
        """
780
 
        Test that the chord/lyric comparing works.
781
 
        """
782
 
        # GIVEN: A chord and some lyric
783
 
        chord = 'C'
784
 
        lyrics = 'alleluya'
785
 
 
786
 
        # WHEN: Comparing the chord and lyrics
787
 
        ret = compare_chord_lyric(chord, lyrics)
788
 
 
789
 
        # THEN: The returned value should 0 because the lyric is longer than the chord
790
 
        assert 0 == ret, 'The returned value should 0 because the lyric is longer than the chord'
791
 
 
792
 
    def test_compare_chord_lyric_long_chord(self):
793
 
        """
794
 
        Test that the chord/lyric comparing works.
795
 
        """
796
 
        # GIVEN: A chord and some lyric
797
 
        chord = 'Gsus'
798
 
        lyrics = 'me'
799
 
 
800
 
        # WHEN: Comparing the chord and lyrics
801
 
        ret = compare_chord_lyric(chord, lyrics)
802
 
 
803
 
        # THEN: The returned value should 4 because the chord is longer than the lyric
804
 
        assert 4 == ret, 'The returned value should 4 because the chord is longer than the lyric'
805
 
 
806
 
    def test_find_formatting_tags(self):
807
 
        """
808
 
        Test that find_formatting_tags works as expected
809
 
        """
810
 
        # GIVEN: Lyrics with formatting tags and a empty list of formatting tags
811
 
        lyrics = '{st}Amazing {r}grace{/r} how sweet the sound'
812
 
        tags = []
813
 
        FormattingTags.load_tags()
814
 
 
815
 
        # WHEN: Detecting active formatting tags
816
 
        active_tags = find_formatting_tags(lyrics, tags)
817
 
 
818
 
        # THEN: The list of active tags should contain only 'st'
819
 
        assert ['st'] == active_tags, 'The list of active tags should contain only "st"'
820
 
 
821
 
    def test_expand_chords_for_printing(self):
822
 
        """
823
 
        Test that the expanding of chords for printing works as expected.
824
 
        """
825
 
        # GIVEN: A lyrics-line with chords
826
 
        text_with_chords = '{st}[D]Amazing {r}gr[D7]ace{/r}  how [G]sweet the [D]sound  [F]{/st}'
827
 
        FormattingTags.load_tags()
828
 
 
829
 
        # WHEN: Expanding the chords
830
 
        text_with_expanded_chords = expand_chords_for_printing(text_with_chords, '{br}')
831
 
 
832
 
        # THEN: We should get html that looks like below
833
 
        expected_html = '<table class="line" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td><table ' \
834
 
                        'class="segment" cellpadding="0" cellspacing="0" border="0" align="left"><tr class="chordrow">'\
835
 
                        '<td class="chord">&nbsp;</td><td class="chord">D</td></tr><tr><td class="lyrics">{st}{/st}' \
836
 
                        '</td><td class="lyrics">{st}Amazing&nbsp;{/st}</td></tr></table><table class="segment" ' \
837
 
                        'cellpadding="0" cellspacing="0" border="0" align="left"><tr class="chordrow">' \
838
 
                        '<td class="chord">&nbsp;</td><td class="chord">D7</td></tr><tr><td class="lyrics">{st}{r}gr' \
839
 
                        '{/r}{/st}</td><td class="lyrics">{r}{st}ace{/r}&nbsp;{/st}</td></tr></table><table ' \
840
 
                        'class="segment" cellpadding="0" cellspacing="0" border="0" align="left"><tr class="chordrow">'\
841
 
                        '<td class="chord">&nbsp;</td></tr><tr><td class="lyrics">{st}&nbsp;{/st}</td></tr></table>' \
842
 
                        '<table class="segment" cellpadding="0" cellspacing="0" border="0" align="left"><tr ' \
843
 
                        'class="chordrow"><td class="chord">&nbsp;</td></tr><tr><td class="lyrics">{st}how&nbsp;{/st}' \
844
 
                        '</td></tr></table><table class="segment" cellpadding="0" cellspacing="0" border="0" ' \
845
 
                        'align="left"><tr class="chordrow"><td class="chord">G</td></tr><tr><td class="lyrics">{st}' \
846
 
                        'sweet&nbsp;{/st}</td></tr></table><table class="segment" cellpadding="0" cellspacing="0" ' \
847
 
                        'border="0" align="left"><tr class="chordrow"><td class="chord">&nbsp;</td></tr><tr><td ' \
848
 
                        'class="lyrics">{st}the&nbsp;{/st}</td></tr></table><table class="segment" cellpadding="0" ' \
849
 
                        'cellspacing="0" border="0" align="left"><tr class="chordrow"><td class="chord">D</td></tr>' \
850
 
                        '<tr><td class="lyrics">{st}sound&nbsp;{/st}</td></tr></table><table class="segment" ' \
851
 
                        'cellpadding="0" cellspacing="0" border="0" align="left"><tr class="chordrow"><td ' \
852
 
                        'class="chord">&nbsp;</td></tr><tr><td class="lyrics">{st}&nbsp;{/st}</td></tr></table>' \
853
 
                        '<table class="segment" cellpadding="0" cellspacing="0" border="0" align="left"><tr ' \
854
 
                        'class="chordrow"><td class="chord">F</td></tr><tr><td class="lyrics">{st}{/st}&nbsp;</td>' \
855
 
                        '</tr></table></td></tr></table>'
856
 
        assert expected_html == text_with_expanded_chords, 'The expanded chords should look as expected!'