• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.membersinject;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import dagger.BindsInstance;
22 import dagger.Component;
23 import dagger.Module;
24 import dagger.Provides;
25 import javax.inject.Inject;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 @RunWith(JUnit4.class)
31 public final class MembersWithInstanceNameTest {
32 
33   static final class Foo {
34     // Checks that member injection fields can use "instance" as a name (b/175818837).
35     @Inject String instance;
36 
Foo()37     @Inject Foo() {}
38   }
39 
40   static final class Bar {
41     // Checks that member injection fields can use injecting a bound instance that was
42     // named "instance" when bound. Note that the field name here doesn't matter as of
43     // this writing, but name it "instance" anyway in case that changes.
44     // https://github.com/google/dagger/issues/4352
45     @Inject BoundInstance instance;
46 
Bar()47     @Inject Bar() {}
48   }
49 
50   @Module
51   interface TestModule {
52     @Provides
provideString()53     static String provideString() {
54       return "test";
55     }
56   }
57 
58   public static final class BoundInstance {}
59 
60   @Component(modules = TestModule.class)
61   interface TestComponent {
foo()62     Foo foo();
63 
bar()64     Bar bar();
65 
66     @Component.Builder
67     interface Builder {
68       // As of writing, the method name is the one that matters, but name the
69       // parameter the same anyway in case that changes.
instance(@indsInstance BoundInstance instance)70       Builder instance(@BindsInstance BoundInstance instance);
build()71       TestComponent build();
72     }
73   }
74 
75   @Component(modules = TestModule.class)
76   interface TestComponentWithFactory {
foo()77     Foo foo();
78 
bar()79     Bar bar();
80 
81     @Component.Factory
82     interface Factory {
83       // As of writing, the parameter name is the one that matters, but name the
84       // method the same anyway in case that changes.
instance(@indsInstance BoundInstance instance)85       TestComponentWithFactory instance(@BindsInstance BoundInstance instance);
86     }
87   }
88 
89   @Test
testMemberWithInstanceName()90   public void testMemberWithInstanceName() {
91     BoundInstance boundInstance = new BoundInstance();
92     TestComponent component = DaggerMembersWithInstanceNameTest_TestComponent
93         .builder().instance(boundInstance).build();
94     Foo foo = component.foo();
95     assertThat(foo).isNotNull();
96     assertThat(foo.instance).isEqualTo("test");
97     Bar bar = component.bar();
98     assertThat(bar).isNotNull();
99     assertThat(bar.instance).isSameInstanceAs(boundInstance);
100   }
101 
102   @Test
testMemberWithInstanceNameUsingFactory()103   public void testMemberWithInstanceNameUsingFactory() {
104     BoundInstance boundInstance = new BoundInstance();
105     TestComponentWithFactory component = DaggerMembersWithInstanceNameTest_TestComponentWithFactory
106         .factory().instance(boundInstance);
107     Foo foo = component.foo();
108     assertThat(foo).isNotNull();
109     assertThat(foo.instance).isEqualTo("test");
110     Bar bar = component.bar();
111     assertThat(bar).isNotNull();
112     assertThat(bar.instance).isSameInstanceAs(boundInstance);
113   }
114 }
115