• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
9import os
10import sys
11if __name__ == '__main__':
12  sys.path.append(os.path.join(os.path.dirname(__file__), '../../../..'))
13
14import unittest
15
16from grit.format.policy_templates.writers import writer_unittest_common
17
18
19class 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 testIntPolicy(self):
169    # Tests a policy group with a single policy of type 'int'.
170    grd = self.PrepareTest('''
171      {
172        'policy_definitions': [
173          {
174            'name': 'IntGroup',
175            'type': 'group',
176            'caption': '',
177            'desc': '',
178            'policies': [{
179              'name': 'IntPolicy',
180              'type': 'int',
181              'caption': '',
182              'desc': '',
183              'supported_on': ['chrome.mac:8-'],
184            }],
185          },
186        ],
187        'placeholders': [],
188        'messages': {},
189      }''')
190    output = self.GetOutput(
191        grd,
192        'fr',
193        {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
194        'plist',
195        'en')
196    expected_output = self._GetExpectedOutputs(
197        'Chromium', 'com.example.Test', '''<array>
198      <dict>
199        <key>pfm_name</key>
200        <string>IntPolicy</string>
201        <key>pfm_description</key>
202        <string/>
203        <key>pfm_title</key>
204        <string/>
205        <key>pfm_targets</key>
206        <array>
207          <string>user-managed</string>
208        </array>
209        <key>pfm_type</key>
210        <string>integer</string>
211      </dict>
212    </array>''')
213    self.assertEquals(output.strip(), expected_output.strip())
214
215  def testIntEnumPolicy(self):
216    # Tests a policy group with a single policy of type 'int-enum'.
217    grd = self.PrepareTest('''
218      {
219        'policy_definitions': [
220          {
221            'name': 'EnumGroup',
222            'type': 'group',
223            'caption': '',
224            'desc': '',
225            'policies': [{
226              'name': 'EnumPolicy',
227              'type': 'int-enum',
228              'desc': '',
229              'caption': '',
230              'items': [
231                {'name': 'ProxyServerDisabled', 'value': 0, 'caption': ''},
232                {'name': 'ProxyServerAutoDetect', 'value': 1, 'caption': ''},
233              ],
234              'supported_on': ['chrome.mac:8-'],
235            }],
236          },
237        ],
238        'placeholders': [],
239        'messages': {},
240      }''')
241    output = self.GetOutput(
242        grd,
243        'fr',
244        {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'},
245        'plist',
246        'en')
247    expected_output = self._GetExpectedOutputs(
248        'Google_Chrome', 'com.example.Test2', '''<array>
249      <dict>
250        <key>pfm_name</key>
251        <string>EnumPolicy</string>
252        <key>pfm_description</key>
253        <string/>
254        <key>pfm_title</key>
255        <string/>
256        <key>pfm_targets</key>
257        <array>
258          <string>user-managed</string>
259        </array>
260        <key>pfm_type</key>
261        <string>integer</string>
262        <key>pfm_range_list</key>
263        <array>
264          <integer>0</integer>
265          <integer>1</integer>
266        </array>
267      </dict>
268    </array>''')
269    self.assertEquals(output.strip(), expected_output.strip())
270
271  def testStringEnumPolicy(self):
272    # Tests a policy group with a single policy of type 'string-enum'.
273    grd = self.PrepareTest('''
274      {
275        'policy_definitions': [
276          {
277            'name': 'EnumGroup',
278            'type': 'group',
279            'caption': '',
280            'desc': '',
281            'policies': [{
282              'name': 'EnumPolicy',
283              'type': 'string-enum',
284              'desc': '',
285              'caption': '',
286              'items': [
287                {'name': 'ProxyServerDisabled', 'value': 'one', 'caption': ''},
288                {'name': 'ProxyServerAutoDetect', 'value': 'two', 'caption': ''},
289              ],
290              'supported_on': ['chrome.mac:8-'],
291            }],
292          },
293        ],
294        'placeholders': [],
295        'messages': {},
296      }''')
297    output = self.GetOutput(
298        grd,
299        'fr',
300        {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'},
301        'plist',
302        'en')
303    expected_output = self._GetExpectedOutputs(
304        'Google_Chrome', 'com.example.Test2', '''<array>
305      <dict>
306        <key>pfm_name</key>
307        <string>EnumPolicy</string>
308        <key>pfm_description</key>
309        <string/>
310        <key>pfm_title</key>
311        <string/>
312        <key>pfm_targets</key>
313        <array>
314          <string>user-managed</string>
315        </array>
316        <key>pfm_type</key>
317        <string>string</string>
318        <key>pfm_range_list</key>
319        <array>
320          <string>one</string>
321          <string>two</string>
322        </array>
323      </dict>
324    </array>''')
325    self.assertEquals(output.strip(), expected_output.strip())
326
327  def testDictionaryPolicy(self):
328    # Tests a policy group with a single policy of type 'dict'.
329    grd = self.PrepareTest('''
330      {
331        'policy_definitions': [
332          {
333            'name': 'DictionaryGroup',
334            'type': 'group',
335            'desc': '',
336            'caption': '',
337            'policies': [{
338              'name': 'DictionaryPolicy',
339              'type': 'dict',
340              'supported_on': ['chrome.mac:8-'],
341              'desc': '',
342              'caption': '',
343            }],
344          },
345        ],
346        'placeholders': [],
347        'messages': {},
348      }''')
349    output = self.GetOutput(
350        grd,
351        'fr',
352        {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'},
353        'plist',
354        'en')
355    expected_output = self._GetExpectedOutputs(
356        'Chromium', 'com.example.Test', '''<array>
357      <dict>
358        <key>pfm_name</key>
359        <string>DictionaryPolicy</string>
360        <key>pfm_description</key>
361        <string/>
362        <key>pfm_title</key>
363        <string/>
364        <key>pfm_targets</key>
365        <array>
366          <string>user-managed</string>
367        </array>
368        <key>pfm_type</key>
369        <string>dictionary</string>
370      </dict>
371    </array>''')
372    self.assertEquals(output.strip(), expected_output.strip())
373
374  def testNonSupportedPolicy(self):
375    # Tests a policy that is not supported on Mac, so it shouldn't
376    # be included in the plist file.
377    grd = self.PrepareTest('''
378      {
379        'policy_definitions': [
380          {
381            'name': 'NonMacGroup',
382            'type': 'group',
383            'caption': '',
384            'desc': '',
385            'policies': [{
386              'name': 'NonMacPolicy',
387              'type': 'string',
388              'supported_on': ['chrome.linux:8-', 'chrome.win:7-'],
389              'caption': '',
390              'desc': '',
391            }],
392          },
393        ],
394        'placeholders': [],
395        'messages': {},
396      }''')
397    output = self.GetOutput(
398        grd,
399        'fr',
400        {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'},
401        'plist',
402        'en')
403    expected_output = self._GetExpectedOutputs(
404        'Google_Chrome', 'com.example.Test2', '''<array/>''')
405    self.assertEquals(output.strip(), expected_output.strip())
406
407
408if __name__ == '__main__':
409  unittest.main()
410