blob: a46d2a607aa0f3a038a9c19bc02c477bc72bdc6c [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001# Copyright 2015 The Bazel Authors. All rights reserved.
Alex Humeskya4ecde62015-05-21 17:08:42 +00002#
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
17import unittest
18from xml.etree import ElementTree
19
20from tools.android.stubify_manifest import ANDROID
21from tools.android.stubify_manifest import BadManifestException
Alex Humesky5b815a52016-06-10 22:43:39 +000022from tools.android.stubify_manifest import INSTANT_RUN_BOOTSTRAP_APPLICATION
23from tools.android.stubify_manifest import MOBILE_INSTALL_STUB_APPLICATION
Alex Humeskya4ecde62015-05-21 17:08:42 +000024from tools.android.stubify_manifest import READ_EXTERNAL_STORAGE
Alex Humesky5b815a52016-06-10 22:43:39 +000025from tools.android.stubify_manifest import StubifyInstantRun
26from tools.android.stubify_manifest import StubifyMobileInstall
Alex Humeskya4ecde62015-05-21 17:08:42 +000027
28
29MANIFEST_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 Berkif97a4472015-11-13 16:13:17 +000038MANIFEST_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 Humeskya4ecde62015-05-21 17:08:42 +000047MANIFEST_WITHOUT_APPLICATION = """
48<manifest
49 xmlns:android="http://schemas.android.com/apk/res/android"
50 package="com.google.package">
51</manifest>
52"""
53
54MANIFEST_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
62BAD_MANIFEST = """
63<b>Hello World!</b>
64"""
65
66MULTIPLE_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 Humesky3632f302017-01-26 23:05:28 +000078NO_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 Humesky5b815a52016-06-10 22:43:39 +000087class StubifyMobileInstallTest(unittest.TestCase):
Alex Humeskya4ecde62015-05-21 17:08:42 +000088
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 Humesky5b815a52016-06-10 22:43:39 +000095 new_manifest, old_application, app_pkg = StubifyMobileInstall(
96 MANIFEST_WITH_APPLICATION)
Alex Humeskya4ecde62015-05-21 17:08:42 +000097 self.assertEqual("com.google.package", app_pkg)
98 self.assertEqual("old.application", old_application)
Alex Humesky5b815a52016-06-10 22:43:39 +000099 self.assertEqual(
100 MOBILE_INSTALL_STUB_APPLICATION, self.GetApplication(new_manifest))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000101
102 def testAddsNewAplication(self):
103 new_manifest, old_application, app_pkg = (
Alex Humesky5b815a52016-06-10 22:43:39 +0000104 StubifyMobileInstall(MANIFEST_WITHOUT_APPLICATION))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000105 self.assertEqual("com.google.package", app_pkg)
106 self.assertEqual("android.app.Application", old_application)
Alex Humesky5b815a52016-06-10 22:43:39 +0000107 self.assertEqual(
108 MOBILE_INSTALL_STUB_APPLICATION, self.GetApplication(new_manifest))
Alex Humeskya4ecde62015-05-21 17:08:42 +0000109
Lukacs Berkif97a4472015-11-13 16:13:17 +0000110 def testRemovesHasCode(self):
Alex Humesky5b815a52016-06-10 22:43:39 +0000111 new_manifest, _, _ = StubifyMobileInstall(MANIFEST_WITH_HASCODE)
Lukacs Berkif97a4472015-11-13 16:13:17 +0000112 application = ElementTree.fromstring(new_manifest).find("application")
113 self.assertFalse(("{%s}hasCode" % ANDROID) in application.attrib)
114
Alex Humeskya4ecde62015-05-21 17:08:42 +0000115 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 Humesky5b815a52016-06-10 22:43:39 +0000124 StubifyMobileInstall(
125 MANIFEST_WITH_APPLICATION)[0], READ_EXTERNAL_STORAGE)
Alex Humeskya4ecde62015-05-21 17:08:42 +0000126
127 def testDoesNotDuplicatePermission(self):
128 self.assertHasPermission(
Alex Humesky5b815a52016-06-10 22:43:39 +0000129 StubifyMobileInstall(
130 MANIFEST_WITH_PERMISSION)[0], READ_EXTERNAL_STORAGE)
Alex Humeskya4ecde62015-05-21 17:08:42 +0000131
132 def testBadManifest(self):
133 with self.assertRaises(BadManifestException):
Alex Humesky5b815a52016-06-10 22:43:39 +0000134 StubifyMobileInstall(BAD_MANIFEST)
Alex Humeskya4ecde62015-05-21 17:08:42 +0000135
136 def testTooManyApplications(self):
137 with self.assertRaises(BadManifestException):
Alex Humesky5b815a52016-06-10 22:43:39 +0000138 StubifyMobileInstall(MULTIPLE_APPLICATIONS)
139
Alex Humesky3632f302017-01-26 23:05:28 +0000140 def testNoPackageInManifest(self):
141 with self.assertRaises(BadManifestException):
142 StubifyMobileInstall(NO_PACKAGE_MANIFEST)
143
Alex Humesky5b815a52016-06-10 22:43:39 +0000144
145class 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 Humeskya4ecde62015-05-21 17:08:42 +0000162
163
164if __name__ == "__main__":
165 unittest.main()