From 741dbc081e2479fd8b0b9c289802b8a0e4af7c06 Mon Sep 17 00:00:00 2001 From: jingwen Date: Tue, 19 Dec 2017 09:30:45 -0800 Subject: Ensure that the target package in the 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 --- tools/android/instrumentation_test_check_test.py | 73 ++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tools/android/instrumentation_test_check_test.py (limited to 'tools/android/instrumentation_test_check_test.py') 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 = """ + + + + +""" + +INCORRECT_INSTRUMENTATION_MANIFEST = """ + + + + +""" + +TARGET_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() -- cgit v1.2.3