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