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.
7 """Unittests for grit.format.policy_templates.writers.adml_writer."""
13 if __name__ == '__main__':
14 sys.path.append(os.path.join(os.path.dirname(__file__), '../../../..'))
17 from grit.format.policy_templates.writers import adml_writer
18 from grit.format.policy_templates.writers import xml_writer_base_unittest
21 class AdmlWriterUnittest(xml_writer_base_unittest.XmlWriterBaseTest):
27 'win_supported_os': 'SUPPORTED_TESTOS',
29 self.writer = adml_writer.GetWriter(config)
30 self.writer.messages = {
31 'win_supported_winxpsp2': {
32 'text': 'Supported on Test OS or higher',
36 'text': 'Recommended',
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.
47 writer.BeginTemplate()
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.
54 self._InitWriterForAddingPolicyGroups(writer)
56 'name': 'PolicyGroup',
57 'caption': 'Test Caption',
58 'desc': 'This is the test description of the test policy group.',
61 writer.BeginPolicyGroup(policy_group)
64 self.writer._string_table_elem.getElementsByTagName('string')
65 for elem in string_elements:
66 self.writer._string_table_elem.removeChild(elem)
69 self.writer.BeginTemplate()
70 self.writer.EndTemplate()
71 output = self.writer.GetTemplateText()
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)
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()
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)
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.',
100 {'name': 'PolicyStub2',
102 {'name': 'PolicyStub1',
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)
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)
119 self.AssertXMLEquals(output, expected_output)
121 def testMainPolicy(self):
123 'name': 'DummyMainPolicy',
125 'caption': 'Main policy caption',
126 'desc': 'Main policy test description.'
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)
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)
142 def testStringPolicy(self):
144 'name': 'StringPolicyStub',
146 'caption': 'String policy caption',
147 'label': 'String policy label',
148 'desc': 'This is a test description.',
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)
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)
162 '<presentation id="StringPolicyStub">\n'
163 ' <textBox refId="StringPolicyStub">\n'
164 ' <label>String policy label</label>\n'
167 self.AssertXMLEquals(output, expected_output)
169 def testIntPolicy(self):
171 'name': 'IntPolicyStub',
173 'caption': 'Int policy caption',
174 'label': 'Int policy label',
175 'desc': 'This is a test description.',
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)
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)
189 '<presentation id="IntPolicyStub">\n'
190 ' <decimalTextBox refId="IntPolicyStub">'
191 'Int policy label:</decimalTextBox>\n'
193 self.AssertXMLEquals(output, expected_output)
195 def testIntEnumPolicy(self):
197 'name': 'EnumPolicyStub',
199 'caption': 'Enum policy caption',
200 'label': 'Enum policy label',
201 'desc': 'This is a test description.',
206 'caption': 'Caption Item 1',
211 'caption': 'Caption Item 2',
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)
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)
229 '<presentation id="EnumPolicyStub">\n'
230 ' <dropdownList refId="EnumPolicyStub">'
231 'Enum policy label</dropdownList>\n'
233 self.AssertXMLEquals(output, expected_output)
235 def testStringEnumPolicy(self):
237 'name': 'EnumPolicyStub',
238 'type': 'string-enum',
239 'caption': 'Enum policy caption',
240 'label': 'Enum policy label',
241 'desc': 'This is a test description.',
246 'caption': 'Caption Item 1',
251 'caption': 'Caption Item 2',
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)
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)
269 '<presentation id="EnumPolicyStub">\n'
270 ' <dropdownList refId="EnumPolicyStub">'
271 'Enum policy label</dropdownList>\n'
273 self.AssertXMLEquals(output, expected_output)
275 def testListPolicy(self):
277 'name': 'ListPolicyStub',
279 'caption': 'List policy caption',
280 'label': 'List policy label',
281 'desc': 'This is a test description.',
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)
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)
296 '<presentation id="ListPolicyStub">\n'
297 ' <listBox refId="ListPolicyStubDesc">List policy label</listBox>\n'
299 self.AssertXMLEquals(output, expected_output)
301 def testStringEnumListPolicy(self):
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.',
312 'caption': 'Caption Item 1',
317 'caption': 'Caption Item 2',
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)
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)
334 '<presentation id="ListPolicyStub">\n'
335 ' <listBox refId="ListPolicyStubDesc">List policy label</listBox>\n'
337 self.AssertXMLEquals(output, expected_output)
339 def testDictionaryPolicy(self):
341 'name': 'DictionaryPolicyStub',
343 'caption': 'Dictionary policy caption',
344 'label': 'Dictionary policy label',
345 'desc': 'This is a test description.',
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)
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)
359 '<presentation id="DictionaryPolicyStub">\n'
360 ' <textBox refId="DictionaryPolicyStub">\n'
361 ' <label>Dictionary policy label</label>\n'
364 self.AssertXMLEquals(output, expected_output)
366 def testPlatform(self):
367 # Test that the writer correctly chooses policies of platform Windows.
368 self.assertTrue(self.writer.IsPolicySupported({
370 {'platforms': ['win', 'zzz']}, {'platforms': ['aaa']}
373 self.assertFalse(self.writer.IsPolicySupported({
375 {'platforms': ['mac', 'linux']}, {'platforms': ['aaa']}
379 def testStringEncodings(self):
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.',
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.',
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)
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)
425 '<presentation id="EnumPolicy.A">\n'
426 ' <dropdownList refId="EnumPolicy.A">'
427 'Enum policy A label</dropdownList>\n'
429 '<presentation id="EnumPolicy.B">\n'
430 ' <dropdownList refId="EnumPolicy.B">'
431 'Enum policy B label</dropdownList>\n'
433 self.AssertXMLEquals(output, expected_output)
436 if __name__ == '__main__':