aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/android/instrumentation_test_check_test.py
diff options
context:
space:
mode:
authorGravatar jingwen <jingwen@google.com>2017-12-19 09:30:45 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2017-12-19 09:32:20 -0800
commit741dbc081e2479fd8b0b9c289802b8a0e4af7c06 (patch)
tree45430c4303439099540e77619f3c1b949e2416be /tools/android/instrumentation_test_check_test.py
parent691bd158149602aeb6bbcc46fd4b22367d0157b4 (diff)
Ensure that the target package in the <instrumentation> tag of the instrumentation android_binary's AndroidManifest.xml references the correct package name of the instrumented android_binary.
During an instrumentation test, ART will use the targetPackage specified in the instrumentation APK's AndroidManifest to determine the application to be instrumented. We can perform this check in Bazel at execution time, before the apps are loaded onto the device. See android_instrumentation_test_integration_test.sh for the e2e example. GITHUB: https://github.com/bazelbuild/bazel/issues/903 RELNOTES: None. PiperOrigin-RevId: 179564246
Diffstat (limited to 'tools/android/instrumentation_test_check_test.py')
-rw-r--r--tools/android/instrumentation_test_check_test.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/tools/android/instrumentation_test_check_test.py b/tools/android/instrumentation_test_check_test.py
new file mode 100644
index 0000000000..b255bee168
--- /dev/null
+++ b/tools/android/instrumentation_test_check_test.py
@@ -0,0 +1,73 @@
+# Copyright 2017 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""Unit tests for instrumentation_test_check."""
+
+import unittest
+
+from tools.android.instrumentation_test_check import _ExtractTargetPackageName
+from tools.android.instrumentation_test_check import _ExtractTargetPackageToInstrument
+from tools.android.instrumentation_test_check import _ValidateManifestPackageNames
+from tools.android.instrumentation_test_check import ManifestError
+
+INSTRUMENTATION_MANIFEST = """<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.example.test" >
+ <instrumentation android:targetPackage="com.example"
+ android:name="android.support.test.runner.AndroidJUnitRunner"/>
+ <application android:label="Test"/>
+</manifest>
+"""
+
+INCORRECT_INSTRUMENTATION_MANIFEST = """<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.example.test" >
+ <instrumentation android:targetPackage="not.com.example"
+ android:name="android.support.test.runner.AndroidJUnitRunner"/>
+ <application android:label="Test"/>
+</manifest>
+"""
+
+TARGET_MANIFEST = """<?xml version="2.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.example" >
+ <application android:label="App" />
+</manifest>
+"""
+
+
+class InstrumentationTestCheckTest(unittest.TestCase):
+
+ def test_extract_instrumentation_target_package(self):
+ self.assertEqual(
+ _ExtractTargetPackageToInstrument(INSTRUMENTATION_MANIFEST, ""),
+ "com.example")
+
+ def test_extract_target_package(self):
+ self.assertEqual(
+ _ExtractTargetPackageName(TARGET_MANIFEST, "unused"), "com.example")
+
+ def test_target_package_check(self):
+ self.assertEqual(
+ _ValidateManifestPackageNames(INSTRUMENTATION_MANIFEST, "unused",
+ TARGET_MANIFEST, "unused"),
+ ("com.example", "com.example"))
+
+ def test_target_package_check_failure(self):
+ with self.assertRaises(ManifestError):
+ _ValidateManifestPackageNames(INCORRECT_INSTRUMENTATION_MANIFEST,
+ "unused", TARGET_MANIFEST, "unused")
+
+
+if __name__ == "__main__":
+ unittest.main()