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.
6 '''Unit tests for grit.format.policy_templates.writers.json_writer'''
11 if __name__ == '__main__':
12 sys.path.append(os.path.join(os.path.dirname(__file__), '../../../..'))
16 from grit.format.policy_templates.writers import writer_unittest_common
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!
27 TEMPLATE_HEADER_WITH_VERSION="""\
28 // chromium version: 39.0.0.0
29 // Policy template for Linux.
30 // Uncomment the policies you wish to activate and change their values to
31 // something useful for your case. The provided values are for reference only
32 // and do not provide meaningful defaults!
38 //-------------------------------------------------------------------------
42 class JsonWriterUnittest(writer_unittest_common.WriterUnittestCommon):
43 '''Unit tests for JsonWriter.'''
45 def CompareOutputs(self, output, expected_output):
46 '''Compares the output of the json_writer with its expected output.
49 output: The output of the json writer as returned by grit.
50 expected_output: The expected output.
53 AssertionError: if the two strings are not equivalent.
57 expected_output.strip())
60 # Test the handling of an empty policy list.
61 grd = self.PrepareTest(
63 ' "policy_definitions": [],'
64 ' "placeholders": [],'
67 output = self.GetOutput(grd, 'fr', {'_chromium': '1'}, 'json', 'en')
68 expected_output = TEMPLATE_HEADER + '}'
69 self.CompareOutputs(output, expected_output)
71 def testEmptyWithVersion(self):
72 # Test the handling of an empty policy list.
73 grd = self.PrepareTest(
75 ' "policy_definitions": [],'
76 ' "placeholders": [],'
79 output = self.GetOutput(
80 grd, 'fr', {'_chromium': '1', 'version':'39.0.0.0'}, 'json', 'en')
81 expected_output = TEMPLATE_HEADER_WITH_VERSION + '}'
82 self.CompareOutputs(output, expected_output)
84 def testMainPolicy(self):
85 # Tests a policy group with a single policy of type 'main'.
86 grd = self.PrepareTest(
88 ' "policy_definitions": ['
90 ' "name": "MainPolicy",'
92 ' "caption": "Example Main Policy",'
93 ' "desc": "Example Main Policy",'
94 ' "supported_on": ["chrome.linux:8-"],'
95 ' "example_value": True'
98 ' "placeholders": [],'
101 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'json', 'en')
104 ' // Example Main Policy\n' +
106 ' // Example Main Policy\n\n'
107 ' //"MainPolicy": true\n\n'
109 self.CompareOutputs(output, expected_output)
111 def testRecommendedOnlyPolicy(self):
112 # Tests a policy group with a single policy of type 'main'.
113 grd = self.PrepareTest(
115 ' "policy_definitions": ['
117 ' "name": "MainPolicy",'
119 ' "caption": "Example Main Policy",'
120 ' "desc": "Example Main Policy",'
122 ' "can_be_recommended": True,'
123 ' "can_be_mandatory": False'
125 ' "supported_on": ["chrome.linux:8-"],'
126 ' "example_value": True'
129 ' "placeholders": [],'
132 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'json', 'en')
135 ' // Note: this policy is supported only in recommended mode.\n' +
136 ' // The JSON file should be placed in' +
137 ' /etc/opt/chrome/policies/recommended.\n' +
138 ' // Example Main Policy\n' +
140 ' // Example Main Policy\n\n'
141 ' //"MainPolicy": true\n\n'
143 self.CompareOutputs(output, expected_output)
145 def testStringPolicy(self):
146 # Tests a policy group with a single policy of type 'string'.
147 grd = self.PrepareTest(
149 ' "policy_definitions": ['
151 ' "name": "StringPolicy",'
153 ' "caption": "Example String Policy",'
154 ' "desc": "Example String Policy",'
155 ' "supported_on": ["chrome.linux:8-"],'
156 ' "example_value": "hello, world!"'
159 ' "placeholders": [],'
162 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
165 ' // Example String Policy\n' +
167 ' // Example String Policy\n\n'
168 ' //"StringPolicy": "hello, world!"\n\n'
170 self.CompareOutputs(output, expected_output)
172 def testIntPolicy(self):
173 # Tests a policy group with a single policy of type 'string'.
174 grd = self.PrepareTest(
176 ' "policy_definitions": ['
178 ' "name": "IntPolicy",'
180 ' "caption": "Example Int Policy",'
181 ' "desc": "Example Int Policy",'
182 ' "supported_on": ["chrome.linux:8-"],'
183 ' "example_value": 15'
186 ' "placeholders": [],'
189 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
192 ' // Example Int Policy\n' +
194 ' // Example Int Policy\n\n'
195 ' //"IntPolicy": 15\n\n'
197 self.CompareOutputs(output, expected_output)
199 def testIntEnumPolicy(self):
200 # Tests a policy group with a single policy of type 'int-enum'.
201 grd = self.PrepareTest(
203 ' "policy_definitions": ['
205 ' "name": "EnumPolicy",'
206 ' "type": "int-enum",'
207 ' "caption": "Example Int Enum",'
208 ' "desc": "Example Int Enum",'
210 ' {"name": "ProxyServerDisabled", "value": 0, "caption": ""},'
211 ' {"name": "ProxyServerAutoDetect", "value": 1, "caption": ""},'
213 ' "supported_on": ["chrome.linux:8-"],'
214 ' "example_value": 1'
217 ' "placeholders": [],'
220 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'json', 'en')
223 ' // Example Int Enum\n' +
225 ' // Example Int Enum\n\n'
226 ' //"EnumPolicy": 1\n\n'
228 self.CompareOutputs(output, expected_output)
230 def testStringEnumPolicy(self):
231 # Tests a policy group with a single policy of type 'string-enum'.
232 grd = self.PrepareTest(
234 ' "policy_definitions": ['
236 ' "name": "EnumPolicy",'
237 ' "type": "string-enum",'
238 ' "caption": "Example String Enum",'
239 ' "desc": "Example String Enum",'
241 ' {"name": "ProxyServerDisabled", "value": "one",'
243 ' {"name": "ProxyServerAutoDetect", "value": "two",'
246 ' "supported_on": ["chrome.linux:8-"],'
247 ' "example_value": "one"'
250 ' "placeholders": [],'
253 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'json', 'en')
256 ' // Example String Enum\n' +
258 ' // Example String Enum\n\n'
259 ' //"EnumPolicy": "one"\n\n'
261 self.CompareOutputs(output, expected_output)
263 def testListPolicy(self):
264 # Tests a policy group with a single policy of type 'list'.
265 grd = self.PrepareTest(
267 ' "policy_definitions": ['
269 ' "name": "ListPolicy",'
271 ' "caption": "Example List",'
272 ' "desc": "Example List",'
273 ' "supported_on": ["chrome.linux:8-"],'
274 ' "example_value": ["foo", "bar"]'
277 ' "placeholders": [],'
280 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
283 ' // Example List\n' +
285 ' // Example List\n\n'
286 ' //"ListPolicy": ["foo", "bar"]\n\n'
288 self.CompareOutputs(output, expected_output)
290 def testStringEnumListPolicy(self):
291 # Tests a policy group with a single policy of type 'string-enum-list'.
292 grd = self.PrepareTest(
294 ' "policy_definitions": ['
296 ' "name": "ListPolicy",'
297 ' "type": "string-enum-list",'
298 ' "caption": "Example List",'
299 ' "desc": "Example List",'
301 ' {"name": "ProxyServerDisabled", "value": "one",'
303 ' {"name": "ProxyServerAutoDetect", "value": "two",'
306 ' "supported_on": ["chrome.linux:8-"],'
307 ' "example_value": ["one", "two"]'
310 ' "placeholders": [],'
313 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
316 ' // Example List\n' +
318 ' // Example List\n\n'
319 ' //"ListPolicy": ["one", "two"]\n\n'
321 self.CompareOutputs(output, expected_output)
323 def testDictionaryPolicy(self):
324 # Tests a policy group with a single policy of type 'dict'.
335 grd = self.PrepareTest(
337 ' "policy_definitions": ['
339 ' "name": "DictionaryPolicy",'
341 ' "caption": "Example Dictionary Policy",'
342 ' "desc": "Example Dictionary Policy",'
343 ' "supported_on": ["chrome.linux:8-"],'
344 ' "example_value": ' + str(example) +
347 ' "placeholders": [],'
350 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
353 ' // Example Dictionary Policy\n' +
355 ' // Example Dictionary Policy\n\n'
356 ' //"DictionaryPolicy": {"bool": true, "dict": {"a": 1, '
357 '"b": 2}, "int": 10, "list": [1, 2, 3], "string": "abc"}\n\n'
359 self.CompareOutputs(output, expected_output)
361 def testNonSupportedPolicy(self):
362 # Tests a policy that is not supported on Linux, so it shouldn't
363 # be included in the JSON file.
364 grd = self.PrepareTest(
366 ' "policy_definitions": ['
368 ' "name": "NonLinuxPolicy",'
372 ' "supported_on": ["chrome.mac:8-"],'
373 ' "example_value": ["a"]'
376 ' "placeholders": [],'
379 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
380 expected_output = TEMPLATE_HEADER + '}'
381 self.CompareOutputs(output, expected_output)
383 def testPolicyGroup(self):
384 # Tests a policy group that has more than one policies.
385 grd = self.PrepareTest(
387 ' "policy_definitions": ['
394 ' "name": "Policy1",'
396 ' "caption": "Policy One",'
397 ' "desc": "Policy One",'
398 ' "supported_on": ["chrome.linux:8-"],'
399 ' "example_value": ["a", "b"]'
401 ' "name": "Policy2",'
403 ' "caption": "Policy Two",'
404 ' "desc": "Policy Two",'
405 ' "supported_on": ["chrome.linux:8-"],'
406 ' "example_value": "c"'
410 ' "placeholders": [],'
413 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
419 ' //"Policy1": ["a", "b"],\n\n'
423 ' //"Policy2": "c"\n\n'
425 self.CompareOutputs(output, expected_output)
428 if __name__ == '__main__':