1 /* 2 * Copyright (C) 2015 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.subcomponent; 18 19 import static com.google.common.collect.Sets.intersection; 20 import static com.google.common.truth.Truth.assertThat; 21 import static com.google.common.truth.TruthJUnit.assume; 22 23 import java.util.Arrays; 24 import java.util.Collection; 25 import java.util.Set; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.Parameterized; 29 import org.junit.runners.Parameterized.Parameters; 30 31 @RunWith(Parameterized.class) 32 public class SubcomponentTest { 33 private static final ParentComponent parentComponent = DaggerParentComponent.create(); 34 private static final ParentOfGenericComponent parentOfGenericComponent = 35 DaggerParentOfGenericComponent.create(); 36 37 @Parameters parameters()38 public static Collection<Object[]> parameters() { 39 return Arrays.asList(new Object[][] { 40 { parentComponent, parentComponent.newChildComponent() }, 41 { parentComponent, parentComponent.newChildAbstractClassComponent() }, 42 { parentOfGenericComponent, parentOfGenericComponent.subcomponent() }}); 43 } 44 45 private final ParentGetters parentGetters; 46 private final ChildComponent childComponent; 47 SubcomponentTest(ParentGetters parentGetters, ChildComponent childComponent)48 public SubcomponentTest(ParentGetters parentGetters, ChildComponent childComponent) { 49 this.parentGetters = parentGetters; 50 this.childComponent = childComponent; 51 } 52 53 @Test scopePropagatesUpward_class()54 public void scopePropagatesUpward_class() { 55 assertThat(childComponent.requiresSingleton().singletonType()) 56 .isSameInstanceAs(childComponent.requiresSingleton().singletonType()); 57 assertThat(childComponent.requiresSingleton().singletonType()) 58 .isSameInstanceAs( 59 childComponent.newGrandchildComponent().requiresSingleton().singletonType()); 60 } 61 62 @Test scopePropagatesUpward_provides()63 public void scopePropagatesUpward_provides() { 64 assertThat(childComponent.requiresSingleton().unscopedTypeBoundAsSingleton()) 65 .isSameInstanceAs(childComponent.requiresSingleton().unscopedTypeBoundAsSingleton()); 66 assertThat(childComponent.requiresSingleton().unscopedTypeBoundAsSingleton()) 67 .isSameInstanceAs( 68 childComponent 69 .newGrandchildComponent() 70 .requiresSingleton() 71 .unscopedTypeBoundAsSingleton()); 72 } 73 74 @Test multibindingContributions()75 public void multibindingContributions() { 76 Set<Object> parentObjectSet = parentGetters.objectSet(); 77 assertThat(parentObjectSet).hasSize(2); 78 Set<Object> childObjectSet = childComponent.objectSet(); 79 assertThat(childObjectSet).hasSize(3); 80 Set<Object> grandchildObjectSet = 81 childComponent.newGrandchildComponent().objectSet(); 82 assertThat(grandchildObjectSet).hasSize(4); 83 assertThat(intersection(parentObjectSet, childObjectSet)).hasSize(1); 84 assertThat(intersection(parentObjectSet, grandchildObjectSet)).hasSize(1); 85 assertThat(intersection(childObjectSet, grandchildObjectSet)).hasSize(1); 86 } 87 88 @Test unscopedProviders()89 public void unscopedProviders() { 90 assume().that(System.getProperty("dagger.mode")).doesNotContain("FastInit"); 91 assertThat(parentGetters.getUnscopedTypeProvider()) 92 .isSameInstanceAs(childComponent.getUnscopedTypeProvider()); 93 assertThat(parentGetters.getUnscopedTypeProvider()) 94 .isSameInstanceAs(childComponent.newGrandchildComponent().getUnscopedTypeProvider()); 95 } 96 97 @Test passedModules()98 public void passedModules() { 99 ChildModuleWithState childModuleWithState = new ChildModuleWithState(); 100 ChildComponentRequiringModules childComponent1 = 101 parentComponent.newChildComponentRequiringModules( 102 new ChildModuleWithParameters(new Object()), 103 childModuleWithState); 104 ChildComponentRequiringModules childComponent2 = 105 parentComponent.newChildComponentRequiringModules( 106 new ChildModuleWithParameters(new Object()), 107 childModuleWithState); 108 assertThat(childComponent1.getInt()).isEqualTo(0); 109 assertThat(childComponent2.getInt()).isEqualTo(1); 110 } 111 112 @Test dependenceisInASubcomponent()113 public void dependenceisInASubcomponent() { 114 assertThat(childComponent.newGrandchildComponent().needsAnInterface()).isNotNull(); 115 } 116 117 @Test qualifiedSubcomponentIsBound()118 public void qualifiedSubcomponentIsBound() { 119 assertThat(parentComponent.unresolvableChildComponentBuilder().build().unboundString()) 120 .isEqualTo("unbound"); 121 } 122 } 123