Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2018 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 | # |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 17 | """Unit tests for manifest_fixer.py.""" |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 18 | |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 19 | import io |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 20 | import sys |
| 21 | import unittest |
| 22 | from xml.dom import minidom |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 23 | import xml.etree.ElementTree as ElementTree |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 24 | |
| 25 | import manifest_fixer |
| 26 | |
| 27 | sys.dont_write_bytecode = True |
| 28 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 29 | |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 30 | class CompareVersionGtTest(unittest.TestCase): |
| 31 | """Unit tests for compare_version_gt function.""" |
| 32 | |
| 33 | def test_sdk(self): |
| 34 | """Test comparing sdk versions.""" |
| 35 | self.assertTrue(manifest_fixer.compare_version_gt('28', '27')) |
| 36 | self.assertFalse(manifest_fixer.compare_version_gt('27', '28')) |
| 37 | self.assertFalse(manifest_fixer.compare_version_gt('28', '28')) |
| 38 | |
| 39 | def test_codename(self): |
| 40 | """Test comparing codenames.""" |
| 41 | self.assertTrue(manifest_fixer.compare_version_gt('Q', 'P')) |
| 42 | self.assertFalse(manifest_fixer.compare_version_gt('P', 'Q')) |
| 43 | self.assertFalse(manifest_fixer.compare_version_gt('Q', 'Q')) |
| 44 | |
| 45 | def test_sdk_codename(self): |
| 46 | """Test comparing sdk versions with codenames.""" |
| 47 | self.assertTrue(manifest_fixer.compare_version_gt('Q', '28')) |
| 48 | self.assertFalse(manifest_fixer.compare_version_gt('28', 'Q')) |
| 49 | |
| 50 | def test_compare_numeric(self): |
| 51 | """Test that numbers are compared in numeric and not lexicographic order.""" |
| 52 | self.assertTrue(manifest_fixer.compare_version_gt('18', '8')) |
| 53 | |
| 54 | |
| 55 | class RaiseMinSdkVersionTest(unittest.TestCase): |
| 56 | """Unit tests for raise_min_sdk_version function.""" |
| 57 | |
Colin Cross | 496d66d | 2018-09-10 14:02:18 -0700 | [diff] [blame] | 58 | def raise_min_sdk_version_test(self, input_manifest, min_sdk_version, |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 59 | target_sdk_version, library): |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 60 | doc = minidom.parseString(input_manifest) |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 61 | manifest_fixer.raise_min_sdk_version(doc, min_sdk_version, |
| 62 | target_sdk_version, library) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 63 | output = io.StringIO() |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 64 | manifest_fixer.write_xml(output, doc) |
| 65 | return output.getvalue() |
| 66 | |
| 67 | manifest_tmpl = ( |
| 68 | '<?xml version="1.0" encoding="utf-8"?>\n' |
| 69 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' |
| 70 | '%s' |
| 71 | '</manifest>\n') |
| 72 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 73 | def uses_sdk(self, min_sdk=None, target_sdk=None, extra=''): |
Colin Cross | 496d66d | 2018-09-10 14:02:18 -0700 | [diff] [blame] | 74 | attrs = '' |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 75 | if min_sdk: |
| 76 | attrs += ' android:minSdkVersion="%s"' % min_sdk |
| 77 | if target_sdk: |
| 78 | attrs += ' android:targetSdkVersion="%s"' % target_sdk |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 79 | if extra: |
Colin Cross | 496d66d | 2018-09-10 14:02:18 -0700 | [diff] [blame] | 80 | attrs += ' ' + extra |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 81 | return ' <uses-sdk%s/>\n' % attrs |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 82 | |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 83 | def assert_xml_equal(self, output, expected): |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 84 | self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected)) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 85 | |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 86 | def test_no_uses_sdk(self): |
| 87 | """Tests inserting a uses-sdk element into a manifest.""" |
| 88 | |
| 89 | manifest_input = self.manifest_tmpl % '' |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 90 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='28') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 91 | output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 92 | self.assert_xml_equal(output, expected) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 93 | |
| 94 | def test_no_min(self): |
| 95 | """Tests inserting a minSdkVersion attribute into a uses-sdk element.""" |
| 96 | |
| 97 | manifest_input = self.manifest_tmpl % ' <uses-sdk extra="foo"/>\n' |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 98 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='28', |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 99 | extra='extra="foo"') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 100 | output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 101 | self.assert_xml_equal(output, expected) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 102 | |
| 103 | def test_raise_min(self): |
| 104 | """Tests inserting a minSdkVersion attribute into a uses-sdk element.""" |
| 105 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 106 | manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='27') |
| 107 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='28') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 108 | output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 109 | self.assert_xml_equal(output, expected) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 110 | |
| 111 | def test_raise(self): |
| 112 | """Tests raising a minSdkVersion attribute.""" |
| 113 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 114 | manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='27') |
| 115 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='28') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 116 | output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 117 | self.assert_xml_equal(output, expected) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 118 | |
| 119 | def test_no_raise_min(self): |
| 120 | """Tests a minSdkVersion that doesn't need raising.""" |
| 121 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 122 | manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='28') |
| 123 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='27') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 124 | output = self.raise_min_sdk_version_test(manifest_input, '27', '27', False) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 125 | self.assert_xml_equal(output, expected) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 126 | |
| 127 | def test_raise_codename(self): |
| 128 | """Tests raising a minSdkVersion attribute to a codename.""" |
| 129 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 130 | manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='28') |
| 131 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='P', target_sdk='P') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 132 | output = self.raise_min_sdk_version_test(manifest_input, 'P', 'P', False) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 133 | self.assert_xml_equal(output, expected) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 134 | |
| 135 | def test_no_raise_codename(self): |
| 136 | """Tests a minSdkVersion codename that doesn't need raising.""" |
| 137 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 138 | manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='P') |
| 139 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='P', target_sdk='28') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 140 | output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 141 | self.assert_xml_equal(output, expected) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 142 | |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 143 | def test_target(self): |
| 144 | """Tests an existing targetSdkVersion is preserved.""" |
| 145 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 146 | manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='26', target_sdk='27') |
| 147 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='27') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 148 | output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 149 | self.assert_xml_equal(output, expected) |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 150 | |
| 151 | def test_no_target(self): |
| 152 | """Tests inserting targetSdkVersion when minSdkVersion exists.""" |
| 153 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 154 | manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='27') |
| 155 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='29') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 156 | output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 157 | self.assert_xml_equal(output, expected) |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 158 | |
| 159 | def test_target_no_min(self): |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 160 | """"Tests inserting targetSdkVersion when minSdkVersion exists.""" |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 161 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 162 | manifest_input = self.manifest_tmpl % self.uses_sdk(target_sdk='27') |
| 163 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='27') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 164 | output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 165 | self.assert_xml_equal(output, expected) |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 166 | |
| 167 | def test_no_target_no_min(self): |
| 168 | """Tests inserting targetSdkVersion when minSdkVersion does not exist.""" |
| 169 | |
| 170 | manifest_input = self.manifest_tmpl % '' |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 171 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='29') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 172 | output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 173 | self.assert_xml_equal(output, expected) |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 174 | |
| 175 | def test_library_no_target(self): |
Colin Cross | 496d66d | 2018-09-10 14:02:18 -0700 | [diff] [blame] | 176 | """Tests inserting targetSdkVersion when minSdkVersion exists.""" |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 177 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 178 | manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='27') |
| 179 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='16') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 180 | output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 181 | self.assert_xml_equal(output, expected) |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 182 | |
| 183 | def test_library_target_no_min(self): |
Colin Cross | 496d66d | 2018-09-10 14:02:18 -0700 | [diff] [blame] | 184 | """Tests inserting targetSdkVersion when minSdkVersion exists.""" |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 185 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 186 | manifest_input = self.manifest_tmpl % self.uses_sdk(target_sdk='27') |
| 187 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='27') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 188 | output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 189 | self.assert_xml_equal(output, expected) |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 190 | |
| 191 | def test_library_no_target_no_min(self): |
Colin Cross | 496d66d | 2018-09-10 14:02:18 -0700 | [diff] [blame] | 192 | """Tests inserting targetSdkVersion when minSdkVersion does not exist.""" |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 193 | |
Colin Cross | 496d66d | 2018-09-10 14:02:18 -0700 | [diff] [blame] | 194 | manifest_input = self.manifest_tmpl % '' |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 195 | expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='16') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 196 | output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 197 | self.assert_xml_equal(output, expected) |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 198 | |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 199 | def test_extra(self): |
| 200 | """Tests that extra attributes and elements are maintained.""" |
| 201 | |
| 202 | manifest_input = self.manifest_tmpl % ( |
| 203 | ' <!-- comment -->\n' |
| 204 | ' <uses-sdk android:minSdkVersion="27" extra="foo"/>\n' |
| 205 | ' <application/>\n') |
| 206 | |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 207 | # pylint: disable=line-too-long |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 208 | expected = self.manifest_tmpl % ( |
| 209 | ' <!-- comment -->\n' |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 210 | ' <uses-sdk android:minSdkVersion="28" extra="foo" android:targetSdkVersion="29"/>\n' |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 211 | ' <application/>\n') |
| 212 | |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 213 | output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 214 | |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 215 | self.assert_xml_equal(output, expected) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 216 | |
| 217 | def test_indent(self): |
| 218 | """Tests that an inserted element copies the existing indentation.""" |
| 219 | |
| 220 | manifest_input = self.manifest_tmpl % ' <!-- comment -->\n' |
| 221 | |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 222 | # pylint: disable=line-too-long |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 223 | expected = self.manifest_tmpl % ( |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 224 | ' <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="29"/>\n' |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 225 | ' <!-- comment -->\n') |
| 226 | |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 227 | output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 228 | |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 229 | self.assert_xml_equal(output, expected) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 230 | |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 231 | def test_multiple_uses_sdks(self): |
| 232 | """Tests a manifest that contains multiple uses_sdks elements.""" |
| 233 | |
| 234 | manifest_input = self.manifest_tmpl % ( |
| 235 | ' <uses-sdk android:featureFlag="foo" android:minSdkVersion="21" />\n' |
| 236 | ' <uses-sdk android:featureFlag="!foo" android:minSdkVersion="22" />\n') |
| 237 | expected = self.manifest_tmpl % ( |
| 238 | ' <uses-sdk android:featureFlag="foo" android:minSdkVersion="28" android:targetSdkVersion="28" />\n' |
| 239 | ' <uses-sdk android:featureFlag="!foo" android:minSdkVersion="28" android:targetSdkVersion="28" />\n') |
| 240 | |
| 241 | output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False) |
| 242 | self.assert_xml_equal(output, expected) |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 243 | |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 244 | class AddLoggingParentTest(unittest.TestCase): |
| 245 | """Unit tests for add_logging_parent function.""" |
| 246 | |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 247 | def assert_xml_equal(self, output, expected): |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 248 | self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected)) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 249 | |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 250 | def add_logging_parent_test(self, input_manifest, logging_parent=None): |
| 251 | doc = minidom.parseString(input_manifest) |
| 252 | if logging_parent: |
| 253 | manifest_fixer.add_logging_parent(doc, logging_parent) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 254 | output = io.StringIO() |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 255 | manifest_fixer.write_xml(output, doc) |
| 256 | return output.getvalue() |
| 257 | |
| 258 | manifest_tmpl = ( |
| 259 | '<?xml version="1.0" encoding="utf-8"?>\n' |
| 260 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' |
| 261 | '%s' |
| 262 | '</manifest>\n') |
| 263 | |
| 264 | def uses_logging_parent(self, logging_parent=None): |
| 265 | attrs = '' |
| 266 | if logging_parent: |
| 267 | meta_text = ('<meta-data android:name="android.content.pm.LOGGING_PARENT" ' |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 268 | 'android:value="%s"/>\n') % logging_parent |
| 269 | attrs += ' <application>\n %s </application>\n' % meta_text |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 270 | |
| 271 | return attrs |
| 272 | |
| 273 | def test_no_logging_parent(self): |
| 274 | """Tests manifest_fixer with no logging_parent.""" |
| 275 | manifest_input = self.manifest_tmpl % '' |
| 276 | expected = self.manifest_tmpl % self.uses_logging_parent() |
| 277 | output = self.add_logging_parent_test(manifest_input) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 278 | self.assert_xml_equal(output, expected) |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 279 | |
| 280 | def test_logging_parent(self): |
| 281 | """Tests manifest_fixer with no logging_parent.""" |
| 282 | manifest_input = self.manifest_tmpl % '' |
| 283 | expected = self.manifest_tmpl % self.uses_logging_parent('FOO') |
| 284 | output = self.add_logging_parent_test(manifest_input, 'FOO') |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 285 | self.assert_xml_equal(output, expected) |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 286 | |
| 287 | |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 288 | class AddUsesLibrariesTest(unittest.TestCase): |
| 289 | """Unit tests for add_uses_libraries function.""" |
| 290 | |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 291 | def assert_xml_equal(self, output, expected): |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 292 | self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected)) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 293 | |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 294 | def run_test(self, input_manifest, new_uses_libraries): |
| 295 | doc = minidom.parseString(input_manifest) |
Jaewoong Jung | e4948c7 | 2019-05-14 08:06:16 -0700 | [diff] [blame] | 296 | manifest_fixer.add_uses_libraries(doc, new_uses_libraries, True) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 297 | output = io.StringIO() |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 298 | manifest_fixer.write_xml(output, doc) |
| 299 | return output.getvalue() |
| 300 | |
| 301 | manifest_tmpl = ( |
| 302 | '<?xml version="1.0" encoding="utf-8"?>\n' |
| 303 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 304 | '%s' |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 305 | '</manifest>\n') |
| 306 | |
| 307 | def uses_libraries(self, name_required_pairs): |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 308 | ret = ' <application>\n' |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 309 | for name, required in name_required_pairs: |
| 310 | ret += ( |
| 311 | ' <uses-library android:name="%s" android:required="%s"/>\n' |
| 312 | ) % (name, required) |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 313 | ret += ' </application>\n' |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 314 | return ret |
| 315 | |
| 316 | def test_empty(self): |
| 317 | """Empty new_uses_libraries must not touch the manifest.""" |
| 318 | manifest_input = self.manifest_tmpl % self.uses_libraries([ |
| 319 | ('foo', 'true'), |
| 320 | ('bar', 'false')]) |
| 321 | expected = manifest_input |
| 322 | output = self.run_test(manifest_input, []) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 323 | self.assert_xml_equal(output, expected) |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 324 | |
| 325 | def test_not_overwrite(self): |
| 326 | """new_uses_libraries must not overwrite existing tags.""" |
| 327 | manifest_input = self.manifest_tmpl % self.uses_libraries([ |
| 328 | ('foo', 'true'), |
| 329 | ('bar', 'false')]) |
| 330 | expected = manifest_input |
| 331 | output = self.run_test(manifest_input, ['foo', 'bar']) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 332 | self.assert_xml_equal(output, expected) |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 333 | |
| 334 | def test_add(self): |
| 335 | """New names are added with 'required:true'.""" |
| 336 | manifest_input = self.manifest_tmpl % self.uses_libraries([ |
| 337 | ('foo', 'true'), |
| 338 | ('bar', 'false')]) |
| 339 | expected = self.manifest_tmpl % self.uses_libraries([ |
| 340 | ('foo', 'true'), |
| 341 | ('bar', 'false'), |
| 342 | ('baz', 'true'), |
| 343 | ('qux', 'true')]) |
| 344 | output = self.run_test(manifest_input, ['bar', 'baz', 'qux']) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 345 | self.assert_xml_equal(output, expected) |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 346 | |
| 347 | def test_no_application(self): |
| 348 | """When there is no <application> tag, the tag is added.""" |
| 349 | manifest_input = ( |
| 350 | '<?xml version="1.0" encoding="utf-8"?>\n' |
| 351 | '<manifest xmlns:android=' |
| 352 | '"http://schemas.android.com/apk/res/android">\n' |
| 353 | '</manifest>\n') |
| 354 | expected = self.manifest_tmpl % self.uses_libraries([ |
| 355 | ('foo', 'true'), |
| 356 | ('bar', 'true')]) |
| 357 | output = self.run_test(manifest_input, ['foo', 'bar']) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 358 | self.assert_xml_equal(output, expected) |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 359 | |
| 360 | def test_empty_application(self): |
| 361 | """Even when here is an empty <application/> tag, the libs are added.""" |
| 362 | manifest_input = ( |
| 363 | '<?xml version="1.0" encoding="utf-8"?>\n' |
| 364 | '<manifest xmlns:android=' |
| 365 | '"http://schemas.android.com/apk/res/android">\n' |
| 366 | ' <application/>\n' |
| 367 | '</manifest>\n') |
| 368 | expected = self.manifest_tmpl % self.uses_libraries([ |
| 369 | ('foo', 'true'), |
| 370 | ('bar', 'true')]) |
| 371 | output = self.run_test(manifest_input, ['foo', 'bar']) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 372 | self.assert_xml_equal(output, expected) |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 373 | |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 374 | def test_multiple_application(self): |
| 375 | """When there are multiple applications, the libs are added to each.""" |
| 376 | manifest_input = self.manifest_tmpl % ( |
| 377 | self.uses_libraries([('foo', 'false')]) + |
| 378 | self.uses_libraries([('bar', 'false')])) |
| 379 | expected = self.manifest_tmpl % ( |
| 380 | self.uses_libraries([('foo', 'false'), ('bar', 'true')]) + |
| 381 | self.uses_libraries([('bar', 'false'), ('foo', 'true')])) |
| 382 | output = self.run_test(manifest_input, ['foo', 'bar']) |
| 383 | self.assert_xml_equal(output, expected) |
| 384 | |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 385 | |
David Brazdil | d5b7499 | 2018-08-28 12:41:01 +0100 | [diff] [blame] | 386 | class AddUsesNonSdkApiTest(unittest.TestCase): |
| 387 | """Unit tests for add_uses_libraries function.""" |
| 388 | |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 389 | def assert_xml_equal(self, output, expected): |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 390 | self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected)) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 391 | |
David Brazdil | d5b7499 | 2018-08-28 12:41:01 +0100 | [diff] [blame] | 392 | def run_test(self, input_manifest): |
| 393 | doc = minidom.parseString(input_manifest) |
| 394 | manifest_fixer.add_uses_non_sdk_api(doc) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 395 | output = io.StringIO() |
David Brazdil | d5b7499 | 2018-08-28 12:41:01 +0100 | [diff] [blame] | 396 | manifest_fixer.write_xml(output, doc) |
| 397 | return output.getvalue() |
| 398 | |
| 399 | manifest_tmpl = ( |
| 400 | '<?xml version="1.0" encoding="utf-8"?>\n' |
| 401 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 402 | ' %s\n' |
David Brazdil | d5b7499 | 2018-08-28 12:41:01 +0100 | [diff] [blame] | 403 | '</manifest>\n') |
| 404 | |
| 405 | def uses_non_sdk_api(self, value): |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 406 | return '<application %s/>' % ('android:usesNonSdkApi="true"' if value else '') |
David Brazdil | d5b7499 | 2018-08-28 12:41:01 +0100 | [diff] [blame] | 407 | |
| 408 | def test_set_true(self): |
| 409 | """Empty new_uses_libraries must not touch the manifest.""" |
| 410 | manifest_input = self.manifest_tmpl % self.uses_non_sdk_api(False) |
| 411 | expected = self.manifest_tmpl % self.uses_non_sdk_api(True) |
| 412 | output = self.run_test(manifest_input) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 413 | self.assert_xml_equal(output, expected) |
David Brazdil | d5b7499 | 2018-08-28 12:41:01 +0100 | [diff] [blame] | 414 | |
| 415 | def test_already_set(self): |
| 416 | """new_uses_libraries must not overwrite existing tags.""" |
| 417 | manifest_input = self.manifest_tmpl % self.uses_non_sdk_api(True) |
| 418 | expected = manifest_input |
| 419 | output = self.run_test(manifest_input) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 420 | self.assert_xml_equal(output, expected) |
David Brazdil | d5b7499 | 2018-08-28 12:41:01 +0100 | [diff] [blame] | 421 | |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 422 | def test_multiple_applications(self): |
| 423 | """new_uses_libraries must be added to all applications.""" |
| 424 | manifest_input = self.manifest_tmpl % (self.uses_non_sdk_api(True) + self.uses_non_sdk_api(False)) |
| 425 | expected = self.manifest_tmpl % (self.uses_non_sdk_api(True) + self.uses_non_sdk_api(True)) |
| 426 | output = self.run_test(manifest_input) |
| 427 | self.assert_xml_equal(output, expected) |
| 428 | |
David Brazdil | d5b7499 | 2018-08-28 12:41:01 +0100 | [diff] [blame] | 429 | |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 430 | class UseEmbeddedDexTest(unittest.TestCase): |
| 431 | """Unit tests for add_use_embedded_dex function.""" |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 432 | |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 433 | def assert_xml_equal(self, output, expected): |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 434 | self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected)) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 435 | |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 436 | def run_test(self, input_manifest): |
| 437 | doc = minidom.parseString(input_manifest) |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 438 | manifest_fixer.add_use_embedded_dex(doc) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 439 | output = io.StringIO() |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 440 | manifest_fixer.write_xml(output, doc) |
| 441 | return output.getvalue() |
| 442 | |
| 443 | manifest_tmpl = ( |
| 444 | '<?xml version="1.0" encoding="utf-8"?>\n' |
| 445 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 446 | ' %s\n' |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 447 | '</manifest>\n') |
| 448 | |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 449 | def use_embedded_dex(self, value): |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 450 | return '<application android:useEmbeddedDex="%s" />' % value |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 451 | |
| 452 | def test_manifest_with_undeclared_preference(self): |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 453 | manifest_input = self.manifest_tmpl % '<application/>' |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 454 | expected = self.manifest_tmpl % self.use_embedded_dex('true') |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 455 | output = self.run_test(manifest_input) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 456 | self.assert_xml_equal(output, expected) |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 457 | |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 458 | def test_manifest_with_use_embedded_dex(self): |
| 459 | manifest_input = self.manifest_tmpl % self.use_embedded_dex('true') |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 460 | expected = manifest_input |
| 461 | output = self.run_test(manifest_input) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 462 | self.assert_xml_equal(output, expected) |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 463 | |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 464 | def test_manifest_with_not_use_embedded_dex(self): |
| 465 | manifest_input = self.manifest_tmpl % self.use_embedded_dex('false') |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 466 | self.assertRaises(RuntimeError, self.run_test, manifest_input) |
| 467 | |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 468 | def test_multiple_applications(self): |
| 469 | manifest_input = self.manifest_tmpl % ( |
| 470 | self.use_embedded_dex('true') + |
| 471 | '<application/>' |
| 472 | ) |
| 473 | expected = self.manifest_tmpl % ( |
| 474 | self.use_embedded_dex('true') + |
| 475 | self.use_embedded_dex('true') |
| 476 | ) |
| 477 | output = self.run_test(manifest_input) |
| 478 | self.assert_xml_equal(output, expected) |
| 479 | |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 480 | |
| 481 | class AddExtractNativeLibsTest(unittest.TestCase): |
| 482 | """Unit tests for add_extract_native_libs function.""" |
| 483 | |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 484 | def assert_xml_equal(self, output, expected): |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 485 | self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected)) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 486 | |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 487 | def run_test(self, input_manifest, value): |
| 488 | doc = minidom.parseString(input_manifest) |
| 489 | manifest_fixer.add_extract_native_libs(doc, value) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 490 | output = io.StringIO() |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 491 | manifest_fixer.write_xml(output, doc) |
| 492 | return output.getvalue() |
| 493 | |
| 494 | manifest_tmpl = ( |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 495 | '<?xml version="1.0" encoding="utf-8"?>\n' |
| 496 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 497 | ' %s\n' |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 498 | '</manifest>\n') |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 499 | |
| 500 | def extract_native_libs(self, value): |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 501 | return '<application android:extractNativeLibs="%s" />' % value |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 502 | |
| 503 | def test_set_true(self): |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 504 | manifest_input = self.manifest_tmpl % '<application/>' |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 505 | expected = self.manifest_tmpl % self.extract_native_libs('true') |
| 506 | output = self.run_test(manifest_input, True) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 507 | self.assert_xml_equal(output, expected) |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 508 | |
| 509 | def test_set_false(self): |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 510 | manifest_input = self.manifest_tmpl % '<application/>' |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 511 | expected = self.manifest_tmpl % self.extract_native_libs('false') |
| 512 | output = self.run_test(manifest_input, False) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 513 | self.assert_xml_equal(output, expected) |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 514 | |
| 515 | def test_match(self): |
| 516 | manifest_input = self.manifest_tmpl % self.extract_native_libs('true') |
| 517 | expected = manifest_input |
| 518 | output = self.run_test(manifest_input, True) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 519 | self.assert_xml_equal(output, expected) |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 520 | |
| 521 | def test_conflict(self): |
Jiyong Park | d044bb4 | 2024-05-15 02:09:54 +0900 | [diff] [blame] | 522 | manifest_input = self.manifest_tmpl % self.extract_native_libs('true') |
| 523 | self.assertRaises(RuntimeError, self.run_test, manifest_input, False) |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 524 | |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 525 | def test_multiple_applications(self): |
| 526 | manifest_input = self.manifest_tmpl % (self.extract_native_libs('true') + '<application/>') |
| 527 | expected = self.manifest_tmpl % (self.extract_native_libs('true') + self.extract_native_libs('true')) |
| 528 | output = self.run_test(manifest_input, True) |
| 529 | self.assert_xml_equal(output, expected) |
| 530 | |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 531 | |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 532 | class AddNoCodeApplicationTest(unittest.TestCase): |
| 533 | """Unit tests for set_has_code_to_false function.""" |
| 534 | |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 535 | def assert_xml_equal(self, output, expected): |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 536 | self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected)) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 537 | |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 538 | def run_test(self, input_manifest): |
| 539 | doc = minidom.parseString(input_manifest) |
| 540 | manifest_fixer.set_has_code_to_false(doc) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 541 | output = io.StringIO() |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 542 | manifest_fixer.write_xml(output, doc) |
| 543 | return output.getvalue() |
| 544 | |
| 545 | manifest_tmpl = ( |
| 546 | '<?xml version="1.0" encoding="utf-8"?>\n' |
| 547 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' |
| 548 | '%s' |
| 549 | '</manifest>\n') |
| 550 | |
| 551 | def test_no_application(self): |
| 552 | manifest_input = self.manifest_tmpl % '' |
| 553 | expected = self.manifest_tmpl % ' <application android:hasCode="false"/>\n' |
| 554 | output = self.run_test(manifest_input) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 555 | self.assert_xml_equal(output, expected) |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 556 | |
| 557 | def test_has_application_no_has_code(self): |
| 558 | manifest_input = self.manifest_tmpl % ' <application/>\n' |
| 559 | expected = self.manifest_tmpl % ' <application android:hasCode="false"/>\n' |
| 560 | output = self.run_test(manifest_input) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 561 | self.assert_xml_equal(output, expected) |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 562 | |
| 563 | def test_has_application_has_code_false(self): |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 564 | """ Do nothing if there's already an application element. """ |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 565 | manifest_input = self.manifest_tmpl % ' <application android:hasCode="false"/>\n' |
| 566 | output = self.run_test(manifest_input) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 567 | self.assert_xml_equal(output, manifest_input) |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 568 | |
| 569 | def test_has_application_has_code_true(self): |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 570 | """ Do nothing if there's already an application element even if its |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 571 | hasCode attribute is true. """ |
| 572 | manifest_input = self.manifest_tmpl % ' <application android:hasCode="true"/>\n' |
| 573 | output = self.run_test(manifest_input) |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 574 | self.assert_xml_equal(output, manifest_input) |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 575 | |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 576 | def test_multiple_applications(self): |
| 577 | """ Apply to all applications """ |
| 578 | manifest_input = self.manifest_tmpl % ( |
| 579 | ' <application android:hasCode="true" />\n' + |
| 580 | ' <application android:hasCode="false" />\n' + |
| 581 | ' <application/>\n') |
| 582 | expected = self.manifest_tmpl % ( |
| 583 | ' <application android:hasCode="true" />\n' + |
| 584 | ' <application android:hasCode="false" />\n' + |
| 585 | ' <application android:hasCode="false" />\n') |
| 586 | output = self.run_test(manifest_input) |
| 587 | self.assert_xml_equal(output, expected) |
| 588 | |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 589 | |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 590 | class AddTestOnlyApplicationTest(unittest.TestCase): |
| 591 | """Unit tests for set_test_only_flag_to_true function.""" |
| 592 | |
| 593 | def assert_xml_equal(self, output, expected): |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 594 | self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected)) |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 595 | |
| 596 | def run_test(self, input_manifest): |
| 597 | doc = minidom.parseString(input_manifest) |
| 598 | manifest_fixer.set_test_only_flag_to_true(doc) |
| 599 | output = io.StringIO() |
| 600 | manifest_fixer.write_xml(output, doc) |
| 601 | return output.getvalue() |
| 602 | |
| 603 | manifest_tmpl = ( |
| 604 | '<?xml version="1.0" encoding="utf-8"?>\n' |
| 605 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' |
| 606 | '%s' |
| 607 | '</manifest>\n') |
| 608 | |
| 609 | def test_no_application(self): |
| 610 | manifest_input = self.manifest_tmpl % '' |
| 611 | expected = self.manifest_tmpl % ' <application android:testOnly="true"/>\n' |
| 612 | output = self.run_test(manifest_input) |
| 613 | self.assert_xml_equal(output, expected) |
| 614 | |
| 615 | def test_has_application_no_test_only(self): |
| 616 | manifest_input = self.manifest_tmpl % ' <application/>\n' |
| 617 | expected = self.manifest_tmpl % ' <application android:testOnly="true"/>\n' |
| 618 | output = self.run_test(manifest_input) |
| 619 | self.assert_xml_equal(output, expected) |
| 620 | |
| 621 | def test_has_application_test_only_true(self): |
| 622 | """ If there's already an application element.""" |
| 623 | manifest_input = self.manifest_tmpl % ' <application android:testOnly="true"/>\n' |
| 624 | output = self.run_test(manifest_input) |
| 625 | self.assert_xml_equal(output, manifest_input) |
| 626 | |
| 627 | def test_has_application_test_only_false(self): |
| 628 | """ If there's already an application element with the testOnly attribute as false.""" |
| 629 | manifest_input = self.manifest_tmpl % ' <application android:testOnly="false"/>\n' |
| 630 | output = self.run_test(manifest_input) |
| 631 | self.assert_xml_equal(output, manifest_input) |
| 632 | |
Colin Cross | e1ab849 | 2024-09-11 13:28:16 -0700 | [diff] [blame] | 633 | def test_multiple_applications(self): |
| 634 | manifest_input = self.manifest_tmpl % ( |
| 635 | ' <application android:testOnly="true" />\n' + |
| 636 | ' <application android:testOnly="false" />\n' + |
| 637 | ' <application/>\n' |
| 638 | ) |
| 639 | expected = self.manifest_tmpl % ( |
| 640 | ' <application android:testOnly="true" />\n' + |
| 641 | ' <application android:testOnly="false" />\n' + |
| 642 | ' <application android:testOnly="true" />\n' |
| 643 | ) |
| 644 | output = self.run_test(manifest_input) |
| 645 | self.assert_xml_equal(output, expected) |
| 646 | |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 647 | |
| 648 | class SetMaxSdkVersionTest(unittest.TestCase): |
| 649 | """Unit tests for set_max_sdk_version function.""" |
| 650 | |
| 651 | def assert_xml_equal(self, output, expected): |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 652 | self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected)) |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 653 | |
| 654 | def run_test(self, input_manifest, max_sdk_version): |
| 655 | doc = minidom.parseString(input_manifest) |
| 656 | manifest_fixer.set_max_sdk_version(doc, max_sdk_version) |
| 657 | output = io.StringIO() |
| 658 | manifest_fixer.write_xml(output, doc) |
| 659 | return output.getvalue() |
| 660 | |
| 661 | manifest_tmpl = ( |
| 662 | '<?xml version="1.0" encoding="utf-8"?>\n' |
| 663 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' |
| 664 | '%s' |
| 665 | '</manifest>\n') |
| 666 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 667 | def permission(self, max_sdk=None): |
| 668 | if max_sdk is None: |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 669 | return ' <permission/>' |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 670 | return ' <permission android:maxSdkVersion="%s"/>\n' % max_sdk |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 671 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 672 | def uses_permission(self, max_sdk=None): |
| 673 | if max_sdk is None: |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 674 | return ' <uses-permission/>' |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 675 | return ' <uses-permission android:maxSdkVersion="%s"/>\n' % max_sdk |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 676 | |
| 677 | def test_permission_no_max_sdk_version(self): |
| 678 | """Tests if permission has no maxSdkVersion attribute""" |
| 679 | manifest_input = self.manifest_tmpl % self.permission() |
| 680 | expected = self.manifest_tmpl % self.permission() |
| 681 | output = self.run_test(manifest_input, '9000') |
| 682 | self.assert_xml_equal(output, expected) |
| 683 | |
| 684 | def test_permission_max_sdk_version_changed(self): |
| 685 | """Tests if permission maxSdkVersion attribute is set to current""" |
| 686 | manifest_input = self.manifest_tmpl % self.permission('current') |
| 687 | expected = self.manifest_tmpl % self.permission(9000) |
| 688 | output = self.run_test(manifest_input, '9000') |
| 689 | self.assert_xml_equal(output, expected) |
| 690 | |
| 691 | def test_permission_max_sdk_version_not_changed(self): |
| 692 | """Tests if permission maxSdkVersion attribute is not set to current""" |
| 693 | manifest_input = self.manifest_tmpl % self.permission(30) |
| 694 | expected = self.manifest_tmpl % self.permission(30) |
| 695 | output = self.run_test(manifest_input, '9000') |
| 696 | self.assert_xml_equal(output, expected) |
| 697 | |
| 698 | def test_uses_permission_no_max_sdk_version(self): |
| 699 | """Tests if uses-permission has no maxSdkVersion attribute""" |
| 700 | manifest_input = self.manifest_tmpl % self.uses_permission() |
| 701 | expected = self.manifest_tmpl % self.uses_permission() |
| 702 | output = self.run_test(manifest_input, '9000') |
| 703 | self.assert_xml_equal(output, expected) |
| 704 | |
| 705 | def test_uses_permission_max_sdk_version_changed(self): |
| 706 | """Tests if uses-permission maxSdkVersion attribute is set to current""" |
| 707 | manifest_input = self.manifest_tmpl % self.uses_permission('current') |
| 708 | expected = self.manifest_tmpl % self.uses_permission(9000) |
| 709 | output = self.run_test(manifest_input, '9000') |
| 710 | self.assert_xml_equal(output, expected) |
| 711 | |
| 712 | def test_uses_permission_max_sdk_version_not_changed(self): |
| 713 | """Tests if uses-permission maxSdkVersion attribute is not set to current""" |
| 714 | manifest_input = self.manifest_tmpl % self.uses_permission(30) |
| 715 | expected = self.manifest_tmpl % self.uses_permission(30) |
| 716 | output = self.run_test(manifest_input, '9000') |
| 717 | self.assert_xml_equal(output, expected) |
| 718 | |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 719 | |
Alexei Nicoara | 69cf0f3 | 2022-07-27 14:59:18 +0100 | [diff] [blame] | 720 | class OverrideDefaultVersionTest(unittest.TestCase): |
| 721 | """Unit tests for override_default_version function.""" |
| 722 | |
| 723 | def assert_xml_equal(self, output, expected): |
Colin Cross | 6cb462b | 2024-09-11 11:34:35 -0700 | [diff] [blame] | 724 | self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected)) |
Alexei Nicoara | 69cf0f3 | 2022-07-27 14:59:18 +0100 | [diff] [blame] | 725 | |
| 726 | def run_test(self, input_manifest, version): |
| 727 | doc = minidom.parseString(input_manifest) |
| 728 | manifest_fixer.override_placeholder_version(doc, version) |
| 729 | output = io.StringIO() |
| 730 | manifest_fixer.write_xml(output, doc) |
| 731 | return output.getvalue() |
| 732 | |
| 733 | manifest_tmpl = ( |
| 734 | '<?xml version="1.0" encoding="utf-8"?>\n' |
| 735 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android" ' |
| 736 | 'android:versionCode="%s">\n' |
| 737 | '</manifest>\n') |
| 738 | |
| 739 | def test_doesnt_override_existing_version(self): |
| 740 | """Tests that an existing version is not overridden""" |
| 741 | manifest_input = self.manifest_tmpl % '12345' |
| 742 | expected = manifest_input |
| 743 | output = self.run_test(manifest_input, '67890') |
| 744 | self.assert_xml_equal(output, expected) |
| 745 | |
| 746 | def test_overrides_default_version(self): |
| 747 | """Tests that a default version is overridden""" |
| 748 | manifest_input = self.manifest_tmpl % '0' |
| 749 | expected = self.manifest_tmpl % '67890' |
| 750 | output = self.run_test(manifest_input, '67890') |
| 751 | self.assert_xml_equal(output, expected) |
| 752 | |
| 753 | |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 754 | if __name__ == '__main__': |
Colin Cross | 4af387c | 2019-05-16 13:16:29 -0700 | [diff] [blame] | 755 | unittest.main(verbosity=2) |