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 testMainPolicy(self):
52 # Tests a policy group with a single policy of type 'main'.
53 grd = self.PrepareTest(
55 ' "policy_definitions": ['
57 ' "name": "MainPolicy",'
59 ' "features": { "can_be_recommended": True },'
62 ' "supported_on": ["chrome.win:8-"],'
63 ' "example_value": True'
66 ' "placeholders": [],'
69 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'reg', 'en')
70 expected_output = self.NEWLINE.join([
71 'Windows Registry Editor Version 5.00',
73 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome]',
74 '"MainPolicy"=dword:00000001',
76 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome\\Recommended]',
77 '"MainPolicy"=dword:00000001'])
78 self.CompareOutputs(output, expected_output)
80 def testRecommendedMainPolicy(self):
81 # Tests a policy group with a single policy of type 'main'.
82 grd = self.PrepareTest(
84 ' "policy_definitions": ['
86 ' "name": "MainPolicy",'
89 ' "can_be_recommended": True,'
90 ' "can_be_mandatory": False '
94 ' "supported_on": ["chrome.win:8-"],'
95 ' "example_value": True'
98 ' "placeholders": [],'
101 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'reg', 'en')
102 expected_output = self.NEWLINE.join([
103 'Windows Registry Editor Version 5.00',
105 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome\\Recommended]',
106 '"MainPolicy"=dword:00000001'])
107 self.CompareOutputs(output, expected_output)
109 def testStringPolicy(self):
110 # Tests a policy group with a single policy of type 'string'.
111 grd = self.PrepareTest(
113 ' "policy_definitions": ['
115 ' "name": "StringPolicy",'
119 ' "supported_on": ["chrome.win:8-"],'
120 ' "example_value": "hello, world! \\\" \\\\"'
123 ' "placeholders": [],'
126 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en')
127 expected_output = self.NEWLINE.join([
128 'Windows Registry Editor Version 5.00',
130 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]',
131 '"StringPolicy"="hello, world! \\\" \\\\"'])
132 self.CompareOutputs(output, expected_output)
134 def testIntPolicy(self):
135 # Tests a policy group with a single policy of type 'int'.
136 grd = self.PrepareTest(
138 ' "policy_definitions": ['
140 ' "name": "IntPolicy",'
144 ' "supported_on": ["chrome.win:8-"],'
145 ' "example_value": 26'
148 ' "placeholders": [],'
151 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en')
152 expected_output = self.NEWLINE.join([
153 'Windows Registry Editor Version 5.00',
155 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]',
156 '"IntPolicy"=dword:0000001a'])
157 self.CompareOutputs(output, expected_output)
159 def testIntEnumPolicy(self):
160 # Tests a policy group with a single policy of type 'int-enum'.
161 grd = self.PrepareTest(
163 ' "policy_definitions": ['
165 ' "name": "EnumPolicy",'
166 ' "type": "int-enum",'
170 ' {"name": "ProxyServerDisabled", "value": 0, "caption": ""},'
171 ' {"name": "ProxyServerAutoDetect", "value": 1, "caption": ""},'
173 ' "supported_on": ["chrome.win:8-"],'
174 ' "example_value": 1'
177 ' "placeholders": [],'
180 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'reg', 'en')
181 expected_output = self.NEWLINE.join([
182 'Windows Registry Editor Version 5.00',
184 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome]',
185 '"EnumPolicy"=dword:00000001'])
186 self.CompareOutputs(output, expected_output)
188 def testStringEnumPolicy(self):
189 # Tests a policy group with a single policy of type 'string-enum'.
190 grd = self.PrepareTest(
192 ' "policy_definitions": ['
194 ' "name": "EnumPolicy",'
195 ' "type": "string-enum",'
199 ' {"name": "ProxyServerDisabled", "value": "one",'
201 ' {"name": "ProxyServerAutoDetect", "value": "two",'
204 ' "supported_on": ["chrome.win:8-"],'
205 ' "example_value": "two"'
208 ' "placeholders": [],'
211 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'reg', 'en')
212 expected_output = self.NEWLINE.join([
213 'Windows Registry Editor Version 5.00',
215 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome]',
216 '"EnumPolicy"="two"'])
217 self.CompareOutputs(output, expected_output)
219 def testListPolicy(self):
220 # Tests a policy group with a single policy of type 'list'.
221 grd = self.PrepareTest(
223 ' "policy_definitions": ['
225 ' "name": "ListPolicy",'
229 ' "supported_on": ["chrome.linux:8-"],'
230 ' "example_value": ["foo", "bar"]'
233 ' "placeholders": [],'
236 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en')
237 expected_output = self.NEWLINE.join([
238 'Windows Registry Editor Version 5.00',
240 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium\\ListPolicy]',
244 def testStringEnumListPolicy(self):
245 # Tests a policy group with a single policy of type 'string-enum-list'.
246 grd = self.PrepareTest(
248 ' "policy_definitions": ['
250 ' "name": "ListPolicy",'
251 ' "type": "string-enum-list",'
255 ' {"name": "ProxyServerDisabled", "value": "foo",'
257 ' {"name": "ProxyServerAutoDetect", "value": "bar",'
260 ' "supported_on": ["chrome.linux:8-"],'
261 ' "example_value": ["foo", "bar"]'
264 ' "placeholders": [],'
267 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en')
268 expected_output = self.NEWLINE.join([
269 'Windows Registry Editor Version 5.00',
271 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium\\ListPolicy]',
275 def testDictionaryPolicy(self):
276 # Tests a policy group with a single policy of type 'dict'.
287 grd = self.PrepareTest(
289 ' "policy_definitions": ['
291 ' "name": "DictionaryPolicy",'
295 ' "supported_on": ["chrome.win:8-"],'
296 ' "example_value": ' + str(example) +
299 ' "placeholders": [],'
302 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en')
303 expected_output = self.NEWLINE.join([
304 'Windows Registry Editor Version 5.00',
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)
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(
316 ' "policy_definitions": ['
318 ' "name": "NonWindowsPolicy",'
322 ' "supported_on": ["chrome.mac:8-"],'
323 ' "example_value": ["a"]'
326 ' "placeholders": [],'
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)
334 def testPolicyGroup(self):
335 # Tests a policy group that has more than one policies.
336 grd = self.PrepareTest(
338 ' "policy_definitions": ['
345 ' "name": "Policy1",'
349 ' "supported_on": ["chrome.win:8-"],'
350 ' "example_value": ["a", "b"]'
352 ' "name": "Policy2",'
356 ' "supported_on": ["chrome.win:8-"],'
357 ' "example_value": "c"'
361 ' "placeholders": [],'
364 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en')
365 expected_output = self.NEWLINE.join([
366 'Windows Registry Editor Version 5.00',
368 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]',
371 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium\\Policy1]',
374 self.CompareOutputs(output, expected_output)
377 if __name__ == '__main__':