|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: UTF-8 -*- |
| 3 | + |
| 4 | +# Copyright (c) 2020, Sandflow Consulting LLC |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# |
| 9 | +# 1. Redistributions of source code must retain the above copyright notice, this |
| 10 | +# list of conditions and the following disclaimer. |
| 11 | +# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | +# this list of conditions and the following disclaimer in the documentation |
| 13 | +# and/or other materials provided with the distribution. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 16 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 19 | +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 22 | +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + |
| 26 | +'''Unit tests for the IMSC \\<position\\> parser''' |
| 27 | + |
| 28 | +# pylint: disable=R0201,C0115,C0116 |
| 29 | + |
| 30 | +import unittest |
| 31 | +from ttconv.imsc.utils import parse_position |
| 32 | +import ttconv.style_properties as styles |
| 33 | + |
| 34 | +def position(h_edge, h_offval, h_offunit, v_edge, v_offval, v_offunit): |
| 35 | + return ( |
| 36 | + h_edge, |
| 37 | + styles.LengthType(value=h_offval, units=styles.LengthType.Units(h_offunit)), |
| 38 | + v_edge, |
| 39 | + styles.LengthType(value=v_offval, units=styles.LengthType.Units(v_offunit)) |
| 40 | + ) |
| 41 | + |
| 42 | +class IMSCPositionTest(unittest.TestCase): |
| 43 | + |
| 44 | + tests = [ |
| 45 | + # one component |
| 46 | + ["center", position("left", 50, "%", "top", 50, "%")], |
| 47 | + ["left", position("left", 0, "%", "top", 50, "%")], |
| 48 | + ["right", position("right", 0, "%", "top", 50, "%")], |
| 49 | + ["top", position("left", 50, "%", "top", 0, "%")], |
| 50 | + ["bottom", position("left", 50, "%", "bottom", 0, "%")], |
| 51 | + ["25%", position("left", 25, "%", "top", 50, "%")], |
| 52 | + # two components |
| 53 | + ["bottom center", position("left", 50, "%", "bottom", 0, "%")], |
| 54 | + ["bottom left", position("left", 0, "%", "bottom", 0, "%")], |
| 55 | + ["bottom right", position("right", 0, "%", "bottom", 0, "%")], |
| 56 | + ["center center", position("left", 50, "%", "top", 50, "%")], |
| 57 | + ["center top", position("left", 50, "%", "top", 0, "%")], |
| 58 | + ["center bottom", position("left", 50, "%", "bottom", 0, "%")], |
| 59 | + ["center left", position("left", 0, "%", "top", 50, "%")], |
| 60 | + ["center right", position("right", 0, "%", "top", 50, "%")], |
| 61 | + ["center 33%", position("left", 50, "%", "top", 33, "%")], |
| 62 | + ["left center", position("left", 0, "%", "top", 50, "%")], |
| 63 | + ["left top", position("left", 0, "%", "top", 0, "%")], |
| 64 | + ["left bottom", position("left", 0, "%", "bottom", 0, "%")], |
| 65 | + ["left 45%", position("left", 0, "%", "top", 45, "%")], |
| 66 | + ["right center", position("right", 0, "%", "top", 50, "%")], |
| 67 | + ["right top", position("right", 0, "%", "top", 0, "%")], |
| 68 | + ["right bottom", position("right", 0, "%", "bottom", 0, "%")], |
| 69 | + ["right 20%", position("right", 0, "%", "top", 20, "%")], |
| 70 | + ["top center", position("left", 50, "%", "top", 0, "%")], |
| 71 | + ["top left", position("left", 0, "%", "top", 0, "%")], |
| 72 | + ["top right", position("right", 0, "%", "top", 0, "%")], |
| 73 | + ["75% center", position("left", 75, "%", "top", 50, "%")], |
| 74 | + ["75% top", position("left", 75, "%", "top", 0, "%")], |
| 75 | + ["75% bottom", position("left", 75, "%", "bottom", 0, "%")], |
| 76 | + ["75% 75%", position("left", 75, "%", "top", 75, "%")], |
| 77 | + # three components |
| 78 | + ["bottom left 1%", position("left", 1, "%", "bottom", 0, "%")], |
| 79 | + ["bottom right 2%", position("right", 2, "%", "bottom", 0, "%")], |
| 80 | + ["bottom 3% center", position("left", 50, "%", "bottom", 3, "%")], |
| 81 | + ["bottom 4% left", position("left", 0, "%", "bottom", 4, "%")], |
| 82 | + ["bottom 5% right", position("right", 0, "%", "bottom", 5, "%")], |
| 83 | + ["center bottom 6%", position("left", 50, "%", "bottom", 6, "%")], |
| 84 | + ["center left 7%", position("left", 7, "%", "top", 50, "%")], |
| 85 | + ["center right 8%", position("right", 8, "%", "top", 50, "%")], |
| 86 | + ["center top 9%", position("left", 50, "%", "top", 9, "%")], |
| 87 | + ["left bottom 10%", position("left", 0, "%", "bottom", 10, "%")], |
| 88 | + ["left top 11%", position("left", 0, "%", "top", 11, "%")], |
| 89 | + ["left 12% bottom", position("left", 12, "%", "bottom", 0, "%")], |
| 90 | + ["left 13% center", position("left", 13, "%", "top", 50, "%")], |
| 91 | + ["left 14% top", position("left", 14, "%", "top", 0, "%")], |
| 92 | + ["right bottom 15%", position("right", 0, "%", "bottom", 15, "%")], |
| 93 | + ["right top 16%", position("right", 0, "%", "top", 16, "%")], |
| 94 | + ["right 17% bottom", position("right", 17, "%", "bottom", 0, "%")], |
| 95 | + ["right 18% center", position("right", 18, "%", "top", 50, "%")], |
| 96 | + ["right 19% top", position("right", 19, "%", "top", 0, "%")], |
| 97 | + ["top left 20%", position("left", 20, "%", "top", 0, "%")], |
| 98 | + ["top right 21%", position("right", 21, "%", "top", 0, "%")], |
| 99 | + ["top 22% center", position("left", 50, "%", "top", 22, "%")], |
| 100 | + ["top 23% left", position("left", 0, "%", "top", 23, "%")], |
| 101 | + ["top 24% right", position("right", 0, "%", "top", 24, "%")], |
| 102 | + # four components |
| 103 | + ["bottom 25% left 75%", position("left", 75, "%", "bottom", 25, "%")], |
| 104 | + ["bottom 25% right 75%", position("right", 75, "%", "bottom", 25, "%")], |
| 105 | + ["left 25% bottom 75%", position("left", 25, "%", "bottom", 75, "%")], |
| 106 | + ["right 25% bottom 75%", position("right", 25, "%", "bottom", 75, "%")], |
| 107 | + ["top 25% left 75%", position("left", 75, "%", "top", 25, "%")], |
| 108 | + ["top 25% right 75%", position("right", 75, "%", "top", 25, "%")], |
| 109 | + ["left 25% top 75%", position("left", 25, "%", "top", 75, "%")], |
| 110 | + ["right 25% top 75%", position("right", 25, "%", "top", 75, "%")] |
| 111 | + ] |
| 112 | + |
| 113 | + def test_positions(self): |
| 114 | + for test in self.tests: |
| 115 | + with self.subTest(test[0]): |
| 116 | + c = parse_position(test[0]) |
| 117 | + self.assertEqual(c, test[1]) |
| 118 | + |
| 119 | +if __name__ == '__main__': |
| 120 | + unittest.main() |
0 commit comments