From f0e63b4b3b44cb5a0f3099a7f3102d4fe7acf3a7 Mon Sep 17 00:00:00 2001 From: "danno@chromium.org" Date: Thu, 13 Jan 2011 12:23:06 +0000 Subject: Policy: Add ProxyMode and deprecate ProxyServerMode. - Add support for 'deprecated' attribute in template generator - Add support for both int- and string- based enums in the template generator - Add logic to handle ProxyMode and fall back to ProxyServerMode BUG=68134 TEST=ConfigurationPolicyPrefStore*, new policy template generator tests Review URL: http://codereview.chromium.org/5958014 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71315 0039d316-1c4b-4281-b951-d872f2087c98 --- .../policy_templates/policy_template_generator.py | 2 +- .../policy_template_generator_unittest.py | 39 ++++++++++-- .../format/policy_templates/writers/adm_writer.py | 13 ++-- .../writers/adm_writer_unittest.py | 71 ++++++++++++++++++++-- .../format/policy_templates/writers/adml_writer.py | 2 +- .../writers/adml_writer_unittest.py | 52 +++++++++++++++- .../format/policy_templates/writers/admx_writer.py | 15 +++-- .../writers/admx_writer_unittest.py | 49 +++++++++++++-- .../format/policy_templates/writers/doc_writer.py | 26 ++++++-- .../writers/doc_writer_unittest.py | 47 ++++++++++++-- .../format/policy_templates/writers/json_writer.py | 6 +- .../writers/json_writer_unittest.py | 41 ++++++++++++- .../writers/plist_strings_writer.py | 4 +- .../writers/plist_strings_writer_unittest.py | 56 +++++++++++++++-- .../policy_templates/writers/plist_writer.py | 11 +++- .../writers/plist_writer_unittest.py | 70 +++++++++++++++++++-- .../format/policy_templates/writers/reg_writer.py | 4 +- .../writers/reg_writer_unittest.py | 42 ++++++++++++- .../policy_templates/writers/template_writer.py | 16 +++++ 19 files changed, 501 insertions(+), 65 deletions(-) (limited to 'tools/grit') diff --git a/tools/grit/grit/format/policy_templates/policy_template_generator.py b/tools/grit/grit/format/policy_templates/policy_template_generator.py index cce0116..c80bd64 100644 --- a/tools/grit/grit/format/policy_templates/policy_template_generator.py +++ b/tools/grit/grit/format/policy_templates/policy_template_generator.py @@ -129,7 +129,7 @@ class PolicyTemplateGenerator: policy['supported_on']) if policy['type'] == 'group': self._ProcessPolicyList(policy['policies']) - elif policy['type'] == 'enum': + elif policy['type'] in ('string-enum', 'int-enum'): # Iterate through all the items of an enum-type policy, and add captions. for item in policy['items']: self._AddMessageToItem('ENUM_' + item['name'], item, 'caption') diff --git a/tools/grit/grit/format/policy_templates/policy_template_generator_unittest.py b/tools/grit/grit/format/policy_templates/policy_template_generator_unittest.py index d51c0a5..fe27216 100644 --- a/tools/grit/grit/format/policy_templates/policy_template_generator_unittest.py +++ b/tools/grit/grit/format/policy_templates/policy_template_generator_unittest.py @@ -211,7 +211,7 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): self.tester.fail() self.do_test(messages_mock, policy_defs_mock, LocalMockWriter()) - def testEnumTexts(self): + def testIntEnumTexts(self): # Test that GUI messages are assigned correctly to enums # (aka dropdown menus). messages_mock = { @@ -224,12 +224,41 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): } policy_defs_mock = [{ 'name': 'Policy1', - 'type': 'enum', + 'type': 'int-enum', 'supported_on': [], 'items': [ - {'name': 'item1', 'value': '0'}, - {'name': 'item2', 'value': '1'}, - {'name': 'item3', 'value': '3'}, + {'name': 'item1', 'value': 0}, + {'name': 'item2', 'value': 1}, + {'name': 'item3', 'value': 3}, + ] + }] + + class LocalMockWriter(mock_writer.MockWriter): + def WritePolicy(self, policy): + self.tester.assertEquals(policy['items'][0]['caption'], 'string1') + self.tester.assertEquals(policy['items'][1]['caption'], 'string2') + self.tester.assertEquals(policy['items'][2]['caption'], 'string3') + self.do_test(messages_mock, policy_defs_mock, LocalMockWriter()) + + def testStringEnumTexts(self): + # Test that GUI messages are assigned correctly to enums + # (aka dropdown menus). + messages_mock = { + 'IDS_POLICY_ENUM_ITEM1_CAPTION': 'string1', + 'IDS_POLICY_ENUM_ITEM2_CAPTION': 'string2', + 'IDS_POLICY_ENUM_ITEM3_CAPTION': 'string3', + 'IDS_POLICY_POLICY1_CAPTION': '', + 'IDS_POLICY_POLICY1_DESC': '', + + } + policy_defs_mock = [{ + 'name': 'Policy1', + 'type': 'string-enum', + 'supported_on': [], + 'items': [ + {'name': 'item1', 'value': 'one'}, + {'name': 'item2', 'value': 'two'}, + {'name': 'item3', 'value': 'three'}, ] }] diff --git a/tools/grit/grit/format/policy_templates/writers/adm_writer.py b/tools/grit/grit/format/policy_templates/writers/adm_writer.py index 23df695..3067282 100644 --- a/tools/grit/grit/format/policy_templates/writers/adm_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/adm_writer.py @@ -21,7 +21,8 @@ class AdmWriter(template_writer.TemplateWriter): TYPE_TO_INPUT = { 'string': 'EDITTEXT', - 'enum': 'DROPDOWNLIST', + 'string-enum': 'DROPDOWNLIST', + 'int-enum': 'DROPDOWNLIST', 'list': 'LISTBOX'} NEWLINE = '\r\n' @@ -76,11 +77,15 @@ class AdmWriter(template_writer.TemplateWriter): self._PrintLine('VALUEPREFIX ""') else: self._PrintLine('VALUENAME "%s"' % policy['name']) - if policy['type'] == 'enum': + if policy['type'] in ('int-enum', 'string-enum'): self._PrintLine('ITEMLIST', 1) for item in policy['items']: - self._PrintLine('NAME !!%s_DropDown VALUE NUMERIC %s' % - (item['name'], item['value'])) + if policy['type'] == 'int-enum': + value_text = 'NUMERIC ' + str(item['value']) + else: + value_text = '"' + item['value'] + '"' + self._PrintLine('NAME !!%s_DropDown VALUE %s' % + (item['name'], value_text)) self._AddGuiString(item['name'] + '_DropDown', item['caption']) self._PrintLine('END ITEMLIST', -1) self._PrintLine('END PART', -1) diff --git a/tools/grit/grit/format/policy_templates/writers/adm_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/adm_writer_unittest.py index b823aae..91d71b8d 100644 --- a/tools/grit/grit/format/policy_templates/writers/adm_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/adm_writer_unittest.py @@ -154,17 +154,17 @@ StringPolicy_Part="Caption of policy." ''' self.CompareOutputs(output, expected_output) - def testEnumPolicy(self): - # Tests a policy group with a single policy of type 'enum'. + def testIntEnumPolicy(self): + # Tests a policy group with a single policy of type 'int-enum'. grd = self.PrepareTest(''' { 'policy_definitions': [ { 'name': 'EnumPolicy', - 'type': 'enum', + 'type': 'int-enum', 'items': [ - {'name': 'ProxyServerDisabled', 'value': '0'}, - {'name': 'ProxyServerAutoDetect', 'value': '1'}, + {'name': 'ProxyServerDisabled', 'value': 0}, + {'name': 'ProxyServerAutoDetect', 'value': 1}, ], 'supported_on': ['chrome.win:8-'] }, @@ -215,6 +215,67 @@ ProxyServerAutoDetect_DropDown="Option2" ''' self.CompareOutputs(output, expected_output) + def testStringEnumPolicy(self): + # Tests a policy group with a single policy of type 'int-enum'. + grd = self.PrepareTest(''' + { + 'policy_definitions': [ + { + 'name': 'EnumPolicy', + 'type': 'string-enum', + 'items': [ + {'name': 'ProxyServerDisabled', 'value': 'one'}, + {'name': 'ProxyServerAutoDetect', 'value': 'two'}, + ], + 'supported_on': ['chrome.win:8-'] + }, + ], + 'placeholders': [], + }''', ''' + + Caption of policy. + Description of policy. + Option1 + Option2 + At least Windows 3.14 + + ''' ) + output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'adm', 'en') + expected_output = '''CLASS MACHINE + CATEGORY !!google + CATEGORY !!googlechrome + KEYNAME "Software\\Policies\\Google\\Chrome" + + POLICY !!EnumPolicy_Policy + #if version >= 4 + SUPPORTED !!SUPPORTED_WINXPSP2 + #endif + EXPLAIN !!EnumPolicy_Explain + + PART !!EnumPolicy_Part DROPDOWNLIST + VALUENAME "EnumPolicy" + ITEMLIST + NAME !!ProxyServerDisabled_DropDown VALUE "one" + NAME !!ProxyServerAutoDetect_DropDown VALUE "two" + END ITEMLIST + END PART + END POLICY + + END CATEGORY + END CATEGORY + +[Strings] +SUPPORTED_WINXPSP2="At least Windows 3.14" +google="Google" +googlechrome="Google Chrome" +EnumPolicy_Policy="Caption of policy." +EnumPolicy_Explain="Description of policy." +EnumPolicy_Part="Caption of policy." +ProxyServerDisabled_DropDown="Option1" +ProxyServerAutoDetect_DropDown="Option2" +''' + self.CompareOutputs(output, expected_output) + def testListPolicy(self): # Tests a policy group with a single policy of type 'list'. grd = self.PrepareTest(''' diff --git a/tools/grit/grit/format/policy_templates/writers/adml_writer.py b/tools/grit/grit/format/policy_templates/writers/adml_writer.py index e656dfd..99f9895 100644 --- a/tools/grit/grit/format/policy_templates/writers/adml_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/adml_writer.py @@ -87,7 +87,7 @@ class ADMLWriter(xml_formatted_writer.XMLFormattedWriter): {'refId': policy_name}) label_elem = self.AddElement(textbox_elem, 'label') label_elem.appendChild(self._doc.createTextNode(policy_label)) - elif policy_type == 'enum': + elif policy_type in ('int-enum', 'string-enum'): for item in policy['items']: self._AddString(self._string_table_elem, item['name'], item['caption']) dropdownlist_elem = self.AddElement(presentation_elem, 'dropdownList', diff --git a/tools/grit/grit/format/policy_templates/writers/adml_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/adml_writer_unittest.py index dd4d42e..03a4399 100644 --- a/tools/grit/grit/format/policy_templates/writers/adml_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/adml_writer_unittest.py @@ -158,10 +158,58 @@ class AdmlWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): '') self.AssertXMLEquals(output, expected_output) - def testEnumPolicy(self): + def testIntEnumPolicy(self): enum_policy = { 'name': 'EnumPolicyStub', - 'type': 'enum', + 'type': 'int-enum', + 'caption': 'Enum policy caption', + 'label': 'Enum policy label', + 'desc': 'This is a test description.', + 'items': [ + { + 'name': 'item 1', + 'value': 1, + 'caption': 'Caption Item 1', + }, + { + 'name': 'item 2', + 'value': 2, + 'caption': 'Caption Item 2', + }, + ], + } + self. _InitWriterForAddingPolicies(self.writer, enum_policy) + self.writer.WritePolicy(enum_policy) + # Assert generated string elements. + output = self.GetXMLOfChildren(self.writer._string_table_elem) + expected_output = ( + '\n' + ' Enum policy caption\n' + '\n' + '\n' + ' This is a test description.\n' + '\n' + '\n' + ' Caption Item 1\n' + '\n' + '\n' + ' Caption Item 2\n' + '') + self.AssertXMLEquals(output, expected_output) + # Assert generated presentation elements. + output = self.GetXMLOfChildren(self.writer._presentation_table_elem) + expected_output = ( + '\n' + ' \n' + ' Enum policy label\n' + ' \n' + '') + self.AssertXMLEquals(output, expected_output) + + def testStringEnumPolicy(self): + enum_policy = { + 'name': 'EnumPolicyStub', + 'type': 'string-enum', 'caption': 'Enum policy caption', 'label': 'Enum policy label', 'desc': 'This is a test description.', diff --git a/tools/grit/grit/format/policy_templates/writers/admx_writer.py b/tools/grit/grit/format/policy_templates/writers/admx_writer.py index 164175b..1573d78 100644 --- a/tools/grit/grit/format/policy_templates/writers/admx_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/admx_writer.py @@ -157,11 +157,12 @@ class ADMXWriter(xml_formatted_writer.XMLFormattedWriter): 'valueName': name, } self.AddElement(parent, 'text', attributes) - - def _AddEnumPolicy(self, parent, name, items): + def _AddEnumPolicy(self, parent, policy): '''Generates ADMX elements for an Enum-Policy and adds them to the passed parent element. ''' + name = policy['name'] + items = policy['items'] attributes = { 'id': name, 'valueName': name, @@ -172,7 +173,11 @@ class ADMXWriter(xml_formatted_writer.XMLFormattedWriter): item_elem = self.AddElement(enum_elem, 'item', attributes) value_elem = self.AddElement(item_elem, 'value') attributes = {'value': str(item['value'])} - self.AddElement(value_elem, 'decimal', attributes) + if policy['type'] == 'int-enum': + element_type = 'decimal' + else: + element_type = 'string' + self.AddElement(value_elem, element_type, attributes) def _AddListPolicy(self, parent, name): '''Generates ADMX XML elements for a List-Policy and adds them to the @@ -249,9 +254,9 @@ class ADMXWriter(xml_formatted_writer.XMLFormattedWriter): elif policy_type == 'string': parent = self._GetElements(policy_elem) self._AddStringPolicy(parent, policy_name) - elif policy_type == 'enum': + elif policy_type in ('int-enum', 'string-enum'): parent = self._GetElements(policy_elem) - self._AddEnumPolicy(parent, policy_name, policy['items']) + self._AddEnumPolicy(parent, policy) elif policy_type == 'list': parent = self._GetElements(policy_elem) self._AddListPolicy(parent, policy_name) diff --git a/tools/grit/grit/format/policy_templates/writers/admx_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/admx_writer_unittest.py index c645be0..f8b3fa0 100644 --- a/tools/grit/grit/format/policy_templates/writers/admx_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/admx_writer_unittest.py @@ -201,13 +201,13 @@ class AdmxWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): '') self.AssertXMLEquals(output, expected_output) - def testEnumPolicy(self): + def testIntEnumPolicy(self): enum_policy = { 'name': 'SampleEnumPolicy', - 'type': 'enum', + 'type': 'int-enum', 'items': [ - {'name': 'item 1', 'value': '0'}, - {'name': 'item 2', 'value': '1'}, + {'name': 'item_1', 'value': 0}, + {'name': 'item_2', 'value': 1}, ] } @@ -223,12 +223,12 @@ class AdmxWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): ' \n' ' \n' ' \n' - ' \n' + ' \n' ' \n' ' \n' ' \n' ' \n' - ' \n' + ' \n' ' \n' ' \n' ' \n' @@ -238,6 +238,43 @@ class AdmxWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): '') self.AssertXMLEquals(output, expected_output) + def testStringEnumPolicy(self): + enum_policy = { + 'name': 'SampleEnumPolicy', + 'type': 'string-enum', + 'items': [ + {'name': 'item_1', 'value': 'one'}, + {'name': 'item_2', 'value': 'two'}, + ] + } + + self._initWriterForPolicy(self.writer, enum_policy) + self.writer.WritePolicy(enum_policy) + output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) + expected_output = ( + '\n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + '') + self.AssertXMLEquals(output, expected_output) + def testListPolicy(self): list_policy = { 'name': 'SampleListPolicy', diff --git a/tools/grit/grit/format/policy_templates/writers/doc_writer.py b/tools/grit/grit/format/policy_templates/writers/doc_writer.py index 320e79d..7e410bc 100644 --- a/tools/grit/grit/format/policy_templates/writers/doc_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/doc_writer.py @@ -107,7 +107,7 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): def _AddDescription(self, parent, policy): '''Adds a string containing the description of the policy. URLs are replaced with links and the possible choices are enumerated in case - of 'enum' type policies. + of 'string-enum' and 'int-enum' type policies. Args: parent: The DOM node for which the feature list will be added. @@ -116,11 +116,15 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): # Replace URLs with links in the description. self._AddTextWithLinks(parent, policy['desc']) # Add list of enum items. - if policy['type'] == 'enum': + if policy['type'] in ('string-enum', 'int-enum'): ul = self.AddElement(parent, 'ul') for item in policy['items']: + if policy['type'] == 'int-enum': + value_string = str(item['value']) + else: + value_string = '"%s"' % item['value'] self.AddElement( - ul, 'li', {}, '%s = %s' % (item['value'], item['caption'])) + ul, 'li', {}, '%s = %s' % (value_string, item['caption'])) def _AddFeatures(self, parent, policy): '''Adds a string containing the list of supported features of a policy @@ -253,10 +257,12 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): raise Exception('Expected boolean value.') elif policy_type == 'string': self.AddText(parent, '"%s"' % example_value) - elif policy_type == 'enum': + elif policy_type == 'int-enum': self.AddText( parent, '0x%08x (Windows), %d (Linux/Mac)' % (example_value, example_value)) + elif policy_type == 'string-enum': + self.AddText(parent, '"%s"' % (example_value)) elif policy_type == 'list': self._AddListExample(parent, policy) else: @@ -407,7 +413,11 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): self.AddElement(h2, 'a', {'name': policy['name']}) if policy['type'] != 'group': # Normal policies get a full description. - self.AddText(h2, policy['name']) + policy_name_text = policy['name'] + if 'deprecated' in policy and policy['deprecated'] == True: + policy_name_text += " (" + policy_name_text += self._GetLocalizedMessage('deprecated') + ")" + self.AddText(h2, policy_name_text) self.AddElement(parent2, 'span', {}, policy['caption']) self._AddPolicyNote(parent2, policy) self._AddPolicyDetails(parent2, policy) @@ -424,6 +434,9 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): # Implementation of abstract methods of TemplateWriter: # + def IsDeprecatedPolicySupported(self, policy): + return True + def WritePolicy(self, policy): self._AddPolicyRow(self._summary_tbody, policy) self._AddPolicySection(self._details_div, policy) @@ -490,7 +503,8 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): self._TYPE_MAP = { 'string': 'String (REG_SZ)', 'main': 'Boolean (REG_DWORD)', - 'enum': 'Integer (REG_DWORD)', + 'int-enum': 'Integer (REG_DWORD)', + 'string-enum': 'String (REG_SZ)', 'list': 'List of strings', } # The CSS style-sheet used for the document. It will be used in Google diff --git a/tools/grit/grit/format/policy_templates/writers/doc_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/doc_writer_unittest.py index 7d9e1fb0..6a5fd42 100644 --- a/tools/grit/grit/format/policy_templates/writers/doc_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/doc_writer_unittest.py @@ -153,11 +153,11 @@ class DocWriterUnittest(writer_unittest_common.WriterUnittestCommon): e3.toxml(), 'text') - def testAddDescription(self): - # Test if URLs are replaced and choices of 'enum' policies are listed + def testAddDescriptionIntEnum(self): + # Test if URLs are replaced and choices of 'int-enum' policies are listed # correctly. policy = { - 'type': 'enum', + 'type': 'int-enum', 'items': [ {'value': 0, 'caption': 'Disable foo'}, {'value': 2, 'caption': 'Solve your problem'}, @@ -174,6 +174,27 @@ See http://policy-explanation.example.com for more details. See http://policy-explanation.example.com for more details.
  • 0 = Disable foo
  • 2 = Solve your problem
  • 5 = Enable bar
''') + def testAddDescriptionStringEnum(self): + # Test if URLs are replaced and choices of 'int-enum' policies are listed + # correctly. + policy = { + 'type': 'string-enum', + 'items': [ + {'value': "one", 'caption': 'Disable foo'}, + {'value': "two", 'caption': 'Solve your problem'}, + {'value': "three", 'caption': 'Enable bar'}, + ], + 'desc': '''This policy disables foo, except in case of bar. +See http://policy-explanation.example.com for more details. +''' + } + self.writer._AddDescription(self.doc_root, policy) + self.assertEquals( + self.doc_root.toxml(), + '''This policy disables foo, except in case of bar. +See http://policy-explanation.example.com for more details. +
  • "one" = Disable foo
  • "two" = Solve your problem
  • "three" = Enable bar
''') + def testAddFeatures(self): # Test if the list of features of a policy is handled correctly. policy = { @@ -252,11 +273,11 @@ See http://policy-explanation.ex e2.toxml(), '0x00000000 (Windows), false (Linux), <false /> (Mac)') - def testEnumExample(self): - # Test representation of 'enum' example values. + def testIntEnumExample(self): + # Test representation of 'int-enum' example values. policy = { 'name': 'PolicyName', - 'type': 'enum', + 'type': 'int-enum', 'annotations': { 'example_value': 16 } @@ -266,6 +287,20 @@ See http://policy-explanation.ex self.doc_root.toxml(), '0x00000010 (Windows), 16 (Linux/Mac)') + def testStringEnumExample(self): + # Test representation of 'int-enum' example values. + policy = { + 'name': 'PolicyName', + 'type': 'string-enum', + 'annotations': { + 'example_value': "wacky" + } + } + self.writer._AddExample(self.doc_root, policy) + self.assertEquals( + self.doc_root.toxml(), + '"wacky"') + def testStringExample(self): # Test representation of 'string' example values. policy = { diff --git a/tools/grit/grit/format/policy_templates/writers/json_writer.py b/tools/grit/grit/format/policy_templates/writers/json_writer.py index 8a6e35f..bc52ed1 100644 --- a/tools/grit/grit/format/policy_templates/writers/json_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/json_writer.py @@ -39,8 +39,10 @@ class JsonWriter(template_writer.TemplateWriter): example_value_str = 'true' else: example_value_str = 'false' - elif policy['type'] == 'enum': - example_value_str = example_value + elif policy['type'] == 'string-enum': + example_value_str = '"%s"' % example_value; + elif policy['type'] == 'int-enum': + example_value_str = str(example_value) else: raise Exception('unknown policy type %s:' % policy['type']) diff --git a/tools/grit/grit/format/policy_templates/writers/json_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/json_writer_unittest.py index 744eeaa..16ad11b 100644 --- a/tools/grit/grit/format/policy_templates/writers/json_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/json_writer_unittest.py @@ -98,14 +98,14 @@ class JsonWriterUnittest(writer_unittest_common.WriterUnittestCommon): '}') self.CompareOutputs(output, expected_output) - def testEnumPolicy(self): - # Tests a policy group with a single policy of type 'enum'. + def testIntEnumPolicy(self): + # Tests a policy group with a single policy of type 'int-enum'. grd = self.PrepareTest( '{' ' "policy_definitions": [' ' {' ' "name": "EnumPolicy",' - ' "type": "enum",' + ' "type": "int-enum",' ' "items": [' ' {"name": "ProxyServerDisabled", "value": 0},' ' {"name": "ProxyServerAutoDetect", "value": 1},' @@ -133,6 +133,41 @@ class JsonWriterUnittest(writer_unittest_common.WriterUnittestCommon): '}') self.CompareOutputs(output, expected_output) + def testStringEnumPolicy(self): + # Tests a policy group with a single policy of type 'string-enum'. + grd = self.PrepareTest( + '{' + ' "policy_definitions": [' + ' {' + ' "name": "EnumPolicy",' + ' "type": "string-enum",' + ' "items": [' + ' {"name": "ProxyServerDisabled", "value": "one"},' + ' {"name": "ProxyServerAutoDetect", "value": "two"},' + ' ],' + ' "supported_on": ["chrome.linux:8-"],' + ' "annotations": {' + ' "example_value": "one"' + ' }' + ' },' + ' ],' + ' "placeholders": [],' + '}', + '' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + '') + output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'json', 'en') + expected_output = ( + '{\n' + ' "EnumPolicy": "one"\n' + '}') + self.CompareOutputs(output, expected_output) + def testListPolicy(self): # Tests a policy group with a single policy of type 'list'. grd = self.PrepareTest( diff --git a/tools/grit/grit/format/policy_templates/writers/plist_strings_writer.py b/tools/grit/grit/format/policy_templates/writers/plist_strings_writer.py index 4f699f2..bb1a634 100644 --- a/tools/grit/grit/format/policy_templates/writers/plist_strings_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/plist_strings_writer.py @@ -48,11 +48,11 @@ class PListStringsWriter(template_writer.TemplateWriter): string table. ''' desc = policy['desc'] - if (policy['type'] == 'enum'): + if policy['type'] in ('int-enum','string-enum'): # Append the captions of enum items to the description string. item_descs = [] for item in policy['items']: - item_descs.append(item['value'] + ' - ' + item['caption']) + item_descs.append(str(item['value']) + ' - ' + item['caption']) desc = '\n'.join(item_descs) + '\n' + desc self._AddToStringTable(policy['name'], policy['label'], desc) diff --git a/tools/grit/grit/format/policy_templates/writers/plist_strings_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/plist_strings_writer_unittest.py index 1fc19c8..f921c16 100644 --- a/tools/grit/grit/format/policy_templates/writers/plist_strings_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/plist_strings_writer_unittest.py @@ -126,8 +126,8 @@ With a newline. '"Description of policy.\\nWith a newline.";') self.assertEquals(output.strip(), expected_output.strip()) - def testEnumPolicy(self): - # Tests a policy group with a single policy of type 'enum'. + def testIntEnumPolicy(self): + # Tests a policy group with a single policy of type 'int-enum'. grd = self.PrepareTest(''' { 'policy_definitions': [ @@ -136,10 +136,10 @@ With a newline. 'type': 'group', 'policies': [{ 'name': 'EnumPolicy', - 'type': 'enum', + 'type': 'int-enum', 'items': [ - {'name': 'ProxyServerDisabled', 'value': '0'}, - {'name': 'ProxyServerAutoDetect', 'value': '1'}, + {'name': 'ProxyServerDisabled', 'value': 0}, + {'name': 'ProxyServerAutoDetect', 'value': 1}, ], 'supported_on': ['chrome.mac:8-'], }], @@ -172,6 +172,52 @@ With a newline. self.assertEquals(output.strip(), expected_output.strip()) + def testStringEnumPolicy(self): + # Tests a policy group with a single policy of type 'string-enum'. + grd = self.PrepareTest(''' + { + 'policy_definitions': [ + { + 'name': 'EnumGroup', + 'type': 'group', + 'policies': [{ + 'name': 'EnumPolicy', + 'type': 'string-enum', + 'items': [ + {'name': 'ProxyServerDisabled', 'value': "one"}, + {'name': 'ProxyServerAutoDetect', 'value': "two"}, + ], + 'supported_on': ['chrome.mac:8-'], + }], + }, + ], + 'placeholders': [], + }''', ''' + + Caption of group. + Description of group. + Caption of policy. + Description of policy. + Option1 + Option2 + $1 preferences + + ''' ) + output = self.GetOutput( + grd, + 'fr', + {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'}, + 'plist_strings', + 'en') + expected_output = ( + 'Google_Chrome.pfm_title = "Google Chrome";\n' + 'Google_Chrome.pfm_description = "Google Chrome preferences";\n' + 'EnumPolicy.pfm_title = "Caption of policy.";\n' + 'EnumPolicy.pfm_description = ' + '"one - Option1\\ntwo - Option2\\nDescription of policy.";\n') + + self.assertEquals(output.strip(), expected_output.strip()) + def testNonSupportedPolicy(self): # Tests a policy that is not supported on Mac, so its strings shouldn't # be included in the plist string table. diff --git a/tools/grit/grit/format/policy_templates/writers/plist_writer.py b/tools/grit/grit/format/policy_templates/writers/plist_writer.py index a0068ac..53461d9 100644 --- a/tools/grit/grit/format/policy_templates/writers/plist_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/plist_writer.py @@ -24,7 +24,8 @@ class PListWriter(xml_formatted_writer.XMLFormattedWriter): STRING_TABLE = 'Localizable.strings' TYPE_TO_INPUT = { 'string': 'string', - 'enum': 'integer', + 'int-enum': 'integer', + 'string-enum': 'string', 'main': 'boolean', 'list': 'array', } @@ -88,10 +89,14 @@ class PListWriter(xml_formatted_writer.XMLFormattedWriter): self._AddTargets(dict) self._AddStringKeyValuePair(dict, 'pfm_type', self.TYPE_TO_INPUT[policy_type]) - if (policy_type == 'enum'): + if policy_type in ('int-enum', 'string-enum'): range_list = self._AddKeyValuePair(dict, 'pfm_range_list', 'array') for item in policy['items']: - self.AddElement(range_list, 'integer', {}, item['value']) + if policy_type == 'int-enum': + element_type = 'integer' + else: + element_type = 'string' + self.AddElement(range_list, element_type, {}, str(item['value'])) def BeginTemplate(self): self._plist.attributes['version'] = '1' diff --git a/tools/grit/grit/format/policy_templates/writers/plist_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/plist_writer_unittest.py index 88240cd..bbb0f52 100644 --- a/tools/grit/grit/format/policy_templates/writers/plist_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/plist_writer_unittest.py @@ -175,8 +175,8 @@ class PListWriterUnittest(writer_unittest_common.WriterUnittestCommon): ''') self.assertEquals(output.strip(), expected_output.strip()) - def testEnumPolicy(self): - # Tests a policy group with a single policy of type 'enum'. + def testIntEnumPolicy(self): + # Tests a policy group with a single policy of type 'int-enum'. grd = self.PrepareTest(''' { 'policy_definitions': [ @@ -185,10 +185,10 @@ class PListWriterUnittest(writer_unittest_common.WriterUnittestCommon): 'type': 'group', 'policies': [{ 'name': 'EnumPolicy', - 'type': 'enum', + 'type': 'int-enum', 'items': [ - {'name': 'ProxyServerDisabled', 'value': '0'}, - {'name': 'ProxyServerAutoDetect', 'value': '1'}, + {'name': 'ProxyServerDisabled', 'value': 0}, + {'name': 'ProxyServerAutoDetect', 'value': 1}, ], 'supported_on': ['chrome.mac:8-'], }], @@ -235,6 +235,66 @@ class PListWriterUnittest(writer_unittest_common.WriterUnittestCommon): ''') self.assertEquals(output.strip(), expected_output.strip()) + def testStringEnumPolicy(self): + # Tests a policy group with a single policy of type 'string-enum'. + grd = self.PrepareTest(''' + { + 'policy_definitions': [ + { + 'name': 'EnumGroup', + 'type': 'group', + 'policies': [{ + 'name': 'EnumPolicy', + 'type': 'string-enum', + 'items': [ + {'name': 'ProxyServerDisabled', 'value': 'one'}, + {'name': 'ProxyServerAutoDetect', 'value': 'two'}, + ], + 'supported_on': ['chrome.mac:8-'], + }], + }, + ], + 'placeholders': [], + }''', ''' + + This is not tested here. + This is not tested here. + This is not tested here. + This is not tested here. + This is not tested here. + This is not tested here. + + ''' ) + output = self.GetOutput( + grd, + 'fr', + {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'}, + 'plist', + 'en') + expected_output = self._GetExpectedOutputs( + 'Google_Chrome', 'com.example.Test2', ''' + + pfm_name + EnumPolicy + pfm_description + + pfm_title + + pfm_targets + + user-managed + + pfm_type + string + pfm_range_list + + one + two + + + ''') + self.assertEquals(output.strip(), expected_output.strip()) + def testNonSupportedPolicy(self): # Tests a policy that is not supported on Mac, so it shouldn't # be included in the plist file. diff --git a/tools/grit/grit/format/policy_templates/writers/reg_writer.py b/tools/grit/grit/format/policy_templates/writers/reg_writer.py index 92a39c9..5aea50e 100644 --- a/tools/grit/grit/format/policy_templates/writers/reg_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/reg_writer.py @@ -69,8 +69,10 @@ class RegWriter(template_writer.TemplateWriter): example_value_str = 'dword:1' else: example_value_str = 'dword:0' - elif policy['type'] == 'enum': + elif policy['type'] == 'int-enum': example_value_str = 'dword:%d' % example_value + elif policy['type'] == 'string-enum': + example_value_str = '"%s"' % example_value else: raise Exception('unknown policy type %s:' % policy['type']) diff --git a/tools/grit/grit/format/policy_templates/writers/reg_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/reg_writer_unittest.py index 9cd614b..2464efd 100644 --- a/tools/grit/grit/format/policy_templates/writers/reg_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/reg_writer_unittest.py @@ -103,14 +103,14 @@ class RegWriterUnittest(writer_unittest_common.WriterUnittestCommon): '"StringPolicy"="hello, world! \\\" \\\\"']) self.CompareOutputs(output, expected_output) - def testEnumPolicy(self): - # Tests a policy group with a single policy of type 'enum'. + def testIntEnumPolicy(self): + # Tests a policy group with a single policy of type 'int-enum'. grd = self.PrepareTest( '{' ' "policy_definitions": [' ' {' ' "name": "EnumPolicy",' - ' "type": "enum",' + ' "type": "int-enum",' ' "items": [' ' {"name": "ProxyServerDisabled", "value": 0},' ' {"name": "ProxyServerAutoDetect", "value": 1},' @@ -139,6 +139,42 @@ class RegWriterUnittest(writer_unittest_common.WriterUnittestCommon): '"EnumPolicy"=dword:1']) self.CompareOutputs(output, expected_output) + def testStringEnumPolicy(self): + # Tests a policy group with a single policy of type 'string-enum'. + grd = self.PrepareTest( + '{' + ' "policy_definitions": [' + ' {' + ' "name": "EnumPolicy",' + ' "type": "string-enum",' + ' "items": [' + ' {"name": "ProxyServerDisabled", "value": "one"},' + ' {"name": "ProxyServerAutoDetect", "value": "two"},' + ' ],' + ' "supported_on": ["chrome.win:8-"],' + ' "annotations": {' + ' "example_value": "two"' + ' }' + ' },' + ' ],' + ' "placeholders": [],' + '}', + '' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + '') + output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'reg', 'en') + expected_output = self.NEWLINE.join([ + 'Windows Registry Editor Version 5.00', + '', + '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome]', + '"EnumPolicy"="two"']) + self.CompareOutputs(output, expected_output) + def testListPolicy(self): # Tests a policy group with a single policy of type 'list'. grd = self.PrepareTest( diff --git a/tools/grit/grit/format/policy_templates/writers/template_writer.py b/tools/grit/grit/format/policy_templates/writers/template_writer.py index 01c2ef4..ecff851 100644 --- a/tools/grit/grit/format/policy_templates/writers/template_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/template_writer.py @@ -33,6 +33,18 @@ class TemplateWriter(object): self.config = config self.messages = messages + def IsDeprecatedPolicySupported(self, policy): + '''Checks if the given deprecated policy is supported by the writer. + + Args: + policy: The dictionary of the policy. + + Returns: + True if the writer chooses to include the deprecated 'policy' in its + output. + ''' + return False + def IsPolicySupported(self, policy): '''Checks if the given policy is supported by the writer. In other words, the set of platforms supported by the writer @@ -45,6 +57,10 @@ class TemplateWriter(object): Returns: True if the writer chooses to include 'policy' in its output. ''' + if ('deprecated' in policy and policy['deprecated'] is True and + not self.IsDeprecatedPolicySupported(policy)): + return False + if '*' in self.platforms: # Currently chrome_os is only catched here. return True -- cgit v1.1