aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build
diff options
context:
space:
mode:
authorGravatar ajmichael <ajmichael@google.com>2017-05-05 20:32:18 +0200
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2017-05-05 23:19:24 +0200
commit4b3f9dbf813d7a5def8aac844a3a1023f04fd61a (patch)
tree8c1aa3880354600da1cc6ac8d807dfd9fb639f73 /src/test/java/com/google/devtools/build
parentb89f9faf3c064f3f2f8f0959dda5687f7ca10221 (diff)
Create new android_instrumentation rule.
This rule is responsible for building the target and instrumentation APKs used by an Android instrumentation test. If they are provided as APKs (e.g. from an android_binary or a genrule) they will be used as is. If they are provided as libraries, APKs will be created. This CL does not actually implement building target and instrumentation APKs from libraries, that will come in a follow-up CL as it will require some heavy refactoring of AndroidBinary.java. Follow-up CLs will add features such as repackaging the APKs to remove duplicate classes, reproguarding the target APK with the test code, validating that the target and instrumentation APKs were signed with the same debug key and verifying that instrumentation stanza appears in the instrumentation APKs manifest. Note that this CL does _not_ install the rule in the BazelRuleClassProvider, so this CL does not make it usable by anyone. Once the other android testing rules are ready, I will install them all. One small step towards https://github.com/bazelbuild/bazel/issues/903. RELNOTES: None PiperOrigin-RevId: 155220900
Diffstat (limited to 'src/test/java/com/google/devtools/build')
-rw-r--r--src/test/java/com/google/devtools/build/lib/rules/android/AndroidBuildViewTestCase.java1
-rw-r--r--src/test/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationRuleImplTest.java163
-rw-r--r--src/test/java/com/google/devtools/build/lib/rules/android/BUILD18
3 files changed, 182 insertions, 0 deletions
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 3b2d2001ac..8c3d325bd4 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
@@ -55,6 +55,7 @@ public abstract class AndroidBuildViewTestCase extends BuildViewTestCase {
return builder
// TODO(b/35097211): Remove this once the new testing rules are released.
.addRuleDefinition(new AndroidDeviceScriptFixtureRule())
+ .addRuleDefinition(new AndroidInstrumentationRule())
.build();
}
diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationRuleImplTest.java b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationRuleImplTest.java
new file mode 100644
index 0000000000..e71be0a2bc
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationRuleImplTest.java
@@ -0,0 +1,163 @@
+// 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 com.google.devtools.build.lib.actions.Artifact;
+import com.google.devtools.build.lib.analysis.ConfiguredTarget;
+import com.google.devtools.build.lib.analysis.actions.SpawnAction;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * Tests for {@link AndroidInstrumentation}.
+ *
+ * <p>Note that this class is not named {@code AndroidInstrumentationTest} per the usual naming
+ * pattern due to a conflict with the upcoming {@link AndroidInstrumentationTest} class which
+ * implements the {@code android_instrumentation_test} rule.
+ */
+@RunWith(JUnit4.class)
+public class AndroidInstrumentationRuleImplTest extends AndroidBuildViewTestCase {
+
+ @Before
+ public void createFiles() throws Exception {
+ scratch.file(
+ "java/com/app/BUILD",
+ "android_library(",
+ " name = 'lib',",
+ " manifest = 'AndroidManifest.xml',",
+ ")",
+ "android_binary(",
+ " name = 'app',",
+ " manifest = 'AndroidManifest.xml',",
+ " deps = [':lib'],",
+ ")");
+ scratch.file(
+ "javatests/com/app/BUILD",
+ "android_library(",
+ " name = 'lib',",
+ " manifest = 'AndroidManifest.xml',",
+ ")",
+ "android_binary(",
+ " name = 'instrumentation',",
+ " manifest = 'AndroidManifest.xml',",
+ " deps = [':lib'],",
+ ")");
+ }
+
+ @Test
+ public void testTargetAndInstrumentationAreBinaries() throws Exception {
+ scratch.file(
+ "javatests/com/app/instrumentation/BUILD",
+ "android_instrumentation(",
+ " name = 'instrumentation',",
+ " target = '//java/com/app',",
+ " instrumentation = '//javatests/com/app:instrumentation',",
+ ")");
+ ConfiguredTarget instrumentation = getConfiguredTarget("//javatests/com/app/instrumentation");
+ assertThat(instrumentation).isNotNull();
+ AndroidInstrumentationInfoProvider instrumentationProvider =
+ (AndroidInstrumentationInfoProvider)
+ instrumentation.get(
+ AndroidInstrumentationInfoProvider.ANDROID_INSTRUMENTATION_INFO.getKey());
+ assertThat(instrumentationProvider.getTargetApk()).isNotNull();
+ assertThat(instrumentationProvider.getTargetApk().prettyPrint())
+ .isEqualTo("javatests/com/app/instrumentation/instrumentation-target.apk");
+ assertThat(instrumentationProvider.getInstrumentationApk()).isNotNull();
+ assertThat(instrumentationProvider.getInstrumentationApk().prettyPrint())
+ .isEqualTo("javatests/com/app/instrumentation/instrumentation-instrumentation.apk");
+ }
+
+ // TODO(b/37856762): Re-enable and expand on this test when android_instrumentation is fullly
+ // implemented to build APKs from libraries.
+ @Ignore
+ @Test
+ public void testTargetAndInstrumentationAreLibraries() throws Exception {
+ scratch.file(
+ "javatests/com/app/instrumentation/BUILD",
+ "android_instrumentation(",
+ " name = 'instrumentation',",
+ " target_library = '//java/com/app:lib',",
+ " instrumentation_library = '//javatests/com/app:lib',",
+ ")");
+ ConfiguredTarget instrumentation = getConfiguredTarget("//javatests/com/app/instrumentation");
+ assertThat(instrumentation).isNotNull();
+ AndroidInstrumentationInfoProvider instrumentationProvider =
+ (AndroidInstrumentationInfoProvider)
+ instrumentation.get(
+ AndroidInstrumentationInfoProvider.ANDROID_INSTRUMENTATION_INFO.getKey());
+
+ Artifact targetApk = instrumentationProvider.getTargetApk();
+ assertThat(targetApk).isNotNull();
+ assertThat(targetApk.prettyPrint())
+ .isEqualTo("javatests/com/app/instrumentation/instrumentation-target.apk");
+ SpawnAction targetSpawnAction = getGeneratingSpawnAction(targetApk);
+ assertThat(targetSpawnAction).isNotNull();
+
+ Artifact instrumentationApk = instrumentationProvider.getInstrumentationApk();
+ assertThat(instrumentationApk).isNotNull();
+ assertThat(instrumentationApk.prettyPrint())
+ .isEqualTo("javatests/com/app/instrumentation/instrumentation-instrumentation.apk");
+ SpawnAction instrumentationSpawnAction = getGeneratingSpawnAction(instrumentationApk);
+ assertThat(instrumentationSpawnAction).isNotNull();
+ }
+
+ @Test
+ public void testInvalidAttributeCombinations() throws Exception {
+ checkError(
+ "javatests/com/app/instrumentation1",
+ "instrumentation",
+ "android_instrumentation requires that exactly one of the instrumentation and "
+ + "instrumentation_library attributes be specified.",
+ "android_instrumentation(",
+ " name = 'instrumentation',",
+ " target = '//java/com/app',",
+ ")");
+ checkError(
+ "javatests/com/app/instrumentation2",
+ "instrumentation",
+ "android_instrumentation requires that exactly one of the instrumentation and "
+ + "instrumentation_library attributes be specified.",
+ "android_instrumentation(",
+ " name = 'instrumentation',",
+ " target = '//java/com/app',",
+ " instrumentation = '//javatests/com/app:instrumentation',",
+ " instrumentation_library = '//javatests/com/app:lib',",
+ ")");
+ checkError(
+ "javatests/com/app/instrumentation3",
+ "instrumentation",
+ "android_instrumentation requires that exactly one of the target and target_library "
+ + "attributes be specified.",
+ "android_instrumentation(",
+ " name = 'instrumentation',",
+ " instrumentation = '//javatests/com/app:instrumentation',",
+ ")");
+ checkError(
+ "javatests/com/app/instrumentation4",
+ "instrumentation",
+ "android_instrumentation requires that exactly one of the target and target_library "
+ + "attributes be specified.",
+ "android_instrumentation(",
+ " name = 'instrumentation',",
+ " target = '//java/com/app',",
+ " target_library = '//java/com/app:lib',",
+ " instrumentation = '//javatests/com/app:instrumentation',",
+ ")");
+ }
+}
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 c438825f59..db8e70ecda 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
@@ -95,6 +95,24 @@ java_test(
)
java_test(
+ name = "AndroidInstrumentationRuleImplTest",
+ srcs = ["AndroidInstrumentationRuleImplTest.java"],
+ 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:packages-internal",
+ "//src/main/java/com/google/devtools/build/lib:vfs",
+ "//src/main/java/com/google/devtools/build/lib/actions",
+ "//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 = [