1# Copyright (C) 2023 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import unittest 16import xml.etree.ElementTree as ET 17import os 18 19from generate_adservices_public_xml import AdServicesUiUtil 20 21class AdServiceUiTests(unittest.TestCase): 22 23 TEST_STRINGS_DIR = 'test_strings.xml' 24 TEST_STRINGS_XML = ''' 25 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 26 <string name="permlab_accessAdServicesTopics">access AdServices Topics API</string> 27 <string name="permdesc_accessAdServicesTopics">Allows an application to access AdServices Topics API.</string> 28 </resources> 29 ''' 30 31 TEST_PUBLIC_DIR = 'test_public.xml' 32 TEST_PUBLIC_XML = ''' 33 <resources> 34 <public type="string" name="permlab_accessAdServicesTopics" id="0x7f017fff" /> 35 <public type="string" name="permdesc_accessAdServicesTopics" id="0x7f017ffe" /> 36 </resources> 37 ''' 38 39 util = AdServicesUiUtil() 40 41 def _generate_test_files(self): 42 test_strings_xml = ET.ElementTree(ET.fromstring(self.TEST_STRINGS_XML)) 43 test_public_xml = ET.ElementTree(ET.fromstring(self.TEST_PUBLIC_XML)) 44 45 test_strings_xml.write(self.TEST_STRINGS_DIR) 46 test_public_xml.write(self.TEST_PUBLIC_DIR) 47 48 def _delete_test_files(self): 49 os.remove(self.TEST_STRINGS_DIR) 50 os.remove(self.TEST_PUBLIC_DIR) 51 52 def _update_strings_xml(self, n): 53 root = ET.parse(self.TEST_STRINGS_DIR).getroot() 54 for string in [f"testString{i}" for i in range(n)]: 55 added_element = ET.SubElement(root, 'string') 56 added_element.set('name', string) 57 58 ET.indent(root, space=' ') 59 with open(self.TEST_STRINGS_DIR, "w+") as file: 60 file.write(ET.tostring(root, encoding="unicode")) 61 62 def test_adding_strings(self): 63 self._generate_test_files() 64 65 new_strings_count = 5 66 self._update_strings_xml(new_strings_count) 67 self.util.update_public_xml(self.TEST_STRINGS_DIR, self.TEST_PUBLIC_DIR) 68 69 old_root = ET.ElementTree(ET.fromstring(self.TEST_PUBLIC_XML)).getroot() 70 old_mapping = {node.attrib['name']:node.attrib['id'] for node in old_root} 71 72 root = ET.parse(self.TEST_PUBLIC_DIR).getroot() 73 mapping = {node.attrib['name']:node.attrib['id'] for node in root} 74 75 assert(len(old_mapping) + new_strings_count == len(mapping)) 76 assert(len(mapping) == len(set(mapping.values()))) 77 for name, _id in mapping.items(): 78 if name in old_mapping: 79 assert(_id == old_mapping[name]) 80 81 82 self._delete_test_files() 83 84if __name__ == '__main__': 85 unittest.main()