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