• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 static com.google.common.truth.Truth.assertThat;
20 import static dagger.hilt.android.testing.compile.HiltCompilerTests.compileWithKapt;
21 import static dagger.hilt.android.testing.compile.HiltCompilerTests.javaSource;
22 
23 import androidx.room.compiler.processing.util.DiagnosticMessage;
24 import androidx.room.compiler.processing.util.Source;
25 import com.google.common.collect.ImmutableCollection;
26 import com.google.common.collect.ImmutableList;
27 import com.google.common.collect.ImmutableMap;
28 import java.util.List;
29 import javax.tools.Diagnostic.Kind;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.TemporaryFolder;
33 import org.junit.runner.RunWith;
34 import org.junit.runners.Parameterized;
35 import org.junit.runners.Parameterized.Parameters;
36 
37 @RunWith(Parameterized.class)
38 public final class MyAppPreviousCompilationTest {
39 
40   @Parameters(name = "{0}")
parameters()41   public static ImmutableCollection<Object[]> parameters() {
42     return ImmutableList.copyOf(new Object[][] {{true}, {false}});
43   }
44 
45   @Rule public TemporaryFolder tempFolderRule = new TemporaryFolder();
46 
47   private final boolean disableCrossCompilationRootValidation;
48 
MyAppPreviousCompilationTest(boolean disableCrossCompilationRootValidation)49   public MyAppPreviousCompilationTest(boolean disableCrossCompilationRootValidation) {
50     this.disableCrossCompilationRootValidation = disableCrossCompilationRootValidation;
51   }
52 
processorOptions()53   private ImmutableMap<String, String> processorOptions() {
54     return ImmutableMap.of(
55         "dagger.hilt.disableCrossCompilationRootValidation",
56         Boolean.toString(disableCrossCompilationRootValidation));
57   }
58 
59   @Test
testRootTest()60   public void testRootTest() {
61     Source testRoot =
62         javaSource(
63             "test.TestRoot",
64             "package test;",
65             "",
66             "import dagger.hilt.android.testing.HiltAndroidTest;",
67             "",
68             "@HiltAndroidTest",
69             "public class TestRoot {}");
70 
71     // TODO(danysantiago): Add KSP test once b/288966076 is resolved.
72     // This test case should succeed independent of disableCrossCompilationRootValidation.
73     compileWithKapt(
74         ImmutableList.of(testRoot),
75         processorOptions(),
76         tempFolderRule,
77         result -> assertThat(result.getSuccess()).isTrue());
78   }
79 
80   @Test
appRootTest()81   public void appRootTest() {
82     Source appRoot =
83         javaSource(
84             "test.AppRoot",
85             "package test;",
86             "",
87             "import android.app.Application;",
88             "import dagger.hilt.android.HiltAndroidApp;",
89             "",
90             "@HiltAndroidApp(Application.class)",
91             "public class AppRoot extends Hilt_AppRoot {}");
92 
93     // TODO(danysantiago): Add KSP test once b/288966076 is resolved.
94     compileWithKapt(
95         ImmutableList.of(appRoot),
96         processorOptions(),
97         tempFolderRule,
98         result -> {
99           if (disableCrossCompilationRootValidation) {
100             assertThat(result.getSuccess()).isTrue();
101           } else {
102             List<DiagnosticMessage> errors = result.getDiagnostics().get(Kind.ERROR);
103             assertThat(errors).hasSize(1);
104             assertThat(errors.get(0).getMsg())
105                 .contains(
106                     "Cannot process new app roots when there are app roots from a "
107                         + "previous compilation unit:"
108                         + "\n    App roots in previous compilation unit: "
109                         + "dagger.hilt.processor.internal.root.MyAppPreviousCompilation.MyApp"
110                         + "\n    App roots in this compilation unit: test.AppRoot");
111           }
112         });
113   }
114 }
115