2 # Copyright (c) 2014 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.ios_plist_writer'''
14 if __name__ == '__main__':
15 sys.path.append(os.path.join(os.path.dirname(__file__), '../../../..'))
24 from grit.format.policy_templates.writers import writer_unittest_common
27 class IOSPListWriterUnittest(writer_unittest_common.WriterUnittestCommon):
28 '''Unit tests for IOSPListWriter.'''
30 def _ParseWithPython(self, decode, text):
31 '''Parses a serialized Plist, using Python's plistlib.
33 If |decode| is true then |text| is decoded as Base64 before being
34 deserialized as a Plist.'''
36 text = base64.b64decode(text)
37 return plistlib.readPlistFromString(text)
39 def _ParseWithCocoa(self, decode, text):
40 '''Parses a serialized Plist, using Cocoa's python bindings.
42 If |decode| is true then |text| is decoded as Base64 before being
43 deserialized as a Plist.'''
45 data = Cocoa.NSData.alloc().initWithBase64EncodedString_options_(text, 0)
47 data = Cocoa.NSData.alloc().initWithBytes_length_(text, len(text))
48 result = Cocoa.NSPropertyListSerialization. \
49 propertyListFromData_mutabilityOption_format_errorDescription_(
50 data, Cocoa.NSPropertyListImmutable, None, None)
53 def _VerifyGeneratedOutputWithParsers(self,
58 # Generate the grit output for |templates|.
59 output = self.GetOutput(
60 self.PrepareTest(templates),
62 { '_chromium': '1', 'mac_bundle_id': 'com.example.Test' },
66 # Parse it as a Plist.
68 self.assertEquals(len(plist), 2)
69 self.assertTrue('ChromePolicy' in plist)
70 self.assertTrue('EncodedChromePolicy' in plist)
72 # Get the 2 expected fields.
73 chrome_policy = plist['ChromePolicy']
74 encoded_chrome_policy = plist['EncodedChromePolicy']
76 # Verify the ChromePolicy.
77 self.assertEquals(chrome_policy, expected_output)
79 # Decode the EncodedChromePolicy and verify it.
80 decoded_chrome_policy = decode_and_parse(encoded_chrome_policy)
81 self.assertEquals(decoded_chrome_policy, expected_output)
83 def _VerifyGeneratedOutput(self, templates, expected):
84 # plistlib is available on all Python platforms.
85 parse = functools.partial(self._ParseWithPython, False)
86 decode_and_parse = functools.partial(self._ParseWithPython, True)
87 self._VerifyGeneratedOutputWithParsers(
88 templates, expected, parse, decode_and_parse)
90 # The Cocoa bindings are available on Mac OS X only.
92 parse = functools.partial(self._ParseWithCocoa, False)
93 decode_and_parse = functools.partial(self._ParseWithCocoa, True)
94 self._VerifyGeneratedOutputWithParsers(
95 templates, expected, parse, decode_and_parse)
97 def _MakeTemplate(self, name, type, example, extra=''):
100 'policy_definitions': [
106 'supported_on': ['ios:35-'],
114 ''' % (name, type, example, extra)
119 'policy_definitions': [],
125 self._VerifyGeneratedOutput(templates, expected)
127 def testBoolean(self):
128 templates = self._MakeTemplate('BooleanPolicy', 'main', 'True')
130 'BooleanPolicy': True,
132 self._VerifyGeneratedOutput(templates, expected)
134 def testString(self):
135 templates = self._MakeTemplate('StringPolicy', 'string', '"Foo"')
137 'StringPolicy': 'Foo',
139 self._VerifyGeneratedOutput(templates, expected)
141 def testStringEnum(self):
142 templates = self._MakeTemplate(
143 'StringEnumPolicy', 'string-enum', '"Foo"',
146 { 'name': 'Foo', 'value': 'Foo', 'caption': '' },
147 { 'name': 'Bar', 'value': 'Bar', 'caption': '' },
151 'StringEnumPolicy': 'Foo',
153 self._VerifyGeneratedOutput(templates, expected)
156 templates = self._MakeTemplate('IntPolicy', 'int', '42')
160 self._VerifyGeneratedOutput(templates, expected)
162 def testIntEnum(self):
163 templates = self._MakeTemplate(
164 'IntEnumPolicy', 'int-enum', '42',
167 { 'name': 'Foo', 'value': 100, 'caption': '' },
168 { 'name': 'Bar', 'value': 42, 'caption': '' },
174 self._VerifyGeneratedOutput(templates, expected)
176 def testStringList(self):
177 templates = self._MakeTemplate('StringListPolicy', 'list', '["a", "b"]')
179 'StringListPolicy': [ "a", "b" ],
181 self._VerifyGeneratedOutput(templates, expected)
183 def testListOfDictionary(self):
184 templates = self._MakeTemplate(
185 'ManagedBookmarks', 'dict',
189 "name": "Google Search",
190 "url": "www.google.com",
194 "url": "www.youtube.com",
199 'ManagedBookmarks': [
200 { "name": "Google Search", "url": "www.google.com" },
201 { "name": "Youtube", "url": "www.youtube.com" },
204 self._VerifyGeneratedOutput(templates, expected)
207 if __name__ == '__main__':