• 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 androidx.room.compiler.processing.util.Source;
20 import com.google.common.collect.ImmutableCollection;
21 import com.google.common.collect.ImmutableList;
22 import com.google.common.collect.ImmutableMap;
23 import dagger.hilt.android.testing.compile.HiltCompilerTests;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.Parameterized;
27 import org.junit.runners.Parameterized.Parameters;
28 
29 @RunWith(Parameterized.class)
30 public final class RootProcessorErrorsTest {
31 
32   @Parameters(name = "{0}")
parameters()33   public static ImmutableCollection<Object[]> parameters() {
34     return ImmutableList.copyOf(new Object[][] {{true}, {false}});
35   }
36 
37   private final boolean disableCrossCompilationRootValidation;
38 
RootProcessorErrorsTest(boolean disableCrossCompilationRootValidation)39   public RootProcessorErrorsTest(boolean disableCrossCompilationRootValidation) {
40     this.disableCrossCompilationRootValidation = disableCrossCompilationRootValidation;
41   }
42 
processorOptions()43   private ImmutableMap<String, String> processorOptions() {
44     return ImmutableMap.of(
45         "dagger.hilt.disableCrossCompilationRootValidation",
46         Boolean.toString(disableCrossCompilationRootValidation));
47   }
48 
49   @Test
multipleAppRootsTest()50   public void multipleAppRootsTest() {
51     Source appRoot1 =
52         HiltCompilerTests.javaSource(
53             "test.AppRoot1",
54             "package test;",
55             "",
56             "import android.app.Application;",
57             "import dagger.hilt.android.HiltAndroidApp;",
58             "",
59             "@HiltAndroidApp(Application.class)",
60             "public class AppRoot1 extends Hilt_AppRoot1 {}");
61 
62     Source appRoot2 =
63         HiltCompilerTests.javaSource(
64             "test.AppRoot2",
65             "package test;",
66             "",
67             "import android.app.Application;",
68             "import dagger.hilt.android.HiltAndroidApp;",
69             "",
70             "@HiltAndroidApp(Application.class)",
71             "public class AppRoot2 extends Hilt_AppRoot2 {}");
72 
73     HiltCompilerTests.hiltCompiler(appRoot1, appRoot2)
74         .withProcessorOptions(processorOptions())
75         .compile(
76             subject -> {
77               // This test case should fail independent of disableCrossCompilationRootValidation.
78               subject.hasErrorCount(1);
79               subject.hasErrorContaining(
80                   "Cannot process multiple app roots in the same compilation unit: "
81                       + "test.AppRoot1, test.AppRoot2");
82             });
83   }
84 
85   @Test
appRootWithTestRootTest()86   public void appRootWithTestRootTest() {
87     Source appRoot =
88         HiltCompilerTests.javaSource(
89             "test.AppRoot",
90             "package test;",
91             "",
92             "import android.app.Application;",
93             "import dagger.hilt.android.HiltAndroidApp;",
94             "",
95             "@HiltAndroidApp(Application.class)",
96             "public class AppRoot extends Hilt_AppRoot {}");
97 
98     Source testRoot =
99         HiltCompilerTests.javaSource(
100             "test.TestRoot",
101             "package test;",
102             "",
103             "import dagger.hilt.android.testing.HiltAndroidTest;",
104             "",
105             "@HiltAndroidTest",
106             "public class TestRoot {}");
107 
108     HiltCompilerTests.hiltCompiler(appRoot, testRoot)
109         .withProcessorOptions(processorOptions())
110         .compile(
111             subject -> {
112               // This test case should fail independent of disableCrossCompilationRootValidation.
113               subject.hasErrorCount(1);
114               subject.hasErrorContaining(
115                   "Cannot process test roots and app roots in the same compilation unit:"
116                       + "\n    App root in this compilation unit: test.AppRoot"
117                       + "\n    Test roots in this compilation unit: test.TestRoot");
118             });
119   }
120 }
121