xref: /aosp_15_r20/external/dagger2/javatests/dagger/functional/kotlinsrc/cycle/CycleTest.kt (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2022 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.cycle
18 
19 import com.google.common.truth.Truth.assertThat
20 import org.junit.Test
21 import org.junit.runner.RunWith
22 import org.junit.runners.JUnit4
23 
24 @RunWith(JUnit4::class)
25 class CycleTest {
26   @Test
providerIndirectionSelfCyclenull27   fun providerIndirectionSelfCycle() {
28     val selfCycleComponent = DaggerCycles_SelfCycleComponent.create()
29     val s = selfCycleComponent.s()
30     assertThat(s.sProvider.get()).isNotNull()
31   }
32 
33   @Test
providerIndirectionCyclenull34   fun providerIndirectionCycle() {
35     val cycleComponent = DaggerCycles_CycleComponent.create()
36     val a = cycleComponent.a()
37     val c = cycleComponent.c()
38     assertThat(c.aProvider.get()).isNotNull()
39     assertThat(a.b.c.aProvider.get()).isNotNull()
40     assertThat(a.e.d.b.c.aProvider.get()).isNotNull()
41   }
42 
43   @Test
lazyIndirectionSelfCyclenull44   fun lazyIndirectionSelfCycle() {
45     val selfCycleComponent = DaggerCycles_SelfCycleComponent.create()
46     val s = selfCycleComponent.s()
47     assertThat(s.sLazy.get()).isNotNull()
48   }
49 
50   @Test
lazyIndirectionCyclenull51   fun lazyIndirectionCycle() {
52     val cycleComponent = DaggerCycles_CycleComponent.create()
53     val a = cycleComponent.a()
54     val c = cycleComponent.c()
55     assertThat(c.aLazy.get()).isNotNull()
56     assertThat(a.b.c.aLazy.get()).isNotNull()
57     assertThat(a.e.d.b.c.aLazy.get()).isNotNull()
58   }
59 
60   @Test
subcomponentIndirectionCyclenull61   fun subcomponentIndirectionCycle() {
62     val childCycleComponent = DaggerCycles_CycleComponent.create().child()
63     val a = childCycleComponent.a()
64     assertThat(a.b.c.aProvider.get()).isNotNull()
65     assertThat(a.e.d.b.c.aProvider.get()).isNotNull()
66   }
67 
68   @Test
providerMapIndirectionCyclenull69   fun providerMapIndirectionCycle() {
70     val cycleMapComponent = DaggerCycles_CycleMapComponent.create()
71     assertThat(cycleMapComponent.y()).isNotNull()
72     assertThat(cycleMapComponent.y().mapOfProvidersOfX).containsKey("X")
73     assertThat(cycleMapComponent.y().mapOfProvidersOfX.get("X")).isNotNull()
74     assertThat(cycleMapComponent.y().mapOfProvidersOfX.get("X")!!.get()).isNotNull()
75     assertThat(cycleMapComponent.y().mapOfProvidersOfX.get("X")!!.get().y).isNotNull()
76     assertThat(cycleMapComponent.y().mapOfProvidersOfX).hasSize(1)
77     assertThat(cycleMapComponent.y().mapOfProvidersOfY).containsKey("Y")
78     assertThat(cycleMapComponent.y().mapOfProvidersOfY.get("Y")).isNotNull()
79     assertThat(cycleMapComponent.y().mapOfProvidersOfY.get("Y")!!.get()).isNotNull()
80     assertThat(cycleMapComponent.y().mapOfProvidersOfY.get("Y")!!.get().mapOfProvidersOfX).hasSize(1)
81     assertThat(cycleMapComponent.y().mapOfProvidersOfY.get("Y")!!.get().mapOfProvidersOfY).hasSize(1)
82     assertThat(cycleMapComponent.y().mapOfProvidersOfY).hasSize(1)
83   }
84 
85   /**
86    * Tests that a cycle where a `@Binds` binding depends on a binding that has to be deferred works.
87    */
88   @Test
cycleWithDeferredBindsnull89   fun cycleWithDeferredBinds() {
90     val bindsCycleComponent = DaggerCycles_BindsCycleComponent.create()
91     assertThat(bindsCycleComponent.bar()).isNotNull()
92   }
93 }
94