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.plist_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
19 class PListWriterUnittest(writer_unittest_common.WriterUnittestCommon):
20 '''Unit tests for PListWriter.'''
22 def _GetExpectedOutputs(self, product_name, bundle_id, policies):
23 '''Substitutes the variable parts into a plist template. The result
24 of this function can be used as an expected result to test the output
28 product_name: The name of the product, normally Chromium or Google Chrome.
29 bundle_id: The mac bundle id of the product.
30 policies: The list of policies.
33 The text of a plist template with the variable parts substituted.
36 <?xml version="1.0" ?>
37 <!DOCTYPE plist PUBLIC '-//Apple//DTD PLIST 1.0//EN' 'http://www.apple.com/DTDs/PropertyList-1.0.dtd'>
42 <key>pfm_description</key>
46 <key>pfm_version</key>
50 <key>pfm_subkeys</key>
53 </plist>''' % (product_name, bundle_id, policies)
56 # Test PListWriter in case of empty polices.
57 grd = self.PrepareTest('''
59 'policy_definitions': [],
64 output = self.GetOutput(
67 {'_chromium': '1', 'mac_bundle_id': 'com.example.Test'},
70 expected_output = self._GetExpectedOutputs(
71 'Chromium', 'com.example.Test', '<array/>')
72 self.assertEquals(output.strip(), expected_output.strip())
74 def testMainPolicy(self):
75 # Tests a policy group with a single policy of type 'main'.
76 grd = self.PrepareTest('''
78 'policy_definitions': [
87 'supported_on': ['chrome.mac:8-'],
96 output = self.GetOutput(
99 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
102 expected_output = self._GetExpectedOutputs(
103 'Chromium', 'com.example.Test', '''<array>
106 <string>MainPolicy</string>
107 <key>pfm_description</key>
111 <key>pfm_targets</key>
113 <string>user-managed</string>
116 <string>boolean</string>
119 self.assertEquals(output.strip(), expected_output.strip())
121 def testStringPolicy(self):
122 # Tests a policy group with a single policy of type 'string'.
123 grd = self.PrepareTest('''
125 'policy_definitions': [
127 'name': 'StringGroup',
132 'name': 'StringPolicy',
134 'supported_on': ['chrome.mac:8-'],
143 output = self.GetOutput(
146 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
149 expected_output = self._GetExpectedOutputs(
150 'Chromium', 'com.example.Test', '''<array>
153 <string>StringPolicy</string>
154 <key>pfm_description</key>
158 <key>pfm_targets</key>
160 <string>user-managed</string>
163 <string>string</string>
166 self.assertEquals(output.strip(), expected_output.strip())
168 def testListPolicy(self):
169 # Tests a policy group with a single policy of type 'list'.
170 grd = self.PrepareTest('''
172 'policy_definitions': [
179 'name': 'ListPolicy',
183 'items': { 'type': 'string' },
185 'supported_on': ['chrome.mac:8-'],
194 output = self.GetOutput(
197 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
200 expected_output = self._GetExpectedOutputs(
201 'Chromium', 'com.example.Test', '''<array>
204 <string>ListPolicy</string>
205 <key>pfm_description</key>
209 <key>pfm_targets</key>
211 <string>user-managed</string>
214 <string>array</string>
215 <key>pfm_subkeys</key>
219 <string>string</string>
224 self.assertEquals(output.strip(), expected_output.strip())
226 def testStringEnumListPolicy(self):
227 # Tests a policy group with a single policy of type 'string-enum-list'.
228 grd = self.PrepareTest('''
230 'policy_definitions': [
237 'name': 'ListPolicy',
238 'type': 'string-enum-list',
241 'items': { 'type': 'string' },
244 {'name': 'ProxyServerDisabled', 'value': 'one', 'caption': ''},
245 {'name': 'ProxyServerAutoDetect', 'value': 'two', 'caption': ''},
247 'supported_on': ['chrome.mac:8-'],
248 'supported_on': ['chrome.mac:8-'],
257 output = self.GetOutput(
260 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
263 expected_output = self._GetExpectedOutputs(
264 'Chromium', 'com.example.Test', '''<array>
267 <string>ListPolicy</string>
268 <key>pfm_description</key>
272 <key>pfm_targets</key>
274 <string>user-managed</string>
277 <string>array</string>
278 <key>pfm_subkeys</key>
282 <string>string</string>
287 self.assertEquals(output.strip(), expected_output.strip())
289 def testIntPolicy(self):
290 # Tests a policy group with a single policy of type 'int'.
291 grd = self.PrepareTest('''
293 'policy_definitions': [
304 'supported_on': ['chrome.mac:8-'],
311 output = self.GetOutput(
314 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
317 expected_output = self._GetExpectedOutputs(
318 'Chromium', 'com.example.Test', '''<array>
321 <string>IntPolicy</string>
322 <key>pfm_description</key>
326 <key>pfm_targets</key>
328 <string>user-managed</string>
331 <string>integer</string>
334 self.assertEquals(output.strip(), expected_output.strip())
336 def testIntEnumPolicy(self):
337 # Tests a policy group with a single policy of type 'int-enum'.
338 grd = self.PrepareTest('''
340 'policy_definitions': [
347 'name': 'EnumPolicy',
352 {'name': 'ProxyServerDisabled', 'value': 0, 'caption': ''},
353 {'name': 'ProxyServerAutoDetect', 'value': 1, 'caption': ''},
355 'supported_on': ['chrome.mac:8-'],
362 output = self.GetOutput(
365 {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'},
368 expected_output = self._GetExpectedOutputs(
369 'Google_Chrome', 'com.example.Test2', '''<array>
372 <string>EnumPolicy</string>
373 <key>pfm_description</key>
377 <key>pfm_targets</key>
379 <string>user-managed</string>
382 <string>integer</string>
383 <key>pfm_range_list</key>
390 self.assertEquals(output.strip(), expected_output.strip())
392 def testStringEnumPolicy(self):
393 # Tests a policy group with a single policy of type 'string-enum'.
394 grd = self.PrepareTest('''
396 'policy_definitions': [
403 'name': 'EnumPolicy',
404 'type': 'string-enum',
408 {'name': 'ProxyServerDisabled', 'value': 'one', 'caption': ''},
409 {'name': 'ProxyServerAutoDetect', 'value': 'two', 'caption': ''},
411 'supported_on': ['chrome.mac:8-'],
418 output = self.GetOutput(
421 {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'},
424 expected_output = self._GetExpectedOutputs(
425 'Google_Chrome', 'com.example.Test2', '''<array>
428 <string>EnumPolicy</string>
429 <key>pfm_description</key>
433 <key>pfm_targets</key>
435 <string>user-managed</string>
438 <string>string</string>
439 <key>pfm_range_list</key>
446 self.assertEquals(output.strip(), expected_output.strip())
448 def testDictionaryPolicy(self):
449 # Tests a policy group with a single policy of type 'dict'.
450 grd = self.PrepareTest('''
452 'policy_definitions': [
454 'name': 'DictionaryGroup',
459 'name': 'DictionaryPolicy',
461 'supported_on': ['chrome.mac:8-'],
470 output = self.GetOutput(
473 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
476 expected_output = self._GetExpectedOutputs(
477 'Chromium', 'com.example.Test', '''<array>
480 <string>DictionaryPolicy</string>
481 <key>pfm_description</key>
485 <key>pfm_targets</key>
487 <string>user-managed</string>
490 <string>dictionary</string>
493 self.assertEquals(output.strip(), expected_output.strip())
495 def testNonSupportedPolicy(self):
496 # Tests a policy that is not supported on Mac, so it shouldn't
497 # be included in the plist file.
498 grd = self.PrepareTest('''
500 'policy_definitions': [
502 'name': 'NonMacGroup',
507 'name': 'NonMacPolicy',
509 'supported_on': ['chrome.linux:8-', 'chrome.win:7-'],
518 output = self.GetOutput(
521 {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'},
524 expected_output = self._GetExpectedOutputs(
525 'Google_Chrome', 'com.example.Test2', '''<array/>''')
526 self.assertEquals(output.strip(), expected_output.strip())
529 if __name__ == '__main__':