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 '''Unit tests for grit.format.policy_templates.writers.reg_writer'''
12 if __name__ == '__main__':
13 sys.path.append(os.path.join(os.path.dirname(__file__), '../../../..'))
17 from grit.format.policy_templates.writers import writer_unittest_common
20 class RegWriterUnittest(writer_unittest_common.WriterUnittestCommon):
21 '''Unit tests for RegWriter.'''
25 def CompareOutputs(self, output, expected_output):
26 '''Compares the output of the reg_writer with its expected output.
29 output: The output of the reg writer as returned by grit.
30 expected_output: The expected output.
33 AssertionError: if the two strings are not equivalent.
37 expected_output.strip())
40 # Test the handling of an empty policy list.
41 grd = self.PrepareTest(
43 ' "policy_definitions": [],'
44 ' "placeholders": [],'
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)
51 def testEmptyVersion(self):
52 # Test the handling of an empty policy list.
53 grd = self.PrepareTest(
55 ' "policy_definitions": [],'
56 ' "placeholders": [],'
59 output = self.GetOutput(
60 grd, 'fr', {'_chromium': '1', 'version': '39.0.0.0' }, 'reg', 'en')
61 expected_output = ('Windows Registry Editor Version 5.00\r\n'
62 '; chromium version: 39.0.0.0\r\n')
63 self.CompareOutputs(output, expected_output)
65 def testMainPolicy(self):
66 # Tests a policy group with a single policy of type 'main'.
67 grd = self.PrepareTest(
69 ' "policy_definitions": ['
71 ' "name": "MainPolicy",'
73 ' "features": { "can_be_recommended": True },'
76 ' "supported_on": ["chrome.win:8-"],'
77 ' "example_value": True'
80 ' "placeholders": [],'
83 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'reg', 'en')
84 expected_output = self.NEWLINE.join([
85 'Windows Registry Editor Version 5.00',
87 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome]',
88 '"MainPolicy"=dword:00000001',
90 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome\\Recommended]',
91 '"MainPolicy"=dword:00000001'])
92 self.CompareOutputs(output, expected_output)
94 def testRecommendedMainPolicy(self):
95 # Tests a policy group with a single policy of type 'main'.
96 grd = self.PrepareTest(
98 ' "policy_definitions": ['
100 ' "name": "MainPolicy",'
103 ' "can_be_recommended": True,'
104 ' "can_be_mandatory": False '
108 ' "supported_on": ["chrome.win:8-"],'
109 ' "example_value": True'
112 ' "placeholders": [],'
115 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'reg', 'en')
116 expected_output = self.NEWLINE.join([
117 'Windows Registry Editor Version 5.00',
119 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome\\Recommended]',
120 '"MainPolicy"=dword:00000001'])
121 self.CompareOutputs(output, expected_output)
123 def testStringPolicy(self):
124 # Tests a policy group with a single policy of type 'string'.
125 grd = self.PrepareTest(
127 ' "policy_definitions": ['
129 ' "name": "StringPolicy",'
133 ' "supported_on": ["chrome.win:8-"],'
134 ' "example_value": "hello, world! \\\" \\\\"'
137 ' "placeholders": [],'
140 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en')
141 expected_output = self.NEWLINE.join([
142 'Windows Registry Editor Version 5.00',
144 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]',
145 '"StringPolicy"="hello, world! \\\" \\\\"'])
146 self.CompareOutputs(output, expected_output)
148 def testIntPolicy(self):
149 # Tests a policy group with a single policy of type 'int'.
150 grd = self.PrepareTest(
152 ' "policy_definitions": ['
154 ' "name": "IntPolicy",'
158 ' "supported_on": ["chrome.win:8-"],'
159 ' "example_value": 26'
162 ' "placeholders": [],'
165 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en')
166 expected_output = self.NEWLINE.join([
167 'Windows Registry Editor Version 5.00',
169 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]',
170 '"IntPolicy"=dword:0000001a'])
171 self.CompareOutputs(output, expected_output)
173 def testIntEnumPolicy(self):
174 # Tests a policy group with a single policy of type 'int-enum'.
175 grd = self.PrepareTest(
177 ' "policy_definitions": ['
179 ' "name": "EnumPolicy",'
180 ' "type": "int-enum",'
184 ' {"name": "ProxyServerDisabled", "value": 0, "caption": ""},'
185 ' {"name": "ProxyServerAutoDetect", "value": 1, "caption": ""},'
187 ' "supported_on": ["chrome.win:8-"],'
188 ' "example_value": 1'
191 ' "placeholders": [],'
194 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'reg', 'en')
195 expected_output = self.NEWLINE.join([
196 'Windows Registry Editor Version 5.00',
198 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome]',
199 '"EnumPolicy"=dword:00000001'])
200 self.CompareOutputs(output, expected_output)
202 def testStringEnumPolicy(self):
203 # Tests a policy group with a single policy of type 'string-enum'.
204 grd = self.PrepareTest(
206 ' "policy_definitions": ['
208 ' "name": "EnumPolicy",'
209 ' "type": "string-enum",'
213 ' {"name": "ProxyServerDisabled", "value": "one",'
215 ' {"name": "ProxyServerAutoDetect", "value": "two",'
218 ' "supported_on": ["chrome.win:8-"],'
219 ' "example_value": "two"'
222 ' "placeholders": [],'
225 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'reg', 'en')
226 expected_output = self.NEWLINE.join([
227 'Windows Registry Editor Version 5.00',
229 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome]',
230 '"EnumPolicy"="two"'])
231 self.CompareOutputs(output, expected_output)
233 def testListPolicy(self):
234 # Tests a policy group with a single policy of type 'list'.
235 grd = self.PrepareTest(
237 ' "policy_definitions": ['
239 ' "name": "ListPolicy",'
243 ' "supported_on": ["chrome.linux:8-"],'
244 ' "example_value": ["foo", "bar"]'
247 ' "placeholders": [],'
250 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en')
251 expected_output = self.NEWLINE.join([
252 'Windows Registry Editor Version 5.00',
254 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium\\ListPolicy]',
258 def testStringEnumListPolicy(self):
259 # Tests a policy group with a single policy of type 'string-enum-list'.
260 grd = self.PrepareTest(
262 ' "policy_definitions": ['
264 ' "name": "ListPolicy",'
265 ' "type": "string-enum-list",'
269 ' {"name": "ProxyServerDisabled", "value": "foo",'
271 ' {"name": "ProxyServerAutoDetect", "value": "bar",'
274 ' "supported_on": ["chrome.linux:8-"],'
275 ' "example_value": ["foo", "bar"]'
278 ' "placeholders": [],'
281 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en')
282 expected_output = self.NEWLINE.join([
283 'Windows Registry Editor Version 5.00',
285 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium\\ListPolicy]',
289 def testDictionaryPolicy(self):
290 # Tests a policy group with a single policy of type 'dict'.
301 grd = self.PrepareTest(
303 ' "policy_definitions": ['
305 ' "name": "DictionaryPolicy",'
309 ' "supported_on": ["chrome.win:8-"],'
310 ' "example_value": ' + str(example) +
313 ' "placeholders": [],'
316 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en')
317 expected_output = self.NEWLINE.join([
318 'Windows Registry Editor Version 5.00',
320 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]',
321 '"DictionaryPolicy"="{"bool": true, "dict": {"a": 1, '
322 '"b": 2}, "int": 10, "list": [1, 2, 3], "string": "abc"}"'])
323 self.CompareOutputs(output, expected_output)
325 def testNonSupportedPolicy(self):
326 # Tests a policy that is not supported on Windows, so it shouldn't
327 # be included in the .REG file.
328 grd = self.PrepareTest(
330 ' "policy_definitions": ['
332 ' "name": "NonWindowsPolicy",'
336 ' "supported_on": ["chrome.mac:8-"],'
337 ' "example_value": ["a"]'
340 ' "placeholders": [],'
343 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en')
344 expected_output = self.NEWLINE.join([
345 'Windows Registry Editor Version 5.00'])
346 self.CompareOutputs(output, expected_output)
348 def testPolicyGroup(self):
349 # Tests a policy group that has more than one policies.
350 grd = self.PrepareTest(
352 ' "policy_definitions": ['
359 ' "name": "Policy1",'
363 ' "supported_on": ["chrome.win:8-"],'
364 ' "example_value": ["a", "b"]'
366 ' "name": "Policy2",'
370 ' "supported_on": ["chrome.win:8-"],'
371 ' "example_value": "c"'
375 ' "placeholders": [],'
378 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en')
379 expected_output = self.NEWLINE.join([
380 'Windows Registry Editor Version 5.00',
382 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]',
385 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium\\Policy1]',
388 self.CompareOutputs(output, expected_output)
391 if __name__ == '__main__':