Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / tools / grit / grit / format / policy_templates / writers / adml_writer_unittest.py
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 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.',
85       'policies': [
86           {'name': 'PolicyStub2',
87            'type': 'main'},
88           {'name': 'PolicyStub1',
89            'type': 'main'},
90       ],
91     }
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)
97     expected_output = (
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)
104     expected_output = ''
105     self.AssertXMLEquals(output, expected_output)
106
107   def testMainPolicy(self):
108     main_policy = {
109       'name': 'DummyMainPolicy',
110       'type': 'main',
111       'caption': 'Main policy caption',
112       'desc': 'Main policy test description.'
113     }
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)
118     expected_output = (
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)
127
128   def testStringPolicy(self):
129     string_policy = {
130       'name': 'StringPolicyStub',
131       'type': 'string',
132       'caption': 'String policy caption',
133       'label': 'String policy label',
134       'desc': 'This is a test description.',
135     }
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)
140     expected_output = (
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)
147     expected_output = (
148         '<presentation id="StringPolicyStub">\n'
149         '  <textBox refId="StringPolicyStub">\n'
150         '    <label>String policy label</label>\n'
151         '  </textBox>\n'
152         '</presentation>')
153     self.AssertXMLEquals(output, expected_output)
154
155   def testIntPolicy(self):
156     int_policy = {
157       'name': 'IntPolicyStub',
158       'type': 'int',
159       'caption': 'Int policy caption',
160       'label': 'Int policy label',
161       'desc': 'This is a test description.',
162     }
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)
167     expected_output = (
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)
174     expected_output = (
175         '<presentation id="IntPolicyStub">\n'
176         '  <decimalTextBox refId="IntPolicyStub">'
177         'Int policy label:</decimalTextBox>\n'
178         '</presentation>')
179     self.AssertXMLEquals(output, expected_output)
180
181   def testIntEnumPolicy(self):
182     enum_policy = {
183       'name': 'EnumPolicyStub',
184       'type': 'int-enum',
185       'caption': 'Enum policy caption',
186       'label': 'Enum policy label',
187       'desc': 'This is a test description.',
188       'items': [
189           {
190            'name': 'item 1',
191            'value': 1,
192            'caption': 'Caption Item 1',
193           },
194           {
195            'name': 'item 2',
196            'value': 2,
197            'caption': 'Caption Item 2',
198           },
199       ],
200     }
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)
205     expected_output = (
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)
214     expected_output = (
215         '<presentation id="EnumPolicyStub">\n'
216         '  <dropdownList refId="EnumPolicyStub">'
217         'Enum policy label</dropdownList>\n'
218         '</presentation>')
219     self.AssertXMLEquals(output, expected_output)
220
221   def testStringEnumPolicy(self):
222     enum_policy = {
223       'name': 'EnumPolicyStub',
224       'type': 'string-enum',
225       'caption': 'Enum policy caption',
226       'label': 'Enum policy label',
227       'desc': 'This is a test description.',
228       'items': [
229           {
230            'name': 'item 1',
231            'value': 'value 1',
232            'caption': 'Caption Item 1',
233           },
234           {
235            'name': 'item 2',
236            'value': 'value 2',
237            'caption': 'Caption Item 2',
238           },
239       ],
240     }
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)
245     expected_output = (
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)
254     expected_output = (
255         '<presentation id="EnumPolicyStub">\n'
256         '  <dropdownList refId="EnumPolicyStub">'
257         'Enum policy label</dropdownList>\n'
258         '</presentation>')
259     self.AssertXMLEquals(output, expected_output)
260
261   def testListPolicy(self):
262     list_policy = {
263       'name': 'ListPolicyStub',
264       'type': 'list',
265       'caption': 'List policy caption',
266       'label': 'List policy label',
267       'desc': 'This is a test description.',
268     }
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)
273     expected_output = (
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)
281     expected_output = (
282         '<presentation id="ListPolicyStub">\n'
283         '  <listBox refId="ListPolicyStubDesc">List policy label</listBox>\n'
284         '</presentation>')
285     self.AssertXMLEquals(output, expected_output)
286
287   def testDictionaryPolicy(self):
288     dict_policy = {
289       'name': 'DictionaryPolicyStub',
290       'type': 'dict',
291       'caption': 'Dictionary policy caption',
292       'label': 'Dictionary policy label',
293       'desc': 'This is a test description.',
294     }
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)
299     expected_output = (
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)
306     expected_output = (
307         '<presentation id="DictionaryPolicyStub">\n'
308         '  <textBox refId="DictionaryPolicyStub">\n'
309         '    <label>Dictionary policy label</label>\n'
310         '  </textBox>\n'
311         '</presentation>')
312     self.AssertXMLEquals(output, expected_output)
313
314   def testPlatform(self):
315     # Test that the writer correctly chooses policies of platform Windows.
316     self.assertTrue(self.writer.IsPolicySupported({
317       'supported_on': [
318         {'platforms': ['win', 'zzz']}, {'platforms': ['aaa']}
319       ]
320     }))
321     self.assertFalse(self.writer.IsPolicySupported({
322       'supported_on': [
323         {'platforms': ['mac', 'linux']}, {'platforms': ['aaa']}
324       ]
325     }))
326
327
328 if __name__ == '__main__':
329   unittest.main()