xref: /aosp_15_r20/external/dagger2/javatests/dagger/functional/subcomponent/SubcomponentTest.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2015 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.functional.subcomponent;
18 
19 import static com.google.common.collect.Sets.intersection;
20 import static com.google.common.truth.Truth.assertThat;
21 import static com.google.common.truth.TruthJUnit.assume;
22 
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Set;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.Parameterized;
29 import org.junit.runners.Parameterized.Parameters;
30 
31 @RunWith(Parameterized.class)
32 public class SubcomponentTest {
33   private static final ParentComponent parentComponent = DaggerParentComponent.create();
34   private static final ParentOfGenericComponent parentOfGenericComponent =
35       DaggerParentOfGenericComponent.create();
36 
37   @Parameters
parameters()38   public static Collection<Object[]> parameters() {
39     return Arrays.asList(new Object[][] {
40         { parentComponent, parentComponent.newChildComponent() },
41         { parentComponent, parentComponent.newChildAbstractClassComponent() },
42         { parentOfGenericComponent, parentOfGenericComponent.subcomponent() }});
43   }
44 
45   private final ParentGetters parentGetters;
46   private final ChildComponent childComponent;
47 
SubcomponentTest(ParentGetters parentGetters, ChildComponent childComponent)48   public SubcomponentTest(ParentGetters parentGetters, ChildComponent childComponent) {
49     this.parentGetters = parentGetters;
50     this.childComponent = childComponent;
51   }
52 
53 
54   @Test
scopePropagatesUpward_class()55   public void scopePropagatesUpward_class() {
56     assertThat(childComponent.requiresSingleton().singletonType())
57         .isSameInstanceAs(childComponent.requiresSingleton().singletonType());
58     assertThat(childComponent.requiresSingleton().singletonType())
59         .isSameInstanceAs(
60             childComponent.newGrandchildComponent().requiresSingleton().singletonType());
61   }
62 
63   @Test
scopePropagatesUpward_provides()64   public void scopePropagatesUpward_provides() {
65     assertThat(childComponent.requiresSingleton().unscopedTypeBoundAsSingleton())
66         .isSameInstanceAs(childComponent.requiresSingleton().unscopedTypeBoundAsSingleton());
67     assertThat(childComponent.requiresSingleton().unscopedTypeBoundAsSingleton())
68         .isSameInstanceAs(
69             childComponent
70                 .newGrandchildComponent()
71                 .requiresSingleton()
72                 .unscopedTypeBoundAsSingleton());
73   }
74 
75   @Test
multibindingContributions()76   public void multibindingContributions() {
77     Set<Object> parentObjectSet = parentGetters.objectSet();
78     assertThat(parentObjectSet).hasSize(2);
79     Set<Object> childObjectSet = childComponent.objectSet();
80     assertThat(childObjectSet).hasSize(3);
81     Set<Object> grandchildObjectSet =
82         childComponent.newGrandchildComponent().objectSet();
83     assertThat(grandchildObjectSet).hasSize(4);
84     assertThat(intersection(parentObjectSet, childObjectSet)).hasSize(1);
85     assertThat(intersection(parentObjectSet, grandchildObjectSet)).hasSize(1);
86     assertThat(intersection(childObjectSet, grandchildObjectSet)).hasSize(1);
87   }
88 
89   @Test
unscopedProviders()90   public void unscopedProviders() {
91     assume().that(System.getProperty("dagger.mode")).doesNotContain("FastInit");
92     assertThat(parentGetters.getUnscopedTypeProvider())
93         .isSameInstanceAs(childComponent.getUnscopedTypeProvider());
94     assertThat(parentGetters.getUnscopedTypeProvider())
95         .isSameInstanceAs(childComponent.newGrandchildComponent().getUnscopedTypeProvider());
96   }
97 
98   @Test
passedModules()99   public void passedModules() {
100     ChildModuleWithState childModuleWithState = new ChildModuleWithState();
101     ChildComponentRequiringModules childComponent1 =
102         parentComponent.newChildComponentRequiringModules(
103             new ChildModuleWithParameters(new Object()),
104             childModuleWithState);
105     ChildComponentRequiringModules childComponent2 =
106         parentComponent.newChildComponentRequiringModules(
107             new ChildModuleWithParameters(new Object()),
108             childModuleWithState);
109     assertThat(childComponent1.getInt()).isEqualTo(0);
110     assertThat(childComponent2.getInt()).isEqualTo(1);
111   }
112 
113   @Test
dependenceisInASubcomponent()114   public void dependenceisInASubcomponent() {
115     assertThat(childComponent.newGrandchildComponent().needsAnInterface()).isNotNull();
116   }
117 
118   @Test
qualifiedSubcomponentIsBound()119   public void qualifiedSubcomponentIsBound() {
120     assertThat(parentComponent.unresolvableChildComponentBuilder().build().unboundString())
121         .isEqualTo("unbound");
122   }
123 }
124