forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure that the target package in the <instrumentation> tag of the in…
…strumentation 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: bazelbuild#903 RELNOTES: None. PiperOrigin-RevId: 179564246
- Loading branch information
Showing
12 changed files
with
524 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
235 changes: 235 additions & 0 deletions
235
src/test/shell/bazel/android/android_instrumentation_test_integration_test.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
#!/bin/bash | ||
# | ||
# 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. | ||
|
||
# For these tests to run do the following: | ||
# | ||
# 1. Install an Android SDK from https://developer.android.com | ||
# 2. Set the $ANDROID_HOME environment variable | ||
# 3. Uncomment the line in WORKSPACE containing android_sdk_repository | ||
|
||
# Load the test setup defined in the parent directory | ||
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
source "${CURRENT_DIR}/android_helper.sh" \ | ||
|| { echo "android_helper.sh not found!" >&2; exit 1; } | ||
fail_if_no_android_sdk | ||
|
||
source "${CURRENT_DIR}/../../integration_test_setup.sh" \ | ||
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; } | ||
|
||
function setup_android_instrumentation_test_env() { | ||
mkdir -p java/com/bin/res/values | ||
mkdir -p javatests/com/bin | ||
|
||
# Targets for android_binary application under test | ||
cat > java/com/bin/BUILD <<EOF | ||
android_binary( | ||
name = 'target', | ||
manifest = 'AndroidManifest.xml', | ||
deps = [':lib'], | ||
visibility = ["//visibility:public"], | ||
) | ||
android_library( | ||
name = 'lib', | ||
manifest = 'AndroidManifest.xml', | ||
exports_manifest = 0, | ||
resource_files = ['res/values/values.xml'], | ||
srcs = ['Bar.java'], | ||
visibility = ["//visibility:public"], | ||
) | ||
EOF | ||
cat > java/com/bin/AndroidManifest.xml <<EOF | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package='com.bin' xmlns:android="http://schemas.android.com/apk/res/android" /> | ||
EOF | ||
cat > java/com/bin/Bar.java <<EOF | ||
package com.bin; | ||
public class Bar { } | ||
EOF | ||
cat > java/com/bin/res/values/values.xml <<EOF | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources xmlns:android="http://schemas.android.com/apk/res/android"> | ||
</resources> | ||
EOF | ||
|
||
# Targets for instrumentation android_binary | ||
cat > javatests/com/bin/BUILD <<EOF | ||
android_binary( | ||
name = 'instr', | ||
srcs = ['BarTest.java'], | ||
manifest = 'AndroidManifest.xml', | ||
instruments = '//java/com/bin:target', | ||
deps = ['//java/com/bin:lib'], | ||
) | ||
EOF | ||
cat > javatests/com/bin/AndroidManifest.xml <<EOF | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package='com.bin' xmlns:android='http://schemas.android.com/apk/res/android'> | ||
<instrumentation android:targetPackage='com.bin' android:name='some.test.runner' /> | ||
</manifest> | ||
EOF | ||
cat > javatests/com/bin/BarTest.java <<EOF | ||
package com.bin; | ||
public class BarTest { | ||
public Bar getBar() { | ||
return new Bar(); | ||
} | ||
} | ||
EOF | ||
} | ||
|
||
function test_correct_target_package_build_succeed() { | ||
create_new_workspace | ||
setup_android_sdk_support | ||
setup_android_instrumentation_test_env | ||
assert_build //javatests/com/bin:instr | ||
} | ||
|
||
function test_incorrect_target_package_build_failure() { | ||
create_new_workspace | ||
setup_android_sdk_support | ||
setup_android_instrumentation_test_env | ||
|
||
cat > javatests/com/bin/AndroidManifest.xml <<EOF | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package='com.bin' xmlns:android='http://schemas.android.com/apk/res/android'> | ||
<instrumentation android:targetPackage='not.com.bin' android:name='some.test.runner' /> | ||
</manifest> | ||
EOF | ||
|
||
assert_build_fails //javatests/com/bin:instr | ||
expect_log "does not match the package name of" | ||
} | ||
|
||
function test_multiple_instrumentations_with_different_package_names_build_failure() { | ||
create_new_workspace | ||
setup_android_sdk_support | ||
setup_android_instrumentation_test_env | ||
|
||
cat > javatests/com/bin/AndroidManifest.xml <<EOF | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package='com.bin' xmlns:android='http://schemas.android.com/apk/res/android'> | ||
<instrumentation android:targetPackage='com.bin' android:name='some.test.runner' /> | ||
<instrumentation android:targetPackage='not.com.bin' android:name='some.test.runner' /> | ||
</manifest> | ||
EOF | ||
|
||
assert_build_fails //javatests/com/bin:instr | ||
expect_log "do not reference the same target package" | ||
} | ||
|
||
function test_no_target_package_attribute_build_failure() { | ||
create_new_workspace | ||
setup_android_sdk_support | ||
setup_android_instrumentation_test_env | ||
|
||
cat > javatests/com/bin/AndroidManifest.xml <<EOF | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package='com.bin' xmlns:android='http://schemas.android.com/apk/res/android'> | ||
<instrumentation /> | ||
</manifest> | ||
EOF | ||
|
||
assert_build_fails //javatests/com/bin:instr | ||
expect_log "No <instrumentation> tag containing the targetPackage attribute" | ||
} | ||
|
||
function test_target_package_no_package_specified_build_failure() { | ||
create_new_workspace | ||
setup_android_sdk_support | ||
setup_android_instrumentation_test_env | ||
|
||
cat > java/com/bin/AndroidManifest.xml <<EOF | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" /> | ||
EOF | ||
|
||
assert_build_fails //javatests/com/bin:instr | ||
expect_log "needs to specify the package name" | ||
} | ||
|
||
function test_android_instrumentation_binary_class_filtering() { | ||
create_new_workspace | ||
setup_android_sdk_support | ||
mkdir -p java/com/bin | ||
cat > java/com/bin/BUILD <<EOF | ||
android_binary( | ||
name = 'instr', | ||
srcs = ['Foo.java'], | ||
manifest = 'TestAndroidManifest.xml', | ||
instruments = ':target', | ||
deps = [':lib'], | ||
) | ||
android_binary( | ||
name = 'target', | ||
manifest = 'AndroidManifest.xml', | ||
deps = [':lib'], | ||
) | ||
android_library( | ||
name = 'lib', | ||
manifest = 'AndroidManifest.xml', | ||
resource_files = ['res/values/values.xml'], | ||
srcs = ['Bar.java', 'Baz.java'], | ||
) | ||
EOF | ||
cat > java/com/bin/AndroidManifest.xml <<EOF | ||
<manifest package='com.bin' /> | ||
EOF | ||
cat > java/com/bin/TestAndroidManifest.xml <<EOF | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package='com.bin' xmlns:android='http://schemas.android.com/apk/res/android'> | ||
<instrumentation android:targetPackage='com.bin' android:name='some.test.runner' /> | ||
</manifest> | ||
EOF | ||
cat > java/com/bin/Foo.java <<EOF | ||
package com.bin; | ||
public class Foo { | ||
public Bar getBar() { | ||
return new Bar(); | ||
} | ||
public Baz getBaz() { | ||
return new Baz(); | ||
} | ||
} | ||
EOF | ||
cat > java/com/bin/Bar.java <<EOF | ||
package com.bin; | ||
public class Bar { | ||
public Baz getBaz() { | ||
return new Baz(); | ||
} | ||
} | ||
EOF | ||
cat > java/com/bin/Baz.java <<EOF | ||
package com.bin; | ||
public class Baz {} | ||
EOF | ||
mkdir -p java/com/bin/res/values | ||
cat > java/com/bin/res/values/values.xml <<EOF | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources xmlns:android="http://schemas.android.com/apk/res/android"> | ||
</resources> | ||
EOF | ||
assert_build //java/com/bin:instr | ||
output_classes=$(zipinfo -1 bazel-bin/java/com/bin/instr_filtered.jar) | ||
assert_one_of $output_classes "META-INF/MANIFEST.MF" | ||
assert_one_of $output_classes "com/bin/Foo.class" | ||
assert_not_one_of $output_classes "com/bin/R.class" | ||
assert_not_one_of $output_classes "com/bin/Bar.class" | ||
assert_not_one_of $output_classes "com/bin/Baz.class" | ||
} | ||
|
||
run_suite "android_instrumentation_test integration tests" | ||
|
Oops, something went wrong.