Skip to content

Commit

Permalink
Add a SkipTestInjection annotation to disable injecting the test clas…
Browse files Browse the repository at this point in the history
…s. This may be useful for outside rules that wants to inject the test class from some other Hilt component.

This annotation may be used directly on the test class or on some other annotation, as typically outside rules would have some other code generators on their own to generate needed entry points.

RELNOTES=Add @SkipTestInjection
PiperOrigin-RevId: 608724405
  • Loading branch information
Chang-Eric authored and Dagger Team committed Feb 20, 2024
1 parent 02d62d6 commit c40811e
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 2 deletions.
9 changes: 9 additions & 0 deletions java/dagger/hilt/android/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ android_library(
],
)

android_library(
name = "skip_test_injection",
testonly = 1,
srcs = ["SkipTestInjection.java"],
deps = [
":package_info",
],
)

java_library(
name = "package_info",
srcs = ["package-info.java"],
Expand Down
33 changes: 33 additions & 0 deletions java/dagger/hilt/android/testing/SkipTestInjection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2024 The Dagger Authors.
*
* 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 dagger.hilt.android.testing;

import static java.lang.annotation.RetentionPolicy.CLASS;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
* Annotation used for skipping test injection in a Hilt Android Test. This may be useful if
* building separate custom test infrastructure to inject the test class from another Hilt
* component. This may be used on either the test class or an annotation that annotates
* the test class.
*/
@Retention(CLASS)
@Target({ElementType.TYPE})
public @interface SkipTestInjection {}
2 changes: 2 additions & 0 deletions java/dagger/hilt/processor/internal/ClassNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ public final class ClassNames {
get("dagger.hilt.android.internal.testing", "TestInstanceHolder");
public static final ClassName HILT_ANDROID_TEST =
get("dagger.hilt.android.testing", "HiltAndroidTest");
public static final ClassName SKIP_TEST_INJECTION =
get("dagger.hilt.android.testing", "SkipTestInjection");
public static final ClassName CUSTOM_TEST_APPLICATION =
get("dagger.hilt.android.testing", "CustomTestApplication");
public static final ClassName ON_COMPONENT_READY_RUNNER =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ public void processEach(ClassName annotation, XElement element) throws Exception
// for unrelated changes in Gradle.
RootType rootType = RootType.of(rootElement);
if (rootType.isTestRoot()) {
new TestInjectorGenerator(processingEnv(), TestRootMetadata.of(processingEnv(), rootElement))
.generate();
TestRootMetadata testRootMetadata = TestRootMetadata.of(processingEnv(), rootElement);
if (testRootMetadata.skipTestInjectionAnnotation().isEmpty()) {
new TestInjectorGenerator(processingEnv(), testRootMetadata).generate();
}
}

XTypeElement originatingRootElement =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static javax.lang.model.element.Modifier.STATIC;

import androidx.room.compiler.processing.JavaPoetExtKt;
import androidx.room.compiler.processing.XAnnotation;
import androidx.room.compiler.processing.XConstructorElement;
import androidx.room.compiler.processing.XFiler.Mode;
import androidx.room.compiler.processing.XProcessingEnv;
Expand All @@ -41,6 +42,7 @@
import dagger.hilt.processor.internal.Processors;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

/** Generates an implementation of {@link dagger.hilt.android.internal.TestComponentData}. */
public final class TestComponentDataGenerator {
Expand Down Expand Up @@ -222,6 +224,13 @@ private MethodSpec getTestInjectInternalMethod() {
}

private CodeBlock callInjectTest(XTypeElement testElement) {
Optional<XAnnotation> skipTestInjection =
rootMetadata.testRootMetadata().skipTestInjectionAnnotation();
if (skipTestInjection.isPresent()) {
return CodeBlock.of(
"throw new IllegalStateException(\"Cannot inject test when using @$L\")",
skipTestInjection.get().getName());
}
return CodeBlock.of(
"(($T) (($T) $T.getApplication($T.getApplicationContext()))"
+ ".generatedComponent()).injectTest(testInstance)",
Expand Down
25 changes: 25 additions & 0 deletions java/dagger/hilt/processor/internal/root/TestRootMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.hilt.processor.internal.root;

import androidx.room.compiler.processing.XAnnotation;
import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XTypeElement;
Expand All @@ -25,6 +26,8 @@
import dagger.hilt.processor.internal.ProcessorErrors;
import dagger.hilt.processor.internal.Processors;
import dagger.internal.codegen.xprocessing.XElements;
import java.util.Optional;
import java.util.Set;
import javax.lang.model.element.TypeElement;

/** Metadata class for {@code InternalTestRoot} annotated classes. */
Expand Down Expand Up @@ -57,6 +60,28 @@ ClassName testInjectorName() {
return Processors.append(Processors.getEnclosedClassName(testName()), "_GeneratedInjector");
}

/**
* Returns either the SkipTestInjection annotation or the first annotation that was annotated
* with SkipTestInjection, if present.
*/
Optional<XAnnotation> skipTestInjectionAnnotation() {
XAnnotation skipTestAnnotation = testElement().getAnnotation(ClassNames.SKIP_TEST_INJECTION);
if (skipTestAnnotation != null) {
return Optional.of(skipTestAnnotation);
}

Set<XAnnotation> annotatedAnnotations = testElement().getAnnotationsAnnotatedWith(
ClassNames.SKIP_TEST_INJECTION);
if (!annotatedAnnotations.isEmpty()) {
// Just return the first annotation that skips test injection if there are multiple since
// at this point it doesn't really matter and the specific annotation is only really useful
// for communicating back to the user.
return Optional.of(annotatedAnnotations.iterator().next());
}

return Optional.empty();
}

static TestRootMetadata of(XProcessingEnv env, XElement element) {

XTypeElement testElement = XElements.asTypeElement(element);
Expand Down
34 changes: 34 additions & 0 deletions javatests/dagger/hilt/android/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,37 @@ android_local_test(
"//third_party/java/truth",
],
)

android_local_test(
name = "SkipTestInjectionTest",
srcs = ["SkipTestInjectionTest.java"],
manifest_values = {
"minSdkVersion": "15",
"targetSdkVersion": "27",
},
deps = [
"//:android_local_test_exports",
"//:dagger_with_compiler",
"//java/dagger/hilt/android/testing:hilt_android_test",
"//java/dagger/hilt/android/testing:skip_test_injection",
"//third_party/java/jsr330_inject",
"//third_party/java/truth",
],
)

android_local_test(
name = "SkipTestInjectionAnnotationTest",
srcs = ["SkipTestInjectionAnnotationTest.java"],
manifest_values = {
"minSdkVersion": "15",
"targetSdkVersion": "27",
},
deps = [
"//:android_local_test_exports",
"//:dagger_with_compiler",
"//java/dagger/hilt/android/testing:hilt_android_test",
"//java/dagger/hilt/android/testing:skip_test_injection",
"//third_party/java/jsr330_inject",
"//third_party/java/truth",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2024 The Dagger Authors.
*
* 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 dagger.hilt.android.testing;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import javax.inject.Inject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

@HiltAndroidTest
@SkipTestInjectionAnnotationTest.TestAnnotation
@RunWith(AndroidJUnit4.class)
@Config(application = HiltTestApplication.class)
public final class SkipTestInjectionAnnotationTest {
@Rule public final HiltAndroidRule rule = new HiltAndroidRule(this);

@SkipTestInjection
@interface TestAnnotation {}

@Inject String string; // Never provided, shouldn't compile without @SkipTestInjection

@Test
public void testCannotCallInjectOnTestRule() throws Exception {
IllegalStateException exception =
assertThrows(
IllegalStateException.class,
() -> rule.inject());
assertThat(exception)
.hasMessageThat()
.isEqualTo("Cannot inject test when using @TestAnnotation");
}
}
48 changes: 48 additions & 0 deletions javatests/dagger/hilt/android/testing/SkipTestInjectionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2024 The Dagger Authors.
*
* 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 dagger.hilt.android.testing;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import javax.inject.Inject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

@HiltAndroidTest
@SkipTestInjection
@RunWith(AndroidJUnit4.class)
@Config(application = HiltTestApplication.class)
public final class SkipTestInjectionTest {
@Rule public final HiltAndroidRule rule = new HiltAndroidRule(this);

@Inject String string; // Never provided, shouldn't compile without @SkipTestInjection

@Test
public void testCannotCallInjectOnTestRule() throws Exception {
IllegalStateException exception =
assertThrows(
IllegalStateException.class,
() -> rule.inject());
assertThat(exception)
.hasMessageThat()
.isEqualTo("Cannot inject test when using @SkipTestInjection");
}
}

0 comments on commit c40811e

Please sign in to comment.