| 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 | # | 
|  | 17 | """Unit tests for manifest_fixer_test.py.""" | 
|  | 18 |  | 
|  | 19 | import StringIO | 
|  | 20 | import sys | 
|  | 21 | import unittest | 
|  | 22 | from xml.dom import minidom | 
|  | 23 |  | 
|  | 24 | import manifest_fixer | 
|  | 25 |  | 
|  | 26 | sys.dont_write_bytecode = True | 
|  | 27 |  | 
|  | 28 |  | 
|  | 29 | class CompareVersionGtTest(unittest.TestCase): | 
|  | 30 | """Unit tests for compare_version_gt function.""" | 
|  | 31 |  | 
|  | 32 | def test_sdk(self): | 
|  | 33 | """Test comparing sdk versions.""" | 
|  | 34 | self.assertTrue(manifest_fixer.compare_version_gt('28', '27')) | 
|  | 35 | self.assertFalse(manifest_fixer.compare_version_gt('27', '28')) | 
|  | 36 | self.assertFalse(manifest_fixer.compare_version_gt('28', '28')) | 
|  | 37 |  | 
|  | 38 | def test_codename(self): | 
|  | 39 | """Test comparing codenames.""" | 
|  | 40 | self.assertTrue(manifest_fixer.compare_version_gt('Q', 'P')) | 
|  | 41 | self.assertFalse(manifest_fixer.compare_version_gt('P', 'Q')) | 
|  | 42 | self.assertFalse(manifest_fixer.compare_version_gt('Q', 'Q')) | 
|  | 43 |  | 
|  | 44 | def test_sdk_codename(self): | 
|  | 45 | """Test comparing sdk versions with codenames.""" | 
|  | 46 | self.assertTrue(manifest_fixer.compare_version_gt('Q', '28')) | 
|  | 47 | self.assertFalse(manifest_fixer.compare_version_gt('28', 'Q')) | 
|  | 48 |  | 
|  | 49 | def test_compare_numeric(self): | 
|  | 50 | """Test that numbers are compared in numeric and not lexicographic order.""" | 
|  | 51 | self.assertTrue(manifest_fixer.compare_version_gt('18', '8')) | 
|  | 52 |  | 
|  | 53 |  | 
|  | 54 | class RaiseMinSdkVersionTest(unittest.TestCase): | 
|  | 55 | """Unit tests for raise_min_sdk_version function.""" | 
|  | 56 |  | 
|  | 57 | def raise_min_sdk_version_test(self, input_manifest, min_sdk_version): | 
|  | 58 | doc = minidom.parseString(input_manifest) | 
|  | 59 | manifest_fixer.raise_min_sdk_version(doc, min_sdk_version) | 
|  | 60 | output = StringIO.StringIO() | 
|  | 61 | manifest_fixer.write_xml(output, doc) | 
|  | 62 | return output.getvalue() | 
|  | 63 |  | 
|  | 64 | manifest_tmpl = ( | 
|  | 65 | '<?xml version="1.0" encoding="utf-8"?>\n' | 
|  | 66 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' | 
|  | 67 | '%s' | 
|  | 68 | '</manifest>\n') | 
|  | 69 |  | 
|  | 70 | def uses_sdk(self, v, extra=''): | 
|  | 71 | if extra: | 
|  | 72 | extra = ' ' + extra | 
|  | 73 | return '    <uses-sdk android:minSdkVersion="%s"%s/>\n' % (v, extra) | 
|  | 74 |  | 
|  | 75 | def test_no_uses_sdk(self): | 
|  | 76 | """Tests inserting a uses-sdk element into a manifest.""" | 
|  | 77 |  | 
|  | 78 | manifest_input = self.manifest_tmpl % '' | 
|  | 79 | expected = self.manifest_tmpl % self.uses_sdk('28') | 
|  | 80 | output = self.raise_min_sdk_version_test(manifest_input, '28') | 
|  | 81 | self.assertEqual(output, expected) | 
|  | 82 |  | 
|  | 83 | def test_no_min(self): | 
|  | 84 | """Tests inserting a minSdkVersion attribute into a uses-sdk element.""" | 
|  | 85 |  | 
|  | 86 | manifest_input = self.manifest_tmpl % '    <uses-sdk extra="foo"/>\n' | 
|  | 87 | expected = self.manifest_tmpl % self.uses_sdk('28', 'extra="foo"') | 
|  | 88 | output = self.raise_min_sdk_version_test(manifest_input, '28') | 
|  | 89 | self.assertEqual(output, expected) | 
|  | 90 |  | 
|  | 91 | def test_raise_min(self): | 
|  | 92 | """Tests inserting a minSdkVersion attribute into a uses-sdk element.""" | 
|  | 93 |  | 
|  | 94 | manifest_input = self.manifest_tmpl % self.uses_sdk('27') | 
|  | 95 | expected = self.manifest_tmpl % self.uses_sdk('28') | 
|  | 96 | output = self.raise_min_sdk_version_test(manifest_input, '28') | 
|  | 97 | self.assertEqual(output, expected) | 
|  | 98 |  | 
|  | 99 | def test_raise(self): | 
|  | 100 | """Tests raising a minSdkVersion attribute.""" | 
|  | 101 |  | 
|  | 102 | manifest_input = self.manifest_tmpl % self.uses_sdk('27') | 
|  | 103 | expected = self.manifest_tmpl % self.uses_sdk('28') | 
|  | 104 | output = self.raise_min_sdk_version_test(manifest_input, '28') | 
|  | 105 | self.assertEqual(output, expected) | 
|  | 106 |  | 
|  | 107 | def test_no_raise_min(self): | 
|  | 108 | """Tests a minSdkVersion that doesn't need raising.""" | 
|  | 109 |  | 
|  | 110 | manifest_input = self.manifest_tmpl % self.uses_sdk('28') | 
|  | 111 | expected = manifest_input | 
|  | 112 | output = self.raise_min_sdk_version_test(manifest_input, '27') | 
|  | 113 | self.assertEqual(output, expected) | 
|  | 114 |  | 
|  | 115 | def test_raise_codename(self): | 
|  | 116 | """Tests raising a minSdkVersion attribute to a codename.""" | 
|  | 117 |  | 
|  | 118 | manifest_input = self.manifest_tmpl % self.uses_sdk('28') | 
|  | 119 | expected = self.manifest_tmpl % self.uses_sdk('P') | 
|  | 120 | output = self.raise_min_sdk_version_test(manifest_input, 'P') | 
|  | 121 | self.assertEqual(output, expected) | 
|  | 122 |  | 
|  | 123 | def test_no_raise_codename(self): | 
|  | 124 | """Tests a minSdkVersion codename that doesn't need raising.""" | 
|  | 125 |  | 
|  | 126 | manifest_input = self.manifest_tmpl % self.uses_sdk('P') | 
|  | 127 | expected = manifest_input | 
|  | 128 | output = self.raise_min_sdk_version_test(manifest_input, '28') | 
|  | 129 | self.assertEqual(output, expected) | 
|  | 130 |  | 
|  | 131 | def test_extra(self): | 
|  | 132 | """Tests that extra attributes and elements are maintained.""" | 
|  | 133 |  | 
|  | 134 | manifest_input = self.manifest_tmpl % ( | 
|  | 135 | '    <!-- comment -->\n' | 
|  | 136 | '    <uses-sdk android:minSdkVersion="27" extra="foo"/>\n' | 
|  | 137 | '    <application/>\n') | 
|  | 138 |  | 
|  | 139 | expected = self.manifest_tmpl % ( | 
|  | 140 | '    <!-- comment -->\n' | 
|  | 141 | '    <uses-sdk android:minSdkVersion="28" extra="foo"/>\n' | 
|  | 142 | '    <application/>\n') | 
|  | 143 |  | 
|  | 144 | output = self.raise_min_sdk_version_test(manifest_input, '28') | 
|  | 145 |  | 
|  | 146 | self.assertEqual(output, expected) | 
|  | 147 |  | 
|  | 148 | def test_indent(self): | 
|  | 149 | """Tests that an inserted element copies the existing indentation.""" | 
|  | 150 |  | 
|  | 151 | manifest_input = self.manifest_tmpl % '  <!-- comment -->\n' | 
|  | 152 |  | 
|  | 153 | expected = self.manifest_tmpl % ( | 
|  | 154 | '  <uses-sdk android:minSdkVersion="28"/>\n' | 
|  | 155 | '  <!-- comment -->\n') | 
|  | 156 |  | 
|  | 157 | output = self.raise_min_sdk_version_test(manifest_input, '28') | 
|  | 158 |  | 
|  | 159 | self.assertEqual(output, expected) | 
|  | 160 |  | 
| Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 161 |  | 
|  | 162 | class AddUsesLibrariesTest(unittest.TestCase): | 
|  | 163 | """Unit tests for add_uses_libraries function.""" | 
|  | 164 |  | 
|  | 165 | def run_test(self, input_manifest, new_uses_libraries): | 
|  | 166 | doc = minidom.parseString(input_manifest) | 
|  | 167 | manifest_fixer.add_uses_libraries(doc, new_uses_libraries) | 
|  | 168 | output = StringIO.StringIO() | 
|  | 169 | manifest_fixer.write_xml(output, doc) | 
|  | 170 | return output.getvalue() | 
|  | 171 |  | 
|  | 172 | manifest_tmpl = ( | 
|  | 173 | '<?xml version="1.0" encoding="utf-8"?>\n' | 
|  | 174 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' | 
|  | 175 | '    <application>\n' | 
|  | 176 | '%s' | 
|  | 177 | '    </application>\n' | 
|  | 178 | '</manifest>\n') | 
|  | 179 |  | 
|  | 180 | def uses_libraries(self, name_required_pairs): | 
|  | 181 | ret = '' | 
|  | 182 | for name, required in name_required_pairs: | 
|  | 183 | ret += ( | 
|  | 184 | '        <uses-library android:name="%s" android:required="%s"/>\n' | 
|  | 185 | ) % (name, required) | 
|  | 186 |  | 
|  | 187 | return ret | 
|  | 188 |  | 
|  | 189 | def test_empty(self): | 
|  | 190 | """Empty new_uses_libraries must not touch the manifest.""" | 
|  | 191 | manifest_input = self.manifest_tmpl % self.uses_libraries([ | 
|  | 192 | ('foo', 'true'), | 
|  | 193 | ('bar', 'false')]) | 
|  | 194 | expected = manifest_input | 
|  | 195 | output = self.run_test(manifest_input, []) | 
|  | 196 | self.assertEqual(output, expected) | 
|  | 197 |  | 
|  | 198 | def test_not_overwrite(self): | 
|  | 199 | """new_uses_libraries must not overwrite existing tags.""" | 
|  | 200 | manifest_input = self.manifest_tmpl % self.uses_libraries([ | 
|  | 201 | ('foo', 'true'), | 
|  | 202 | ('bar', 'false')]) | 
|  | 203 | expected = manifest_input | 
|  | 204 | output = self.run_test(manifest_input, ['foo', 'bar']) | 
|  | 205 | self.assertEqual(output, expected) | 
|  | 206 |  | 
|  | 207 | def test_add(self): | 
|  | 208 | """New names are added with 'required:true'.""" | 
|  | 209 | manifest_input = self.manifest_tmpl % self.uses_libraries([ | 
|  | 210 | ('foo', 'true'), | 
|  | 211 | ('bar', 'false')]) | 
|  | 212 | expected = self.manifest_tmpl % self.uses_libraries([ | 
|  | 213 | ('foo', 'true'), | 
|  | 214 | ('bar', 'false'), | 
|  | 215 | ('baz', 'true'), | 
|  | 216 | ('qux', 'true')]) | 
|  | 217 | output = self.run_test(manifest_input, ['bar', 'baz', 'qux']) | 
|  | 218 | self.assertEqual(output, expected) | 
|  | 219 |  | 
|  | 220 | def test_no_application(self): | 
|  | 221 | """When there is no <application> tag, the tag is added.""" | 
|  | 222 | manifest_input = ( | 
|  | 223 | '<?xml version="1.0" encoding="utf-8"?>\n' | 
|  | 224 | '<manifest xmlns:android=' | 
|  | 225 | '"http://schemas.android.com/apk/res/android">\n' | 
|  | 226 | '</manifest>\n') | 
|  | 227 | expected = self.manifest_tmpl % self.uses_libraries([ | 
|  | 228 | ('foo', 'true'), | 
|  | 229 | ('bar', 'true')]) | 
|  | 230 | output = self.run_test(manifest_input, ['foo', 'bar']) | 
|  | 231 | self.assertEqual(output, expected) | 
|  | 232 |  | 
|  | 233 | def test_empty_application(self): | 
|  | 234 | """Even when here is an empty <application/> tag, the libs are added.""" | 
|  | 235 | manifest_input = ( | 
|  | 236 | '<?xml version="1.0" encoding="utf-8"?>\n' | 
|  | 237 | '<manifest xmlns:android=' | 
|  | 238 | '"http://schemas.android.com/apk/res/android">\n' | 
|  | 239 | '    <application/>\n' | 
|  | 240 | '</manifest>\n') | 
|  | 241 | expected = self.manifest_tmpl % self.uses_libraries([ | 
|  | 242 | ('foo', 'true'), | 
|  | 243 | ('bar', 'true')]) | 
|  | 244 | output = self.run_test(manifest_input, ['foo', 'bar']) | 
|  | 245 | self.assertEqual(output, expected) | 
|  | 246 |  | 
|  | 247 |  | 
| Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 248 | if __name__ == '__main__': | 
|  | 249 | unittest.main() |