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.
6 '''Unit tests for grit.format.policy_templates.writers.adm_writer'''
11 if __name__ == '__main__':
12 sys.path.append(os.path.join(os.path.dirname(__file__), '../../../..'))
16 from grit.format.policy_templates.writers import writer_unittest_common
19 class AdmWriterUnittest(writer_unittest_common.WriterUnittestCommon):
20 '''Unit tests for AdmWriter.'''
22 def ConstructOutput(self, classes, body, strings):
25 result.append('CLASS ' + clazz)
27 result.append(strings)
28 return ''.join(result)
30 def CompareOutputs(self, output, expected_output):
31 '''Compares the output of the adm_writer with its expected output.
34 output: The output of the adm writer as returned by grit.
35 expected_output: The expected output.
38 AssertionError: if the two strings are not equivalent.
42 expected_output.strip().replace('\n', '\r\n'))
45 # Test PListWriter in case of empty polices.
46 grd = self.PrepareTest('''
48 'policy_definitions': [],
51 'win_supported_winxpsp2': {
52 'text': 'At least "Windows 3.11', 'desc': 'blah'
55 'text': 'Recommended', 'desc': 'bleh'
59 output = self.GetOutput(grd, 'fr', {'_chromium': '1',}, 'adm', 'en')
60 expected_output = self.ConstructOutput(
61 ['MACHINE', 'USER'], '''
63 KEYNAME "Software\\Policies\\Chromium"
67 CATEGORY !!chromium_recommended
68 KEYNAME "Software\\Policies\\Chromium\\Recommended"
74 SUPPORTED_WINXPSP2="At least "Windows 3.11"
76 chromium_recommended="Chromium - Recommended"''')
77 self.CompareOutputs(output, expected_output)
79 def testMainPolicy(self):
80 # Tests a policy group with a single policy of type 'main'.
81 grd = self.PrepareTest('''
83 'policy_definitions': [
87 'supported_on': ['chrome.win:8-'],
88 'features': { 'can_be_recommended': True },
89 'caption': 'Caption of main.',
90 'desc': 'Description of main.',
95 'win_supported_winxpsp2': {
96 'text': 'At least Windows 3.12', 'desc': 'blah'
99 'text': 'Recommended', 'desc': 'bleh'
103 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'adm', 'en')
104 expected_output = self.ConstructOutput(
105 ['MACHINE', 'USER'], '''
107 CATEGORY !!googlechrome
108 KEYNAME "Software\\Policies\\Google\\Chrome"
110 POLICY !!MainPolicy_Policy
112 SUPPORTED !!SUPPORTED_WINXPSP2
114 EXPLAIN !!MainPolicy_Explain
115 VALUENAME "MainPolicy"
124 CATEGORY !!googlechrome_recommended
125 KEYNAME "Software\\Policies\\Google\\Chrome\\Recommended"
127 POLICY !!MainPolicy_Policy
129 SUPPORTED !!SUPPORTED_WINXPSP2
131 EXPLAIN !!MainPolicy_Explain
132 VALUENAME "MainPolicy"
142 SUPPORTED_WINXPSP2="At least Windows 3.12"
144 googlechrome="Google Chrome"
145 googlechrome_recommended="Google Chrome - Recommended"
146 MainPolicy_Policy="Caption of main."
147 MainPolicy_Explain="Description of main."''')
148 self.CompareOutputs(output, expected_output)
150 def testMainPolicyRecommendedOnly(self):
151 # Tests a policy group with a single policy of type 'main'.
152 grd = self.PrepareTest('''
154 'policy_definitions': [
156 'name': 'MainPolicy',
158 'supported_on': ['chrome.win:8-'],
160 'can_be_recommended': True,
161 'can_be_mandatory': False
163 'caption': 'Caption of main.',
164 'desc': 'Description of main.',
169 'win_supported_winxpsp2': {
170 'text': 'At least Windows 3.12', 'desc': 'blah'
173 'text': 'Recommended', 'desc': 'bleh'
177 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'adm', 'en')
178 expected_output = self.ConstructOutput(
179 ['MACHINE', 'USER'], '''
181 CATEGORY !!googlechrome
182 KEYNAME "Software\\Policies\\Google\\Chrome"
188 CATEGORY !!googlechrome_recommended
189 KEYNAME "Software\\Policies\\Google\\Chrome\\Recommended"
191 POLICY !!MainPolicy_Policy
193 SUPPORTED !!SUPPORTED_WINXPSP2
195 EXPLAIN !!MainPolicy_Explain
196 VALUENAME "MainPolicy"
206 SUPPORTED_WINXPSP2="At least Windows 3.12"
208 googlechrome="Google Chrome"
209 googlechrome_recommended="Google Chrome - Recommended"
210 MainPolicy_Policy="Caption of main."
211 MainPolicy_Explain="Description of main."''')
212 self.CompareOutputs(output, expected_output)
214 def testStringPolicy(self):
215 # Tests a policy group with a single policy of type 'string'.
216 grd = self.PrepareTest('''
218 'policy_definitions': [
220 'name': 'StringPolicy',
222 'supported_on': ['chrome.win:8-'],
223 'features': { 'can_be_recommended': True },
224 'desc': """Description of group.
226 'caption': 'Caption of policy.',
231 'win_supported_winxpsp2': {
232 'text': 'At least Windows 3.13', 'desc': 'blah'
235 'text': 'Recommended', 'desc': 'bleh'
239 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'adm', 'en')
240 expected_output = self.ConstructOutput(
241 ['MACHINE', 'USER'], '''
243 KEYNAME "Software\\Policies\\Chromium"
245 POLICY !!StringPolicy_Policy
247 SUPPORTED !!SUPPORTED_WINXPSP2
249 EXPLAIN !!StringPolicy_Explain
251 PART !!StringPolicy_Part EDITTEXT
252 VALUENAME "StringPolicy"
258 CATEGORY !!chromium_recommended
259 KEYNAME "Software\\Policies\\Chromium\\Recommended"
261 POLICY !!StringPolicy_Policy
263 SUPPORTED !!SUPPORTED_WINXPSP2
265 EXPLAIN !!StringPolicy_Explain
267 PART !!StringPolicy_Part EDITTEXT
268 VALUENAME "StringPolicy"
276 SUPPORTED_WINXPSP2="At least Windows 3.13"
278 chromium_recommended="Chromium - Recommended"
279 StringPolicy_Policy="Caption of policy."
280 StringPolicy_Explain="Description of group.\\nWith a newline."
281 StringPolicy_Part="Caption of policy."
283 self.CompareOutputs(output, expected_output)
285 def testIntPolicy(self):
286 # Tests a policy group with a single policy of type 'string'.
287 grd = self.PrepareTest('''
289 'policy_definitions': [
293 'caption': 'Caption of policy.',
294 'features': { 'can_be_recommended': True },
295 'desc': 'Description of policy.',
296 'supported_on': ['chrome.win:8-']
301 'win_supported_winxpsp2': {
302 'text': 'At least Windows 3.13', 'desc': 'blah'
305 'text': 'Recommended', 'desc': 'bleh'
309 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'adm', 'en')
310 expected_output = self.ConstructOutput(
311 ['MACHINE', 'USER'], '''
313 KEYNAME "Software\\Policies\\Chromium"
315 POLICY !!IntPolicy_Policy
317 SUPPORTED !!SUPPORTED_WINXPSP2
319 EXPLAIN !!IntPolicy_Explain
321 PART !!IntPolicy_Part NUMERIC
322 VALUENAME "IntPolicy"
329 CATEGORY !!chromium_recommended
330 KEYNAME "Software\\Policies\\Chromium\\Recommended"
332 POLICY !!IntPolicy_Policy
334 SUPPORTED !!SUPPORTED_WINXPSP2
336 EXPLAIN !!IntPolicy_Explain
338 PART !!IntPolicy_Part NUMERIC
339 VALUENAME "IntPolicy"
348 SUPPORTED_WINXPSP2="At least Windows 3.13"
350 chromium_recommended="Chromium - Recommended"
351 IntPolicy_Policy="Caption of policy."
352 IntPolicy_Explain="Description of policy."
353 IntPolicy_Part="Caption of policy."
355 self.CompareOutputs(output, expected_output)
357 def testIntEnumPolicy(self):
358 # Tests a policy group with a single policy of type 'int-enum'.
359 grd = self.PrepareTest('''
361 'policy_definitions': [
363 'name': 'EnumPolicy',
367 'name': 'ProxyServerDisabled',
369 'caption': 'Option1',
372 'name': 'ProxyServerAutoDetect',
374 'caption': 'Option2',
377 'desc': 'Description of policy.',
378 'caption': 'Caption of policy.',
379 'supported_on': ['chrome.win:8-'],
380 'features': { 'can_be_recommended': True },
385 'win_supported_winxpsp2': {
386 'text': 'At least Windows 3.14', 'desc': 'blah'
389 'text': 'Recommended', 'desc': 'bleh'
393 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'adm', 'en')
394 expected_output = self.ConstructOutput(
395 ['MACHINE', 'USER'], '''
397 CATEGORY !!googlechrome
398 KEYNAME "Software\\Policies\\Google\\Chrome"
400 POLICY !!EnumPolicy_Policy
402 SUPPORTED !!SUPPORTED_WINXPSP2
404 EXPLAIN !!EnumPolicy_Explain
406 PART !!EnumPolicy_Part DROPDOWNLIST
407 VALUENAME "EnumPolicy"
409 NAME !!ProxyServerDisabled_DropDown VALUE NUMERIC 0
410 NAME !!ProxyServerAutoDetect_DropDown VALUE NUMERIC 1
419 CATEGORY !!googlechrome_recommended
420 KEYNAME "Software\\Policies\\Google\\Chrome\\Recommended"
422 POLICY !!EnumPolicy_Policy
424 SUPPORTED !!SUPPORTED_WINXPSP2
426 EXPLAIN !!EnumPolicy_Explain
428 PART !!EnumPolicy_Part DROPDOWNLIST
429 VALUENAME "EnumPolicy"
431 NAME !!ProxyServerDisabled_DropDown VALUE NUMERIC 0
432 NAME !!ProxyServerAutoDetect_DropDown VALUE NUMERIC 1
442 SUPPORTED_WINXPSP2="At least Windows 3.14"
444 googlechrome="Google Chrome"
445 googlechrome_recommended="Google Chrome - Recommended"
446 EnumPolicy_Policy="Caption of policy."
447 EnumPolicy_Explain="Description of policy."
448 EnumPolicy_Part="Caption of policy."
449 ProxyServerDisabled_DropDown="Option1"
450 ProxyServerAutoDetect_DropDown="Option2"
452 self.CompareOutputs(output, expected_output)
454 def testStringEnumPolicy(self):
455 # Tests a policy group with a single policy of type 'int-enum'.
456 grd = self.PrepareTest('''
458 'policy_definitions': [
460 'name': 'EnumPolicy',
461 'type': 'string-enum',
462 'caption': 'Caption of policy.',
463 'desc': 'Description of policy.',
465 {'name': 'ProxyServerDisabled', 'value': 'one',
466 'caption': 'Option1'},
467 {'name': 'ProxyServerAutoDetect', 'value': 'two',
468 'caption': 'Option2'},
470 'supported_on': ['chrome.win:8-'],
471 'features': { 'can_be_recommended': True },
476 'win_supported_winxpsp2': {
477 'text': 'At least Windows 3.14', 'desc': 'blah'
480 'text': 'Recommended', 'desc': 'bleh'
484 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'adm', 'en')
485 expected_output = self.ConstructOutput(
486 ['MACHINE', 'USER'], '''
488 CATEGORY !!googlechrome
489 KEYNAME "Software\\Policies\\Google\\Chrome"
491 POLICY !!EnumPolicy_Policy
493 SUPPORTED !!SUPPORTED_WINXPSP2
495 EXPLAIN !!EnumPolicy_Explain
497 PART !!EnumPolicy_Part DROPDOWNLIST
498 VALUENAME "EnumPolicy"
500 NAME !!ProxyServerDisabled_DropDown VALUE "one"
501 NAME !!ProxyServerAutoDetect_DropDown VALUE "two"
510 CATEGORY !!googlechrome_recommended
511 KEYNAME "Software\\Policies\\Google\\Chrome\\Recommended"
513 POLICY !!EnumPolicy_Policy
515 SUPPORTED !!SUPPORTED_WINXPSP2
517 EXPLAIN !!EnumPolicy_Explain
519 PART !!EnumPolicy_Part DROPDOWNLIST
520 VALUENAME "EnumPolicy"
522 NAME !!ProxyServerDisabled_DropDown VALUE "one"
523 NAME !!ProxyServerAutoDetect_DropDown VALUE "two"
533 SUPPORTED_WINXPSP2="At least Windows 3.14"
535 googlechrome="Google Chrome"
536 googlechrome_recommended="Google Chrome - Recommended"
537 EnumPolicy_Policy="Caption of policy."
538 EnumPolicy_Explain="Description of policy."
539 EnumPolicy_Part="Caption of policy."
540 ProxyServerDisabled_DropDown="Option1"
541 ProxyServerAutoDetect_DropDown="Option2"
543 self.CompareOutputs(output, expected_output)
545 def testListPolicy(self):
546 # Tests a policy group with a single policy of type 'list'.
547 grd = self.PrepareTest('''
549 'policy_definitions': [
551 'name': 'ListPolicy',
553 'supported_on': ['chrome.win:8-'],
554 'features': { 'can_be_recommended': True },
555 'desc': """Description of list policy.
557 'caption': 'Caption of list policy.',
558 'label': 'Label of list policy.'
563 'win_supported_winxpsp2': {
564 'text': 'At least Windows 3.15', 'desc': 'blah'
567 'text': 'Recommended', 'desc': 'bleh'
571 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'adm', 'en')
572 expected_output = self.ConstructOutput(
573 ['MACHINE', 'USER'], '''
575 KEYNAME "Software\\Policies\\Chromium"
577 POLICY !!ListPolicy_Policy
579 SUPPORTED !!SUPPORTED_WINXPSP2
581 EXPLAIN !!ListPolicy_Explain
583 PART !!ListPolicy_Part LISTBOX
584 KEYNAME "Software\\Policies\\Chromium\\ListPolicy"
591 CATEGORY !!chromium_recommended
592 KEYNAME "Software\\Policies\\Chromium\\Recommended"
594 POLICY !!ListPolicy_Policy
596 SUPPORTED !!SUPPORTED_WINXPSP2
598 EXPLAIN !!ListPolicy_Explain
600 PART !!ListPolicy_Part LISTBOX
601 KEYNAME "Software\\Policies\\Chromium\\Recommended\\ListPolicy"
610 SUPPORTED_WINXPSP2="At least Windows 3.15"
612 chromium_recommended="Chromium - Recommended"
613 ListPolicy_Policy="Caption of list policy."
614 ListPolicy_Explain="Description of list policy.\\nWith a newline."
615 ListPolicy_Part="Label of list policy."
617 self.CompareOutputs(output, expected_output)
619 def testStringEnumListPolicy(self):
620 # Tests a policy group with a single policy of type 'string-enum-list'.
621 grd = self.PrepareTest('''
623 'policy_definitions': [
625 'name': 'ListPolicy',
626 'type': 'string-enum-list',
627 'supported_on': ['chrome.win:8-'],
628 'features': { 'can_be_recommended': True },
629 'desc': """Description of list policy.
632 {'name': 'ProxyServerDisabled', 'value': 'one',
633 'caption': 'Option1'},
634 {'name': 'ProxyServerAutoDetect', 'value': 'two',
635 'caption': 'Option2'},
637 'caption': 'Caption of list policy.',
638 'label': 'Label of list policy.'
643 'win_supported_winxpsp2': {
644 'text': 'At least Windows 3.15', 'desc': 'blah'
647 'text': 'Recommended', 'desc': 'bleh'
651 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'adm', 'en')
652 expected_output = self.ConstructOutput(
653 ['MACHINE', 'USER'], '''
655 KEYNAME "Software\\Policies\\Chromium"
657 POLICY !!ListPolicy_Policy
659 SUPPORTED !!SUPPORTED_WINXPSP2
661 EXPLAIN !!ListPolicy_Explain
663 PART !!ListPolicy_Part LISTBOX
664 KEYNAME "Software\\Policies\\Chromium\\ListPolicy"
671 CATEGORY !!chromium_recommended
672 KEYNAME "Software\\Policies\\Chromium\\Recommended"
674 POLICY !!ListPolicy_Policy
676 SUPPORTED !!SUPPORTED_WINXPSP2
678 EXPLAIN !!ListPolicy_Explain
680 PART !!ListPolicy_Part LISTBOX
681 KEYNAME "Software\\Policies\\Chromium\\Recommended\\ListPolicy"
690 SUPPORTED_WINXPSP2="At least Windows 3.15"
692 chromium_recommended="Chromium - Recommended"
693 ListPolicy_Policy="Caption of list policy."
694 ListPolicy_Explain="Description of list policy.\\nWith a newline."
695 ListPolicy_Part="Label of list policy."
697 self.CompareOutputs(output, expected_output)
699 def testDictionaryPolicy(self):
700 # Tests a policy group with a single policy of type 'dict'.
701 grd = self.PrepareTest('''
703 'policy_definitions': [
705 'name': 'DictionaryPolicy',
707 'supported_on': ['chrome.win:8-'],
708 'features': { 'can_be_recommended': True },
709 'desc': 'Description of group.',
710 'caption': 'Caption of policy.',
715 'win_supported_winxpsp2': {
716 'text': 'At least Windows 3.13', 'desc': 'blah'
719 'text': 'Recommended', 'desc': 'bleh'
723 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'adm', 'en')
724 expected_output = self.ConstructOutput(
725 ['MACHINE', 'USER'], '''
727 KEYNAME "Software\\Policies\\Chromium"
729 POLICY !!DictionaryPolicy_Policy
731 SUPPORTED !!SUPPORTED_WINXPSP2
733 EXPLAIN !!DictionaryPolicy_Explain
735 PART !!DictionaryPolicy_Part EDITTEXT
736 VALUENAME "DictionaryPolicy"
742 CATEGORY !!chromium_recommended
743 KEYNAME "Software\\Policies\\Chromium\\Recommended"
745 POLICY !!DictionaryPolicy_Policy
747 SUPPORTED !!SUPPORTED_WINXPSP2
749 EXPLAIN !!DictionaryPolicy_Explain
751 PART !!DictionaryPolicy_Part EDITTEXT
752 VALUENAME "DictionaryPolicy"
760 SUPPORTED_WINXPSP2="At least Windows 3.13"
762 chromium_recommended="Chromium - Recommended"
763 DictionaryPolicy_Policy="Caption of policy."
764 DictionaryPolicy_Explain="Description of group."
765 DictionaryPolicy_Part="Caption of policy."
767 self.CompareOutputs(output, expected_output)
769 def testNonSupportedPolicy(self):
770 # Tests a policy that is not supported on Windows, so it shouldn't
771 # be included in the ADM file.
772 grd = self.PrepareTest('''
774 'policy_definitions': [
776 'name': 'NonWinGroup',
779 'name': 'NonWinPolicy',
781 'supported_on': ['chrome.linux:8-', 'chrome.mac:8-'],
782 'caption': 'Caption of list policy.',
783 'desc': 'Desc of list policy.',
785 'caption': 'Group caption.',
786 'desc': 'Group description.',
791 'win_supported_winxpsp2': {
792 'text': 'At least Windows 3.16', 'desc': 'blah'
795 'text': 'Recommended', 'desc': 'bleh'
799 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'adm', 'en')
800 expected_output = self.ConstructOutput(
801 ['MACHINE', 'USER'], '''
803 KEYNAME "Software\\Policies\\Chromium"
807 CATEGORY !!chromium_recommended
808 KEYNAME "Software\\Policies\\Chromium\\Recommended"
814 SUPPORTED_WINXPSP2="At least Windows 3.16"
816 chromium_recommended="Chromium - Recommended"
818 self.CompareOutputs(output, expected_output)
820 def testNonRecommendedPolicy(self):
821 # Tests a policy that is not recommended, so it should be included.
822 grd = self.PrepareTest('''
824 'policy_definitions': [
826 'name': 'MainPolicy',
828 'supported_on': ['chrome.win:8-'],
829 'caption': 'Caption of main.',
830 'desc': 'Description of main.',
835 'win_supported_winxpsp2': {
836 'text': 'At least Windows 3.12', 'desc': 'blah'
839 'text': 'Recommended', 'desc': 'bleh'
843 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'adm', 'en')
844 expected_output = self.ConstructOutput(
845 ['MACHINE', 'USER'], '''
847 CATEGORY !!googlechrome
848 KEYNAME "Software\\Policies\\Google\\Chrome"
850 POLICY !!MainPolicy_Policy
852 SUPPORTED !!SUPPORTED_WINXPSP2
854 EXPLAIN !!MainPolicy_Explain
855 VALUENAME "MainPolicy"
864 CATEGORY !!googlechrome_recommended
865 KEYNAME "Software\\Policies\\Google\\Chrome\\Recommended"
872 SUPPORTED_WINXPSP2="At least Windows 3.12"
874 googlechrome="Google Chrome"
875 googlechrome_recommended="Google Chrome - Recommended"
876 MainPolicy_Policy="Caption of main."
877 MainPolicy_Explain="Description of main."''')
878 self.CompareOutputs(output, expected_output)
880 def testPolicyGroup(self):
881 # Tests a policy group that has more than one policies.
882 grd = self.PrepareTest('''
884 'policy_definitions': [
888 'desc': 'Description of group.',
889 'caption': 'Caption of group.',
893 'supported_on': ['chrome.win:8-'],
894 'features': { 'can_be_recommended': True },
895 'caption': 'Caption of policy1.',
896 'desc': """Description of policy1.
901 'supported_on': ['chrome.win:8-'],
902 'caption': 'Caption of policy2.',
903 'desc': """Description of policy2.
910 'win_supported_winxpsp2': {
911 'text': 'At least Windows 3.16', 'desc': 'blah'
914 'text': 'Recommended', 'desc': 'bleh'
918 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'adm', 'en')
919 expected_output = self.ConstructOutput(
920 ['MACHINE', 'USER'], '''
922 KEYNAME "Software\\Policies\\Chromium"
924 CATEGORY !!Group1_Category
925 POLICY !!Policy1_Policy
927 SUPPORTED !!SUPPORTED_WINXPSP2
929 EXPLAIN !!Policy1_Explain
931 PART !!Policy1_Part LISTBOX
932 KEYNAME "Software\\Policies\\Chromium\\Policy1"
937 POLICY !!Policy2_Policy
939 SUPPORTED !!SUPPORTED_WINXPSP2
941 EXPLAIN !!Policy2_Explain
943 PART !!Policy2_Part EDITTEXT
952 CATEGORY !!chromium_recommended
953 KEYNAME "Software\\Policies\\Chromium\\Recommended"
955 CATEGORY !!Group1_Category
956 POLICY !!Policy1_Policy
958 SUPPORTED !!SUPPORTED_WINXPSP2
960 EXPLAIN !!Policy1_Explain
962 PART !!Policy1_Part LISTBOX
963 KEYNAME "Software\\Policies\\Chromium\\Recommended\\Policy1"
974 SUPPORTED_WINXPSP2="At least Windows 3.16"
976 chromium_recommended="Chromium - Recommended"
977 Group1_Category="Caption of group."
978 Policy1_Policy="Caption of policy1."
979 Policy1_Explain="Description of policy1.\\nWith a newline."
980 Policy1_Part="Caption of policy1."
981 Policy2_Policy="Caption of policy2."
982 Policy2_Explain="Description of policy2.\\nWith a newline."
983 Policy2_Part="Caption of policy2."
985 self.CompareOutputs(output, expected_output)
987 if __name__ == '__main__':