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 testPolicyGroup(self):
81 empty_policy_group = {
82 'name': 'PolicyGroup',
83 'caption': 'Test Group Caption',
84 'desc': 'This is the test description of the test policy group.',
86 {'name': 'PolicyStub2',
88 {'name': 'PolicyStub1',
92 self._InitWriterForAddingPolicyGroups(self.writer)
93 self.writer.BeginPolicyGroup(empty_policy_group)
94 self.writer.EndPolicyGroup
95 # Assert generated string elements.
96 output = self.GetXMLOfChildren(self.writer._string_table_elem)
98 '<string id="SUPPORTED_TESTOS">'
99 'Supported on Test OS or higher</string>\n'
100 '<string id="PolicyGroup_group">Test Group Caption</string>')
101 self.AssertXMLEquals(output, expected_output)
102 # Assert generated presentation elements.
103 output = self.GetXMLOfChildren(self.writer._presentation_table_elem)
105 self.AssertXMLEquals(output, expected_output)
107 def testMainPolicy(self):
109 'name': 'DummyMainPolicy',
111 'caption': 'Main policy caption',
112 'desc': 'Main policy test description.'
114 self. _InitWriterForAddingPolicies(self.writer, main_policy)
115 self.writer.WritePolicy(main_policy)
116 # Assert generated string elements.
117 output = self.GetXMLOfChildren(self.writer._string_table_elem)
119 '<string id="DummyMainPolicy">Main policy caption</string>\n'
120 '<string id="DummyMainPolicy_Explain">'
121 'Main policy test description.</string>')
122 self.AssertXMLEquals(output, expected_output)
123 # Assert generated presentation elements.
124 output = self.GetXMLOfChildren(self.writer._presentation_table_elem)
125 expected_output = '<presentation id="DummyMainPolicy"/>'
126 self.AssertXMLEquals(output, expected_output)
128 def testStringPolicy(self):
130 'name': 'StringPolicyStub',
132 'caption': 'String policy caption',
133 'label': 'String policy label',
134 'desc': 'This is a test description.',
136 self. _InitWriterForAddingPolicies(self.writer, string_policy)
137 self.writer.WritePolicy(string_policy)
138 # Assert generated string elements.
139 output = self.GetXMLOfChildren(self.writer._string_table_elem)
141 '<string id="StringPolicyStub">String policy caption</string>\n'
142 '<string id="StringPolicyStub_Explain">'
143 'This is a test description.</string>')
144 self.AssertXMLEquals(output, expected_output)
145 # Assert generated presentation elements.
146 output = self.GetXMLOfChildren(self.writer._presentation_table_elem)
148 '<presentation id="StringPolicyStub">\n'
149 ' <textBox refId="StringPolicyStub">\n'
150 ' <label>String policy label</label>\n'
153 self.AssertXMLEquals(output, expected_output)
155 def testIntPolicy(self):
157 'name': 'IntPolicyStub',
159 'caption': 'Int policy caption',
160 'label': 'Int policy label',
161 'desc': 'This is a test description.',
163 self. _InitWriterForAddingPolicies(self.writer, int_policy)
164 self.writer.WritePolicy(int_policy)
165 # Assert generated string elements.
166 output = self.GetXMLOfChildren(self.writer._string_table_elem)
168 '<string id="IntPolicyStub">Int policy caption</string>\n'
169 '<string id="IntPolicyStub_Explain">'
170 'This is a test description.</string>')
171 self.AssertXMLEquals(output, expected_output)
172 # Assert generated presentation elements.
173 output = self.GetXMLOfChildren(self.writer._presentation_table_elem)
175 '<presentation id="IntPolicyStub">\n'
176 ' <decimalTextBox refId="IntPolicyStub">'
177 'Int policy label:</decimalTextBox>\n'
179 self.AssertXMLEquals(output, expected_output)
181 def testIntEnumPolicy(self):
183 'name': 'EnumPolicyStub',
185 'caption': 'Enum policy caption',
186 'label': 'Enum policy label',
187 'desc': 'This is a test description.',
192 'caption': 'Caption Item 1',
197 'caption': 'Caption Item 2',
201 self. _InitWriterForAddingPolicies(self.writer, enum_policy)
202 self.writer.WritePolicy(enum_policy)
203 # Assert generated string elements.
204 output = self.GetXMLOfChildren(self.writer._string_table_elem)
206 '<string id="EnumPolicyStub">Enum policy caption</string>\n'
207 '<string id="EnumPolicyStub_Explain">'
208 'This is a test description.</string>\n'
209 '<string id="item 1">Caption Item 1</string>\n'
210 '<string id="item 2">Caption Item 2</string>')
211 self.AssertXMLEquals(output, expected_output)
212 # Assert generated presentation elements.
213 output = self.GetXMLOfChildren(self.writer._presentation_table_elem)
215 '<presentation id="EnumPolicyStub">\n'
216 ' <dropdownList refId="EnumPolicyStub">'
217 'Enum policy label</dropdownList>\n'
219 self.AssertXMLEquals(output, expected_output)
221 def testStringEnumPolicy(self):
223 'name': 'EnumPolicyStub',
224 'type': 'string-enum',
225 'caption': 'Enum policy caption',
226 'label': 'Enum policy label',
227 'desc': 'This is a test description.',
232 'caption': 'Caption Item 1',
237 'caption': 'Caption Item 2',
241 self. _InitWriterForAddingPolicies(self.writer, enum_policy)
242 self.writer.WritePolicy(enum_policy)
243 # Assert generated string elements.
244 output = self.GetXMLOfChildren(self.writer._string_table_elem)
246 '<string id="EnumPolicyStub">Enum policy caption</string>\n'
247 '<string id="EnumPolicyStub_Explain">'
248 'This is a test description.</string>\n'
249 '<string id="item 1">Caption Item 1</string>\n'
250 '<string id="item 2">Caption Item 2</string>')
251 self.AssertXMLEquals(output, expected_output)
252 # Assert generated presentation elements.
253 output = self.GetXMLOfChildren(self.writer._presentation_table_elem)
255 '<presentation id="EnumPolicyStub">\n'
256 ' <dropdownList refId="EnumPolicyStub">'
257 'Enum policy label</dropdownList>\n'
259 self.AssertXMLEquals(output, expected_output)
261 def testListPolicy(self):
263 'name': 'ListPolicyStub',
265 'caption': 'List policy caption',
266 'label': 'List policy label',
267 'desc': 'This is a test description.',
269 self. _InitWriterForAddingPolicies(self.writer, list_policy)
270 self.writer.WritePolicy(list_policy)
271 # Assert generated string elements.
272 output = self.GetXMLOfChildren(self.writer._string_table_elem)
274 '<string id="ListPolicyStub">List policy caption</string>\n'
275 '<string id="ListPolicyStub_Explain">'
276 'This is a test description.</string>\n'
277 '<string id="ListPolicyStubDesc">List policy caption</string>')
278 self.AssertXMLEquals(output, expected_output)
279 # Assert generated presentation elements.
280 output = self.GetXMLOfChildren(self.writer._presentation_table_elem)
282 '<presentation id="ListPolicyStub">\n'
283 ' <listBox refId="ListPolicyStubDesc">List policy label</listBox>\n'
285 self.AssertXMLEquals(output, expected_output)
287 def testDictionaryPolicy(self):
289 'name': 'DictionaryPolicyStub',
291 'caption': 'Dictionary policy caption',
292 'label': 'Dictionary policy label',
293 'desc': 'This is a test description.',
295 self. _InitWriterForAddingPolicies(self.writer, dict_policy)
296 self.writer.WritePolicy(dict_policy)
297 # Assert generated string elements.
298 output = self.GetXMLOfChildren(self.writer._string_table_elem)
300 '<string id="DictionaryPolicyStub">Dictionary policy caption</string>\n'
301 '<string id="DictionaryPolicyStub_Explain">'
302 'This is a test description.</string>')
303 self.AssertXMLEquals(output, expected_output)
304 # Assert generated presentation elements.
305 output = self.GetXMLOfChildren(self.writer._presentation_table_elem)
307 '<presentation id="DictionaryPolicyStub">\n'
308 ' <textBox refId="DictionaryPolicyStub">\n'
309 ' <label>Dictionary policy label</label>\n'
312 self.AssertXMLEquals(output, expected_output)
314 def testPlatform(self):
315 # Test that the writer correctly chooses policies of platform Windows.
316 self.assertTrue(self.writer.IsPolicySupported({
318 {'platforms': ['win', 'zzz']}, {'platforms': ['aaa']}
321 self.assertFalse(self.writer.IsPolicySupported({
323 {'platforms': ['mac', 'linux']}, {'platforms': ['aaa']}
328 if __name__ == '__main__':