diff options
3 files changed, 385 insertions, 0 deletions
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 new file mode 100644 index 0000000..e345ae0 --- /dev/null +++ b/tools/grit/grit/format/policy_templates/writers/adml_writer_unittest.py @@ -0,0 +1,186 @@ +#!/usr/bin/python2.4 +# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + + +"""Unittests for grit.format.policy_templates.writers.adml_writer.""" + + +import os +import sys +import unittest +if __name__ == '__main__': + sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../..')) + + +from grit.format.policy_templates.writers import adml_writer +from grit.format.policy_templates.writers import xml_writer_base_unittest +from xml.dom import minidom + + +class AdmlWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): + + def setUp(self): + config = { + 'build': 'test', + 'win_supported_os': 'SUPPORTED_TESTOS', + 'win_supported_os_msg': 'IDS_POLICY_WIN_SUPPORTED_WINXPSP2', + } + # Grid messages + messages = { + 'IDS_POLICY_WIN_SUPPORTED_WINXPSP2': 'Supported on Test OS or higher' + } + self.writer = adml_writer.ADMLWriter(config, messages) + self.writer.Prepare() + + def _InitWriterForAddingPolicyGroups(self, writer): + '''Initialize the writer for adding policy groups. This method must be + called before the method "BeginPolicyGroup" can be called. It initializes + attributes of the writer. + ''' + dom_impl = minidom.getDOMImplementation('') + writer._doc = dom_impl.createDocument( + None, 'policyDefinitionRessources', None) + writer._presentation_table_elem = self.writer.AddElement( + writer._doc.documentElement, 'presentationTable') + writer._string_table_elem = self.writer.AddElement( + writer._doc.documentElement, 'stringTable') + + def _InitWriterForAddingPolicies(self, writer): + '''Initialize the writer for adding policies. This method must be + called before the method "WritePolicy" can be called. It initializes + attributes of the writer. + ''' + self._InitWriterForAddingPolicyGroups(writer) + writer._active_presentation_elem = self.writer.AddElement( + self.writer._presentation_table_elem, 'presentation', + {'id': 'PolicyGroupStub'}) + + def testEmpty(self): + self.writer.BeginTemplate() + self.writer.EndTemplate() + output = self.writer.GetTemplateText() + expected_output = ( + '<?xml version="1.0" ?><policyDefinitionResources' + ' revision="1.0" schemaVersion="1.0"><displayName/><description/>' + '<resources><stringTable><string id="SUPPORTED_TESTOS">Supported on' + ' Test OS or higher</string></stringTable><presentationTable/>' + '</resources></policyDefinitionResources>') + self.AssertXMLEquals(output, expected_output) + + def testPolicyGroup(self): + self._InitWriterForAddingPolicyGroups(self.writer) + empty_policy_group = { + 'name': 'PolicyGroup', + 'caption': 'Test Caption', + 'desc': 'This is the test description of the test policy group.', + } + self.writer.BeginPolicyGroup(empty_policy_group) + self.writer.EndPolicyGroup + # Assert generated string elements. + output = self.GetXMLOfChildren(self.writer._string_table_elem) + expected_output = ( + '<string id="PolicyGroup">\n Test Caption\n</string>\n<string' + ' id="PolicyGroup_Explain">\n This is the test description of the' + ' test policy group.\n</string>') + self.AssertXMLEquals(output, expected_output) + # Assert generated presentation elements. + output = self.GetXMLOfChildren(self.writer._presentation_table_elem) + expected_output = '<presentation id="PolicyGroup"/>' + self.AssertXMLEquals(output, expected_output) + + def testMainPolicy(self): + self. _InitWriterForAddingPolicies(self.writer) + main_policy = { + 'name': 'DummyMainPolicy', + 'type': 'main', + } + self.writer.WritePolicy(main_policy) + # Assert generated string elements. + output = self.GetXMLOfChildren(self.writer._string_table_elem) + expected_output = '' + self.AssertXMLEquals(output, expected_output) + # Assert generated presentation elements. + output = self.GetXMLOfChildren(self.writer._active_presentation_elem) + expected_output = '' + self.AssertXMLEquals(output, expected_output) + + def testStringPolicy(self): + self. _InitWriterForAddingPolicies(self.writer) + string_policy = { + 'name': 'StringPolicyStub', + 'type': 'string', + 'caption': 'String Policy Caption', + 'desc': 'This is a test description.', + } + self.writer.WritePolicy(string_policy) + # Assert generated string elements. + output = self.GetXMLOfChildren(self.writer._string_table_elem) + expected_output = '' + self.AssertXMLEquals(output, expected_output) + # Assert generated presentation elements. + output = self.GetXMLOfChildren(self.writer._active_presentation_elem) + expected_output = ( + '<textBox refId="StringPolicyStub">\n <label>\n' + ' String Policy Caption\n </label>\n</textBox>') + self.AssertXMLEquals(output, expected_output) + + def testEnumPolicy(self): + self. _InitWriterForAddingPolicies(self.writer) + enum_policy = { + 'name': 'EnumPolicyStub', + 'type': 'enum', + 'caption': 'Enum Policy Caption', + 'desc': 'This is a test description.', + 'items': [ + { + 'name': 'item 1', + 'value': 'value 1', + 'caption': 'Caption Item 1', + }, + { + 'name': 'item 2', + 'value': 'value 2', + 'caption': 'Caption Item 2', + }, + ], + } + self.writer.WritePolicy(enum_policy) + # Assert generated string elements. + output = self.GetXMLOfChildren(self.writer._string_table_elem) + expected_output = ( + '<string id="item 1">\n Caption Item 1\n</string>\n<string id="item' + ' 2">\n Caption Item 2\n</string>') + self.AssertXMLEquals(output, expected_output) + # Assert generated presentation elements. + output = self.GetXMLOfChildren(self.writer._active_presentation_elem) + expected_output = ( + '<dropdownList refId="EnumPolicyStub">\n Enum Policy Caption\n' + '</dropdownList>') + self.AssertXMLEquals(output, expected_output) + + def testListPolicy(self): + self. _InitWriterForAddingPolicies(self.writer) + list_policy = { + 'name': 'ListPolicyStub', + 'type': 'list', + 'caption': 'List Policy Caption', + 'desc': 'This is a test description.', + } + self.writer.WritePolicy(list_policy) + # Assert generated string elements. + output = self.GetXMLOfChildren(self.writer._string_table_elem) + expected_output = ( + '<string id="ListPolicyStubDesc">\n List Policy Caption\n</string>') + self.AssertXMLEquals(output, expected_output) + # Assert generated presentation elements. + output = self.GetXMLOfChildren(self.writer._active_presentation_elem) + expected_output = ( + '<listBox refId="ListPolicyStubDesc">\n List Policy Caption\n' + '</listBox>') + self.AssertXMLEquals(output, expected_output) + + +if __name__ == '__main__': + unittest.main() 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 new file mode 100644 index 0000000..532733c --- /dev/null +++ b/tools/grit/grit/format/policy_templates/writers/admx_writer_unittest.py @@ -0,0 +1,160 @@ +#!/usr/bin/python2.4 +# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + + +"""Unittests for grit.format.policy_templates.writers.admx_writer.""" + + +import os +import sys +import unittest +if __name__ == '__main__': + sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../..')) + + +from grit.format.policy_templates.writers import admx_writer +from grit.format.policy_templates.writers import xml_writer_base_unittest +from xml.dom import minidom + + +class AdmxWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): + + def _CreateDocumentElement(self): + dom_impl = minidom.getDOMImplementation('') + doc = dom_impl.createDocument(None, 'root', None) + return doc.documentElement + + def setUp(self): + # Writer configuration. This dictionary contains parameter used by the ADMX + # Writer + config = { + 'win_group_policy_class': 'TestClass', + 'win_supported_os': 'SUPPORTED_TESTOS', + 'win_supported_os_msg': 'IDS_POLICY_WIN_SUPPORTED_WINXPSP2', + 'win_reg_key_name': 'Software\\Policies\\Test', + 'win_category_path': ['test_category'], + 'admx_namespace': 'ADMXWriter.Test.Namespace', + 'admx_prefix': 'test_prefix' + } + # Grit messages. + messages = {} + self.writer = admx_writer.ADMXWriter(config, messages) + self.writer.Prepare() + + def testEmpty(self): + self.writer.BeginTemplate() + self.writer.EndTemplate() + + output = self.writer.GetTemplateText() + expected_output = ( + '<?xml version="1.0" ?>\n<policyDefinitions revision="1.0"' + ' schemaVersion="1.0">\n <policyNamespaces>\n <target' + ' namespace="ADMXWriter.Test.Namespace" prefix="test_prefix"/>\n' + ' <using namespace="Microsoft.Policies.Windows" prefix="windows"/>' + '\n </policyNamespaces>\n <resources' + ' minRequiredRevision="1.0"/>\n <supportedOn>\n' + ' <definitions>\n <definition displayName="' + '$(string.SUPPORTED_TESTOS)" name="SUPPORTED_TESTOS"/>\n' + ' </definitions>\n </supportedOn>\n <categories>\n <category' + ' displayName="$(string.test_category)" name="test_category"/>\n' + ' </categories>\n <policies/>\n</policyDefinitions>') + self.AssertXMLEquals(output, expected_output) + + def testPolicyGroup(self): + parent_elem = self._CreateDocumentElement() + self.writer._active_policies_elem = parent_elem + + empty_policy_group = { + 'name': 'PolicyGroup' + } + self.writer.BeginPolicyGroup(empty_policy_group) + self.writer.EndPolicyGroup() + + output = self.GetXMLOfChildren(parent_elem) + expected_output = ( + '<policy class="TestClass" displayName="$(string.PolicyGroup)"' + ' explainText="$(string.PolicyGroup_Explain)" key=' + '"Software\Policies\Test" name="PolicyGroup"' + ' presentation="$(presentation.PolicyGroup)" valueName="PolicyGroup">' + '\n <parentCategory ref="test_category"/>\n' + ' <supportedOn ref="SUPPORTED_TESTOS"/>\n</policy>') + self.AssertXMLEquals(output, expected_output) + + def testMainPolicy(self): + main_policy = { + 'name': 'DummyMainPolicy', + 'type': 'main', + } + + parent_elem = self._CreateDocumentElement() + self.writer._active_policy_elem = parent_elem + + self.writer.WritePolicy(main_policy) + + output = self.GetXMLOfChildren(parent_elem) + expected_output = ( + '<enabledValue>\n <decimal value="1"/>\n</enabledValue>\n' + '<disabledValue>\n <decimal value="0"/>\n</disabledValue>') + self.AssertXMLEquals(output, expected_output) + + def testStringPolicy(self): + string_policy = { + 'name': 'SampleStringPolicy', + 'type': 'string', + } + parent_elem = self.writer.AddElement(self._CreateDocumentElement(), + 'policy') + self.writer._active_policy_elem = parent_elem + self.writer.WritePolicy(string_policy) + output = self.GetXMLOfChildren(parent_elem) + expected_output = ( + '<elements>\n <text id="SampleStringPolicy"' + ' valueName="SampleStringPolicy"/>\n</elements>') + self.AssertXMLEquals(output, expected_output) + + def testEnumPolicy(self): + parent_elem = self.writer.AddElement(self._CreateDocumentElement(), + 'policy') + self.writer._active_policy_elem = parent_elem + + enum_policy = { + 'name': 'SampleEnumPolicy', + 'type': 'enum', + 'items': [ + {'name': 'item 1', 'value': '0'}, + {'name': 'item 2', 'value': '1'}, + ] + } + self.writer.WritePolicy(enum_policy) + output = self.GetXMLOfChildren(parent_elem) + expected_output = ( + '<elements>\n <enum id="SampleEnumPolicy" valueName=' + '"SampleEnumPolicy">\n <item displayName="$(string.item 1)">\n' + ' <value>\n <decimal value="0"/>\n </value>\n' + ' </item>\n <item displayName="$(string.item 2)">\n <value>' + '\n <decimal value="1"/>\n </value>\n </item>' + '\n </enum>\n</elements>') + self.AssertXMLEquals(output, expected_output) + + def testListPolicy(self): + list_policy = { + 'name': 'SampleListPolicy', + 'type': 'list', + } + parent_elem = self.writer.AddElement(self._CreateDocumentElement(), + 'policy') + self.writer._active_policy_elem = parent_elem + self.writer.WritePolicy(list_policy) + output = self.GetXMLOfChildren(parent_elem) + expected_output = ( + '<elements>\n <list id="SampleListPolicyDesc"' + ' key="Software\Policies\Test\SampleListPolicy"' + ' valuePrefix=""/>\n</elements>') + + self.AssertXMLEquals(output, expected_output) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/grit/grit/format/policy_templates/writers/xml_writer_base_unittest.py b/tools/grit/grit/format/policy_templates/writers/xml_writer_base_unittest.py new file mode 100644 index 0000000..f5bd429 --- /dev/null +++ b/tools/grit/grit/format/policy_templates/writers/xml_writer_base_unittest.py @@ -0,0 +1,39 @@ +#!/usr/bin/python2.4 +# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + + +"""Unittests for grit.format.policy_templates.writers.admx_writer.""" + + +import os +import sys +import unittest + + +from grit.format.policy_templates.writers import admx_writer +from xml.dom import minidom + + +class XmlWriterBaseTest(unittest.TestCase): + '''Base class for XML writer unit-tests. + ''' + + def GetXMLOfChildren(self, parent): + '''Returns the XML of all child nodes of the given parent node. + Args: + parent: The XML of the children of this node will be returned. + + Return: XML of the chrildren of the parent node. + ''' + return ''.join( + child.toprettyxml(indent = ' ') for child in parent.childNodes) + + def AssertXMLEquals(self, output, expected_output): + '''Asserts if the passed XML arguements are equal. + Args: + output: Actual XML text. + expected_output: Expected XML text. + ''' + self.assertEquals(output.strip(), expected_output.strip()) |