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!
29 //-------------------------------------------------------------------------
33 class JsonWriterUnittest(writer_unittest_common.WriterUnittestCommon):
34 '''Unit tests for JsonWriter.'''
36 def CompareOutputs(self, output, expected_output):
37 '''Compares the output of the json_writer with its expected output.
40 output: The output of the json writer as returned by grit.
41 expected_output: The expected output.
44 AssertionError: if the two strings are not equivalent.
48 expected_output.strip())
51 # Test the handling of an empty policy list.
52 grd = self.PrepareTest(
54 ' "policy_definitions": [],'
55 ' "placeholders": [],'
58 output = self.GetOutput(grd, 'fr', {'_chromium': '1',}, 'json', 'en')
59 expected_output = TEMPLATE_HEADER + '}'
60 self.CompareOutputs(output, expected_output)
62 def testMainPolicy(self):
63 # Tests a policy group with a single policy of type 'main'.
64 grd = self.PrepareTest(
66 ' "policy_definitions": ['
68 ' "name": "MainPolicy",'
70 ' "caption": "Example Main Policy",'
71 ' "desc": "Example Main Policy",'
72 ' "supported_on": ["chrome.linux:8-"],'
73 ' "example_value": True'
76 ' "placeholders": [],'
79 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'json', 'en')
82 ' // Example Main Policy\n' +
84 ' // Example Main Policy\n\n'
85 ' //"MainPolicy": true\n\n'
87 self.CompareOutputs(output, expected_output)
89 def testStringPolicy(self):
90 # Tests a policy group with a single policy of type 'string'.
91 grd = self.PrepareTest(
93 ' "policy_definitions": ['
95 ' "name": "StringPolicy",'
97 ' "caption": "Example String Policy",'
98 ' "desc": "Example String Policy",'
99 ' "supported_on": ["chrome.linux:8-"],'
100 ' "example_value": "hello, world!"'
103 ' "placeholders": [],'
106 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
109 ' // Example String Policy\n' +
111 ' // Example String Policy\n\n'
112 ' //"StringPolicy": "hello, world!"\n\n'
114 self.CompareOutputs(output, expected_output)
116 def testIntPolicy(self):
117 # Tests a policy group with a single policy of type 'string'.
118 grd = self.PrepareTest(
120 ' "policy_definitions": ['
122 ' "name": "IntPolicy",'
124 ' "caption": "Example Int Policy",'
125 ' "desc": "Example Int Policy",'
126 ' "supported_on": ["chrome.linux:8-"],'
127 ' "example_value": 15'
130 ' "placeholders": [],'
133 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
136 ' // Example Int Policy\n' +
138 ' // Example Int Policy\n\n'
139 ' //"IntPolicy": 15\n\n'
141 self.CompareOutputs(output, expected_output)
143 def testIntEnumPolicy(self):
144 # Tests a policy group with a single policy of type 'int-enum'.
145 grd = self.PrepareTest(
147 ' "policy_definitions": ['
149 ' "name": "EnumPolicy",'
150 ' "type": "int-enum",'
151 ' "caption": "Example Int Enum",'
152 ' "desc": "Example Int Enum",'
154 ' {"name": "ProxyServerDisabled", "value": 0, "caption": ""},'
155 ' {"name": "ProxyServerAutoDetect", "value": 1, "caption": ""},'
157 ' "supported_on": ["chrome.linux:8-"],'
158 ' "example_value": 1'
161 ' "placeholders": [],'
164 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'json', 'en')
167 ' // Example Int Enum\n' +
169 ' // Example Int Enum\n\n'
170 ' //"EnumPolicy": 1\n\n'
172 self.CompareOutputs(output, expected_output)
174 def testStringEnumPolicy(self):
175 # Tests a policy group with a single policy of type 'string-enum'.
176 grd = self.PrepareTest(
178 ' "policy_definitions": ['
180 ' "name": "EnumPolicy",'
181 ' "type": "string-enum",'
182 ' "caption": "Example String Enum",'
183 ' "desc": "Example String Enum",'
185 ' {"name": "ProxyServerDisabled", "value": "one",'
187 ' {"name": "ProxyServerAutoDetect", "value": "two",'
190 ' "supported_on": ["chrome.linux:8-"],'
191 ' "example_value": "one"'
194 ' "placeholders": [],'
197 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'json', 'en')
200 ' // Example String Enum\n' +
202 ' // Example String Enum\n\n'
203 ' //"EnumPolicy": "one"\n\n'
205 self.CompareOutputs(output, expected_output)
207 def testListPolicy(self):
208 # Tests a policy group with a single policy of type 'list'.
209 grd = self.PrepareTest(
211 ' "policy_definitions": ['
213 ' "name": "ListPolicy",'
215 ' "caption": "Example List",'
216 ' "desc": "Example List",'
217 ' "supported_on": ["chrome.linux:8-"],'
218 ' "example_value": ["foo", "bar"]'
221 ' "placeholders": [],'
224 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
227 ' // Example List\n' +
229 ' // Example List\n\n'
230 ' //"ListPolicy": ["foo", "bar"]\n\n'
232 self.CompareOutputs(output, expected_output)
234 def testStringEnumListPolicy(self):
235 # Tests a policy group with a single policy of type 'string-enum-list'.
236 grd = self.PrepareTest(
238 ' "policy_definitions": ['
240 ' "name": "ListPolicy",'
241 ' "type": "string-enum-list",'
242 ' "caption": "Example List",'
243 ' "desc": "Example List",'
245 ' {"name": "ProxyServerDisabled", "value": "one",'
247 ' {"name": "ProxyServerAutoDetect", "value": "two",'
250 ' "supported_on": ["chrome.linux:8-"],'
251 ' "example_value": ["one", "two"]'
254 ' "placeholders": [],'
257 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
260 ' // Example List\n' +
262 ' // Example List\n\n'
263 ' //"ListPolicy": ["one", "two"]\n\n'
265 self.CompareOutputs(output, expected_output)
267 def testDictionaryPolicy(self):
268 # Tests a policy group with a single policy of type 'dict'.
279 grd = self.PrepareTest(
281 ' "policy_definitions": ['
283 ' "name": "DictionaryPolicy",'
285 ' "caption": "Example Dictionary Policy",'
286 ' "desc": "Example Dictionary Policy",'
287 ' "supported_on": ["chrome.linux:8-"],'
288 ' "example_value": ' + str(example) +
291 ' "placeholders": [],'
294 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
297 ' // Example Dictionary Policy\n' +
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'
303 self.CompareOutputs(output, expected_output)
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(
310 ' "policy_definitions": ['
312 ' "name": "NonLinuxPolicy",'
316 ' "supported_on": ["chrome.mac:8-"],'
317 ' "example_value": ["a"]'
320 ' "placeholders": [],'
323 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
324 expected_output = TEMPLATE_HEADER + '}'
325 self.CompareOutputs(output, expected_output)
327 def testPolicyGroup(self):
328 # Tests a policy group that has more than one policies.
329 grd = self.PrepareTest(
331 ' "policy_definitions": ['
338 ' "name": "Policy1",'
340 ' "caption": "Policy One",'
341 ' "desc": "Policy One",'
342 ' "supported_on": ["chrome.linux:8-"],'
343 ' "example_value": ["a", "b"]'
345 ' "name": "Policy2",'
347 ' "caption": "Policy Two",'
348 ' "desc": "Policy Two",'
349 ' "supported_on": ["chrome.linux:8-"],'
350 ' "example_value": "c"'
354 ' "placeholders": [],'
357 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
363 ' //"Policy1": ["a", "b"],\n\n'
367 ' //"Policy2": "c"\n\n'
369 self.CompareOutputs(output, expected_output)
372 if __name__ == '__main__':