• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.factory;
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 dagger.Subcomponent;
26 import javax.inject.Inject;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.JUnit4;
30 
31 /**
32  * Tests for {@linkplain Subcomponent.Factory subcomponent factories}.
33  *
34  * <p>Most things are tested in {@code FactoryTest}; this is just intended to test some things like
35  * injecting subcomponent factories and returning them from component methods.
36  */
37 @RunWith(JUnit4.class)
38 public final class SubcomponentFactoryTest {
39 
40   @Component
41   interface ParentWithSubcomponentFactory {
subcomponentFactory()42     Sub.Factory subcomponentFactory();
43 
44     @Component.Factory
45     interface Factory {
create(@indsInstance int i)46       ParentWithSubcomponentFactory create(@BindsInstance int i);
47     }
48   }
49 
50   @Subcomponent
51   interface Sub {
i()52     int i();
s()53     String s();
54 
55     @Subcomponent.Factory
56     interface Factory {
create(@indsInstance String s)57       Sub create(@BindsInstance String s);
58     }
59   }
60 
61   @Test
parentComponentWithSubcomponentFactoryEntryPoint()62   public void parentComponentWithSubcomponentFactoryEntryPoint() {
63     ParentWithSubcomponentFactory parent =
64         DaggerSubcomponentFactoryTest_ParentWithSubcomponentFactory.factory().create(3);
65     Sub subcomponent = parent.subcomponentFactory().create("foo");
66     assertThat(subcomponent.i()).isEqualTo(3);
67     assertThat(subcomponent.s()).isEqualTo("foo");
68   }
69 
70   @Module(subcomponents = Sub.class)
71   abstract static class ModuleWithSubcomponent {
72     @Provides
provideInt()73     static int provideInt() {
74       return 42;
75     }
76   }
77 
78   static class UsesSubcomponentFactory {
79     private final Sub.Factory subFactory;
80 
81     @Inject
UsesSubcomponentFactory(Sub.Factory subFactory)82     UsesSubcomponentFactory(Sub.Factory subFactory) {
83       this.subFactory = subFactory;
84     }
85 
getSubcomponent(String s)86     Sub getSubcomponent(String s) {
87       return subFactory.create(s);
88     }
89   }
90 
91   @Component(modules = ModuleWithSubcomponent.class)
92   interface ParentWithModuleWithSubcomponent {
usesSubcomponentFactory()93     UsesSubcomponentFactory usesSubcomponentFactory();
94   }
95 
96   @Test
parentComponentWithModuleWithSubcomponent()97   public void parentComponentWithModuleWithSubcomponent() {
98     ParentWithModuleWithSubcomponent parent =
99         DaggerSubcomponentFactoryTest_ParentWithModuleWithSubcomponent.create();
100     UsesSubcomponentFactory usesSubcomponentFactory = parent.usesSubcomponentFactory();
101 
102     Sub subcomponent1 = usesSubcomponentFactory.getSubcomponent("foo");
103     assertThat(subcomponent1.i()).isEqualTo(42);
104     assertThat(subcomponent1.s()).isEqualTo("foo");
105 
106     Sub subcomponent2 = usesSubcomponentFactory.getSubcomponent("bar");
107     assertThat(subcomponent2.i()).isEqualTo(42);
108     assertThat(subcomponent2.s()).isEqualTo("bar");
109   }
110 }
111