• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.factory
18 
19 import com.google.common.truth.Truth.assertThat
20 import dagger.BindsInstance
21 import dagger.Component
22 import dagger.Module
23 import dagger.Provides
24 import dagger.Subcomponent
25 import javax.inject.Inject
26 import org.junit.Test
27 import org.junit.runner.RunWith
28 import org.junit.runners.JUnit4
29 
30 /**
31  * Tests for [subcomponent factories][Subcomponent.Factory].
32  *
33  * Most things are tested in `FactoryTest`; this is just intended to test some things like injecting
34  * subcomponent factories and returning them from component methods.
35  */
36 @RunWith(JUnit4::class)
37 class SubcomponentFactoryTest {
38   @Component
39   internal interface ParentWithSubcomponentFactory {
subcomponentFactorynull40     fun subcomponentFactory(): Sub.Factory
41 
42     @Component.Factory
43     interface Factory {
44       fun create(@BindsInstance i: Int): ParentWithSubcomponentFactory
45     }
46   }
47 
48   @Subcomponent
49   internal interface Sub {
inull50     fun i(): Int
51     fun s(): String
52 
53     @Subcomponent.Factory
54     interface Factory {
55       fun create(@BindsInstance s: String): Sub
56     }
57   }
58 
59   @Test
parentComponentWithSubcomponentFactoryEntryPointnull60   fun parentComponentWithSubcomponentFactoryEntryPoint() {
61     val parent = DaggerSubcomponentFactoryTest_ParentWithSubcomponentFactory.factory().create(3)
62     val subcomponent = parent.subcomponentFactory().create("foo")
63     assertThat(subcomponent.i()).isEqualTo(3)
64     assertThat(subcomponent.s()).isEqualTo("foo")
65   }
66 
67   @Module(subcomponents = [Sub::class])
68   internal object ModuleWithSubcomponent {
provideIntnull69     @Provides fun provideInt(): Int = 42
70   }
71 
72   internal class UsesSubcomponentFactory @Inject constructor(private val subFactory: Sub.Factory) {
73     fun getSubcomponent(s: String): Sub {
74       return subFactory.create(s)
75     }
76   }
77 
78   @Component(modules = [ModuleWithSubcomponent::class])
79   internal interface ParentWithModuleWithSubcomponent {
usesSubcomponentFactorynull80     fun usesSubcomponentFactory(): UsesSubcomponentFactory
81   }
82 
83   @Test
84   fun parentComponentWithModuleWithSubcomponent() {
85     val parent = DaggerSubcomponentFactoryTest_ParentWithModuleWithSubcomponent.create()
86     val usesSubcomponentFactory = parent.usesSubcomponentFactory()
87     val subcomponent1 = usesSubcomponentFactory.getSubcomponent("foo")
88     assertThat(subcomponent1.i()).isEqualTo(42)
89     assertThat(subcomponent1.s()).isEqualTo("foo")
90     val subcomponent2 = usesSubcomponentFactory.getSubcomponent("bar")
91     assertThat(subcomponent2.i()).isEqualTo(42)
92     assertThat(subcomponent2.s()).isEqualTo("bar")
93   }
94 }
95