1 /* 2 * Copyright (C) 2020 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.hilt.processor.internal.root; 18 19 import androidx.room.compiler.processing.XAnnotation; 20 import androidx.room.compiler.processing.XElement; 21 import androidx.room.compiler.processing.XProcessingEnv; 22 import androidx.room.compiler.processing.XTypeElement; 23 import com.google.auto.value.AutoValue; 24 import com.squareup.javapoet.ClassName; 25 import dagger.hilt.processor.internal.ClassNames; 26 import dagger.hilt.processor.internal.ProcessorErrors; 27 import dagger.hilt.processor.internal.Processors; 28 import dagger.internal.codegen.xprocessing.XElements; 29 import java.util.Optional; 30 import java.util.Set; 31 import javax.lang.model.element.TypeElement; 32 33 /** Metadata class for {@code InternalTestRoot} annotated classes. */ 34 @AutoValue 35 abstract class TestRootMetadata { 36 37 /** Returns the {@link TypeElement} for the test class. */ testElement()38 abstract XTypeElement testElement(); 39 40 /** Returns the {@link TypeElement} for the base application. */ baseElement()41 abstract XTypeElement baseElement(); 42 43 /** Returns the {@link ClassName} for the test class. */ testName()44 ClassName testName() { 45 return testElement().getClassName(); 46 } 47 48 /** Returns the {@link ClassName} for the base application. */ baseAppName()49 ClassName baseAppName() { 50 return baseElement().getClassName(); 51 } 52 53 /** The name of the generated Hilt test application class for the given test name. */ appName()54 ClassName appName() { 55 return Processors.append(Processors.getEnclosedClassName(testName()), "_Application"); 56 } 57 58 /** The name of the generated Hilt test application class for the given test name. */ testInjectorName()59 ClassName testInjectorName() { 60 return Processors.append(Processors.getEnclosedClassName(testName()), "_GeneratedInjector"); 61 } 62 63 /** 64 * Returns either the SkipTestInjection annotation or the first annotation that was annotated 65 * with SkipTestInjection, if present. 66 */ skipTestInjectionAnnotation()67 Optional<XAnnotation> skipTestInjectionAnnotation() { 68 XAnnotation skipTestAnnotation = testElement().getAnnotation(ClassNames.SKIP_TEST_INJECTION); 69 if (skipTestAnnotation != null) { 70 return Optional.of(skipTestAnnotation); 71 } 72 73 Set<XAnnotation> annotatedAnnotations = testElement().getAnnotationsAnnotatedWith( 74 ClassNames.SKIP_TEST_INJECTION); 75 if (!annotatedAnnotations.isEmpty()) { 76 // Just return the first annotation that skips test injection if there are multiple since 77 // at this point it doesn't really matter and the specific annotation is only really useful 78 // for communicating back to the user. 79 return Optional.of(annotatedAnnotations.iterator().next()); 80 } 81 82 return Optional.empty(); 83 } 84 of(XProcessingEnv env, XElement element)85 static TestRootMetadata of(XProcessingEnv env, XElement element) { 86 87 XTypeElement testElement = XElements.asTypeElement(element); 88 XTypeElement baseElement = env.requireTypeElement(ClassNames.MULTI_DEX_APPLICATION); 89 90 ProcessorErrors.checkState( 91 !element.hasAnnotation(ClassNames.ANDROID_ENTRY_POINT), 92 element, 93 "Tests cannot be annotated with @AndroidEntryPoint. Please use @HiltAndroidTest"); 94 95 ProcessorErrors.checkState( 96 element.hasAnnotation(ClassNames.HILT_ANDROID_TEST), 97 element, 98 "Tests must be annotated with @HiltAndroidTest"); 99 100 return new AutoValue_TestRootMetadata(testElement, baseElement); 101 } 102 } 103