| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 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. | |
| 5 | |
| 6 '''Unit tests for grit.format.policy_templates.writers.ios_plist_writer''' | |
| 7 | |
| 8 | |
| 9 import base64 | |
| 10 import functools | |
| 11 import os | |
| 12 import plistlib | |
| 13 import sys | |
| 14 if __name__ == '__main__': | |
| 15 sys.path.append(os.path.join(os.path.dirname(__file__), '../../../..')) | |
| 16 | |
| 17 import unittest | |
| 18 | |
| 19 try: | |
| 20 import Cocoa | |
| 21 except: | |
| 22 Cocoa = None | |
| 23 | |
| 24 from grit.format.policy_templates.writers import writer_unittest_common | |
| 25 | |
| 26 | |
| 27 class IOSPListWriterUnittest(writer_unittest_common.WriterUnittestCommon): | |
| 28 '''Unit tests for IOSPListWriter.''' | |
| 29 | |
| 30 def _ParseWithPython(self, decode, text): | |
| 31 '''Parses a serialized Plist, using Python's plistlib. | |
| 32 | |
| 33 If |decode| is true then |text| is decoded as Base64 before being | |
| 34 deserialized as a Plist.''' | |
| 35 if decode: | |
| 36 text = base64.b64decode(text) | |
| 37 return plistlib.readPlistFromString(text) | |
| 38 | |
| 39 def _ParseWithCocoa(self, decode, text): | |
| 40 '''Parses a serialized Plist, using Cocoa's python bindings. | |
| 41 | |
| 42 If |decode| is true then |text| is decoded as Base64 before being | |
| 43 deserialized as a Plist.''' | |
| 44 if decode: | |
| 45 data = Cocoa.NSData.alloc().initWithBase64EncodedString_options_(text, 0) | |
| 46 else: | |
| 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) | |
| 51 return result[0] | |
| 52 | |
| 53 def _VerifyGeneratedOutputWithParsers(self, | |
| 54 templates, | |
| 55 expected_output, | |
| 56 parse, | |
| 57 decode_and_parse): | |
| 58 | |
| 59 | |
| 60 _defines = { '_chromium': '1', | |
| 61 'mac_bundle_id': 'com.example.Test', | |
| 62 'version': '39.0.0.0' } | |
| 63 | |
| 64 # Generate the grit output for |templates|. | |
| 65 output = self.GetOutput( | |
| 66 self.PrepareTest(templates), | |
| 67 'fr', | |
| 68 _defines, | |
| 69 'ios_plist', | |
| 70 'en') | |
| 71 | |
| 72 # Parse it as a Plist. | |
| 73 plist = parse(output) | |
| 74 self.assertEquals(len(plist), 2) | |
| 75 self.assertTrue('ChromePolicy' in plist) | |
| 76 self.assertTrue('EncodedChromePolicy' in plist) | |
| 77 | |
| 78 # Get the 2 expected fields. | |
| 79 chrome_policy = plist['ChromePolicy'] | |
| 80 encoded_chrome_policy = plist['EncodedChromePolicy'] | |
| 81 | |
| 82 # Verify the ChromePolicy. | |
| 83 self.assertEquals(chrome_policy, expected_output) | |
| 84 | |
| 85 # Decode the EncodedChromePolicy and verify it. | |
| 86 decoded_chrome_policy = decode_and_parse(encoded_chrome_policy) | |
| 87 self.assertEquals(decoded_chrome_policy, expected_output) | |
| 88 | |
| 89 def _VerifyGeneratedOutput(self, templates, expected): | |
| 90 # plistlib is available on all Python platforms. | |
| 91 parse = functools.partial(self._ParseWithPython, False) | |
| 92 decode_and_parse = functools.partial(self._ParseWithPython, True) | |
| 93 self._VerifyGeneratedOutputWithParsers( | |
| 94 templates, expected, parse, decode_and_parse) | |
| 95 | |
| 96 # The Cocoa bindings are available on Mac OS X only. | |
| 97 if Cocoa: | |
| 98 parse = functools.partial(self._ParseWithCocoa, False) | |
| 99 decode_and_parse = functools.partial(self._ParseWithCocoa, True) | |
| 100 self._VerifyGeneratedOutputWithParsers( | |
| 101 templates, expected, parse, decode_and_parse) | |
| 102 | |
| 103 def _MakeTemplate(self, name, type, example, extra=''): | |
| 104 return ''' | |
| 105 { | |
| 106 'policy_definitions': [ | |
| 107 { | |
| 108 'name': '%s', | |
| 109 'type': '%s', | |
| 110 'desc': '', | |
| 111 'caption': '', | |
| 112 'supported_on': ['ios:35-'], | |
| 113 'example_value': %s, | |
| 114 %s | |
| 115 }, | |
| 116 ], | |
| 117 'placeholders': [], | |
| 118 'messages': {}, | |
| 119 } | |
| 120 ''' % (name, type, example, extra) | |
| 121 | |
| 122 def testEmpty(self): | |
| 123 templates = ''' | |
| 124 { | |
| 125 'policy_definitions': [], | |
| 126 'placeholders': [], | |
| 127 'messages': {}, | |
| 128 } | |
| 129 ''' | |
| 130 expected = {} | |
| 131 self._VerifyGeneratedOutput(templates, expected) | |
| 132 | |
| 133 def testEmptyVersion(self): | |
| 134 templates = ''' | |
| 135 { | |
| 136 'policy_definitions': [], | |
| 137 'placeholders': [], | |
| 138 'messages': {}, | |
| 139 } | |
| 140 ''' | |
| 141 expected = {} | |
| 142 self._VerifyGeneratedOutput(templates, expected) | |
| 143 | |
| 144 def testBoolean(self): | |
| 145 templates = self._MakeTemplate('BooleanPolicy', 'main', 'True') | |
| 146 expected = { | |
| 147 'BooleanPolicy': True, | |
| 148 } | |
| 149 self._VerifyGeneratedOutput(templates, expected) | |
| 150 | |
| 151 def testString(self): | |
| 152 templates = self._MakeTemplate('StringPolicy', 'string', '"Foo"') | |
| 153 expected = { | |
| 154 'StringPolicy': 'Foo', | |
| 155 } | |
| 156 self._VerifyGeneratedOutput(templates, expected) | |
| 157 | |
| 158 def testStringEnum(self): | |
| 159 templates = self._MakeTemplate( | |
| 160 'StringEnumPolicy', 'string-enum', '"Foo"', | |
| 161 ''' | |
| 162 'items': [ | |
| 163 { 'name': 'Foo', 'value': 'Foo', 'caption': '' }, | |
| 164 { 'name': 'Bar', 'value': 'Bar', 'caption': '' }, | |
| 165 ], | |
| 166 ''') | |
| 167 expected = { | |
| 168 'StringEnumPolicy': 'Foo', | |
| 169 } | |
| 170 self._VerifyGeneratedOutput(templates, expected) | |
| 171 | |
| 172 def testInt(self): | |
| 173 templates = self._MakeTemplate('IntPolicy', 'int', '42') | |
| 174 expected = { | |
| 175 'IntPolicy': 42, | |
| 176 } | |
| 177 self._VerifyGeneratedOutput(templates, expected) | |
| 178 | |
| 179 def testIntEnum(self): | |
| 180 templates = self._MakeTemplate( | |
| 181 'IntEnumPolicy', 'int-enum', '42', | |
| 182 ''' | |
| 183 'items': [ | |
| 184 { 'name': 'Foo', 'value': 100, 'caption': '' }, | |
| 185 { 'name': 'Bar', 'value': 42, 'caption': '' }, | |
| 186 ], | |
| 187 ''') | |
| 188 expected = { | |
| 189 'IntEnumPolicy': 42, | |
| 190 } | |
| 191 self._VerifyGeneratedOutput(templates, expected) | |
| 192 | |
| 193 def testStringList(self): | |
| 194 templates = self._MakeTemplate('StringListPolicy', 'list', '["a", "b"]') | |
| 195 expected = { | |
| 196 'StringListPolicy': [ "a", "b" ], | |
| 197 } | |
| 198 self._VerifyGeneratedOutput(templates, expected) | |
| 199 | |
| 200 def testStringEnumList(self): | |
| 201 templates = self._MakeTemplate('StringEnumListPolicy', | |
| 202 'string-enum-list', '["a", "b"]', | |
| 203 ''' | |
| 204 'items': [ | |
| 205 { 'name': 'Foo', 'value': 'a', 'caption': '' }, | |
| 206 { 'name': 'Bar', 'value': 'b', 'caption': '' }, | |
| 207 ], | |
| 208 ''') | |
| 209 | |
| 210 expected = { | |
| 211 'StringEnumListPolicy': [ "a", "b" ], | |
| 212 } | |
| 213 self._VerifyGeneratedOutput(templates, expected) | |
| 214 | |
| 215 def testListOfDictionary(self): | |
| 216 templates = self._MakeTemplate( | |
| 217 'ManagedBookmarks', 'dict', | |
| 218 ''' | |
| 219 [ | |
| 220 { | |
| 221 "name": "Google Search", | |
| 222 "url": "www.google.com", | |
| 223 }, | |
| 224 { | |
| 225 "name": "Youtube", | |
| 226 "url": "www.youtube.com", | |
| 227 } | |
| 228 ] | |
| 229 ''') | |
| 230 expected = { | |
| 231 'ManagedBookmarks': [ | |
| 232 { "name": "Google Search", "url": "www.google.com" }, | |
| 233 { "name": "Youtube", "url": "www.youtube.com" }, | |
| 234 ], | |
| 235 } | |
| 236 self._VerifyGeneratedOutput(templates, expected) | |
| 237 | |
| 238 | |
| 239 if __name__ == '__main__': | |
| 240 unittest.main() | |
| OLD | NEW |