| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2012 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(__file__), '../../../..')) | |
| 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 | |
| 20 | |
| 21 class AdmlWriterUnittest(xml_writer_base_unittest.XmlWriterBaseTest): | |
| 22 | |
| 23 def setUp(self): | |
| 24 config = { | |
| 25 'app_name': 'test', | |
| 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 'doc_recommended': { | |
| 36 'text': 'Recommended', | |
| 37 'desc': 'bleh' | |
| 38 }, | |
| 39 } | |
| 40 self.writer.Init() | |
| 41 | |
| 42 def _InitWriterForAddingPolicyGroups(self, writer): | |
| 43 '''Initialize the writer for adding policy groups. This method must be | |
| 44 called before the method "BeginPolicyGroup" can be called. It initializes | |
| 45 attributes of the writer. | |
| 46 ''' | |
| 47 writer.BeginTemplate() | |
| 48 | |
| 49 def _InitWriterForAddingPolicies(self, writer, policy): | |
| 50 '''Initialize the writer for adding policies. This method must be | |
| 51 called before the method "WritePolicy" can be called. It initializes | |
| 52 attributes of the writer. | |
| 53 ''' | |
| 54 self._InitWriterForAddingPolicyGroups(writer) | |
| 55 policy_group = { | |
| 56 'name': 'PolicyGroup', | |
| 57 'caption': 'Test Caption', | |
| 58 'desc': 'This is the test description of the test policy group.', | |
| 59 'policies': policy, | |
| 60 } | |
| 61 writer.BeginPolicyGroup(policy_group) | |
| 62 | |
| 63 string_elements = \ | |
| 64 self.writer._string_table_elem.getElementsByTagName('string') | |
| 65 for elem in string_elements: | |
| 66 self.writer._string_table_elem.removeChild(elem) | |
| 67 | |
| 68 def testEmpty(self): | |
| 69 self.writer.BeginTemplate() | |
| 70 self.writer.EndTemplate() | |
| 71 output = self.writer.GetTemplateText() | |
| 72 expected_output = ( | |
| 73 '<?xml version="1.0" ?><policyDefinitionResources' | |
| 74 ' revision="1.0" schemaVersion="1.0"><displayName/><description/>' | |
| 75 '<resources><stringTable><string id="SUPPORTED_TESTOS">Supported on' | |
| 76 ' Test OS or higher</string></stringTable><presentationTable/>' | |
| 77 '</resources></policyDefinitionResources>') | |
| 78 self.AssertXMLEquals(output, expected_output) | |
| 79 | |
| 80 def testVersionAnnotation(self): | |
| 81 self.writer.config['version'] = '39.0.0.0' | |
| 82 self.writer.BeginTemplate() | |
| 83 self.writer.EndTemplate() | |
| 84 output = self.writer.GetTemplateText() | |
| 85 expected_output = ( | |
| 86 '<?xml version="1.0" ?><policyDefinitionResources' | |
| 87 ' revision="1.0" schemaVersion="1.0"><!--test version: 39.0.0.0-->' | |
| 88 '<displayName/><description/><resources><stringTable>' | |
| 89 '<string id="SUPPORTED_TESTOS">Supported on' | |
| 90 ' Test OS or higher</string></stringTable><presentationTable/>' | |
| 91 '</resources></policyDefinitionResources>') | |
| 92 self.AssertXMLEquals(output, expected_output) | |
| 93 | |
| 94 def testPolicyGroup(self): | |
| 95 empty_policy_group = { | |
| 96 'name': 'PolicyGroup', | |
| 97 'caption': 'Test Group Caption', | |
| 98 'desc': 'This is the test description of the test policy group.', | |
| 99 'policies': [ | |
| 100 {'name': 'PolicyStub2', | |
| 101 'type': 'main'}, | |
| 102 {'name': 'PolicyStub1', | |
| 103 'type': 'main'}, | |
| 104 ], | |
| 105 } | |
| 106 self._InitWriterForAddingPolicyGroups(self.writer) | |
| 107 self.writer.BeginPolicyGroup(empty_policy_group) | |
| 108 self.writer.EndPolicyGroup | |
| 109 # Assert generated string elements. | |
| 110 output = self.GetXMLOfChildren(self.writer._string_table_elem) | |
| 111 expected_output = ( | |
| 112 '<string id="SUPPORTED_TESTOS">' | |
| 113 'Supported on Test OS or higher</string>\n' | |
| 114 '<string id="PolicyGroup_group">Test Group Caption</string>') | |
| 115 self.AssertXMLEquals(output, expected_output) | |
| 116 # Assert generated presentation elements. | |
| 117 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) | |
| 118 expected_output = '' | |
| 119 self.AssertXMLEquals(output, expected_output) | |
| 120 | |
| 121 def testMainPolicy(self): | |
| 122 main_policy = { | |
| 123 'name': 'DummyMainPolicy', | |
| 124 'type': 'main', | |
| 125 'caption': 'Main policy caption', | |
| 126 'desc': 'Main policy test description.' | |
| 127 } | |
| 128 self. _InitWriterForAddingPolicies(self.writer, main_policy) | |
| 129 self.writer.WritePolicy(main_policy) | |
| 130 # Assert generated string elements. | |
| 131 output = self.GetXMLOfChildren(self.writer._string_table_elem) | |
| 132 expected_output = ( | |
| 133 '<string id="DummyMainPolicy">Main policy caption</string>\n' | |
| 134 '<string id="DummyMainPolicy_Explain">' | |
| 135 'Main policy test description.</string>') | |
| 136 self.AssertXMLEquals(output, expected_output) | |
| 137 # Assert generated presentation elements. | |
| 138 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) | |
| 139 expected_output = '<presentation id="DummyMainPolicy"/>' | |
| 140 self.AssertXMLEquals(output, expected_output) | |
| 141 | |
| 142 def testStringPolicy(self): | |
| 143 string_policy = { | |
| 144 'name': 'StringPolicyStub', | |
| 145 'type': 'string', | |
| 146 'caption': 'String policy caption', | |
| 147 'label': 'String policy label', | |
| 148 'desc': 'This is a test description.', | |
| 149 } | |
| 150 self. _InitWriterForAddingPolicies(self.writer, string_policy) | |
| 151 self.writer.WritePolicy(string_policy) | |
| 152 # Assert generated string elements. | |
| 153 output = self.GetXMLOfChildren(self.writer._string_table_elem) | |
| 154 expected_output = ( | |
| 155 '<string id="StringPolicyStub">String policy caption</string>\n' | |
| 156 '<string id="StringPolicyStub_Explain">' | |
| 157 'This is a test description.</string>') | |
| 158 self.AssertXMLEquals(output, expected_output) | |
| 159 # Assert generated presentation elements. | |
| 160 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) | |
| 161 expected_output = ( | |
| 162 '<presentation id="StringPolicyStub">\n' | |
| 163 ' <textBox refId="StringPolicyStub">\n' | |
| 164 ' <label>String policy label</label>\n' | |
| 165 ' </textBox>\n' | |
| 166 '</presentation>') | |
| 167 self.AssertXMLEquals(output, expected_output) | |
| 168 | |
| 169 def testIntPolicy(self): | |
| 170 int_policy = { | |
| 171 'name': 'IntPolicyStub', | |
| 172 'type': 'int', | |
| 173 'caption': 'Int policy caption', | |
| 174 'label': 'Int policy label', | |
| 175 'desc': 'This is a test description.', | |
| 176 } | |
| 177 self. _InitWriterForAddingPolicies(self.writer, int_policy) | |
| 178 self.writer.WritePolicy(int_policy) | |
| 179 # Assert generated string elements. | |
| 180 output = self.GetXMLOfChildren(self.writer._string_table_elem) | |
| 181 expected_output = ( | |
| 182 '<string id="IntPolicyStub">Int policy caption</string>\n' | |
| 183 '<string id="IntPolicyStub_Explain">' | |
| 184 'This is a test description.</string>') | |
| 185 self.AssertXMLEquals(output, expected_output) | |
| 186 # Assert generated presentation elements. | |
| 187 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) | |
| 188 expected_output = ( | |
| 189 '<presentation id="IntPolicyStub">\n' | |
| 190 ' <decimalTextBox refId="IntPolicyStub">' | |
| 191 'Int policy label:</decimalTextBox>\n' | |
| 192 '</presentation>') | |
| 193 self.AssertXMLEquals(output, expected_output) | |
| 194 | |
| 195 def testIntEnumPolicy(self): | |
| 196 enum_policy = { | |
| 197 'name': 'EnumPolicyStub', | |
| 198 'type': 'int-enum', | |
| 199 'caption': 'Enum policy caption', | |
| 200 'label': 'Enum policy label', | |
| 201 'desc': 'This is a test description.', | |
| 202 'items': [ | |
| 203 { | |
| 204 'name': 'item 1', | |
| 205 'value': 1, | |
| 206 'caption': 'Caption Item 1', | |
| 207 }, | |
| 208 { | |
| 209 'name': 'item 2', | |
| 210 'value': 2, | |
| 211 'caption': 'Caption Item 2', | |
| 212 }, | |
| 213 ], | |
| 214 } | |
| 215 self. _InitWriterForAddingPolicies(self.writer, enum_policy) | |
| 216 self.writer.WritePolicy(enum_policy) | |
| 217 # Assert generated string elements. | |
| 218 output = self.GetXMLOfChildren(self.writer._string_table_elem) | |
| 219 expected_output = ( | |
| 220 '<string id="EnumPolicyStub">Enum policy caption</string>\n' | |
| 221 '<string id="EnumPolicyStub_Explain">' | |
| 222 'This is a test description.</string>\n' | |
| 223 '<string id="item 1">Caption Item 1</string>\n' | |
| 224 '<string id="item 2">Caption Item 2</string>') | |
| 225 self.AssertXMLEquals(output, expected_output) | |
| 226 # Assert generated presentation elements. | |
| 227 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) | |
| 228 expected_output = ( | |
| 229 '<presentation id="EnumPolicyStub">\n' | |
| 230 ' <dropdownList refId="EnumPolicyStub">' | |
| 231 'Enum policy label</dropdownList>\n' | |
| 232 '</presentation>') | |
| 233 self.AssertXMLEquals(output, expected_output) | |
| 234 | |
| 235 def testStringEnumPolicy(self): | |
| 236 enum_policy = { | |
| 237 'name': 'EnumPolicyStub', | |
| 238 'type': 'string-enum', | |
| 239 'caption': 'Enum policy caption', | |
| 240 'label': 'Enum policy label', | |
| 241 'desc': 'This is a test description.', | |
| 242 'items': [ | |
| 243 { | |
| 244 'name': 'item 1', | |
| 245 'value': 'value 1', | |
| 246 'caption': 'Caption Item 1', | |
| 247 }, | |
| 248 { | |
| 249 'name': 'item 2', | |
| 250 'value': 'value 2', | |
| 251 'caption': 'Caption Item 2', | |
| 252 }, | |
| 253 ], | |
| 254 } | |
| 255 self. _InitWriterForAddingPolicies(self.writer, enum_policy) | |
| 256 self.writer.WritePolicy(enum_policy) | |
| 257 # Assert generated string elements. | |
| 258 output = self.GetXMLOfChildren(self.writer._string_table_elem) | |
| 259 expected_output = ( | |
| 260 '<string id="EnumPolicyStub">Enum policy caption</string>\n' | |
| 261 '<string id="EnumPolicyStub_Explain">' | |
| 262 'This is a test description.</string>\n' | |
| 263 '<string id="item 1">Caption Item 1</string>\n' | |
| 264 '<string id="item 2">Caption Item 2</string>') | |
| 265 self.AssertXMLEquals(output, expected_output) | |
| 266 # Assert generated presentation elements. | |
| 267 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) | |
| 268 expected_output = ( | |
| 269 '<presentation id="EnumPolicyStub">\n' | |
| 270 ' <dropdownList refId="EnumPolicyStub">' | |
| 271 'Enum policy label</dropdownList>\n' | |
| 272 '</presentation>') | |
| 273 self.AssertXMLEquals(output, expected_output) | |
| 274 | |
| 275 def testListPolicy(self): | |
| 276 list_policy = { | |
| 277 'name': 'ListPolicyStub', | |
| 278 'type': 'list', | |
| 279 'caption': 'List policy caption', | |
| 280 'label': 'List policy label', | |
| 281 'desc': 'This is a test description.', | |
| 282 } | |
| 283 self. _InitWriterForAddingPolicies(self.writer, list_policy) | |
| 284 self.writer.WritePolicy(list_policy) | |
| 285 # Assert generated string elements. | |
| 286 output = self.GetXMLOfChildren(self.writer._string_table_elem) | |
| 287 expected_output = ( | |
| 288 '<string id="ListPolicyStub">List policy caption</string>\n' | |
| 289 '<string id="ListPolicyStub_Explain">' | |
| 290 'This is a test description.</string>\n' | |
| 291 '<string id="ListPolicyStubDesc">List policy caption</string>') | |
| 292 self.AssertXMLEquals(output, expected_output) | |
| 293 # Assert generated presentation elements. | |
| 294 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) | |
| 295 expected_output = ( | |
| 296 '<presentation id="ListPolicyStub">\n' | |
| 297 ' <listBox refId="ListPolicyStubDesc">List policy label</listBox>\n' | |
| 298 '</presentation>') | |
| 299 self.AssertXMLEquals(output, expected_output) | |
| 300 | |
| 301 def testStringEnumListPolicy(self): | |
| 302 list_policy = { | |
| 303 'name': 'ListPolicyStub', | |
| 304 'type': 'string-enum-list', | |
| 305 'caption': 'List policy caption', | |
| 306 'label': 'List policy label', | |
| 307 'desc': 'This is a test description.', | |
| 308 'items': [ | |
| 309 { | |
| 310 'name': 'item 1', | |
| 311 'value': 'value 1', | |
| 312 'caption': 'Caption Item 1', | |
| 313 }, | |
| 314 { | |
| 315 'name': 'item 2', | |
| 316 'value': 'value 2', | |
| 317 'caption': 'Caption Item 2', | |
| 318 }, | |
| 319 ], | |
| 320 } | |
| 321 self. _InitWriterForAddingPolicies(self.writer, list_policy) | |
| 322 self.writer.WritePolicy(list_policy) | |
| 323 # Assert generated string elements. | |
| 324 output = self.GetXMLOfChildren(self.writer._string_table_elem) | |
| 325 expected_output = ( | |
| 326 '<string id="ListPolicyStub">List policy caption</string>\n' | |
| 327 '<string id="ListPolicyStub_Explain">' | |
| 328 'This is a test description.</string>\n' | |
| 329 '<string id="ListPolicyStubDesc">List policy caption</string>') | |
| 330 self.AssertXMLEquals(output, expected_output) | |
| 331 # Assert generated presentation elements. | |
| 332 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) | |
| 333 expected_output = ( | |
| 334 '<presentation id="ListPolicyStub">\n' | |
| 335 ' <listBox refId="ListPolicyStubDesc">List policy label</listBox>\n' | |
| 336 '</presentation>') | |
| 337 self.AssertXMLEquals(output, expected_output) | |
| 338 | |
| 339 def testDictionaryPolicy(self): | |
| 340 dict_policy = { | |
| 341 'name': 'DictionaryPolicyStub', | |
| 342 'type': 'dict', | |
| 343 'caption': 'Dictionary policy caption', | |
| 344 'label': 'Dictionary policy label', | |
| 345 'desc': 'This is a test description.', | |
| 346 } | |
| 347 self. _InitWriterForAddingPolicies(self.writer, dict_policy) | |
| 348 self.writer.WritePolicy(dict_policy) | |
| 349 # Assert generated string elements. | |
| 350 output = self.GetXMLOfChildren(self.writer._string_table_elem) | |
| 351 expected_output = ( | |
| 352 '<string id="DictionaryPolicyStub">Dictionary policy caption</string>\n' | |
| 353 '<string id="DictionaryPolicyStub_Explain">' | |
| 354 'This is a test description.</string>') | |
| 355 self.AssertXMLEquals(output, expected_output) | |
| 356 # Assert generated presentation elements. | |
| 357 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) | |
| 358 expected_output = ( | |
| 359 '<presentation id="DictionaryPolicyStub">\n' | |
| 360 ' <textBox refId="DictionaryPolicyStub">\n' | |
| 361 ' <label>Dictionary policy label</label>\n' | |
| 362 ' </textBox>\n' | |
| 363 '</presentation>') | |
| 364 self.AssertXMLEquals(output, expected_output) | |
| 365 | |
| 366 def testPlatform(self): | |
| 367 # Test that the writer correctly chooses policies of platform Windows. | |
| 368 self.assertTrue(self.writer.IsPolicySupported({ | |
| 369 'supported_on': [ | |
| 370 {'platforms': ['win', 'zzz']}, {'platforms': ['aaa']} | |
| 371 ] | |
| 372 })) | |
| 373 self.assertFalse(self.writer.IsPolicySupported({ | |
| 374 'supported_on': [ | |
| 375 {'platforms': ['mac', 'linux']}, {'platforms': ['aaa']} | |
| 376 ] | |
| 377 })) | |
| 378 | |
| 379 def testStringEncodings(self): | |
| 380 enum_policy_a = { | |
| 381 'name': 'EnumPolicy.A', | |
| 382 'type': 'string-enum', | |
| 383 'caption': 'Enum policy A caption', | |
| 384 'label': 'Enum policy A label', | |
| 385 'desc': 'This is a test description.', | |
| 386 'items': [ | |
| 387 { | |
| 388 'name': 'tls1.2', | |
| 389 'value': 'tls1.2', | |
| 390 'caption': 'tls1.2', | |
| 391 } | |
| 392 ], | |
| 393 } | |
| 394 enum_policy_b = { | |
| 395 'name': 'EnumPolicy.B', | |
| 396 'type': 'string-enum', | |
| 397 'caption': 'Enum policy B caption', | |
| 398 'label': 'Enum policy B label', | |
| 399 'desc': 'This is a test description.', | |
| 400 'items': [ | |
| 401 { | |
| 402 'name': 'tls1.2', | |
| 403 'value': 'tls1.2', | |
| 404 'caption': 'tls1.2', | |
| 405 } | |
| 406 ], | |
| 407 } | |
| 408 self. _InitWriterForAddingPolicies(self.writer, enum_policy_a) | |
| 409 self.writer.WritePolicy(enum_policy_a) | |
| 410 self.writer.WritePolicy(enum_policy_b) | |
| 411 # Assert generated string elements. | |
| 412 output = self.GetXMLOfChildren(self.writer._string_table_elem) | |
| 413 expected_output = ( | |
| 414 '<string id="EnumPolicy_A">Enum policy A caption</string>\n' | |
| 415 '<string id="EnumPolicy_A_Explain">' | |
| 416 'This is a test description.</string>\n' | |
| 417 '<string id="tls1_2">tls1.2</string>\n' | |
| 418 '<string id="EnumPolicy_B">Enum policy B caption</string>\n' | |
| 419 '<string id="EnumPolicy_B_Explain">' | |
| 420 'This is a test description.</string>\n') | |
| 421 self.AssertXMLEquals(output, expected_output) | |
| 422 # Assert generated presentation elements. | |
| 423 output = self.GetXMLOfChildren(self.writer._presentation_table_elem) | |
| 424 expected_output = ( | |
| 425 '<presentation id="EnumPolicy.A">\n' | |
| 426 ' <dropdownList refId="EnumPolicy.A">' | |
| 427 'Enum policy A label</dropdownList>\n' | |
| 428 '</presentation>\n' | |
| 429 '<presentation id="EnumPolicy.B">\n' | |
| 430 ' <dropdownList refId="EnumPolicy.B">' | |
| 431 'Enum policy B label</dropdownList>\n' | |
| 432 '</presentation>') | |
| 433 self.AssertXMLEquals(output, expected_output) | |
| 434 | |
| 435 | |
| 436 if __name__ == '__main__': | |
| 437 unittest.main() | |
| OLD | NEW |