Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / grit / grit / format / policy_templates / writers / plist_writer_unittest.py
1 #!/usr/bin/env python
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.
5
6 '''Unit tests for grit.format.policy_templates.writers.plist_writer'''
7
8
9 import os
10 import sys
11 if __name__ == '__main__':
12   sys.path.append(os.path.join(os.path.dirname(__file__), '../../../..'))
13
14 import unittest
15
16 from grit.format.policy_templates.writers import writer_unittest_common
17
18
19 class PListWriterUnittest(writer_unittest_common.WriterUnittestCommon):
20   '''Unit tests for PListWriter.'''
21
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
25     of PListWriter.
26
27     Args:
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.
31
32     Returns:
33       The text of a plist template with the variable parts substituted.
34     '''
35     return '''
36 <?xml version="1.0" ?>
37 <!DOCTYPE plist  PUBLIC '-//Apple//DTD PLIST 1.0//EN'  'http://www.apple.com/DTDs/PropertyList-1.0.dtd'>
38 <plist version="1">
39   <dict>
40     <key>pfm_name</key>
41     <string>%s</string>
42     <key>pfm_description</key>
43     <string/>
44     <key>pfm_title</key>
45     <string/>
46     <key>pfm_version</key>
47     <string>1</string>
48     <key>pfm_domain</key>
49     <string>%s</string>
50     <key>pfm_subkeys</key>
51     %s
52   </dict>
53 </plist>''' % (product_name, bundle_id, policies)
54
55   def testEmpty(self):
56     # Test PListWriter in case of empty polices.
57     grd = self.PrepareTest('''
58       {
59         'policy_definitions': [],
60         'placeholders': [],
61         'messages': {},
62       }''')
63
64     output = self.GetOutput(
65         grd,
66         'fr',
67         {'_chromium': '1', 'mac_bundle_id': 'com.example.Test'},
68         'plist',
69         'en')
70     expected_output = self._GetExpectedOutputs(
71         'Chromium', 'com.example.Test', '<array/>')
72     self.assertEquals(output.strip(), expected_output.strip())
73
74   def testMainPolicy(self):
75     # Tests a policy group with a single policy of type 'main'.
76     grd = self.PrepareTest('''
77       {
78         'policy_definitions': [
79           {
80             'name': 'MainGroup',
81             'type': 'group',
82             'policies': [{
83               'name': 'MainPolicy',
84               'type': 'main',
85               'desc': '',
86               'caption': '',
87               'supported_on': ['chrome.mac:8-'],
88             }],
89             'desc': '',
90             'caption': '',
91           },
92         ],
93         'placeholders': [],
94         'messages': {}
95       }''')
96     output = self.GetOutput(
97         grd,
98         'fr',
99         {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
100         'plist',
101         'en')
102     expected_output = self._GetExpectedOutputs(
103         'Chromium', 'com.example.Test', '''<array>
104       <dict>
105         <key>pfm_name</key>
106         <string>MainPolicy</string>
107         <key>pfm_description</key>
108         <string/>
109         <key>pfm_title</key>
110         <string/>
111         <key>pfm_targets</key>
112         <array>
113           <string>user-managed</string>
114         </array>
115         <key>pfm_type</key>
116         <string>boolean</string>
117       </dict>
118     </array>''')
119     self.assertEquals(output.strip(), expected_output.strip())
120
121   def testStringPolicy(self):
122     # Tests a policy group with a single policy of type 'string'.
123     grd = self.PrepareTest('''
124       {
125         'policy_definitions': [
126           {
127             'name': 'StringGroup',
128             'type': 'group',
129             'desc': '',
130             'caption': '',
131             'policies': [{
132               'name': 'StringPolicy',
133               'type': 'string',
134               'supported_on': ['chrome.mac:8-'],
135               'desc': '',
136               'caption': '',
137             }],
138           },
139         ],
140         'placeholders': [],
141         'messages': {},
142       }''')
143     output = self.GetOutput(
144         grd,
145         'fr',
146         {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
147         'plist',
148         'en')
149     expected_output = self._GetExpectedOutputs(
150         'Chromium', 'com.example.Test', '''<array>
151       <dict>
152         <key>pfm_name</key>
153         <string>StringPolicy</string>
154         <key>pfm_description</key>
155         <string/>
156         <key>pfm_title</key>
157         <string/>
158         <key>pfm_targets</key>
159         <array>
160           <string>user-managed</string>
161         </array>
162         <key>pfm_type</key>
163         <string>string</string>
164       </dict>
165     </array>''')
166     self.assertEquals(output.strip(), expected_output.strip())
167
168   def testListPolicy(self):
169     # Tests a policy group with a single policy of type 'list'.
170     grd = self.PrepareTest('''
171       {
172         'policy_definitions': [
173           {
174             'name': 'ListGroup',
175             'type': 'group',
176             'desc': '',
177             'caption': '',
178             'policies': [{
179               'name': 'ListPolicy',
180               'type': 'list',
181               'schema': {
182                 'type': 'array',
183                 'items': { 'type': 'string' },
184               },
185               'supported_on': ['chrome.mac:8-'],
186               'desc': '',
187               'caption': '',
188             }],
189           },
190         ],
191         'placeholders': [],
192         'messages': {},
193       }''')
194     output = self.GetOutput(
195         grd,
196         'fr',
197         {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
198         'plist',
199         'en')
200     expected_output = self._GetExpectedOutputs(
201         'Chromium', 'com.example.Test', '''<array>
202       <dict>
203         <key>pfm_name</key>
204         <string>ListPolicy</string>
205         <key>pfm_description</key>
206         <string/>
207         <key>pfm_title</key>
208         <string/>
209         <key>pfm_targets</key>
210         <array>
211           <string>user-managed</string>
212         </array>
213         <key>pfm_type</key>
214         <string>array</string>
215         <key>pfm_subkeys</key>
216         <array>
217           <dict>
218             <key>pfm_type</key>
219             <string>string</string>
220           </dict>
221         </array>
222       </dict>
223     </array>''')
224     self.assertEquals(output.strip(), expected_output.strip())
225
226   def testStringEnumListPolicy(self):
227     # Tests a policy group with a single policy of type 'string-enum-list'.
228     grd = self.PrepareTest('''
229       {
230         'policy_definitions': [
231           {
232             'name': 'ListGroup',
233             'type': 'group',
234             'desc': '',
235             'caption': '',
236             'policies': [{
237               'name': 'ListPolicy',
238               'type': 'string-enum-list',
239               'schema': {
240                 'type': 'array',
241                 'items': { 'type': 'string' },
242               },
243               'items': [
244                 {'name': 'ProxyServerDisabled', 'value': 'one', 'caption': ''},
245                 {'name': 'ProxyServerAutoDetect', 'value': 'two', 'caption': ''},
246               ],
247               'supported_on': ['chrome.mac:8-'],
248               'supported_on': ['chrome.mac:8-'],
249               'desc': '',
250               'caption': '',
251             }],
252           },
253         ],
254         'placeholders': [],
255         'messages': {},
256       }''')
257     output = self.GetOutput(
258         grd,
259         'fr',
260         {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
261         'plist',
262         'en')
263     expected_output = self._GetExpectedOutputs(
264         'Chromium', 'com.example.Test', '''<array>
265       <dict>
266         <key>pfm_name</key>
267         <string>ListPolicy</string>
268         <key>pfm_description</key>
269         <string/>
270         <key>pfm_title</key>
271         <string/>
272         <key>pfm_targets</key>
273         <array>
274           <string>user-managed</string>
275         </array>
276         <key>pfm_type</key>
277         <string>array</string>
278         <key>pfm_subkeys</key>
279         <array>
280           <dict>
281             <key>pfm_type</key>
282             <string>string</string>
283           </dict>
284         </array>
285       </dict>
286     </array>''')
287     self.assertEquals(output.strip(), expected_output.strip())
288
289   def testIntPolicy(self):
290     # Tests a policy group with a single policy of type 'int'.
291     grd = self.PrepareTest('''
292       {
293         'policy_definitions': [
294           {
295             'name': 'IntGroup',
296             'type': 'group',
297             'caption': '',
298             'desc': '',
299             'policies': [{
300               'name': 'IntPolicy',
301               'type': 'int',
302               'caption': '',
303               'desc': '',
304               'supported_on': ['chrome.mac:8-'],
305             }],
306           },
307         ],
308         'placeholders': [],
309         'messages': {},
310       }''')
311     output = self.GetOutput(
312         grd,
313         'fr',
314         {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
315         'plist',
316         'en')
317     expected_output = self._GetExpectedOutputs(
318         'Chromium', 'com.example.Test', '''<array>
319       <dict>
320         <key>pfm_name</key>
321         <string>IntPolicy</string>
322         <key>pfm_description</key>
323         <string/>
324         <key>pfm_title</key>
325         <string/>
326         <key>pfm_targets</key>
327         <array>
328           <string>user-managed</string>
329         </array>
330         <key>pfm_type</key>
331         <string>integer</string>
332       </dict>
333     </array>''')
334     self.assertEquals(output.strip(), expected_output.strip())
335
336   def testIntEnumPolicy(self):
337     # Tests a policy group with a single policy of type 'int-enum'.
338     grd = self.PrepareTest('''
339       {
340         'policy_definitions': [
341           {
342             'name': 'EnumGroup',
343             'type': 'group',
344             'caption': '',
345             'desc': '',
346             'policies': [{
347               'name': 'EnumPolicy',
348               'type': 'int-enum',
349               'desc': '',
350               'caption': '',
351               'items': [
352                 {'name': 'ProxyServerDisabled', 'value': 0, 'caption': ''},
353                 {'name': 'ProxyServerAutoDetect', 'value': 1, 'caption': ''},
354               ],
355               'supported_on': ['chrome.mac:8-'],
356             }],
357           },
358         ],
359         'placeholders': [],
360         'messages': {},
361       }''')
362     output = self.GetOutput(
363         grd,
364         'fr',
365         {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'},
366         'plist',
367         'en')
368     expected_output = self._GetExpectedOutputs(
369         'Google_Chrome', 'com.example.Test2', '''<array>
370       <dict>
371         <key>pfm_name</key>
372         <string>EnumPolicy</string>
373         <key>pfm_description</key>
374         <string/>
375         <key>pfm_title</key>
376         <string/>
377         <key>pfm_targets</key>
378         <array>
379           <string>user-managed</string>
380         </array>
381         <key>pfm_type</key>
382         <string>integer</string>
383         <key>pfm_range_list</key>
384         <array>
385           <integer>0</integer>
386           <integer>1</integer>
387         </array>
388       </dict>
389     </array>''')
390     self.assertEquals(output.strip(), expected_output.strip())
391
392   def testStringEnumPolicy(self):
393     # Tests a policy group with a single policy of type 'string-enum'.
394     grd = self.PrepareTest('''
395       {
396         'policy_definitions': [
397           {
398             'name': 'EnumGroup',
399             'type': 'group',
400             'caption': '',
401             'desc': '',
402             'policies': [{
403               'name': 'EnumPolicy',
404               'type': 'string-enum',
405               'desc': '',
406               'caption': '',
407               'items': [
408                 {'name': 'ProxyServerDisabled', 'value': 'one', 'caption': ''},
409                 {'name': 'ProxyServerAutoDetect', 'value': 'two', 'caption': ''},
410               ],
411               'supported_on': ['chrome.mac:8-'],
412             }],
413           },
414         ],
415         'placeholders': [],
416         'messages': {},
417       }''')
418     output = self.GetOutput(
419         grd,
420         'fr',
421         {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'},
422         'plist',
423         'en')
424     expected_output = self._GetExpectedOutputs(
425         'Google_Chrome', 'com.example.Test2', '''<array>
426       <dict>
427         <key>pfm_name</key>
428         <string>EnumPolicy</string>
429         <key>pfm_description</key>
430         <string/>
431         <key>pfm_title</key>
432         <string/>
433         <key>pfm_targets</key>
434         <array>
435           <string>user-managed</string>
436         </array>
437         <key>pfm_type</key>
438         <string>string</string>
439         <key>pfm_range_list</key>
440         <array>
441           <string>one</string>
442           <string>two</string>
443         </array>
444       </dict>
445     </array>''')
446     self.assertEquals(output.strip(), expected_output.strip())
447
448   def testDictionaryPolicy(self):
449     # Tests a policy group with a single policy of type 'dict'.
450     grd = self.PrepareTest('''
451       {
452         'policy_definitions': [
453           {
454             'name': 'DictionaryGroup',
455             'type': 'group',
456             'desc': '',
457             'caption': '',
458             'policies': [{
459               'name': 'DictionaryPolicy',
460               'type': 'dict',
461               'supported_on': ['chrome.mac:8-'],
462               'desc': '',
463               'caption': '',
464             }],
465           },
466         ],
467         'placeholders': [],
468         'messages': {},
469       }''')
470     output = self.GetOutput(
471         grd,
472         'fr',
473         {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
474         'plist',
475         'en')
476     expected_output = self._GetExpectedOutputs(
477         'Chromium', 'com.example.Test', '''<array>
478       <dict>
479         <key>pfm_name</key>
480         <string>DictionaryPolicy</string>
481         <key>pfm_description</key>
482         <string/>
483         <key>pfm_title</key>
484         <string/>
485         <key>pfm_targets</key>
486         <array>
487           <string>user-managed</string>
488         </array>
489         <key>pfm_type</key>
490         <string>dictionary</string>
491       </dict>
492     </array>''')
493     self.assertEquals(output.strip(), expected_output.strip())
494
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('''
499       {
500         'policy_definitions': [
501           {
502             'name': 'NonMacGroup',
503             'type': 'group',
504             'caption': '',
505             'desc': '',
506             'policies': [{
507               'name': 'NonMacPolicy',
508               'type': 'string',
509               'supported_on': ['chrome.linux:8-', 'chrome.win:7-'],
510               'caption': '',
511               'desc': '',
512             }],
513           },
514         ],
515         'placeholders': [],
516         'messages': {},
517       }''')
518     output = self.GetOutput(
519         grd,
520         'fr',
521         {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'},
522         'plist',
523         'en')
524     expected_output = self._GetExpectedOutputs(
525         'Google_Chrome', 'com.example.Test2', '''<array/>''')
526     self.assertEquals(output.strip(), expected_output.strip())
527
528
529 if __name__ == '__main__':
530   unittest.main()