• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.android.processor.internal.customtestapplication;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.collect.ImmutableList;
22 import dagger.hilt.android.testing.compile.HiltCompilerTests;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.TemporaryFolder;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 @RunWith(JUnit4.class)
30 public class CustomTestApplicationProcessorTest {
31 
32   @Rule public TemporaryFolder tempFolderRule = new TemporaryFolder();
33 
34   @Test
validBaseClass_succeeds()35   public void validBaseClass_succeeds() {
36     // TODO(danysantiago): Add KSP test once b/288966076 is resolved.
37     HiltCompilerTests.compileWithKapt(
38         ImmutableList.of(
39             HiltCompilerTests.javaSource(
40                 "test.HiltTest",
41                 "package test;",
42                 "",
43                 "import android.app.Application;",
44                 "import dagger.hilt.android.testing.CustomTestApplication;",
45                 "import dagger.hilt.android.testing.HiltAndroidTest;",
46                 "",
47                 "@CustomTestApplication(Application.class)",
48                 "@HiltAndroidTest",
49                 "public class HiltTest {}")),
50         tempFolderRule,
51         result -> assertThat(result.getSuccess()).isTrue());
52   }
53 
54   @Test
incorrectBaseType_fails()55   public void incorrectBaseType_fails() {
56     HiltCompilerTests.hiltCompiler(
57             HiltCompilerTests.javaSource("test.Foo", "package test;", "", "public class Foo {}"),
58             HiltCompilerTests.javaSource(
59                 "test.HiltTest",
60                 "package test;",
61                 "",
62                 "import dagger.hilt.android.testing.CustomTestApplication;",
63                 "",
64                 "@CustomTestApplication(Foo.class)",
65                 "public class HiltTest {}"))
66         .compile(
67             subject -> {
68               subject.hasErrorContaining(
69                   "@CustomTestApplication value should be an instance of android.app.Application. "
70                       + "Found: test.Foo");
71             });
72   }
73 
74   @Test
baseWithHiltAndroidApp_fails()75   public void baseWithHiltAndroidApp_fails() {
76     HiltCompilerTests.hiltCompiler(
77             HiltCompilerTests.javaSource(
78                 "test.BaseApplication",
79                 "package test;",
80                 "",
81                 "import android.app.Application;",
82                 "import dagger.hilt.android.HiltAndroidApp;",
83                 "",
84                 "@HiltAndroidApp(Application.class)",
85                 "public class BaseApplication extends Hilt_BaseApplication {}"),
86             HiltCompilerTests.javaSource(
87                 "test.HiltTest",
88                 "package test;",
89                 "",
90                 "import dagger.hilt.android.testing.CustomTestApplication;",
91                 "",
92                 "@CustomTestApplication(BaseApplication.class)",
93                 "public class HiltTest {}"))
94         .compile(
95             subject -> {
96               subject.hasErrorContaining(
97                   "@CustomTestApplication value cannot be annotated with @HiltAndroidApp. "
98                       + "Found: test.BaseApplication");
99             });
100   }
101 
102   @Test
superclassWithHiltAndroidApp_fails()103   public void superclassWithHiltAndroidApp_fails() {
104     HiltCompilerTests.hiltCompiler(
105             HiltCompilerTests.javaSource(
106                 "test.BaseApplication",
107                 "package test;",
108                 "",
109                 "import android.app.Application;",
110                 "import dagger.hilt.android.HiltAndroidApp;",
111                 "",
112                 "@HiltAndroidApp(Application.class)",
113                 "public class BaseApplication extends Hilt_BaseApplication {}"),
114             HiltCompilerTests.javaSource(
115                 "test.ParentApplication",
116                 "package test;",
117                 "",
118                 "public class ParentApplication extends BaseApplication {}"),
119             HiltCompilerTests.javaSource(
120                 "test.HiltTest",
121                 "package test;",
122                 "",
123                 "import dagger.hilt.android.testing.CustomTestApplication;",
124                 "",
125                 "@CustomTestApplication(ParentApplication.class)",
126                 "public class HiltTest {}"))
127         .compile(
128             subject -> {
129               subject.hasErrorContaining(
130                   "@CustomTestApplication value cannot be annotated with @HiltAndroidApp. "
131                       + "Found: test.BaseApplication");
132             });
133   }
134 
135   @Test
withInjectField_fails()136   public void withInjectField_fails() {
137     HiltCompilerTests.hiltCompiler(
138             HiltCompilerTests.javaSource(
139                 "test.BaseApplication",
140                 "package test;",
141                 "",
142                 "import android.app.Application;",
143                 "import javax.inject.Inject;",
144                 "",
145                 "public class BaseApplication extends Application {",
146                 "  @Inject String str;",
147                 "}"),
148             HiltCompilerTests.javaSource(
149                 "test.HiltTest",
150                 "package test;",
151                 "",
152                 "import dagger.hilt.android.testing.CustomTestApplication;",
153                 "",
154                 "@CustomTestApplication(BaseApplication.class)",
155                 "public class HiltTest {}"))
156         .compile(
157             subject -> {
158               subject.hasErrorContaining(
159                   "@CustomTestApplication does not support application classes (or super classes)"
160                       + " with @Inject fields. Found test.BaseApplication with @Inject fields"
161                       + " [str]");
162             });
163   }
164 
165   @Test
withSuperclassInjectField_fails()166   public void withSuperclassInjectField_fails() {
167     HiltCompilerTests.hiltCompiler(
168             HiltCompilerTests.javaSource(
169                 "test.BaseApplication",
170                 "package test;",
171                 "",
172                 "import android.app.Application;",
173                 "import javax.inject.Inject;",
174                 "",
175                 "public class BaseApplication extends Application {",
176                 "  @Inject String str;",
177                 "}"),
178             HiltCompilerTests.javaSource(
179                 "test.ParentApplication",
180                 "package test;",
181                 "",
182                 "public class ParentApplication extends BaseApplication {}"),
183             HiltCompilerTests.javaSource(
184                 "test.HiltTest",
185                 "package test;",
186                 "",
187                 "import dagger.hilt.android.testing.CustomTestApplication;",
188                 "",
189                 "@CustomTestApplication(ParentApplication.class)",
190                 "public class HiltTest {}"))
191         .compile(
192             subject -> {
193               subject.hasErrorContaining(
194                   "@CustomTestApplication does not support application classes (or super classes)"
195                       + " with @Inject fields. Found test.BaseApplication with @Inject fields"
196                       + " [str]");
197             });
198   }
199 
200   @Test
withInjectMethod_fails()201   public void withInjectMethod_fails() {
202     HiltCompilerTests.hiltCompiler(
203             HiltCompilerTests.javaSource(
204                 "test.BaseApplication",
205                 "package test;",
206                 "",
207                 "import android.app.Application;",
208                 "import javax.inject.Inject;",
209                 "",
210                 "public class BaseApplication extends Application {",
211                 "  @Inject String str() { return null; }",
212                 "}"),
213             HiltCompilerTests.javaSource(
214                 "test.HiltTest",
215                 "package test;",
216                 "",
217                 "import dagger.hilt.android.testing.CustomTestApplication;",
218                 "",
219                 "@CustomTestApplication(BaseApplication.class)",
220                 "public class HiltTest {}"))
221         .compile(
222             subject -> {
223               subject.hasErrorContaining(
224                   "@CustomTestApplication does not support application classes (or super classes)"
225                       + " with @Inject methods. Found test.BaseApplication with @Inject methods"
226                       + " [str()]");
227             });
228   }
229 
230   @Test
withSuperclassInjectMethod_fails()231   public void withSuperclassInjectMethod_fails() {
232     HiltCompilerTests.hiltCompiler(
233             HiltCompilerTests.javaSource(
234                 "test.BaseApplication",
235                 "package test;",
236                 "",
237                 "import android.app.Application;",
238                 "import javax.inject.Inject;",
239                 "",
240                 "public class BaseApplication extends Application {",
241                 "  @Inject String str() { return null; }",
242                 "}"),
243             HiltCompilerTests.javaSource(
244                 "test.ParentApplication",
245                 "package test;",
246                 "",
247                 "public class ParentApplication extends BaseApplication {}"),
248             HiltCompilerTests.javaSource(
249                 "test.HiltTest",
250                 "package test;",
251                 "",
252                 "import dagger.hilt.android.testing.CustomTestApplication;",
253                 "",
254                 "@CustomTestApplication(ParentApplication.class)",
255                 "public class HiltTest {}"))
256         .compile(
257             subject -> {
258               subject.hasErrorContaining(
259                   "@CustomTestApplication does not support application classes (or super classes)"
260                       + " with @Inject methods. Found test.BaseApplication with @Inject methods"
261                       + " [str()]");
262             });
263   }
264 
265   @Test
withInjectConstructor_fails()266   public void withInjectConstructor_fails() {
267     HiltCompilerTests.hiltCompiler(
268             HiltCompilerTests.javaSource(
269                 "test.BaseApplication",
270                 "package test;",
271                 "",
272                 "import android.app.Application;",
273                 "import javax.inject.Inject;",
274                 "",
275                 "public class BaseApplication extends Application {",
276                 "  @Inject BaseApplication() {}",
277                 "}"),
278             HiltCompilerTests.javaSource(
279                 "test.HiltTest",
280                 "package test;",
281                 "",
282                 "import dagger.hilt.android.testing.CustomTestApplication;",
283                 "",
284                 "@CustomTestApplication(BaseApplication.class)",
285                 "public class HiltTest {}"))
286         .compile(
287             subject -> {
288               subject.hasErrorContaining(
289                   "@CustomTestApplication does not support application classes (or super classes)"
290                       + " with @Inject constructors. Found test.BaseApplication with @Inject"
291                       + " constructors [BaseApplication()]");
292             });
293   }
294 
295   @Test
withSuperclassInjectConstructor_fails()296   public void withSuperclassInjectConstructor_fails() {
297     HiltCompilerTests.hiltCompiler(
298             HiltCompilerTests.javaSource(
299                 "test.BaseApplication",
300                 "package test;",
301                 "",
302                 "import android.app.Application;",
303                 "import javax.inject.Inject;",
304                 "",
305                 "public class BaseApplication extends Application {",
306                 "  @Inject BaseApplication() {}",
307                 "}"),
308             HiltCompilerTests.javaSource(
309                 "test.ParentApplication",
310                 "package test;",
311                 "",
312                 "public class ParentApplication extends BaseApplication {}"),
313             HiltCompilerTests.javaSource(
314                 "test.HiltTest",
315                 "package test;",
316                 "",
317                 "import dagger.hilt.android.testing.CustomTestApplication;",
318                 "",
319                 "@CustomTestApplication(ParentApplication.class)",
320                 "public class HiltTest {}"))
321         .compile(
322             subject -> {
323               subject.hasErrorContaining(
324                   "@CustomTestApplication does not support application classes (or super classes)"
325                       + " with @Inject constructors. Found test.BaseApplication with @Inject"
326                       + " constructors [BaseApplication()]");
327             });
328   }
329 }
330