• 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 static com.google.common.collect.Sets.cartesianProduct;
20 import static com.google.common.collect.Sets.immutableEnumSet;
21 import static dagger.internal.codegen.CompilerMode.DEFAULT_MODE;
22 import static dagger.internal.codegen.CompilerMode.FAST_INIT_MODE;
23 import static dagger.internal.codegen.base.ComponentCreatorAnnotation.SUBCOMPONENT_BUILDER;
24 import static dagger.internal.codegen.base.ComponentCreatorAnnotation.SUBCOMPONENT_FACTORY;
25 
26 import androidx.room.compiler.processing.util.Source;
27 import com.google.common.collect.ImmutableList;
28 import com.google.common.collect.Iterables;
29 import dagger.internal.codegen.base.ComponentCreatorAnnotation;
30 import dagger.testing.compile.CompilerTests;
31 import dagger.testing.golden.GoldenFileRule;
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.Set;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.junit.runners.Parameterized;
39 import org.junit.runners.Parameterized.Parameters;
40 
41 @RunWith(Parameterized.class)
42 public class SubcomponentCreatorRequestFulfillmentTest extends ComponentCreatorTestHelper {
43   @Parameters(name = "compilerMode={0}, creatorKind={1}")
parameters()44   public static Collection<Object[]> parameters() {
45     Set<List<Object>> params =
46         cartesianProduct(
47             immutableEnumSet(DEFAULT_MODE, FAST_INIT_MODE),
48             immutableEnumSet(SUBCOMPONENT_FACTORY, SUBCOMPONENT_BUILDER));
49     return ImmutableList.copyOf(Iterables.transform(params, Collection::toArray));
50   }
51 
52   @Rule public GoldenFileRule goldenFileRule = new GoldenFileRule();
53 
SubcomponentCreatorRequestFulfillmentTest( CompilerMode compilerMode, ComponentCreatorAnnotation componentCreatorAnnotation)54   public SubcomponentCreatorRequestFulfillmentTest(
55       CompilerMode compilerMode, ComponentCreatorAnnotation componentCreatorAnnotation) {
56     super(compilerMode, componentCreatorAnnotation);
57   }
58 
59   @Test
testInlinedSubcomponentCreators_componentMethod()60   public void testInlinedSubcomponentCreators_componentMethod() throws Exception {
61     Source subcomponent =
62         preprocessedJavaSource(
63             "test.Sub",
64             "package test;",
65             "",
66             "import dagger.Subcomponent;",
67             "",
68             "@Subcomponent",
69             "interface Sub {",
70             "  @Subcomponent.Builder",
71             "  interface Builder {",
72             "    Sub build();",
73             "  }",
74             "}");
75     Source usesSubcomponent =
76         preprocessedJavaSource(
77             "test.UsesSubcomponent",
78             "package test;",
79             "",
80             "import javax.inject.Inject;",
81             "",
82             "class UsesSubcomponent {",
83             "  @Inject UsesSubcomponent(Sub.Builder subBuilder) {}",
84             "}");
85     Source component =
86         preprocessedJavaSource(
87             "test.C",
88             "package test;",
89             "",
90             "import dagger.Component;",
91             "",
92             "@Component",
93             "interface C {",
94             "  Sub.Builder sBuilder();",
95             "  UsesSubcomponent usesSubcomponent();",
96             "}");
97 
98     CompilerTests.daggerCompiler(subcomponent, usesSubcomponent, component)
99         .compile(
100             subject -> {
101               subject.hasErrorCount(0);
102               subject.generatedSource(goldenFileRule.goldenSource("test/DaggerC"));
103             });
104   }
105 }
106