diff options
author | ajmichael <ajmichael@google.com> | 2017-06-29 01:29:19 +0200 |
---|---|---|
committer | Marcel Hlopko <hlopko@google.com> | 2017-06-29 09:33:56 +0200 |
commit | 8355237f65842d9df45b51cdfaebda9bb44c3a79 (patch) | |
tree | 79d43aa21145a3184ebba3aa4d08d925dfca2d0a /src/test | |
parent | c1e0d7b9f6bfa8df950980f9370c638443d361e1 (diff) |
Introduce new android_instrumentation_test rule.
This rule works with the android_instrumentation, android_device_script_fixture and android_host_service_fixture rules to support testing Android applications.
None of these rules are installed yet, because the forthcoming Android test runner is not yet open sourced.
https://github.com/bazelbuild/bazel/issues/903.
RELNOTES: None
PiperOrigin-RevId: 160465920
Diffstat (limited to 'src/test')
5 files changed, 277 insertions, 2 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java index 34cfc71313..151466a026 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java @@ -234,6 +234,7 @@ public final class BazelAnalysisMock extends AnalysisMock { .add(" jars = [ 'idlclass.jar' ])") .add("exports_files(['adb', 'adb_static'])") .add("sh_binary(name = 'android_runtest', srcs = ['empty.sh'])") + .add("sh_binary(name = 'instrumentation_test_entry_point', srcs = ['empty.sh'])") .add("java_plugin(name = 'databinding_annotation_processor',") .add(" processor_class = 'android.databinding.annotationprocessor.ProcessDataBinding')"); diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidBuildViewTestCase.java b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidBuildViewTestCase.java index ce74662be6..4e9d40a300 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidBuildViewTestCase.java +++ b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidBuildViewTestCase.java @@ -56,6 +56,7 @@ public abstract class AndroidBuildViewTestCase extends BuildViewTestCase { .addRuleDefinition(new AndroidDeviceScriptFixtureRule()) .addRuleDefinition(new AndroidHostServiceFixtureRule()) .addRuleDefinition(new AndroidInstrumentationRule()) + .addRuleDefinition(new AndroidInstrumentationTestRule()) .build(); } diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidHostServiceFixtureTest.java b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidHostServiceFixtureTest.java index 1d77e5db4f..e0954c0c51 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidHostServiceFixtureTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidHostServiceFixtureTest.java @@ -152,11 +152,11 @@ public class AndroidHostServiceFixtureTest extends AndroidBuildViewTestCase { ")"); assertThat( getHostServiceFixtureInfoProvider(getConfiguredTarget("//javatests/com/app:no_daemon")) - .getIsDaemon()) + .getDaemon()) .isFalse(); assertThat( getHostServiceFixtureInfoProvider(getConfiguredTarget("//javatests/com/app:daemon")) - .getIsDaemon()) + .getDaemon()) .isTrue(); } diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTestTest.java b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTestTest.java new file mode 100644 index 0000000000..66f08f83c2 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTestTest.java @@ -0,0 +1,254 @@ +// 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. +package com.google.devtools.build.lib.rules.android; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.devtools.build.lib.actions.util.ActionsTestUtil.getFirstArtifactEndingWith; + +import com.google.common.collect.Iterables; +import com.google.devtools.build.lib.actions.Artifact; +import com.google.devtools.build.lib.analysis.ConfiguredTarget; +import com.google.devtools.build.lib.analysis.FileProvider; +import com.google.devtools.build.lib.analysis.FilesToRunProvider; +import com.google.devtools.build.lib.analysis.RunfilesProvider; +import com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction; +import com.google.devtools.build.lib.collect.nestedset.NestedSet; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link AndroidInstrumentationTest}. */ +@RunWith(JUnit4.class) +public class AndroidInstrumentationTestTest extends AndroidBuildViewTestCase { + + @Before + public void setup() throws Exception { + scratch.file( + "java/com/app/BUILD", + "android_binary(", + " name = 'app1',", + " manifest = 'AndroidManifest.xml',", + ")", + "android_binary(", + " name = 'app2',", + " manifest = 'AndroidManifest.xml',", + ")", + "android_binary(", + " name = 'support',", + " manifest = 'AndroidManifest.xml',", + ")"); + scratch.file( + "javatests/com/app/BUILD", + "android_binary(", + " name = 'instrumentation_app1',", + " manifest = 'AndroidManifest.xml',", + ")", + "android_instrumentation(", + " name = 'instrumentation1',", + " target = '//java/com/app:app1',", + " instrumentation = ':instrumentation_app1',", + ")", + "android_binary(", + " name = 'instrumentation_app2',", + " manifest = 'AndroidManifest.xml',", + ")", + "android_instrumentation(", + " name = 'instrumentation2',", + " target = '//java/com/app:app2',", + " instrumentation = ':instrumentation_app2',", + ")", + "android_device_script_fixture(", + " name = 'device_fixture',", + " cmd = 'foo bar',", + ")", + "android_host_service_fixture(", + " name = 'host_fixture',", + " executable = '//java/com/server',", + " service_names = ['foo', 'bar'],", + ")"); + scratch.file( + "java/com/server/BUILD", + "java_binary(", + " name = 'server',", + " main_class = 'does.not.exist',", + " srcs = [],", + ")"); + scratch.file( + "javatests/com/app/ait/BUILD", + "android_instrumentation_test(", + " name = 'ait',", + " instrumentations = [", + " '//javatests/com/app:instrumentation1',", + " '//javatests/com/app:instrumentation2',", + " ],", + " target_device = '//tools/android/emulated_device:nexus_6',", + " fixtures = [", + " '//javatests/com/app:device_fixture',", + " '//javatests/com/app:host_fixture',", + " ],", + " support_apks = [", + " '//java/com/app:support',", + " ],", + " data = [", + " 'foo.txt',", + " ],", + ")"); + setupTargetDevice(); + } + + // TODO(ajmichael): Share this with AndroidDeviceTest.java + private void setupTargetDevice() throws Exception { + scratch.file( + "tools/android/emulated_device/BUILD", + "filegroup(", + " name = 'emulator_images_android_21_x86',", + " srcs = [", + " 'android_21/x86/kernel-qemu',", + " 'android_21/x86/ramdisk.img',", + " 'android_21/x86/source.properties',", + " 'android_21/x86/system.img.tar.gz',", + " 'android_21/x86/userdata.img.tar.gz'", + " ],", + ")", + "android_device(", + " name = 'nexus_6',", + " ram = 2047,", + " horizontal_resolution = 720, ", + " vertical_resolution = 1280, ", + " cache = 32, ", + " system_image = ':emulator_images_android_21_x86',", + " screen_density = 280, ", + " vm_heap = 256", + ")"); + } + + @Test + public void testTestExecutableRunfiles() throws Exception { + ConfiguredTarget androidInstrumentationTest = getConfiguredTarget("//javatests/com/app/ait"); + NestedSet<Artifact> runfiles = + androidInstrumentationTest + .getProvider(RunfilesProvider.class) + .getDefaultRunfiles() + .getAllArtifacts(); + assertThat(runfiles) + .containsAllIn( + getHostConfiguredTarget("//tools/android/emulated_device:nexus_6") + .getProvider(RunfilesProvider.class) + .getDefaultRunfiles() + .getAllArtifacts()); + assertThat(runfiles) + .containsAllIn( + getHostConfiguredTarget("//java/com/server") + .getProvider(RunfilesProvider.class) + .getDefaultRunfiles() + .getAllArtifacts()); + assertThat(runfiles) + .containsAllIn( + getHostConfiguredTarget( + androidInstrumentationTest + .getTarget() + .getAssociatedRule() + .getAttrDefaultValue("$test_entry_point") + .toString()) + .getProvider(RunfilesProvider.class) + .getDefaultRunfiles() + .getAllArtifacts()); + assertThat(runfiles) + .containsAllOf( + getDeviceFixtureScript(getConfiguredTarget("//javatests/com/app:device_fixture")), + getInstrumentationApk(getConfiguredTarget("//javatests/com/app:instrumentation1")), + getTargetApk(getConfiguredTarget("//javatests/com/app:instrumentation1")), + getInstrumentationApk(getConfiguredTarget("//javatests/com/app:instrumentation2")), + getTargetApk(getConfiguredTarget("//javatests/com/app:instrumentation2")), + Iterables.getOnlyElement( + getConfiguredTarget("//javatests/com/app/ait:foo.txt") + .getProvider(FileProvider.class) + .getFilesToBuild())); + } + + @Test + public void testTestExecutableContents() throws Exception { + ConfiguredTarget androidInstrumentationTest = getConfiguredTarget("//javatests/com/app/ait"); + assertThat(androidInstrumentationTest).isNotNull(); + + String testExecutableScript = + ((TemplateExpansionAction) + getGeneratingAction( + androidInstrumentationTest + .getProvider(FilesToRunProvider.class) + .getExecutable())) + .getFileContents(); + + assertThat(testExecutableScript) + .contains( + "instrumentation_apks=\"javatests/com/app/instrumentation1-instrumentation.apk " + + "javatests/com/app/instrumentation2-instrumentation.apk\""); + assertThat(testExecutableScript) + .contains( + "target_apks=\"javatests/com/app/instrumentation1-target.apk " + + "javatests/com/app/instrumentation2-target.apk\""); + assertThat(testExecutableScript).contains("support_apks=\"java/com/app/support.apk\""); + assertThat(testExecutableScript) + .contains( + "declare -A device_script_fixtures=( " + + "[javatests/com/app/cmd_device_fixtures/device_fixture/cmd.sh]=false,true )"); + assertThat(testExecutableScript).contains("host_service_fixture=\"java/com/server/server\""); + assertThat(testExecutableScript).contains("host_service_fixture_services=\"foo,bar\""); + assertThat(testExecutableScript) + .contains("device_script=\"${WORKSPACE_DIR}/tools/android/emulated_device/nexus_6\""); + assertThat(testExecutableScript).contains("data_deps=\"javatests/com/app/ait/foo.txt\""); + } + + @Test + public void testAtMostOneHostServiceFixture() throws Exception { + checkError( + "javatests/com/app/ait2", + "ait", + "android_instrumentation_test accepts at most one android_host_service_fixture", + "android_host_service_fixture(", + " name = 'host_fixture',", + " executable = '//java/com/server',", + " service_names = ['foo', 'bar'],", + ")", + "android_instrumentation_test(", + " name = 'ait',", + " instrumentations = ['//javatests/com/app:instrumentation1'],", + " target_device = '//tools/android/emulated_device:nexus_6',", + " fixtures = [", + " ':host_fixture',", + " '//javatests/com/app:host_fixture',", + " ],", + ")"); + } + + private static Artifact getDeviceFixtureScript(ConfiguredTarget deviceScriptFixture) { + return getFirstArtifactEndingWith( + deviceScriptFixture.getProvider(FileProvider.class).getFilesToBuild(), ".sh"); + } + + private static Artifact getInstrumentationApk(ConfiguredTarget instrumentation) { + return ((AndroidInstrumentationInfoProvider) + instrumentation.get( + AndroidInstrumentationInfoProvider.ANDROID_INSTRUMENTATION_INFO.getKey())) + .getInstrumentationApk(); + } + + private static Artifact getTargetApk(ConfiguredTarget instrumentation) { + return ((AndroidInstrumentationInfoProvider) + instrumentation.get( + AndroidInstrumentationInfoProvider.ANDROID_INSTRUMENTATION_INFO.getKey())) + .getTargetApk(); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/BUILD b/src/test/java/com/google/devtools/build/lib/rules/android/BUILD index 8e732e018e..96e6b301fe 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/android/BUILD +++ b/src/test/java/com/google/devtools/build/lib/rules/android/BUILD @@ -129,6 +129,25 @@ java_test( ) java_test( + name = "AndroidInstrumentationTestTest", + srcs = ["AndroidInstrumentationTestTest.java"], + tags = ["no_windows"], + deps = [ + ":AndroidBuildViewTestCase", + "//src/main/java/com/google/devtools/build/lib:android-rules", + "//src/main/java/com/google/devtools/build/lib:build-base", + "//src/main/java/com/google/devtools/build/lib:collect", + "//src/main/java/com/google/devtools/build/lib/actions", + "//src/test/java/com/google/devtools/build/lib:actions_testutil", + "//src/test/java/com/google/devtools/build/lib:analysis_testutil", + "//src/test/java/com/google/devtools/build/lib:testutil", + "//third_party:guava", + "//third_party:junit4", + "//third_party:truth", + ], +) + +java_test( name = "AndroidLibraryTest", srcs = ["AndroidLibraryTest.java"], deps = [ |