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 testRecommendedPolicy(self): 122 # Tests a policy group with a single policy of type 'main'. 123 grd = self.PrepareTest(''' 124 { 125 'policy_definitions': [ 126 { 127 'name': 'MainGroup', 128 'type': 'group', 129 'policies': [{ 130 'name': 'MainPolicy', 131 'type': 'main', 132 'desc': '', 133 'caption': '', 134 'features': { 135 'can_be_recommended' : True 136 }, 137 'supported_on': ['chrome.mac:8-'], 138 }], 139 'desc': '', 140 'caption': '', 141 }, 142 ], 143 'placeholders': [], 144 'messages': {} 145 }''') 146 output = self.GetOutput( 147 grd, 148 'fr', 149 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'}, 150 'plist', 151 'en') 152 expected_output = self._GetExpectedOutputs( 153 'Chromium', 'com.example.Test', '''<array> 154 <dict> 155 <key>pfm_name</key> 156 <string>MainPolicy</string> 157 <key>pfm_description</key> 158 <string/> 159 <key>pfm_title</key> 160 <string/> 161 <key>pfm_targets</key> 162 <array> 163 <string>user</string> 164 <string>user-managed</string> 165 </array> 166 <key>pfm_type</key> 167 <string>boolean</string> 168 </dict> 169 </array>''') 170 self.assertEquals(output.strip(), expected_output.strip()) 171 172 def testRecommendedOnlyPolicy(self): 173 # Tests a policy group with a single policy of type 'main'. 174 grd = self.PrepareTest(''' 175 { 176 'policy_definitions': [ 177 { 178 'name': 'MainGroup', 179 'type': 'group', 180 'policies': [{ 181 'name': 'MainPolicy', 182 'type': 'main', 183 'desc': '', 184 'caption': '', 185 'features': { 186 'can_be_recommended' : True, 187 'can_be_mandatory' : False 188 }, 189 'supported_on': ['chrome.mac:8-'], 190 }], 191 'desc': '', 192 'caption': '', 193 }, 194 ], 195 'placeholders': [], 196 'messages': {} 197 }''') 198 output = self.GetOutput( 199 grd, 200 'fr', 201 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'}, 202 'plist', 203 'en') 204 expected_output = self._GetExpectedOutputs( 205 'Chromium', 'com.example.Test', '''<array> 206 <dict> 207 <key>pfm_name</key> 208 <string>MainPolicy</string> 209 <key>pfm_description</key> 210 <string/> 211 <key>pfm_title</key> 212 <string/> 213 <key>pfm_targets</key> 214 <array> 215 <string>user</string> 216 </array> 217 <key>pfm_type</key> 218 <string>boolean</string> 219 </dict> 220 </array>''') 221 self.assertEquals(output.strip(), expected_output.strip()) 222 223 def testStringPolicy(self): 224 # Tests a policy group with a single policy of type 'string'. 225 grd = self.PrepareTest(''' 226 { 227 'policy_definitions': [ 228 { 229 'name': 'StringGroup', 230 'type': 'group', 231 'desc': '', 232 'caption': '', 233 'policies': [{ 234 'name': 'StringPolicy', 235 'type': 'string', 236 'supported_on': ['chrome.mac:8-'], 237 'desc': '', 238 'caption': '', 239 }], 240 }, 241 ], 242 'placeholders': [], 243 'messages': {}, 244 }''') 245 output = self.GetOutput( 246 grd, 247 'fr', 248 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'}, 249 'plist', 250 'en') 251 expected_output = self._GetExpectedOutputs( 252 'Chromium', 'com.example.Test', '''<array> 253 <dict> 254 <key>pfm_name</key> 255 <string>StringPolicy</string> 256 <key>pfm_description</key> 257 <string/> 258 <key>pfm_title</key> 259 <string/> 260 <key>pfm_targets</key> 261 <array> 262 <string>user-managed</string> 263 </array> 264 <key>pfm_type</key> 265 <string>string</string> 266 </dict> 267 </array>''') 268 self.assertEquals(output.strip(), expected_output.strip()) 269 270 def testListPolicy(self): 271 # Tests a policy group with a single policy of type 'list'. 272 grd = self.PrepareTest(''' 273 { 274 'policy_definitions': [ 275 { 276 'name': 'ListGroup', 277 'type': 'group', 278 'desc': '', 279 'caption': '', 280 'policies': [{ 281 'name': 'ListPolicy', 282 'type': 'list', 283 'schema': { 284 'type': 'array', 285 'items': { 'type': 'string' }, 286 }, 287 'supported_on': ['chrome.mac:8-'], 288 'desc': '', 289 'caption': '', 290 }], 291 }, 292 ], 293 'placeholders': [], 294 'messages': {}, 295 }''') 296 output = self.GetOutput( 297 grd, 298 'fr', 299 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'}, 300 'plist', 301 'en') 302 expected_output = self._GetExpectedOutputs( 303 'Chromium', 'com.example.Test', '''<array> 304 <dict> 305 <key>pfm_name</key> 306 <string>ListPolicy</string> 307 <key>pfm_description</key> 308 <string/> 309 <key>pfm_title</key> 310 <string/> 311 <key>pfm_targets</key> 312 <array> 313 <string>user-managed</string> 314 </array> 315 <key>pfm_type</key> 316 <string>array</string> 317 <key>pfm_subkeys</key> 318 <array> 319 <dict> 320 <key>pfm_type</key> 321 <string>string</string> 322 </dict> 323 </array> 324 </dict> 325 </array>''') 326 self.assertEquals(output.strip(), expected_output.strip()) 327 328 def testStringEnumListPolicy(self): 329 # Tests a policy group with a single policy of type 'string-enum-list'. 330 grd = self.PrepareTest(''' 331 { 332 'policy_definitions': [ 333 { 334 'name': 'ListGroup', 335 'type': 'group', 336 'desc': '', 337 'caption': '', 338 'policies': [{ 339 'name': 'ListPolicy', 340 'type': 'string-enum-list', 341 'schema': { 342 'type': 'array', 343 'items': { 'type': 'string' }, 344 }, 345 'items': [ 346 {'name': 'ProxyServerDisabled', 'value': 'one', 'caption': ''}, 347 {'name': 'ProxyServerAutoDetect', 'value': 'two', 'caption': ''}, 348 ], 349 'supported_on': ['chrome.mac:8-'], 350 'supported_on': ['chrome.mac:8-'], 351 'desc': '', 352 'caption': '', 353 }], 354 }, 355 ], 356 'placeholders': [], 357 'messages': {}, 358 }''') 359 output = self.GetOutput( 360 grd, 361 'fr', 362 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'}, 363 'plist', 364 'en') 365 expected_output = self._GetExpectedOutputs( 366 'Chromium', 'com.example.Test', '''<array> 367 <dict> 368 <key>pfm_name</key> 369 <string>ListPolicy</string> 370 <key>pfm_description</key> 371 <string/> 372 <key>pfm_title</key> 373 <string/> 374 <key>pfm_targets</key> 375 <array> 376 <string>user-managed</string> 377 </array> 378 <key>pfm_type</key> 379 <string>array</string> 380 <key>pfm_subkeys</key> 381 <array> 382 <dict> 383 <key>pfm_type</key> 384 <string>string</string> 385 </dict> 386 </array> 387 </dict> 388 </array>''') 389 self.assertEquals(output.strip(), expected_output.strip()) 390 391 def testIntPolicy(self): 392 # Tests a policy group with a single policy of type 'int'. 393 grd = self.PrepareTest(''' 394 { 395 'policy_definitions': [ 396 { 397 'name': 'IntGroup', 398 'type': 'group', 399 'caption': '', 400 'desc': '', 401 'policies': [{ 402 'name': 'IntPolicy', 403 'type': 'int', 404 'caption': '', 405 'desc': '', 406 'supported_on': ['chrome.mac:8-'], 407 }], 408 }, 409 ], 410 'placeholders': [], 411 'messages': {}, 412 }''') 413 output = self.GetOutput( 414 grd, 415 'fr', 416 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'}, 417 'plist', 418 'en') 419 expected_output = self._GetExpectedOutputs( 420 'Chromium', 'com.example.Test', '''<array> 421 <dict> 422 <key>pfm_name</key> 423 <string>IntPolicy</string> 424 <key>pfm_description</key> 425 <string/> 426 <key>pfm_title</key> 427 <string/> 428 <key>pfm_targets</key> 429 <array> 430 <string>user-managed</string> 431 </array> 432 <key>pfm_type</key> 433 <string>integer</string> 434 </dict> 435 </array>''') 436 self.assertEquals(output.strip(), expected_output.strip()) 437 438 def testIntEnumPolicy(self): 439 # Tests a policy group with a single policy of type 'int-enum'. 440 grd = self.PrepareTest(''' 441 { 442 'policy_definitions': [ 443 { 444 'name': 'EnumGroup', 445 'type': 'group', 446 'caption': '', 447 'desc': '', 448 'policies': [{ 449 'name': 'EnumPolicy', 450 'type': 'int-enum', 451 'desc': '', 452 'caption': '', 453 'items': [ 454 {'name': 'ProxyServerDisabled', 'value': 0, 'caption': ''}, 455 {'name': 'ProxyServerAutoDetect', 'value': 1, 'caption': ''}, 456 ], 457 'supported_on': ['chrome.mac:8-'], 458 }], 459 }, 460 ], 461 'placeholders': [], 462 'messages': {}, 463 }''') 464 output = self.GetOutput( 465 grd, 466 'fr', 467 {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'}, 468 'plist', 469 'en') 470 expected_output = self._GetExpectedOutputs( 471 'Google_Chrome', 'com.example.Test2', '''<array> 472 <dict> 473 <key>pfm_name</key> 474 <string>EnumPolicy</string> 475 <key>pfm_description</key> 476 <string/> 477 <key>pfm_title</key> 478 <string/> 479 <key>pfm_targets</key> 480 <array> 481 <string>user-managed</string> 482 </array> 483 <key>pfm_type</key> 484 <string>integer</string> 485 <key>pfm_range_list</key> 486 <array> 487 <integer>0</integer> 488 <integer>1</integer> 489 </array> 490 </dict> 491 </array>''') 492 self.assertEquals(output.strip(), expected_output.strip()) 493 494 def testStringEnumPolicy(self): 495 # Tests a policy group with a single policy of type 'string-enum'. 496 grd = self.PrepareTest(''' 497 { 498 'policy_definitions': [ 499 { 500 'name': 'EnumGroup', 501 'type': 'group', 502 'caption': '', 503 'desc': '', 504 'policies': [{ 505 'name': 'EnumPolicy', 506 'type': 'string-enum', 507 'desc': '', 508 'caption': '', 509 'items': [ 510 {'name': 'ProxyServerDisabled', 'value': 'one', 'caption': ''}, 511 {'name': 'ProxyServerAutoDetect', 'value': 'two', 'caption': ''}, 512 ], 513 'supported_on': ['chrome.mac:8-'], 514 }], 515 }, 516 ], 517 'placeholders': [], 518 'messages': {}, 519 }''') 520 output = self.GetOutput( 521 grd, 522 'fr', 523 {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'}, 524 'plist', 525 'en') 526 expected_output = self._GetExpectedOutputs( 527 'Google_Chrome', 'com.example.Test2', '''<array> 528 <dict> 529 <key>pfm_name</key> 530 <string>EnumPolicy</string> 531 <key>pfm_description</key> 532 <string/> 533 <key>pfm_title</key> 534 <string/> 535 <key>pfm_targets</key> 536 <array> 537 <string>user-managed</string> 538 </array> 539 <key>pfm_type</key> 540 <string>string</string> 541 <key>pfm_range_list</key> 542 <array> 543 <string>one</string> 544 <string>two</string> 545 </array> 546 </dict> 547 </array>''') 548 self.assertEquals(output.strip(), expected_output.strip()) 549 550 def testDictionaryPolicy(self): 551 # Tests a policy group with a single policy of type 'dict'. 552 grd = self.PrepareTest(''' 553 { 554 'policy_definitions': [ 555 { 556 'name': 'DictionaryGroup', 557 'type': 'group', 558 'desc': '', 559 'caption': '', 560 'policies': [{ 561 'name': 'DictionaryPolicy', 562 'type': 'dict', 563 'supported_on': ['chrome.mac:8-'], 564 'desc': '', 565 'caption': '', 566 }], 567 }, 568 ], 569 'placeholders': [], 570 'messages': {}, 571 }''') 572 output = self.GetOutput( 573 grd, 574 'fr', 575 {'_chromium' : '1', 'mac_bundle_id': 'com.example.Test'}, 576 'plist', 577 'en') 578 expected_output = self._GetExpectedOutputs( 579 'Chromium', 'com.example.Test', '''<array> 580 <dict> 581 <key>pfm_name</key> 582 <string>DictionaryPolicy</string> 583 <key>pfm_description</key> 584 <string/> 585 <key>pfm_title</key> 586 <string/> 587 <key>pfm_targets</key> 588 <array> 589 <string>user-managed</string> 590 </array> 591 <key>pfm_type</key> 592 <string>dictionary</string> 593 </dict> 594 </array>''') 595 self.assertEquals(output.strip(), expected_output.strip()) 596 597 def testNonSupportedPolicy(self): 598 # Tests a policy that is not supported on Mac, so it shouldn't 599 # be included in the plist file. 600 grd = self.PrepareTest(''' 601 { 602 'policy_definitions': [ 603 { 604 'name': 'NonMacGroup', 605 'type': 'group', 606 'caption': '', 607 'desc': '', 608 'policies': [{ 609 'name': 'NonMacPolicy', 610 'type': 'string', 611 'supported_on': ['chrome.linux:8-', 'chrome.win:7-'], 612 'caption': '', 613 'desc': '', 614 }], 615 }, 616 ], 617 'placeholders': [], 618 'messages': {}, 619 }''') 620 output = self.GetOutput( 621 grd, 622 'fr', 623 {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'}, 624 'plist', 625 'en') 626 expected_output = self._GetExpectedOutputs( 627 'Google_Chrome', 'com.example.Test2', '''<array/>''') 628 self.assertEquals(output.strip(), expected_output.strip()) 629 630 631 if __name__ == '__main__': 632 unittest.main() 633