xref: /aosp_15_r20/external/dagger2/javatests/dagger/functional/kotlinsrc/subcomponent/SubcomponentTest.kt (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2023 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.kotlinsrc.subcomponent
18 
19 import com.google.common.collect.Sets
20 import com.google.common.truth.Truth.assertThat
21 import com.google.common.truth.TruthJUnit.assume
22 import org.junit.Test
23 import org.junit.runner.RunWith
24 import org.junit.runners.Parameterized
25 
26 @RunWith(Parameterized::class)
27 class SubcomponentTest(val parentGetters: ParentGetters, val childComponent: ChildComponent) {
28   @Test
scopePropagatesUpward_classnull29   fun scopePropagatesUpward_class() {
30     assertThat(childComponent.requiresSingleton().singletonType())
31       .isSameInstanceAs(childComponent.requiresSingleton().singletonType())
32     assertThat(childComponent.requiresSingleton().singletonType())
33       .isSameInstanceAs(childComponent.newGrandchildComponent().requiresSingleton().singletonType())
34   }
35 
36   @Test
scopePropagatesUpward_providesnull37   fun scopePropagatesUpward_provides() {
38     assertThat(childComponent.requiresSingleton().unscopedTypeBoundAsSingleton())
39       .isSameInstanceAs(childComponent.requiresSingleton().unscopedTypeBoundAsSingleton())
40     assertThat(childComponent.requiresSingleton().unscopedTypeBoundAsSingleton())
41       .isSameInstanceAs(
42         childComponent.newGrandchildComponent().requiresSingleton().unscopedTypeBoundAsSingleton()
43       )
44   }
45 
46   @Test
multibindingContributionsnull47   fun multibindingContributions() {
48     val parentObjectSet: Set<Any> = parentGetters.objectSet()
49     assertThat(parentObjectSet).hasSize(2)
50     val childObjectSet: Set<Any> = childComponent.objectSet()
51     assertThat(childObjectSet).hasSize(3)
52     val grandchildObjectSet: Set<Any> = childComponent.newGrandchildComponent().objectSet()
53     assertThat(grandchildObjectSet).hasSize(4)
54     assertThat(Sets.intersection(parentObjectSet, childObjectSet)).hasSize(1)
55     assertThat(Sets.intersection(parentObjectSet, grandchildObjectSet)).hasSize(1)
56     assertThat(Sets.intersection(childObjectSet, grandchildObjectSet)).hasSize(1)
57   }
58 
59   @Test
unscopedProvidersnull60   fun unscopedProviders() {
61     assume().that(System.getProperty("dagger.mode")).doesNotContain("FastInit")
62     assertThat(parentGetters.getUnscopedTypeProvider())
63       .isSameInstanceAs(childComponent.getUnscopedTypeProvider())
64     assertThat(parentGetters.getUnscopedTypeProvider())
65       .isSameInstanceAs(childComponent.newGrandchildComponent().getUnscopedTypeProvider())
66   }
67 
68   @Test
passedModulesnull69   fun passedModules() {
70     val childModuleWithState: ChildModuleWithState = ChildModuleWithState()
71     val childComponent1: ChildComponentRequiringModules =
72       parentComponent.newChildComponentRequiringModules(
73         ChildModuleWithParameters(Any()),
74         childModuleWithState
75       )
76     val childComponent2: ChildComponentRequiringModules =
77       parentComponent.newChildComponentRequiringModules(
78         ChildModuleWithParameters(Any()),
79         childModuleWithState
80       )
81     assertThat(childComponent1.getInt()).isEqualTo(0)
82     assertThat(childComponent2.getInt()).isEqualTo(1)
83   }
84 
85   @Test
dependenceisInASubcomponentnull86   fun dependenceisInASubcomponent() {
87     assertThat(childComponent.newGrandchildComponent().needsAnInterface()).isNotNull()
88   }
89 
90   @Test
qualifiedSubcomponentIsBoundnull91   fun qualifiedSubcomponentIsBound() {
92     assertThat(parentComponent.unresolvableChildComponentBuilder().build().unboundString())
93       .isEqualTo("unbound")
94   }
95 
96   companion object {
97     private val parentComponent = DaggerParentComponent.create()
98     private val parentOfGenericComponent = DaggerParentOfGenericComponent.create()
99 
100     @JvmStatic
101     @Parameterized.Parameters
parametersnull102     fun parameters(): Collection<Array<Any>> {
103       return listOf(
104         arrayOf(parentComponent, parentComponent.newChildComponent()),
105         arrayOf(parentComponent, parentComponent.newChildAbstractClassComponent()),
106         arrayOf(parentOfGenericComponent, parentOfGenericComponent.subcomponent()),
107       )
108     }
109   }
110 }
111