1*f585d8a3SJacky Wang /* 2*f585d8a3SJacky Wang * Copyright (C) 2015 The Dagger Authors. 3*f585d8a3SJacky Wang * 4*f585d8a3SJacky Wang * Licensed under the Apache License, Version 2.0 (the "License"); 5*f585d8a3SJacky Wang * you may not use this file except in compliance with the License. 6*f585d8a3SJacky Wang * You may obtain a copy of the License at 7*f585d8a3SJacky Wang * 8*f585d8a3SJacky Wang * http://www.apache.org/licenses/LICENSE-2.0 9*f585d8a3SJacky Wang * 10*f585d8a3SJacky Wang * Unless required by applicable law or agreed to in writing, software 11*f585d8a3SJacky Wang * distributed under the License is distributed on an "AS IS" BASIS, 12*f585d8a3SJacky Wang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*f585d8a3SJacky Wang * See the License for the specific language governing permissions and 14*f585d8a3SJacky Wang * limitations under the License. 15*f585d8a3SJacky Wang */ 16*f585d8a3SJacky Wang 17*f585d8a3SJacky Wang package dagger.functional.subcomponent; 18*f585d8a3SJacky Wang 19*f585d8a3SJacky Wang import static com.google.common.collect.Sets.intersection; 20*f585d8a3SJacky Wang import static com.google.common.truth.Truth.assertThat; 21*f585d8a3SJacky Wang import static com.google.common.truth.TruthJUnit.assume; 22*f585d8a3SJacky Wang 23*f585d8a3SJacky Wang import java.util.Arrays; 24*f585d8a3SJacky Wang import java.util.Collection; 25*f585d8a3SJacky Wang import java.util.Set; 26*f585d8a3SJacky Wang import org.junit.Test; 27*f585d8a3SJacky Wang import org.junit.runner.RunWith; 28*f585d8a3SJacky Wang import org.junit.runners.Parameterized; 29*f585d8a3SJacky Wang import org.junit.runners.Parameterized.Parameters; 30*f585d8a3SJacky Wang 31*f585d8a3SJacky Wang @RunWith(Parameterized.class) 32*f585d8a3SJacky Wang public class SubcomponentTest { 33*f585d8a3SJacky Wang private static final ParentComponent parentComponent = DaggerParentComponent.create(); 34*f585d8a3SJacky Wang private static final ParentOfGenericComponent parentOfGenericComponent = 35*f585d8a3SJacky Wang DaggerParentOfGenericComponent.create(); 36*f585d8a3SJacky Wang 37*f585d8a3SJacky Wang @Parameters parameters()38*f585d8a3SJacky Wang public static Collection<Object[]> parameters() { 39*f585d8a3SJacky Wang return Arrays.asList(new Object[][] { 40*f585d8a3SJacky Wang { parentComponent, parentComponent.newChildComponent() }, 41*f585d8a3SJacky Wang { parentComponent, parentComponent.newChildAbstractClassComponent() }, 42*f585d8a3SJacky Wang { parentOfGenericComponent, parentOfGenericComponent.subcomponent() }}); 43*f585d8a3SJacky Wang } 44*f585d8a3SJacky Wang 45*f585d8a3SJacky Wang private final ParentGetters parentGetters; 46*f585d8a3SJacky Wang private final ChildComponent childComponent; 47*f585d8a3SJacky Wang SubcomponentTest(ParentGetters parentGetters, ChildComponent childComponent)48*f585d8a3SJacky Wang public SubcomponentTest(ParentGetters parentGetters, ChildComponent childComponent) { 49*f585d8a3SJacky Wang this.parentGetters = parentGetters; 50*f585d8a3SJacky Wang this.childComponent = childComponent; 51*f585d8a3SJacky Wang } 52*f585d8a3SJacky Wang 53*f585d8a3SJacky Wang 54*f585d8a3SJacky Wang @Test scopePropagatesUpward_class()55*f585d8a3SJacky Wang public void scopePropagatesUpward_class() { 56*f585d8a3SJacky Wang assertThat(childComponent.requiresSingleton().singletonType()) 57*f585d8a3SJacky Wang .isSameInstanceAs(childComponent.requiresSingleton().singletonType()); 58*f585d8a3SJacky Wang assertThat(childComponent.requiresSingleton().singletonType()) 59*f585d8a3SJacky Wang .isSameInstanceAs( 60*f585d8a3SJacky Wang childComponent.newGrandchildComponent().requiresSingleton().singletonType()); 61*f585d8a3SJacky Wang } 62*f585d8a3SJacky Wang 63*f585d8a3SJacky Wang @Test scopePropagatesUpward_provides()64*f585d8a3SJacky Wang public void scopePropagatesUpward_provides() { 65*f585d8a3SJacky Wang assertThat(childComponent.requiresSingleton().unscopedTypeBoundAsSingleton()) 66*f585d8a3SJacky Wang .isSameInstanceAs(childComponent.requiresSingleton().unscopedTypeBoundAsSingleton()); 67*f585d8a3SJacky Wang assertThat(childComponent.requiresSingleton().unscopedTypeBoundAsSingleton()) 68*f585d8a3SJacky Wang .isSameInstanceAs( 69*f585d8a3SJacky Wang childComponent 70*f585d8a3SJacky Wang .newGrandchildComponent() 71*f585d8a3SJacky Wang .requiresSingleton() 72*f585d8a3SJacky Wang .unscopedTypeBoundAsSingleton()); 73*f585d8a3SJacky Wang } 74*f585d8a3SJacky Wang 75*f585d8a3SJacky Wang @Test multibindingContributions()76*f585d8a3SJacky Wang public void multibindingContributions() { 77*f585d8a3SJacky Wang Set<Object> parentObjectSet = parentGetters.objectSet(); 78*f585d8a3SJacky Wang assertThat(parentObjectSet).hasSize(2); 79*f585d8a3SJacky Wang Set<Object> childObjectSet = childComponent.objectSet(); 80*f585d8a3SJacky Wang assertThat(childObjectSet).hasSize(3); 81*f585d8a3SJacky Wang Set<Object> grandchildObjectSet = 82*f585d8a3SJacky Wang childComponent.newGrandchildComponent().objectSet(); 83*f585d8a3SJacky Wang assertThat(grandchildObjectSet).hasSize(4); 84*f585d8a3SJacky Wang assertThat(intersection(parentObjectSet, childObjectSet)).hasSize(1); 85*f585d8a3SJacky Wang assertThat(intersection(parentObjectSet, grandchildObjectSet)).hasSize(1); 86*f585d8a3SJacky Wang assertThat(intersection(childObjectSet, grandchildObjectSet)).hasSize(1); 87*f585d8a3SJacky Wang } 88*f585d8a3SJacky Wang 89*f585d8a3SJacky Wang @Test unscopedProviders()90*f585d8a3SJacky Wang public void unscopedProviders() { 91*f585d8a3SJacky Wang assume().that(System.getProperty("dagger.mode")).doesNotContain("FastInit"); 92*f585d8a3SJacky Wang assertThat(parentGetters.getUnscopedTypeProvider()) 93*f585d8a3SJacky Wang .isSameInstanceAs(childComponent.getUnscopedTypeProvider()); 94*f585d8a3SJacky Wang assertThat(parentGetters.getUnscopedTypeProvider()) 95*f585d8a3SJacky Wang .isSameInstanceAs(childComponent.newGrandchildComponent().getUnscopedTypeProvider()); 96*f585d8a3SJacky Wang } 97*f585d8a3SJacky Wang 98*f585d8a3SJacky Wang @Test passedModules()99*f585d8a3SJacky Wang public void passedModules() { 100*f585d8a3SJacky Wang ChildModuleWithState childModuleWithState = new ChildModuleWithState(); 101*f585d8a3SJacky Wang ChildComponentRequiringModules childComponent1 = 102*f585d8a3SJacky Wang parentComponent.newChildComponentRequiringModules( 103*f585d8a3SJacky Wang new ChildModuleWithParameters(new Object()), 104*f585d8a3SJacky Wang childModuleWithState); 105*f585d8a3SJacky Wang ChildComponentRequiringModules childComponent2 = 106*f585d8a3SJacky Wang parentComponent.newChildComponentRequiringModules( 107*f585d8a3SJacky Wang new ChildModuleWithParameters(new Object()), 108*f585d8a3SJacky Wang childModuleWithState); 109*f585d8a3SJacky Wang assertThat(childComponent1.getInt()).isEqualTo(0); 110*f585d8a3SJacky Wang assertThat(childComponent2.getInt()).isEqualTo(1); 111*f585d8a3SJacky Wang } 112*f585d8a3SJacky Wang 113*f585d8a3SJacky Wang @Test dependenceisInASubcomponent()114*f585d8a3SJacky Wang public void dependenceisInASubcomponent() { 115*f585d8a3SJacky Wang assertThat(childComponent.newGrandchildComponent().needsAnInterface()).isNotNull(); 116*f585d8a3SJacky Wang } 117*f585d8a3SJacky Wang 118*f585d8a3SJacky Wang @Test qualifiedSubcomponentIsBound()119*f585d8a3SJacky Wang public void qualifiedSubcomponentIsBound() { 120*f585d8a3SJacky Wang assertThat(parentComponent.unresolvableChildComponentBuilder().build().unboundString()) 121*f585d8a3SJacky Wang .isEqualTo("unbound"); 122*f585d8a3SJacky Wang } 123*f585d8a3SJacky Wang } 124