OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python2.4 |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 |
| 7 """Unittests for grit.format.policy_templates.writers.adml_writer.""" |
| 8 |
| 9 |
| 10 import os |
| 11 import sys |
| 12 import unittest |
| 13 if __name__ == '__main__': |
| 14 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../..')) |
| 15 |
| 16 |
| 17 from grit.format.policy_templates.writers import adml_writer |
| 18 from grit.format.policy_templates.writers import xml_writer_base_unittest |
| 19 from xml.dom import minidom |
| 20 |
| 21 |
| 22 class AdmlWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): |
| 23 |
| 24 def setUp(self): |
| 25 config = { |
| 26 'build': 'test', |
| 27 'win_supported_os': 'SUPPORTED_TESTOS', |
| 28 } |
| 29 self.writer = adml_writer.GetWriter(config) |
| 30 self.writer.messages = { |
| 31 'win_supported_winxpsp2': { |
| 32 'text': 'Supported on Test OS or higher', |
| 33 'desc': 'blah' |
| 34 } |
| 35 } |
| 36 self.writer.Init() |
| 37 |
| 38 def _InitWriterForAddingPolicyGroups(self, writer): |
| 39 '''Initialize the writer for adding policy groups. This method must be |
| 40 called before the method "BeginPolicyGroup" can be called. It initializes |
| 41 attributes of the writer. |
| 42 ''' |
| 43 writer.BeginTemplate() |
| 44 |
| 45 def _InitWriterForAddingPolicies(self, writer, policy): |
| 46 '''Initialize the writer for adding policies. This method must be |
| 47 called before the method "WritePolicy" can be called. It initializes |
| 48 attributes of the writer. |
| 49 ''' |
| 50 self._InitWriterForAddingPolicyGroups(writer) |
| 51 policy_group = { |
| 52 'name': 'PolicyGroup', |
| 53 'caption': 'Test Caption', |
| 54 'desc': 'This is the test description of the test policy group.', |
| 55 'policies': policy, |
| 56 } |
| 57 writer.BeginPolicyGroup(policy_group) |
| 58 |
| 59 string_elements = \ |
| 60 self.writer._string_table_elem.getElementsByTagName('string') |
| 61 for elem in string_elements: |
| 62 self.writer._string_table_elem.removeChild(elem) |
| 63 |
| 64 def testEmpty(self): |
| 65 self.writer.BeginTemplate() |
| 66 self.writer.EndTemplate() |
| 67 output = self.writer.GetTemplateText() |
| 68 expected_output = ( |
| 69 '<?xml version="1.0" ?><policyDefinitionResources' |
| 70 ' revision="1.0" schemaVersion="1.0"><displayName/><description/>' |
| 71 '<resources><stringTable><string id="SUPPORTED_TESTOS">Supported on' |
| 72 ' Test OS or higher</string></stringTable><presentationTable/>' |
| 73 '</resources></policyDefinitionResources>') |
| 74 self.AssertXMLEquals(output, expected_output) |
| 75 |
| 76 def testPolicyGroup(self): |
| 77 empty_policy_group = { |
| 78 'name': 'PolicyGroup', |
| 79 'caption': 'Test Group Caption', |
| 80 'desc': 'This is the test description of the test policy group.', |
| 81 'policies': [ |
| 82 {'name': 'PolicyStub2', |
| 83 'type': 'main'}, |
| 84 {'name': 'PolicyStub1', |
| 85 'type': 'main'}, |
| 86 ], |
| 87 } |
| 88 self._InitWriterForAddingPolicyGroups(self.writer) |
| 89 self.writer.BeginPolicyGroup(empty_policy_group) |
| 90 self.writer.EndPolicyGroup |
| 91 # Assert generated string elements. |
| 92 output = self.GetXMLOfChildren(self.writer._string_table_elem) |
| 93 expected_output = ( |
| 94 '<string id="SUPPORTED_TESTOS">\n' |
| 95 ' Supported on Test OS or higher\n' |
| 96 '</string>\n' |
| 97 '<string id="PolicyGroup_group">\n' |
| 98 ' Test Group Caption\n' |
| 99 '</string>') |
| 100 self.AssertXMLEquals(output, expected_output) |
| 101 # Assert generated presentation elements. |
| 102 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) |
| 103 expected_output = '' |
| 104 self.AssertXMLEquals(output, expected_output) |
| 105 |
| 106 def testMainPolicy(self): |
| 107 main_policy = { |
| 108 'name': 'DummyMainPolicy', |
| 109 'type': 'main', |
| 110 'caption': 'Main policy caption', |
| 111 'desc': 'Main policy test description.' |
| 112 } |
| 113 self. _InitWriterForAddingPolicies(self.writer, main_policy) |
| 114 self.writer.WritePolicy(main_policy) |
| 115 # Assert generated string elements. |
| 116 output = self.GetXMLOfChildren(self.writer._string_table_elem) |
| 117 expected_output = ( |
| 118 '<string id="DummyMainPolicy">\n' |
| 119 ' Main policy caption\n' |
| 120 '</string>\n' |
| 121 '<string id="DummyMainPolicy_Explain">\n' |
| 122 ' Main policy test description.\n' |
| 123 '</string>') |
| 124 self.AssertXMLEquals(output, expected_output) |
| 125 # Assert generated presentation elements. |
| 126 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) |
| 127 expected_output = '<presentation id="DummyMainPolicy"/>' |
| 128 self.AssertXMLEquals(output, expected_output) |
| 129 |
| 130 def testStringPolicy(self): |
| 131 string_policy = { |
| 132 'name': 'StringPolicyStub', |
| 133 'type': 'string', |
| 134 'caption': 'String policy caption', |
| 135 'label': 'String policy label', |
| 136 'desc': 'This is a test description.', |
| 137 } |
| 138 self. _InitWriterForAddingPolicies(self.writer, string_policy) |
| 139 self.writer.WritePolicy(string_policy) |
| 140 # Assert generated string elements. |
| 141 output = self.GetXMLOfChildren(self.writer._string_table_elem) |
| 142 expected_output = ( |
| 143 '<string id="StringPolicyStub">\n' |
| 144 ' String policy caption\n' |
| 145 '</string>\n' |
| 146 '<string id="StringPolicyStub_Explain">\n' |
| 147 ' This is a test description.\n' |
| 148 '</string>') |
| 149 self.AssertXMLEquals(output, expected_output) |
| 150 # Assert generated presentation elements. |
| 151 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) |
| 152 expected_output = ( |
| 153 '<presentation id="StringPolicyStub">\n' |
| 154 ' <textBox refId="StringPolicyStub">\n' |
| 155 ' <label>\n' |
| 156 ' String policy label\n' |
| 157 ' </label>\n' |
| 158 ' </textBox>\n' |
| 159 '</presentation>') |
| 160 self.AssertXMLEquals(output, expected_output) |
| 161 |
| 162 def testIntPolicy(self): |
| 163 int_policy = { |
| 164 'name': 'IntPolicyStub', |
| 165 'type': 'int', |
| 166 'caption': 'Int policy caption', |
| 167 'label': 'Int policy label', |
| 168 'desc': 'This is a test description.', |
| 169 } |
| 170 self. _InitWriterForAddingPolicies(self.writer, int_policy) |
| 171 self.writer.WritePolicy(int_policy) |
| 172 # Assert generated string elements. |
| 173 output = self.GetXMLOfChildren(self.writer._string_table_elem) |
| 174 expected_output = ( |
| 175 '<string id="IntPolicyStub">\n' |
| 176 ' Int policy caption\n' |
| 177 '</string>\n' |
| 178 '<string id="IntPolicyStub_Explain">\n' |
| 179 ' This is a test description.\n' |
| 180 '</string>') |
| 181 self.AssertXMLEquals(output, expected_output) |
| 182 # Assert generated presentation elements. |
| 183 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) |
| 184 expected_output = ( |
| 185 '<presentation id="IntPolicyStub">\n' |
| 186 ' <decimalTextBox refId="IntPolicyStub">\n' |
| 187 ' Int policy label:\n' |
| 188 ' </decimalTextBox>\n' |
| 189 '</presentation>') |
| 190 self.AssertXMLEquals(output, expected_output) |
| 191 |
| 192 def testIntEnumPolicy(self): |
| 193 enum_policy = { |
| 194 'name': 'EnumPolicyStub', |
| 195 'type': 'int-enum', |
| 196 'caption': 'Enum policy caption', |
| 197 'label': 'Enum policy label', |
| 198 'desc': 'This is a test description.', |
| 199 'items': [ |
| 200 { |
| 201 'name': 'item 1', |
| 202 'value': 1, |
| 203 'caption': 'Caption Item 1', |
| 204 }, |
| 205 { |
| 206 'name': 'item 2', |
| 207 'value': 2, |
| 208 'caption': 'Caption Item 2', |
| 209 }, |
| 210 ], |
| 211 } |
| 212 self. _InitWriterForAddingPolicies(self.writer, enum_policy) |
| 213 self.writer.WritePolicy(enum_policy) |
| 214 # Assert generated string elements. |
| 215 output = self.GetXMLOfChildren(self.writer._string_table_elem) |
| 216 expected_output = ( |
| 217 '<string id="EnumPolicyStub">\n' |
| 218 ' Enum policy caption\n' |
| 219 '</string>\n' |
| 220 '<string id="EnumPolicyStub_Explain">\n' |
| 221 ' This is a test description.\n' |
| 222 '</string>\n' |
| 223 '<string id="item 1">\n' |
| 224 ' Caption Item 1\n' |
| 225 '</string>\n' |
| 226 '<string id="item 2">\n' |
| 227 ' Caption Item 2\n' |
| 228 '</string>') |
| 229 self.AssertXMLEquals(output, expected_output) |
| 230 # Assert generated presentation elements. |
| 231 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) |
| 232 expected_output = ( |
| 233 '<presentation id="EnumPolicyStub">\n' |
| 234 ' <dropdownList refId="EnumPolicyStub">\n' |
| 235 ' Enum policy label\n' |
| 236 ' </dropdownList>\n' |
| 237 '</presentation>') |
| 238 self.AssertXMLEquals(output, expected_output) |
| 239 |
| 240 def testStringEnumPolicy(self): |
| 241 enum_policy = { |
| 242 'name': 'EnumPolicyStub', |
| 243 'type': 'string-enum', |
| 244 'caption': 'Enum policy caption', |
| 245 'label': 'Enum policy label', |
| 246 'desc': 'This is a test description.', |
| 247 'items': [ |
| 248 { |
| 249 'name': 'item 1', |
| 250 'value': 'value 1', |
| 251 'caption': 'Caption Item 1', |
| 252 }, |
| 253 { |
| 254 'name': 'item 2', |
| 255 'value': 'value 2', |
| 256 'caption': 'Caption Item 2', |
| 257 }, |
| 258 ], |
| 259 } |
| 260 self. _InitWriterForAddingPolicies(self.writer, enum_policy) |
| 261 self.writer.WritePolicy(enum_policy) |
| 262 # Assert generated string elements. |
| 263 output = self.GetXMLOfChildren(self.writer._string_table_elem) |
| 264 expected_output = ( |
| 265 '<string id="EnumPolicyStub">\n' |
| 266 ' Enum policy caption\n' |
| 267 '</string>\n' |
| 268 '<string id="EnumPolicyStub_Explain">\n' |
| 269 ' This is a test description.\n' |
| 270 '</string>\n' |
| 271 '<string id="item 1">\n' |
| 272 ' Caption Item 1\n' |
| 273 '</string>\n' |
| 274 '<string id="item 2">\n' |
| 275 ' Caption Item 2\n' |
| 276 '</string>') |
| 277 self.AssertXMLEquals(output, expected_output) |
| 278 # Assert generated presentation elements. |
| 279 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) |
| 280 expected_output = ( |
| 281 '<presentation id="EnumPolicyStub">\n' |
| 282 ' <dropdownList refId="EnumPolicyStub">\n' |
| 283 ' Enum policy label\n' |
| 284 ' </dropdownList>\n' |
| 285 '</presentation>') |
| 286 self.AssertXMLEquals(output, expected_output) |
| 287 |
| 288 def testListPolicy(self): |
| 289 list_policy = { |
| 290 'name': 'ListPolicyStub', |
| 291 'type': 'list', |
| 292 'caption': 'List policy caption', |
| 293 'label': 'List policy label', |
| 294 'desc': 'This is a test description.', |
| 295 } |
| 296 self. _InitWriterForAddingPolicies(self.writer, list_policy) |
| 297 self.writer.WritePolicy(list_policy) |
| 298 # Assert generated string elements. |
| 299 output = self.GetXMLOfChildren(self.writer._string_table_elem) |
| 300 expected_output = ( |
| 301 '<string id="ListPolicyStub">\n' |
| 302 ' List policy caption\n' |
| 303 '</string>\n' |
| 304 '<string id="ListPolicyStub_Explain">\n' |
| 305 ' This is a test description.\n' |
| 306 '</string>\n' |
| 307 '<string id="ListPolicyStubDesc">\n' |
| 308 ' List policy caption\n' |
| 309 '</string>') |
| 310 self.AssertXMLEquals(output, expected_output) |
| 311 # Assert generated presentation elements. |
| 312 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) |
| 313 expected_output = ( |
| 314 '<presentation id="ListPolicyStub">\n' |
| 315 ' <listBox refId="ListPolicyStubDesc">\n' |
| 316 ' List policy label\n' |
| 317 ' </listBox>\n' |
| 318 '</presentation>') |
| 319 self.AssertXMLEquals(output, expected_output) |
| 320 |
| 321 def testPlatform(self): |
| 322 # Test that the writer correctly chooses policies of platform Windows. |
| 323 self.assertTrue(self.writer.IsPolicySupported({ |
| 324 'supported_on': [ |
| 325 {'platforms': ['win', 'zzz']}, {'platforms': ['aaa']} |
| 326 ] |
| 327 })) |
| 328 self.assertFalse(self.writer.IsPolicySupported({ |
| 329 'supported_on': [ |
| 330 {'platforms': ['mac', 'linux']}, {'platforms': ['aaa']} |
| 331 ] |
| 332 })) |
| 333 |
| 334 |
| 335 if __name__ == '__main__': |
| 336 unittest.main() |
OLD | NEW |