• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.internal.codegen;
18 
19 import androidx.room.compiler.processing.util.Source;
20 import dagger.testing.compile.CompilerTests;
21 import dagger.testing.golden.GoldenFileRule;
22 import java.util.Collection;
23 import org.junit.Rule;
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 class ElidedFactoriesTest {
31   @Parameters(name = "{0}")
parameters()32   public static Collection<Object[]> parameters() {
33     return CompilerMode.TEST_PARAMETERS;
34   }
35 
36   @Rule public GoldenFileRule goldenFileRule = new GoldenFileRule();
37 
38   private final CompilerMode compilerMode;
39 
ElidedFactoriesTest(CompilerMode compilerMode)40   public ElidedFactoriesTest(CompilerMode compilerMode) {
41     this.compilerMode = compilerMode;
42   }
43 
44   @Test
simpleComponent()45   public void simpleComponent() throws Exception {
46     Source injectedType =
47         CompilerTests.javaSource(
48             "test.InjectedType",
49             "package test;",
50             "",
51             "import javax.inject.Inject;",
52             "",
53             "final class InjectedType {",
54             "  @Inject InjectedType() {}",
55             "}");
56 
57     Source dependsOnInjected =
58         CompilerTests.javaSource(
59             "test.DependsOnInjected",
60             "package test;",
61             "",
62             "import javax.inject.Inject;",
63             "",
64             "final class DependsOnInjected {",
65             "  @Inject DependsOnInjected(InjectedType injected) {}",
66             "}");
67     Source componentFile =
68         CompilerTests.javaSource(
69             "test.SimpleComponent",
70             "package test;",
71             "",
72             "import dagger.Component;",
73             "",
74             "@Component",
75             "interface SimpleComponent {",
76             "  DependsOnInjected dependsOnInjected();",
77             "}");
78 
79     CompilerTests.daggerCompiler(injectedType, dependsOnInjected, componentFile)
80         .withProcessingOptions(compilerMode.processorOptions())
81         .compile(
82             subject -> {
83               subject.hasErrorCount(0);
84               subject.generatedSource(goldenFileRule.goldenSource("test/DaggerSimpleComponent"));
85             });
86   }
87 
88   @Test
simpleComponent_injectsProviderOf_dependsOnScoped()89   public void simpleComponent_injectsProviderOf_dependsOnScoped() throws Exception {
90     Source scopedType =
91         CompilerTests.javaSource(
92             "test.ScopedType",
93             "package test;",
94             "",
95             "import javax.inject.Inject;",
96             "import javax.inject.Singleton;",
97             "",
98             "@Singleton",
99             "final class ScopedType {",
100             "  @Inject ScopedType() {}",
101             "}");
102 
103     Source dependsOnScoped =
104         CompilerTests.javaSource(
105             "test.DependsOnScoped",
106             "package test;",
107             "",
108             "import javax.inject.Inject;",
109             "import javax.inject.Provider;",
110             "",
111             "final class DependsOnScoped {",
112             "  @Inject DependsOnScoped(ScopedType scoped) {}",
113             "}");
114 
115     Source needsProvider =
116         CompilerTests.javaSource(
117             "test.NeedsProvider",
118             "package test;",
119             "",
120             "import javax.inject.Inject;",
121             "import javax.inject.Provider;",
122             "",
123             "class NeedsProvider {",
124             "  @Inject NeedsProvider(Provider<DependsOnScoped> provider) {}",
125             "}");
126     Source componentFile =
127         CompilerTests.javaSource(
128             "test.SimpleComponent",
129             "package test;",
130             "",
131             "import dagger.Component;",
132             "import javax.inject.Singleton;",
133             "",
134             "@Singleton",
135             "@Component",
136             "interface SimpleComponent {",
137             "  NeedsProvider needsProvider();",
138             "}");
139 
140     CompilerTests.daggerCompiler(scopedType, dependsOnScoped, componentFile, needsProvider)
141         .withProcessingOptions(compilerMode.processorOptions())
142         .compile(
143             subject -> {
144               subject.hasErrorCount(0);
145               subject.generatedSource(goldenFileRule.goldenSource("test/DaggerSimpleComponent"));
146             });
147   }
148 
149   @Test
scopedBinding_onlyUsedInSubcomponent()150   public void scopedBinding_onlyUsedInSubcomponent() throws Exception {
151     Source scopedType =
152         CompilerTests.javaSource(
153             "test.ScopedType",
154             "package test;",
155             "",
156             "import javax.inject.Inject;",
157             "import javax.inject.Singleton;",
158             "",
159             "@Singleton",
160             "final class ScopedType {",
161             "  @Inject ScopedType() {}",
162             "}");
163 
164     Source dependsOnScoped =
165         CompilerTests.javaSource(
166             "test.DependsOnScoped",
167             "package test;",
168             "",
169             "import javax.inject.Inject;",
170             "import javax.inject.Provider;",
171             "",
172             "final class DependsOnScoped {",
173             "  @Inject DependsOnScoped(ScopedType scoped) {}",
174             "}");
175     Source componentFile =
176         CompilerTests.javaSource(
177             "test.SimpleComponent",
178             "package test;",
179             "",
180             "import dagger.Component;",
181             "import javax.inject.Singleton;",
182             "",
183             "@Singleton",
184             "@Component",
185             "interface SimpleComponent {",
186             "  Sub sub();",
187             "}");
188     Source subcomponentFile =
189         CompilerTests.javaSource(
190             "test.Sub",
191             "package test;",
192             "",
193             "import dagger.Subcomponent;",
194             "",
195             "@Subcomponent",
196             "interface Sub {",
197             "  DependsOnScoped dependsOnScoped();",
198             "}");
199 
200     CompilerTests.daggerCompiler(scopedType, dependsOnScoped, componentFile, subcomponentFile)
201         .withProcessingOptions(compilerMode.processorOptions())
202         .compile(
203             subject -> {
204               subject.hasErrorCount(0);
205               subject.generatedSource(goldenFileRule.goldenSource("test/DaggerSimpleComponent"));
206             });
207   }
208 }
209