Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / grit / grit / format / policy_templates / writers / json_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 '''Unit tests for grit.format.policy_templates.writers.json_writer'''
7
8
9 import os
10 import sys
11 if __name__ == '__main__':
12   sys.path.append(os.path.join(os.path.dirname(__file__), '../../../..'))
13
14 import unittest
15
16 from grit.format.policy_templates.writers import writer_unittest_common
17
18
19 TEMPLATE_HEADER="""\
20 // Policy template for Linux.
21 // Uncomment the policies you wish to activate and change their values to
22 // something useful for your case. The provided values are for reference only
23 // and do not provide meaningful defaults!
24 {
25 """
26
27
28 HEADER_DELIMETER="""\
29   //-------------------------------------------------------------------------
30 """
31
32
33 class JsonWriterUnittest(writer_unittest_common.WriterUnittestCommon):
34   '''Unit tests for JsonWriter.'''
35
36   def CompareOutputs(self, output, expected_output):
37     '''Compares the output of the json_writer with its expected output.
38
39     Args:
40       output: The output of the json writer as returned by grit.
41       expected_output: The expected output.
42
43     Raises:
44       AssertionError: if the two strings are not equivalent.
45     '''
46     self.assertEquals(
47         output.strip(),
48         expected_output.strip())
49
50   def testEmpty(self):
51     # Test the handling of an empty policy list.
52     grd = self.PrepareTest(
53         '{'
54         '  "policy_definitions": [],'
55         '  "placeholders": [],'
56         '  "messages": {},'
57         '}')
58     output = self.GetOutput(grd, 'fr', {'_chromium': '1',}, 'json', 'en')
59     expected_output = TEMPLATE_HEADER + '}'
60     self.CompareOutputs(output, expected_output)
61
62   def testMainPolicy(self):
63     # Tests a policy group with a single policy of type 'main'.
64     grd = self.PrepareTest(
65         '{'
66         '  "policy_definitions": ['
67         '    {'
68         '      "name": "MainPolicy",'
69         '      "type": "main",'
70         '      "caption": "Example Main Policy",'
71         '      "desc": "Example Main Policy",'
72         '      "supported_on": ["chrome.linux:8-"],'
73         '      "example_value": True'
74         '    },'
75         '  ],'
76         '  "placeholders": [],'
77         '  "messages": {},'
78         '}')
79     output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'json', 'en')
80     expected_output = (
81         TEMPLATE_HEADER +
82         '  // Example Main Policy\n' +
83         HEADER_DELIMETER +
84         '  // Example Main Policy\n\n'
85         '  //"MainPolicy": true\n\n'
86         '}')
87     self.CompareOutputs(output, expected_output)
88
89   def testStringPolicy(self):
90     # Tests a policy group with a single policy of type 'string'.
91     grd = self.PrepareTest(
92         '{'
93         '  "policy_definitions": ['
94         '    {'
95         '      "name": "StringPolicy",'
96         '      "type": "string",'
97         '      "caption": "Example String Policy",'
98         '      "desc": "Example String Policy",'
99         '      "supported_on": ["chrome.linux:8-"],'
100         '      "example_value": "hello, world!"'
101         '    },'
102         '  ],'
103         '  "placeholders": [],'
104         '  "messages": {},'
105         '}')
106     output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
107     expected_output = (
108         TEMPLATE_HEADER +
109         '  // Example String Policy\n' +
110         HEADER_DELIMETER +
111         '  // Example String Policy\n\n'
112         '  //"StringPolicy": "hello, world!"\n\n'
113         '}')
114     self.CompareOutputs(output, expected_output)
115
116   def testIntPolicy(self):
117     # Tests a policy group with a single policy of type 'string'.
118     grd = self.PrepareTest(
119         '{'
120         '  "policy_definitions": ['
121         '    {'
122         '      "name": "IntPolicy",'
123         '      "type": "int",'
124         '      "caption": "Example Int Policy",'
125         '      "desc": "Example Int Policy",'
126         '      "supported_on": ["chrome.linux:8-"],'
127         '      "example_value": 15'
128         '    },'
129         '  ],'
130         '  "placeholders": [],'
131         '  "messages": {},'
132         '}')
133     output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
134     expected_output = (
135         TEMPLATE_HEADER +
136         '  // Example Int Policy\n' +
137         HEADER_DELIMETER +
138         '  // Example Int Policy\n\n'
139         '  //"IntPolicy": 15\n\n'
140         '}')
141     self.CompareOutputs(output, expected_output)
142
143   def testIntEnumPolicy(self):
144     # Tests a policy group with a single policy of type 'int-enum'.
145     grd = self.PrepareTest(
146         '{'
147         '  "policy_definitions": ['
148         '    {'
149         '      "name": "EnumPolicy",'
150         '      "type": "int-enum",'
151         '      "caption": "Example Int Enum",'
152         '      "desc": "Example Int Enum",'
153         '      "items": ['
154         '        {"name": "ProxyServerDisabled", "value": 0, "caption": ""},'
155         '        {"name": "ProxyServerAutoDetect", "value": 1, "caption": ""},'
156         '      ],'
157         '      "supported_on": ["chrome.linux:8-"],'
158         '      "example_value": 1'
159         '    },'
160         '  ],'
161         '  "placeholders": [],'
162         '  "messages": {},'
163         '}')
164     output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'json', 'en')
165     expected_output = (
166         TEMPLATE_HEADER +
167         '  // Example Int Enum\n' +
168         HEADER_DELIMETER +
169         '  // Example Int Enum\n\n'
170         '  //"EnumPolicy": 1\n\n'
171         '}')
172     self.CompareOutputs(output, expected_output)
173
174   def testStringEnumPolicy(self):
175     # Tests a policy group with a single policy of type 'string-enum'.
176     grd = self.PrepareTest(
177         '{'
178         '  "policy_definitions": ['
179         '    {'
180         '      "name": "EnumPolicy",'
181         '      "type": "string-enum",'
182         '      "caption": "Example String Enum",'
183         '      "desc": "Example String Enum",'
184         '      "items": ['
185         '        {"name": "ProxyServerDisabled", "value": "one",'
186         '         "caption": ""},'
187         '        {"name": "ProxyServerAutoDetect", "value": "two",'
188         '         "caption": ""},'
189         '      ],'
190         '      "supported_on": ["chrome.linux:8-"],'
191         '      "example_value": "one"'
192         '    },'
193         '  ],'
194         '  "placeholders": [],'
195         '  "messages": {},'
196         '}')
197     output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'json', 'en')
198     expected_output = (
199         TEMPLATE_HEADER +
200         '  // Example String Enum\n' +
201         HEADER_DELIMETER +
202         '  // Example String Enum\n\n'
203         '  //"EnumPolicy": "one"\n\n'
204         '}')
205     self.CompareOutputs(output, expected_output)
206
207   def testListPolicy(self):
208     # Tests a policy group with a single policy of type 'list'.
209     grd = self.PrepareTest(
210         '{'
211         '  "policy_definitions": ['
212         '    {'
213         '      "name": "ListPolicy",'
214         '      "type": "list",'
215         '      "caption": "Example List",'
216         '      "desc": "Example List",'
217         '      "supported_on": ["chrome.linux:8-"],'
218         '      "example_value": ["foo", "bar"]'
219         '    },'
220         '  ],'
221         '  "placeholders": [],'
222         '  "messages": {},'
223         '}')
224     output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
225     expected_output = (
226         TEMPLATE_HEADER +
227         '  // Example List\n' +
228         HEADER_DELIMETER +
229         '  // Example List\n\n'
230         '  //"ListPolicy": ["foo", "bar"]\n\n'
231         '}')
232     self.CompareOutputs(output, expected_output)
233
234   def testStringEnumListPolicy(self):
235     # Tests a policy group with a single policy of type 'string-enum-list'.
236     grd = self.PrepareTest(
237         '{'
238         '  "policy_definitions": ['
239         '    {'
240         '      "name": "ListPolicy",'
241         '      "type": "string-enum-list",'
242         '      "caption": "Example List",'
243         '      "desc": "Example List",'
244         '      "items": ['
245         '        {"name": "ProxyServerDisabled", "value": "one",'
246         '         "caption": ""},'
247         '        {"name": "ProxyServerAutoDetect", "value": "two",'
248         '         "caption": ""},'
249         '      ],'
250         '      "supported_on": ["chrome.linux:8-"],'
251         '      "example_value": ["one", "two"]'
252         '    },'
253         '  ],'
254         '  "placeholders": [],'
255         '  "messages": {},'
256         '}')
257     output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
258     expected_output = (
259         TEMPLATE_HEADER +
260         '  // Example List\n' +
261         HEADER_DELIMETER +
262         '  // Example List\n\n'
263         '  //"ListPolicy": ["one", "two"]\n\n'
264         '}')
265     self.CompareOutputs(output, expected_output)
266
267   def testDictionaryPolicy(self):
268     # Tests a policy group with a single policy of type 'dict'.
269     example = {
270       'bool': True,
271       'dict': {
272         'a': 1,
273         'b': 2,
274       },
275       'int': 10,
276       'list': [1, 2, 3],
277       'string': 'abc',
278     }
279     grd = self.PrepareTest(
280         '{'
281         '  "policy_definitions": ['
282         '    {'
283         '      "name": "DictionaryPolicy",'
284         '      "type": "dict",'
285         '      "caption": "Example Dictionary Policy",'
286         '      "desc": "Example Dictionary Policy",'
287         '      "supported_on": ["chrome.linux:8-"],'
288         '      "example_value": ' + str(example) +
289         '    },'
290         '  ],'
291         '  "placeholders": [],'
292         '  "messages": {},'
293         '}')
294     output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
295     expected_output = (
296         TEMPLATE_HEADER +
297         '  // Example Dictionary Policy\n' +
298         HEADER_DELIMETER +
299         '  // Example Dictionary Policy\n\n'
300         '  //"DictionaryPolicy": {"bool": true, "dict": {"a": 1, '
301         '"b": 2}, "int": 10, "list": [1, 2, 3], "string": "abc"}\n\n'
302         '}')
303     self.CompareOutputs(output, expected_output)
304
305   def testNonSupportedPolicy(self):
306     # Tests a policy that is not supported on Linux, so it shouldn't
307     # be included in the JSON file.
308     grd = self.PrepareTest(
309         '{'
310         '  "policy_definitions": ['
311         '    {'
312         '      "name": "NonLinuxPolicy",'
313         '      "type": "list",'
314         '      "caption": "",'
315         '      "desc": "",'
316         '      "supported_on": ["chrome.mac:8-"],'
317         '      "example_value": ["a"]'
318         '    },'
319         '  ],'
320         '  "placeholders": [],'
321         '  "messages": {},'
322         '}')
323     output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
324     expected_output = TEMPLATE_HEADER + '}'
325     self.CompareOutputs(output, expected_output)
326
327   def testPolicyGroup(self):
328     # Tests a policy group that has more than one policies.
329     grd = self.PrepareTest(
330         '{'
331         '  "policy_definitions": ['
332         '    {'
333         '      "name": "Group1",'
334         '      "type": "group",'
335         '      "caption": "",'
336         '      "desc": "",'
337         '      "policies": [{'
338         '        "name": "Policy1",'
339         '        "type": "list",'
340         '        "caption": "Policy One",'
341         '        "desc": "Policy One",'
342         '        "supported_on": ["chrome.linux:8-"],'
343         '        "example_value": ["a", "b"]'
344         '      },{'
345         '        "name": "Policy2",'
346         '        "type": "string",'
347         '        "caption": "Policy Two",'
348         '        "desc": "Policy Two",'
349         '        "supported_on": ["chrome.linux:8-"],'
350         '        "example_value": "c"'
351         '      }],'
352         '    },'
353         '  ],'
354         '  "placeholders": [],'
355         '  "messages": {},'
356         '}')
357     output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
358     expected_output = (
359         TEMPLATE_HEADER +
360         '  // Policy One\n' +
361         HEADER_DELIMETER +
362         '  // Policy One\n\n'
363         '  //"Policy1": ["a", "b"],\n\n'
364         '  // Policy Two\n' +
365         HEADER_DELIMETER +
366         '  // Policy Two\n\n'
367         '  //"Policy2": "c"\n\n'
368         '}')
369     self.CompareOutputs(output, expected_output)
370
371
372 if __name__ == '__main__':
373   unittest.main()