• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.generatesrootinput;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import androidx.room.compiler.processing.XElement;
22 import androidx.room.compiler.processing.XFiler.Mode;
23 import androidx.room.compiler.processing.XProcessingEnv;
24 import androidx.room.compiler.processing.XRoundEnv;
25 import androidx.room.compiler.processing.util.Source;
26 import com.google.common.truth.Correspondence;
27 import com.squareup.javapoet.ClassName;
28 import com.squareup.javapoet.JavaFile;
29 import com.squareup.javapoet.TypeSpec;
30 import dagger.hilt.GeneratesRootInput;
31 import dagger.hilt.android.testing.compile.HiltCompilerTests;
32 import dagger.hilt.processor.internal.BaseProcessingStep;
33 import dagger.internal.codegen.xprocessing.XElements;
34 import java.util.ArrayList;
35 import java.util.List;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.junit.runners.JUnit4;
39 
40 /** Tests that {@link GeneratesRootInputs} returns the elements to wait for. */
41 @RunWith(JUnit4.class)
42 public final class GeneratesRootInputProcessorTest {
43   private static final int GENERATED_CLASSES = 5;
44   private static final ClassName TEST_ANNOTATION = ClassName.get("test", "TestAnnotation");
45 
46   private final List<XElement> elementsToWaitFor = new ArrayList<>();
47   private int generatedClasses = 0;
48 
49   public final class TestAnnotationStep extends BaseProcessingStep {
50     private GeneratesRootInputs generatesRootInputs;
51 
TestAnnotationStep(XProcessingEnv env)52     public TestAnnotationStep(XProcessingEnv env) {
53       super(env);
54       generatesRootInputs = new GeneratesRootInputs(env);
55     }
56 
57     @Override
postProcess(XProcessingEnv processingEnv, XRoundEnv round)58     public void postProcess(XProcessingEnv processingEnv, XRoundEnv round) {
59       if (generatedClasses > 0) {
60         elementsToWaitFor.addAll(generatesRootInputs.getElementsToWaitFor(round));
61       }
62       if (generatedClasses < GENERATED_CLASSES) {
63         TypeSpec typeSpec =
64             TypeSpec.classBuilder("Foo" + generatedClasses++)
65                 .addAnnotation(TEST_ANNOTATION)
66                 .build();
67         processingEnv.getFiler().write(JavaFile.builder("foo", typeSpec).build(), Mode.Isolating);
68       }
69     }
70   }
71 
72   @Test
succeeds_ComponentProcessorWaitsForAnnotationsWithGeneratesRootInput()73   public void succeeds_ComponentProcessorWaitsForAnnotationsWithGeneratesRootInput() {
74     Source testAnnotation =
75         HiltCompilerTests.javaSource(
76             "test.TestAnnotation",
77             "package test;",
78             "@" + GeneratesRootInput.class.getCanonicalName(),
79             "public @interface TestAnnotation {}");
80 
81     HiltCompilerTests.hiltCompiler(testAnnotation)
82         .withProcessingSteps(TestAnnotationStep::new)
83         .compile(
84             subject -> {
85               subject.hasErrorCount(0);
86               assertThat(elementsToWaitFor)
87                   .comparingElementsUsing(
88                       Correspondence.<XElement, String>transforming(
89                           element -> XElements.asTypeElement(element).getQualifiedName(),
90                           "has qualified name of"))
91                   .containsExactly("foo.Foo0", "foo.Foo1", "foo.Foo2", "foo.Foo3", "foo.Foo4")
92                   .inOrder();
93               elementsToWaitFor.clear();
94               generatedClasses = 0;
95             });
96   }
97 }
98