OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 '''Unit tests for grit.format.policy_templates.writers.plist_writer''' |
| 6 |
| 7 |
| 8 import os |
| 9 import re |
| 10 import sys |
| 11 if __name__ == '__main__': |
| 12 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../..')) |
| 13 |
| 14 import tempfile |
| 15 import unittest |
| 16 import StringIO |
| 17 from xml.dom import minidom |
| 18 |
| 19 from grit.format import rc |
| 20 from grit.format.policy_templates.writers import writer_unittest_common |
| 21 from grit import grd_reader |
| 22 from grit import util |
| 23 from grit.tool import build |
| 24 |
| 25 |
| 26 class PListWriterUnittest(writer_unittest_common.WriterUnittestCommon): |
| 27 '''Unit tests for PListWriter.''' |
| 28 |
| 29 def _GetExpectedOutputs(self, product_name, bundle_id, policies): |
| 30 '''Substitutes the variable parts into a plist template. The result |
| 31 of this function can be used as an expected result to test the output |
| 32 of PListWriter. |
| 33 |
| 34 Args: |
| 35 product_name: The name of the product, normally Chromium or Google Chrome. |
| 36 bundle_id: The mac bundle id of the product. |
| 37 policies: The list of policies. |
| 38 |
| 39 Returns: |
| 40 The text of a plist template with the variable parts substituted. |
| 41 ''' |
| 42 return ''' |
| 43 <?xml version="1.0" ?> |
| 44 <!DOCTYPE plist PUBLIC '-//Apple//DTD PLIST 1.0//EN' 'http://www.apple.com/DTD
s/PropertyList-1.0.dtd'> |
| 45 <plist version="1"> |
| 46 <dict> |
| 47 <key>pfm_name</key> |
| 48 <string>%s</string> |
| 49 <key>pfm_description</key> |
| 50 <string/> |
| 51 <key>pfm_title</key> |
| 52 <string/> |
| 53 <key>pfm_version</key> |
| 54 <string>1</string> |
| 55 <key>pfm_domain</key> |
| 56 <string>%s</string> |
| 57 <key>pfm_subkeys</key> |
| 58 %s |
| 59 </dict> |
| 60 </plist>''' % (product_name, bundle_id, policies) |
| 61 |
| 62 def testEmpty(self): |
| 63 # Test PListWriter in case of empty polices. |
| 64 grd = self.PrepareTest(''' |
| 65 { |
| 66 'policy_definitions': [], |
| 67 'placeholders': [], |
| 68 'messages': {}, |
| 69 }''') |
| 70 |
| 71 output = self.GetOutput( |
| 72 grd, |
| 73 'fr', |
| 74 {'_chromium': '1', 'mac_bundle_id': 'com.example.Test'}, |
| 75 'plist', |
| 76 'en') |
| 77 expected_output = self._GetExpectedOutputs( |
| 78 'Chromium', 'com.example.Test', '<array/>') |
| 79 self.assertEquals(output.strip(), expected_output.strip()) |
| 80 |
| 81 def testMainPolicy(self): |
| 82 # Tests a policy group with a single policy of type 'main'. |
| 83 grd = self.PrepareTest(''' |
| 84 { |
| 85 'policy_definitions': [ |
| 86 { |
| 87 'name': 'MainGroup', |
| 88 'type': 'group', |
| 89 'policies': [{ |
| 90 'name': 'MainPolicy', |
| 91 'type': 'main', |
| 92 'desc': '', |
| 93 'caption': '', |
| 94 'supported_on': ['chrome.mac:8-'], |
| 95 }], |
| 96 'desc': '', |
| 97 'caption': '', |
| 98 }, |
| 99 ], |
| 100 'placeholders': [], |
| 101 'messages': {} |
| 102 }''') |
| 103 output = self.GetOutput( |
| 104 grd, |
| 105 'fr', |
| 106 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'}, |
| 107 'plist', |
| 108 'en') |
| 109 expected_output = self._GetExpectedOutputs( |
| 110 'Chromium', 'com.example.Test', '''<array> |
| 111 <dict> |
| 112 <key>pfm_name</key> |
| 113 <string>MainPolicy</string> |
| 114 <key>pfm_description</key> |
| 115 <string/> |
| 116 <key>pfm_title</key> |
| 117 <string/> |
| 118 <key>pfm_targets</key> |
| 119 <array> |
| 120 <string>user-managed</string> |
| 121 </array> |
| 122 <key>pfm_type</key> |
| 123 <string>boolean</string> |
| 124 </dict> |
| 125 </array>''') |
| 126 self.assertEquals(output.strip(), expected_output.strip()) |
| 127 |
| 128 def testStringPolicy(self): |
| 129 # Tests a policy group with a single policy of type 'string'. |
| 130 grd = self.PrepareTest(''' |
| 131 { |
| 132 'policy_definitions': [ |
| 133 { |
| 134 'name': 'StringGroup', |
| 135 'type': 'group', |
| 136 'desc': '', |
| 137 'caption': '', |
| 138 'policies': [{ |
| 139 'name': 'StringPolicy', |
| 140 'type': 'string', |
| 141 'supported_on': ['chrome.mac:8-'], |
| 142 'desc': '', |
| 143 'caption': '', |
| 144 }], |
| 145 }, |
| 146 ], |
| 147 'placeholders': [], |
| 148 'messages': {}, |
| 149 }''') |
| 150 output = self.GetOutput( |
| 151 grd, |
| 152 'fr', |
| 153 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'}, |
| 154 'plist', |
| 155 'en') |
| 156 expected_output = self._GetExpectedOutputs( |
| 157 'Chromium', 'com.example.Test', '''<array> |
| 158 <dict> |
| 159 <key>pfm_name</key> |
| 160 <string>StringPolicy</string> |
| 161 <key>pfm_description</key> |
| 162 <string/> |
| 163 <key>pfm_title</key> |
| 164 <string/> |
| 165 <key>pfm_targets</key> |
| 166 <array> |
| 167 <string>user-managed</string> |
| 168 </array> |
| 169 <key>pfm_type</key> |
| 170 <string>string</string> |
| 171 </dict> |
| 172 </array>''') |
| 173 self.assertEquals(output.strip(), expected_output.strip()) |
| 174 |
| 175 def testIntPolicy(self): |
| 176 # Tests a policy group with a single policy of type 'int'. |
| 177 grd = self.PrepareTest(''' |
| 178 { |
| 179 'policy_definitions': [ |
| 180 { |
| 181 'name': 'IntGroup', |
| 182 'type': 'group', |
| 183 'caption': '', |
| 184 'desc': '', |
| 185 'policies': [{ |
| 186 'name': 'IntPolicy', |
| 187 'type': 'int', |
| 188 'caption': '', |
| 189 'desc': '', |
| 190 'supported_on': ['chrome.mac:8-'], |
| 191 }], |
| 192 }, |
| 193 ], |
| 194 'placeholders': [], |
| 195 'messages': {}, |
| 196 }''') |
| 197 output = self.GetOutput( |
| 198 grd, |
| 199 'fr', |
| 200 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'}, |
| 201 'plist', |
| 202 'en') |
| 203 expected_output = self._GetExpectedOutputs( |
| 204 'Chromium', 'com.example.Test', '''<array> |
| 205 <dict> |
| 206 <key>pfm_name</key> |
| 207 <string>IntPolicy</string> |
| 208 <key>pfm_description</key> |
| 209 <string/> |
| 210 <key>pfm_title</key> |
| 211 <string/> |
| 212 <key>pfm_targets</key> |
| 213 <array> |
| 214 <string>user-managed</string> |
| 215 </array> |
| 216 <key>pfm_type</key> |
| 217 <string>integer</string> |
| 218 </dict> |
| 219 </array>''') |
| 220 self.assertEquals(output.strip(), expected_output.strip()) |
| 221 |
| 222 def testIntEnumPolicy(self): |
| 223 # Tests a policy group with a single policy of type 'int-enum'. |
| 224 grd = self.PrepareTest(''' |
| 225 { |
| 226 'policy_definitions': [ |
| 227 { |
| 228 'name': 'EnumGroup', |
| 229 'type': 'group', |
| 230 'caption': '', |
| 231 'desc': '', |
| 232 'policies': [{ |
| 233 'name': 'EnumPolicy', |
| 234 'type': 'int-enum', |
| 235 'desc': '', |
| 236 'caption': '', |
| 237 'items': [ |
| 238 {'name': 'ProxyServerDisabled', 'value': 0, 'caption': ''}, |
| 239 {'name': 'ProxyServerAutoDetect', 'value': 1, 'caption': ''}, |
| 240 ], |
| 241 'supported_on': ['chrome.mac:8-'], |
| 242 }], |
| 243 }, |
| 244 ], |
| 245 'placeholders': [], |
| 246 'messages': {}, |
| 247 }''') |
| 248 output = self.GetOutput( |
| 249 grd, |
| 250 'fr', |
| 251 {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'}, |
| 252 'plist', |
| 253 'en') |
| 254 expected_output = self._GetExpectedOutputs( |
| 255 'Google_Chrome', 'com.example.Test2', '''<array> |
| 256 <dict> |
| 257 <key>pfm_name</key> |
| 258 <string>EnumPolicy</string> |
| 259 <key>pfm_description</key> |
| 260 <string/> |
| 261 <key>pfm_title</key> |
| 262 <string/> |
| 263 <key>pfm_targets</key> |
| 264 <array> |
| 265 <string>user-managed</string> |
| 266 </array> |
| 267 <key>pfm_type</key> |
| 268 <string>integer</string> |
| 269 <key>pfm_range_list</key> |
| 270 <array> |
| 271 <integer>0</integer> |
| 272 <integer>1</integer> |
| 273 </array> |
| 274 </dict> |
| 275 </array>''') |
| 276 self.assertEquals(output.strip(), expected_output.strip()) |
| 277 |
| 278 def testStringEnumPolicy(self): |
| 279 # Tests a policy group with a single policy of type 'string-enum'. |
| 280 grd = self.PrepareTest(''' |
| 281 { |
| 282 'policy_definitions': [ |
| 283 { |
| 284 'name': 'EnumGroup', |
| 285 'type': 'group', |
| 286 'caption': '', |
| 287 'desc': '', |
| 288 'policies': [{ |
| 289 'name': 'EnumPolicy', |
| 290 'type': 'string-enum', |
| 291 'desc': '', |
| 292 'caption': '', |
| 293 'items': [ |
| 294 {'name': 'ProxyServerDisabled', 'value': 'one', 'caption': ''}, |
| 295 {'name': 'ProxyServerAutoDetect', 'value': 'two', 'caption': ''}
, |
| 296 ], |
| 297 'supported_on': ['chrome.mac:8-'], |
| 298 }], |
| 299 }, |
| 300 ], |
| 301 'placeholders': [], |
| 302 'messages': {}, |
| 303 }''') |
| 304 output = self.GetOutput( |
| 305 grd, |
| 306 'fr', |
| 307 {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'}, |
| 308 'plist', |
| 309 'en') |
| 310 expected_output = self._GetExpectedOutputs( |
| 311 'Google_Chrome', 'com.example.Test2', '''<array> |
| 312 <dict> |
| 313 <key>pfm_name</key> |
| 314 <string>EnumPolicy</string> |
| 315 <key>pfm_description</key> |
| 316 <string/> |
| 317 <key>pfm_title</key> |
| 318 <string/> |
| 319 <key>pfm_targets</key> |
| 320 <array> |
| 321 <string>user-managed</string> |
| 322 </array> |
| 323 <key>pfm_type</key> |
| 324 <string>string</string> |
| 325 <key>pfm_range_list</key> |
| 326 <array> |
| 327 <string>one</string> |
| 328 <string>two</string> |
| 329 </array> |
| 330 </dict> |
| 331 </array>''') |
| 332 self.assertEquals(output.strip(), expected_output.strip()) |
| 333 |
| 334 def testNonSupportedPolicy(self): |
| 335 # Tests a policy that is not supported on Mac, so it shouldn't |
| 336 # be included in the plist file. |
| 337 grd = self.PrepareTest(''' |
| 338 { |
| 339 'policy_definitions': [ |
| 340 { |
| 341 'name': 'NonMacGroup', |
| 342 'type': 'group', |
| 343 'caption': '', |
| 344 'desc': '', |
| 345 'policies': [{ |
| 346 'name': 'NonMacPolicy', |
| 347 'type': 'string', |
| 348 'supported_on': ['chrome.linux:8-', 'chrome.win:7-'], |
| 349 'caption': '', |
| 350 'desc': '', |
| 351 }], |
| 352 }, |
| 353 ], |
| 354 'placeholders': [], |
| 355 'messages': {}, |
| 356 }''') |
| 357 output = self.GetOutput( |
| 358 grd, |
| 359 'fr', |
| 360 {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'}, |
| 361 'plist', |
| 362 'en') |
| 363 expected_output = self._GetExpectedOutputs( |
| 364 'Google_Chrome', 'com.example.Test2', '''<array/>''') |
| 365 self.assertEquals(output.strip(), expected_output.strip()) |
| 366 |
| 367 |
| 368 if __name__ == '__main__': |
| 369 unittest.main() |
OLD | NEW |