Igor Murashkin | aa133d3 | 2013-06-28 17:27:49 -0700 | [diff] [blame] | 1 | import unittest |
Igor Murashkin | 0b08045 | 2013-12-27 15:30:25 -0800 | [diff] [blame] | 2 | import itertools |
Igor Murashkin | aa133d3 | 2013-06-28 17:27:49 -0700 | [diff] [blame] | 3 | from unittest import TestCase |
| 4 | from metadata_model import * |
| 5 | from metadata_helpers import * |
Eino-Ville Talvala | 63c0fb2 | 2014-01-02 16:11:44 -0800 | [diff] [blame] | 6 | from metadata_parser_xml import * |
| 7 | |
| 8 | # Simple test metadata block used by the tests below |
| 9 | test_metadata_xml = \ |
| 10 | ''' |
| 11 | <?xml version="1.0" encoding="utf-8"?> |
| 12 | <metadata xmlns="http://schemas.android.com/service/camera/metadata/" |
| 13 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 14 | xsi:schemaLocation="http://schemas.android.com/service/camera/metadata/ metadata_properties.xsd"> |
| 15 | |
| 16 | <namespace name="testOuter1"> |
| 17 | <section name="testSection1"> |
| 18 | <controls> |
| 19 | <entry name="control1" type="byte" visibility="public"> |
| 20 | </entry> |
| 21 | <entry name="control2" type="byte" visibility="public"> |
| 22 | </entry> |
| 23 | </controls> |
| 24 | <dynamic> |
| 25 | <entry name="dynamic1" type="byte" visibility="public"> |
| 26 | </entry> |
| 27 | <entry name="dynamic2" type="byte" visibility="public"> |
| 28 | </entry> |
| 29 | <clone entry="testOuter1.testSection1.control1" kind="controls"> |
| 30 | </clone> |
| 31 | </dynamic> |
| 32 | <static> |
| 33 | <entry name="static1" type="byte" visibility="public"> |
| 34 | </entry> |
| 35 | <entry name="static2" type="byte" visibility="public"> |
| 36 | </entry> |
| 37 | </static> |
| 38 | </section> |
| 39 | </namespace> |
| 40 | <namespace name="testOuter2"> |
| 41 | <section name="testSection2"> |
| 42 | <controls> |
| 43 | <entry name="control1" type="byte" visibility="public"> |
| 44 | </entry> |
| 45 | <entry name="control2" type="byte" visibility="public"> |
| 46 | </entry> |
| 47 | </controls> |
| 48 | <dynamic> |
| 49 | <entry name="dynamic1" type="byte" visibility="public"> |
| 50 | </entry> |
| 51 | <entry name="dynamic2" type="byte" visibility="public"> |
| 52 | </entry> |
Igor Murashkin | 23fed4b | 2014-01-09 17:44:57 -0800 | [diff] [blame] | 53 | <clone entry="testOuter2.testSection2.control1" kind="controls"> |
Eino-Ville Talvala | 63c0fb2 | 2014-01-02 16:11:44 -0800 | [diff] [blame] | 54 | </clone> |
| 55 | </dynamic> |
| 56 | <static> |
| 57 | <namespace name="testInner2"> |
| 58 | <entry name="static1" type="byte" visibility="public"> |
| 59 | </entry> |
| 60 | <entry name="static2" type="byte" visibility="public"> |
| 61 | </entry> |
| 62 | </namespace> |
| 63 | </static> |
| 64 | </section> |
| 65 | </namespace> |
| 66 | </metadata> |
| 67 | ''' |
Igor Murashkin | aa133d3 | 2013-06-28 17:27:49 -0700 | [diff] [blame] | 68 | |
| 69 | class TestHelpers(TestCase): |
| 70 | |
| 71 | def test_enum_calculate_value_string(self): |
| 72 | def compare_values_against_list(expected_list, enum): |
| 73 | for (idx, val) in enumerate(expected_list): |
| 74 | self.assertEquals(val, |
| 75 | enum_calculate_value_string(list(enum.values)[idx])) |
| 76 | |
| 77 | plain_enum = Enum(parent=None, values=['ON', 'OFF']) |
| 78 | |
| 79 | compare_values_against_list(['0', '1'], |
| 80 | plain_enum) |
| 81 | |
| 82 | ### |
| 83 | labeled_enum = Enum(parent=None, values=['A', 'B', 'C'], ids={ |
| 84 | 'A': '12345', |
| 85 | 'B': '0xC0FFEE', |
| 86 | 'C': '0xDEADF00D' |
| 87 | }) |
| 88 | |
| 89 | compare_values_against_list(['12345', '0xC0FFEE', '0xDEADF00D'], |
| 90 | labeled_enum) |
| 91 | |
| 92 | ### |
| 93 | mixed_enum = Enum(parent=None, |
| 94 | values=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], |
| 95 | ids={ |
| 96 | 'C': '0xC0FFEE', |
| 97 | 'E': '123', |
| 98 | 'G': '0xDEADF00D' |
| 99 | }) |
| 100 | |
| 101 | expected_values = ['0', '1', '0xC0FFEE', '0xC0FFEF', '123', '124', |
| 102 | '0xDEADF00D', |
| 103 | '0xDEADF00E'] |
| 104 | |
| 105 | compare_values_against_list(expected_values, mixed_enum) |
| 106 | |
| 107 | def test_enumerate_with_last(self): |
| 108 | empty_list = [] |
| 109 | |
| 110 | for (x, y) in enumerate_with_last(empty_list): |
| 111 | self.fail("Should not return anything for empty list") |
| 112 | |
| 113 | single_value = [1] |
| 114 | for (x, last) in enumerate_with_last(single_value): |
| 115 | self.assertEquals(1, x) |
| 116 | self.assertEquals(True, last) |
| 117 | |
| 118 | multiple_values = [4, 5, 6] |
| 119 | lst = list(enumerate_with_last(multiple_values)) |
| 120 | self.assertListEqual([(4, False), (5, False), (6, True)], lst) |
| 121 | |
Eino-Ville Talvala | 63c0fb2 | 2014-01-02 16:11:44 -0800 | [diff] [blame] | 122 | def test_filter_tags(self): |
| 123 | metadata = MetadataParserXml(test_metadata_xml, 'metadata_helpers_test.py').metadata |
| 124 | |
| 125 | test_text = \ |
| 126 | ''' |
| 127 | In the unlikely event of a |
| 128 | water landing, testOuter1.testSection1.control1 will deploy. |
| 129 | If testOuter2.testSection2.testInner2.static1, |
| 130 | then testOuter1.testSection1. |
| 131 | dynamic1 will ensue. That should be avoided if testOuter2.testSection2. |
| 132 | Barring issues, testOuter1.testSection1.dynamic1, and testOuter2.testSection2.control1. |
| 133 | If the path foo/android.testOuter1.testSection1.control1/bar.txt exists, then oh well. |
| 134 | ''' |
| 135 | def filter_test(node): |
| 136 | return '*' |
| 137 | |
| 138 | def summary_test(node_set): |
| 139 | text = "*" * len(node_set) + "\n" |
| 140 | return text |
| 141 | |
| 142 | expected_text = \ |
| 143 | ''' |
| 144 | In the unlikely event of a |
| 145 | water landing, * will deploy. |
| 146 | If *, |
| 147 | then * will ensue. That should be avoided if testOuter2.testSection2. |
| 148 | Barring issues, *, and *. |
| 149 | If the path foo/android.testOuter1.testSection1.control1/bar.txt exists, then oh well. |
| 150 | **** |
| 151 | ''' |
| 152 | result_text = filter_tags(test_text, metadata, filter_test, summary_test) |
| 153 | |
| 154 | self.assertEqual(result_text, expected_text) |
| 155 | |
Igor Murashkin | 0b08045 | 2013-12-27 15:30:25 -0800 | [diff] [blame] | 156 | def test_wbr(self): |
| 157 | wbr_string = "<wbr/>" |
| 158 | wbr_gen = itertools.repeat(wbr_string) |
| 159 | |
| 160 | # No special characters, do nothing |
| 161 | self.assertEquals("no-op", wbr("no-op")) |
| 162 | # Insert WBR after characters in [ '.', '/', '_' ] |
| 163 | self.assertEquals("word.{0}".format(wbr_string), wbr("word.")) |
| 164 | self.assertEquals("word/{0}".format(wbr_string), wbr("word/")) |
| 165 | self.assertEquals("word_{0}".format(wbr_string), wbr("word_")) |
| 166 | |
| 167 | self.assertEquals("word.{0}break".format(wbr_string), wbr("word.break")) |
| 168 | self.assertEquals("word/{0}break".format(wbr_string), wbr("word/break")) |
| 169 | self.assertEquals("word_{0}break".format(wbr_string), wbr("word_break")) |
| 170 | |
| 171 | # Test words with more components |
| 172 | self.assertEquals("word_{0}break_{0}again".format(wbr_string), |
| 173 | wbr("word_break_again")) |
| 174 | self.assertEquals("word_{0}break_{0}again_{0}emphasis".format(wbr_string), |
| 175 | wbr("word_break_again_emphasis")) |
| 176 | |
| 177 | # Words with 2 or less subcomponents are ignored for the capital letters |
| 178 | self.assertEquals("word_{0}breakIgnored".format(wbr_string), |
| 179 | wbr("word_breakIgnored")) |
| 180 | self.assertEquals("wordIgnored".format(wbr_string), |
| 181 | wbr("wordIgnored")) |
| 182 | |
| 183 | # Words with at least 3 sub components get word breaks before caps |
| 184 | self.assertEquals("word_{0}break_{0}again{0}Capitalized".format(wbr_string), |
| 185 | wbr("word_break_againCapitalized")) |
| 186 | self.assertEquals("word.{0}break.{0}again{0}Capitalized".format(wbr_string), |
| 187 | wbr("word.break.againCapitalized")) |
| 188 | self.assertEquals("a.{0}b{0}C.{0}d{0}E.{0}f{0}G".format(wbr_string), |
| 189 | wbr("a.bC.dE.fG")) |
| 190 | |
| 191 | # Don't be overly aggressive with all caps |
| 192 | self.assertEquals("TRANSFORM_{0}MATRIX".format(wbr_string), |
| 193 | wbr("TRANSFORM_MATRIX")) |
| 194 | |
| 195 | self.assertEquals("SCENE_{0}MODE_{0}FACE_{0}PRIORITY".format(wbr_string), |
| 196 | wbr("SCENE_MODE_FACE_PRIORITY")) |
| 197 | |
| 198 | self.assertEquals("android.{0}color{0}Correction.{0}mode is TRANSFORM_{0}MATRIX.{0}".format(wbr_string), |
| 199 | wbr("android.colorCorrection.mode is TRANSFORM_MATRIX.")) |
| 200 | |
| 201 | self.assertEquals("The overrides listed for SCENE_{0}MODE_{0}FACE_{0}PRIORITY are ignored".format(wbr_string), |
| 202 | wbr("The overrides listed for SCENE_MODE_FACE_PRIORITY are ignored")); |
| 203 | |
Igor Murashkin | 88b858d | 2014-01-14 14:57:46 -0800 | [diff] [blame^] | 204 | def test_dedent(self): |
| 205 | # Remove whitespace from 2nd and 3rd line (equal ws) |
| 206 | self.assertEquals("bar\nline1\nline2", dedent("bar\n line1\n line2")) |
| 207 | # Remove whitespace from all lines (1st line ws < 2/3 line ws) |
| 208 | self.assertEquals("bar\nline1\nline2", dedent(" bar\n line1\n line2")) |
| 209 | # Remove some whitespace from 2nd line, all whitespace from other lines |
| 210 | self.assertEquals("bar\n line1\nline2", dedent(" bar\n line1\n line2")) |
| 211 | |
Igor Murashkin | aa133d3 | 2013-06-28 17:27:49 -0700 | [diff] [blame] | 212 | if __name__ == '__main__': |
| 213 | unittest.main() |