xref: /aosp_15_r20/external/dagger2/javatests/dagger/functional/kotlinsrc/binds/ScopedBindsTest.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.binds
18 
19 import com.google.common.truth.Truth.assertThat
20 import dagger.Binds
21 import dagger.Component
22 import dagger.Module
23 import javax.inject.Inject
24 import javax.inject.Provider
25 import javax.inject.Singleton
26 import org.junit.Test
27 import org.junit.runner.RunWith
28 import org.junit.runners.JUnit4
29 
30 @RunWith(JUnit4::class)
31 class ScopedBindsTest {
32   internal interface Foo
33 
34   internal class FooImpl @Inject constructor() : Foo
35 
36   @Module
37   internal interface FooModule {
38     @Binds
39     @Singleton
bindFoonull40     fun bindFoo(impl: FooImpl): Foo
41   }
42 
43   @Component(modules = [FooModule::class])
44   @Singleton
45   internal interface TestComponent {
46     fun foo(): Foo
47     fun fooProvider(): Provider<Foo>
48   }
49 
50   @Test
fooVsFooProvider_sameInstanceTestnull51   fun fooVsFooProvider_sameInstanceTest() {
52     val component = DaggerScopedBindsTest_TestComponent.create()
53 
54     // These should be the same instance because Foo is scoped
55     assertThat(component.foo()).isSameInstanceAs(component.fooProvider().get())
56   }
57 }
58