| Jaewoong Jung | e5cd4e1 | 2019-11-22 14:34:55 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python | 
|  | 2 | # | 
|  | 3 | # Copyright (C) 2019 The Android Open Source Project | 
|  | 4 | # | 
|  | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | # you may not use this file except in compliance with the License. | 
|  | 7 | # You may obtain a copy of the License at | 
|  | 8 | # | 
|  | 9 | #      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | # | 
|  | 11 | # Unless required by applicable law or agreed to in writing, software | 
|  | 12 | # distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | # See the License for the specific language governing permissions and | 
|  | 15 | # limitations under the License. | 
|  | 16 | # | 
|  | 17 | """Unit tests for test_config_fixer.py.""" | 
|  | 18 |  | 
| Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 19 | import io | 
| Jaewoong Jung | e5cd4e1 | 2019-11-22 14:34:55 -0800 | [diff] [blame] | 20 | import sys | 
|  | 21 | import unittest | 
|  | 22 | from xml.dom import minidom | 
|  | 23 |  | 
|  | 24 | import test_config_fixer | 
|  | 25 |  | 
| Seth Moore | c6f4b53 | 2023-02-02 13:22:26 -0800 | [diff] [blame] | 26 | from manifest import write_xml | 
|  | 27 |  | 
| Jaewoong Jung | e5cd4e1 | 2019-11-22 14:34:55 -0800 | [diff] [blame] | 28 | sys.dont_write_bytecode = True | 
|  | 29 |  | 
|  | 30 |  | 
|  | 31 | class OverwritePackageNameTest(unittest.TestCase): | 
|  | 32 | """ Unit tests for overwrite_package_name function """ | 
|  | 33 |  | 
|  | 34 | manifest = ( | 
|  | 35 | '<?xml version="1.0" encoding="utf-8"?>\n' | 
|  | 36 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android"\n' | 
|  | 37 | '    package="com.android.foo">\n' | 
|  | 38 | '    <application>\n' | 
|  | 39 | '    </application>\n' | 
|  | 40 | '</manifest>\n') | 
|  | 41 |  | 
|  | 42 | test_config = ( | 
|  | 43 | '<?xml version="1.0" encoding="utf-8"?>\n' | 
|  | 44 | '<configuration description="Runs some tests.">\n' | 
|  | 45 | '    <option name="test-suite-tag" value="apct"/>\n' | 
|  | 46 | '    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">\n' | 
|  | 47 | '        <option name="package" value="%s"/>\n' | 
|  | 48 | '    </target_preparer>\n' | 
|  | 49 | '    <test class="com.android.tradefed.testtype.AndroidJUnitTest">\n' | 
|  | 50 | '        <option name="package" value="%s"/>\n' | 
|  | 51 | '        <option name="runtime-hint" value="20s"/>\n' | 
|  | 52 | '    </test>\n' | 
|  | 53 | '    <test class="com.android.tradefed.testtype.AndroidJUnitTest">\n' | 
|  | 54 | '        <option name="package" value="%s"/>\n' | 
|  | 55 | '        <option name="runtime-hint" value="15s"/>\n' | 
|  | 56 | '    </test>\n' | 
|  | 57 | '</configuration>\n') | 
|  | 58 |  | 
|  | 59 | def test_all(self): | 
|  | 60 | doc = minidom.parseString(self.test_config % ("com.android.foo", "com.android.foo", "com.android.bar")) | 
|  | 61 | manifest = minidom.parseString(self.manifest) | 
|  | 62 |  | 
|  | 63 | test_config_fixer.overwrite_package_name(doc, manifest, "com.soong.foo") | 
| Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 64 | output = io.StringIO() | 
| Jaewoong Jung | e5cd4e1 | 2019-11-22 14:34:55 -0800 | [diff] [blame] | 65 | test_config_fixer.write_xml(output, doc) | 
|  | 66 |  | 
|  | 67 | # Only the matching package name in a test node should be updated. | 
|  | 68 | expected = self.test_config % ("com.android.foo", "com.soong.foo", "com.android.bar") | 
|  | 69 | self.assertEqual(expected, output.getvalue()) | 
|  | 70 |  | 
|  | 71 |  | 
| Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 72 | class OverwriteTestFileNameTest(unittest.TestCase): | 
|  | 73 | """ Unit tests for overwrite_test_file_name function """ | 
|  | 74 |  | 
| Rahul Sabnis | 48a8f0d | 2022-04-19 18:00:10 -0700 | [diff] [blame] | 75 | test_config_test_app_install_setup = ( | 
| Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 76 | '<?xml version="1.0" encoding="utf-8"?>\n' | 
|  | 77 | '<configuration description="Runs some tests.">\n' | 
|  | 78 | '    <target_preparer class="com.android.tradefed.targetprep.TestAppInstallSetup">\n' | 
|  | 79 | '        <option name="test-file-name" value="%s"/>\n' | 
|  | 80 | '    </target_preparer>\n' | 
|  | 81 | '    <test class="com.android.tradefed.testtype.AndroidJUnitTest">\n' | 
|  | 82 | '        <option name="package" value="com.android.foo"/>\n' | 
|  | 83 | '        <option name="runtime-hint" value="20s"/>\n' | 
|  | 84 | '    </test>\n' | 
|  | 85 | '</configuration>\n') | 
|  | 86 |  | 
| Rahul Sabnis | 48a8f0d | 2022-04-19 18:00:10 -0700 | [diff] [blame] | 87 | test_config_suite_apk_installer = ( | 
|  | 88 | '<?xml version="1.0" encoding="utf-8"?>\n' | 
|  | 89 | '<configuration description="Runs some tests.">\n' | 
|  | 90 | '    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">\n' | 
|  | 91 | '        <option name="test-file-name" value="%s"/>\n' | 
|  | 92 | '    </target_preparer>\n' | 
|  | 93 | '    <test class="com.android.tradefed.testtype.AndroidJUnitTest">\n' | 
|  | 94 | '        <option name="package" value="com.android.foo"/>\n' | 
|  | 95 | '        <option name="runtime-hint" value="20s"/>\n' | 
|  | 96 | '    </test>\n' | 
|  | 97 | '</configuration>\n') | 
|  | 98 |  | 
|  | 99 | def test_testappinstallsetup(self): | 
|  | 100 | doc = minidom.parseString(self.test_config_test_app_install_setup % ("foo.apk")) | 
| Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 101 |  | 
|  | 102 | test_config_fixer.overwrite_test_file_name(doc, "bar.apk") | 
| Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 103 | output = io.StringIO() | 
| Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 104 | test_config_fixer.write_xml(output, doc) | 
|  | 105 |  | 
|  | 106 | # Only the matching package name in a test node should be updated. | 
| Rahul Sabnis | 48a8f0d | 2022-04-19 18:00:10 -0700 | [diff] [blame] | 107 | expected = self.test_config_test_app_install_setup % ("bar.apk") | 
|  | 108 | self.assertEqual(expected, output.getvalue()) | 
|  | 109 |  | 
|  | 110 | def test_suiteapkinstaller(self): | 
|  | 111 | doc = minidom.parseString(self.test_config_suite_apk_installer % ("foo.apk")) | 
|  | 112 |  | 
|  | 113 | test_config_fixer.overwrite_test_file_name(doc, "bar.apk") | 
|  | 114 | output = io.StringIO() | 
|  | 115 | test_config_fixer.write_xml(output, doc) | 
|  | 116 |  | 
|  | 117 | # Only the matching package name in a test node should be updated. | 
|  | 118 | expected = self.test_config_suite_apk_installer % ("bar.apk") | 
| Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 119 | self.assertEqual(expected, output.getvalue()) | 
|  | 120 |  | 
|  | 121 |  | 
| Seth Moore | c6f4b53 | 2023-02-02 13:22:26 -0800 | [diff] [blame] | 122 | class OverwriteMainlineModulePackageNameTest(unittest.TestCase): | 
|  | 123 | """ Unit tests for overwrite_mainline_module_package_name function """ | 
|  | 124 |  | 
|  | 125 | test_config = ( | 
|  | 126 | '<?xml version="1.0" encoding="utf-8"?>\n' | 
|  | 127 | '<configuration description="Runs some tests.">\n' | 
|  | 128 | '    <target_preparer class="com.android.tradefed.targetprep.TestAppInstallSetup">\n' | 
|  | 129 | '        <option name="test-file-name" value="foo.apk"/>\n' | 
|  | 130 | '    </target_preparer>\n' | 
|  | 131 | '    <test class="com.android.tradefed.testtype.AndroidJUnitTest">\n' | 
|  | 132 | '        <option name="package" value="com.android.foo"/>\n' | 
|  | 133 | '        <option name="runtime-hint" value="20s"/>\n' | 
|  | 134 | '    </test>\n' | 
|  | 135 | '    <object type="module_controller" class="com.android.tradefed.testtype.suite.module.MainlineTestModuleController">\n' | 
|  | 136 | '        <option name="enable" value="true"/>\n' | 
|  | 137 | '        <option name="mainline-module-package-name" value="%s"/>\n' | 
|  | 138 | '    </object>\n' | 
|  | 139 | '</configuration>\n') | 
|  | 140 |  | 
|  | 141 | def test_testappinstallsetup(self): | 
|  | 142 | doc = minidom.parseString(self.test_config % ("com.android.old.package.name")) | 
|  | 143 |  | 
|  | 144 | test_config_fixer.overwrite_mainline_module_package_name(doc, "com.android.new.package.name") | 
|  | 145 | output = io.StringIO() | 
|  | 146 | test_config_fixer.write_xml(output, doc) | 
|  | 147 |  | 
|  | 148 | # Only the mainline module package name should be updated. Format the xml | 
|  | 149 | # with minidom first to avoid mismatches due to trivial reformatting. | 
|  | 150 | expected = io.StringIO() | 
|  | 151 | write_xml(expected, minidom.parseString(self.test_config % ("com.android.new.package.name"))) | 
|  | 152 | self.maxDiff = None | 
|  | 153 | self.assertEqual(expected.getvalue(), output.getvalue()) | 
|  | 154 |  | 
|  | 155 |  | 
| Jaewoong Jung | e5cd4e1 | 2019-11-22 14:34:55 -0800 | [diff] [blame] | 156 | if __name__ == '__main__': | 
|  | 157 | unittest.main(verbosity=2) |