Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | # Copyright 2015 The Bazel Authors. All rights reserved. |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 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 | |
| 15 | """Unit tests for stubify_application_manifest.""" |
| 16 | |
| 17 | import unittest |
| 18 | from xml.etree import ElementTree |
| 19 | |
| 20 | from tools.android.stubify_manifest import ANDROID |
| 21 | from tools.android.stubify_manifest import BadManifestException |
Alex Humesky | 5b815a5 | 2016-06-10 22:43:39 +0000 | [diff] [blame] | 22 | from tools.android.stubify_manifest import INSTANT_RUN_BOOTSTRAP_APPLICATION |
| 23 | from tools.android.stubify_manifest import MOBILE_INSTALL_STUB_APPLICATION |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 24 | from tools.android.stubify_manifest import READ_EXTERNAL_STORAGE |
Alex Humesky | 5b815a5 | 2016-06-10 22:43:39 +0000 | [diff] [blame] | 25 | from tools.android.stubify_manifest import StubifyInstantRun |
| 26 | from tools.android.stubify_manifest import StubifyMobileInstall |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | MANIFEST_WITH_APPLICATION = """ |
| 30 | <manifest |
| 31 | xmlns:android="http://schemas.android.com/apk/res/android" |
| 32 | package="com.google.package"> |
| 33 | <application android:name="old.application"> |
| 34 | </application> |
| 35 | </manifest> |
| 36 | """ |
| 37 | |
Lukacs Berki | f97a447 | 2015-11-13 16:13:17 +0000 | [diff] [blame] | 38 | MANIFEST_WITH_HASCODE = """ |
| 39 | <manifest |
| 40 | xmlns:android="http://schemas.android.com/apk/res/android" |
| 41 | package="com.google.package"> |
| 42 | <application android:name="old.application" android:hasCode="false"> |
| 43 | </application> |
| 44 | </manifest> |
| 45 | """ |
| 46 | |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 47 | MANIFEST_WITHOUT_APPLICATION = """ |
| 48 | <manifest |
| 49 | xmlns:android="http://schemas.android.com/apk/res/android" |
| 50 | package="com.google.package"> |
| 51 | </manifest> |
| 52 | """ |
| 53 | |
| 54 | MANIFEST_WITH_PERMISSION = """ |
| 55 | <manifest |
| 56 | xmlns:android="http://schemas.android.com/apk/res/android" |
| 57 | package="com.google.package"> |
| 58 | <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> |
| 59 | </manifest> |
| 60 | """ |
| 61 | |
| 62 | BAD_MANIFEST = """ |
| 63 | <b>Hello World!</b> |
| 64 | """ |
| 65 | |
| 66 | MULTIPLE_APPLICATIONS = """ |
| 67 | <manifest |
| 68 | xmlns:android="http://schemas.android.com/apk/res/android" |
| 69 | package="com.google.package"> |
| 70 | <application android:name="old.application"> |
| 71 | </application> |
| 72 | <application android:name="old.application"> |
| 73 | </application> |
| 74 | </manifest> |
| 75 | """ |
| 76 | |
| 77 | |
Alex Humesky | 3632f30 | 2017-01-26 23:05:28 +0000 | [diff] [blame] | 78 | NO_PACKAGE_MANIFEST = """ |
| 79 | <manifest |
| 80 | xmlns:android="http://schemas.android.com/apk/res/android"> |
| 81 | <application android:name="old.application"> |
| 82 | </application> |
| 83 | </manifest> |
| 84 | """ |
| 85 | |
| 86 | |
Alex Humesky | 5b815a5 | 2016-06-10 22:43:39 +0000 | [diff] [blame] | 87 | class StubifyMobileInstallTest(unittest.TestCase): |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 88 | |
| 89 | def GetApplication(self, manifest_string): |
| 90 | manifest = ElementTree.fromstring(manifest_string) |
| 91 | application = manifest.find("application") |
| 92 | return application.get("{%s}name" % ANDROID) |
| 93 | |
| 94 | def testReplacesOldApplication(self): |
Alex Humesky | 5b815a5 | 2016-06-10 22:43:39 +0000 | [diff] [blame] | 95 | new_manifest, old_application, app_pkg = StubifyMobileInstall( |
| 96 | MANIFEST_WITH_APPLICATION) |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 97 | self.assertEqual("com.google.package", app_pkg) |
| 98 | self.assertEqual("old.application", old_application) |
Alex Humesky | 5b815a5 | 2016-06-10 22:43:39 +0000 | [diff] [blame] | 99 | self.assertEqual( |
| 100 | MOBILE_INSTALL_STUB_APPLICATION, self.GetApplication(new_manifest)) |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 101 | |
| 102 | def testAddsNewAplication(self): |
| 103 | new_manifest, old_application, app_pkg = ( |
Alex Humesky | 5b815a5 | 2016-06-10 22:43:39 +0000 | [diff] [blame] | 104 | StubifyMobileInstall(MANIFEST_WITHOUT_APPLICATION)) |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 105 | self.assertEqual("com.google.package", app_pkg) |
| 106 | self.assertEqual("android.app.Application", old_application) |
Alex Humesky | 5b815a5 | 2016-06-10 22:43:39 +0000 | [diff] [blame] | 107 | self.assertEqual( |
| 108 | MOBILE_INSTALL_STUB_APPLICATION, self.GetApplication(new_manifest)) |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 109 | |
Lukacs Berki | f97a447 | 2015-11-13 16:13:17 +0000 | [diff] [blame] | 110 | def testRemovesHasCode(self): |
Alex Humesky | 5b815a5 | 2016-06-10 22:43:39 +0000 | [diff] [blame] | 111 | new_manifest, _, _ = StubifyMobileInstall(MANIFEST_WITH_HASCODE) |
Lukacs Berki | f97a447 | 2015-11-13 16:13:17 +0000 | [diff] [blame] | 112 | application = ElementTree.fromstring(new_manifest).find("application") |
| 113 | self.assertFalse(("{%s}hasCode" % ANDROID) in application.attrib) |
| 114 | |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 115 | def assertHasPermission(self, manifest_string, permission): |
| 116 | manifest = ElementTree.fromstring(manifest_string) |
| 117 | nodes = manifest.findall( |
| 118 | 'uses-permission[@android:name="%s"]' % permission, |
| 119 | namespaces={"android": ANDROID}) |
| 120 | self.assertEqual(1, len(nodes)) |
| 121 | |
| 122 | def testAddsPermission(self): |
| 123 | self.assertHasPermission( |
Alex Humesky | 5b815a5 | 2016-06-10 22:43:39 +0000 | [diff] [blame] | 124 | StubifyMobileInstall( |
| 125 | MANIFEST_WITH_APPLICATION)[0], READ_EXTERNAL_STORAGE) |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 126 | |
| 127 | def testDoesNotDuplicatePermission(self): |
| 128 | self.assertHasPermission( |
Alex Humesky | 5b815a5 | 2016-06-10 22:43:39 +0000 | [diff] [blame] | 129 | StubifyMobileInstall( |
| 130 | MANIFEST_WITH_PERMISSION)[0], READ_EXTERNAL_STORAGE) |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 131 | |
| 132 | def testBadManifest(self): |
| 133 | with self.assertRaises(BadManifestException): |
Alex Humesky | 5b815a5 | 2016-06-10 22:43:39 +0000 | [diff] [blame] | 134 | StubifyMobileInstall(BAD_MANIFEST) |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 135 | |
| 136 | def testTooManyApplications(self): |
| 137 | with self.assertRaises(BadManifestException): |
Alex Humesky | 5b815a5 | 2016-06-10 22:43:39 +0000 | [diff] [blame] | 138 | StubifyMobileInstall(MULTIPLE_APPLICATIONS) |
| 139 | |
Alex Humesky | 3632f30 | 2017-01-26 23:05:28 +0000 | [diff] [blame] | 140 | def testNoPackageInManifest(self): |
| 141 | with self.assertRaises(BadManifestException): |
| 142 | StubifyMobileInstall(NO_PACKAGE_MANIFEST) |
| 143 | |
Alex Humesky | 5b815a5 | 2016-06-10 22:43:39 +0000 | [diff] [blame] | 144 | |
| 145 | class StubifyInstantRunTest(unittest.TestCase): |
| 146 | |
| 147 | def testReplacesOldApplication(self): |
| 148 | new_manifest = StubifyInstantRun(MANIFEST_WITH_APPLICATION) |
| 149 | manifest = ElementTree.fromstring(new_manifest) |
| 150 | application = manifest.find("application") |
| 151 | self.assertEqual(INSTANT_RUN_BOOTSTRAP_APPLICATION, |
| 152 | application.get("{%s}name" % ANDROID)) |
| 153 | self.assertEqual("old.application", application.get("name")) |
| 154 | |
| 155 | def testReplacesAndSavesOldApplication(self): |
| 156 | new_manifest = StubifyInstantRun(MANIFEST_WITHOUT_APPLICATION) |
| 157 | manifest = ElementTree.fromstring(new_manifest) |
| 158 | application = manifest.find("application") |
| 159 | self.assertEqual(INSTANT_RUN_BOOTSTRAP_APPLICATION, |
| 160 | application.get("{%s}name" % ANDROID)) |
| 161 | self.assertEqual(None, application.get("name")) |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 162 | |
| 163 | |
| 164 | if __name__ == "__main__": |
| 165 | unittest.main() |