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.hilt.android.internal.testing; 18 19 import dagger.hilt.internal.Preconditions; 20 import java.util.Map; 21 import java.util.Set; 22 23 /** Contains the data needed to create a test's component. */ 24 public final class TestComponentData { 25 private final ComponentSupplier componentSupplier; 26 private final TestInjector<Object> testInjector; 27 private final Set<Class<?>> daggerRequiredModules; 28 private final Set<Class<?>> hiltRequiredModules; 29 private final boolean waitForBindValue; 30 TestComponentData( boolean waitForBindValue, TestInjector<Object> testInjector, Set<Class<?>> daggerRequiredModules, Set<Class<?>> hiltRequiredModules, ComponentSupplier componentSupplier)31 public TestComponentData( 32 boolean waitForBindValue, 33 TestInjector<Object> testInjector, 34 Set<Class<?>> daggerRequiredModules, 35 Set<Class<?>> hiltRequiredModules, 36 ComponentSupplier componentSupplier) { 37 Preconditions.checkState( 38 daggerRequiredModules.containsAll(hiltRequiredModules), 39 "Hilt required modules should be subset of Dagger required modules."); 40 this.componentSupplier = componentSupplier; 41 this.testInjector = testInjector; 42 this.daggerRequiredModules = daggerRequiredModules; 43 this.waitForBindValue = waitForBindValue; 44 this.hiltRequiredModules = hiltRequiredModules; 45 } 46 47 /** Returns the {@link ComponentSupplier}. */ componentSupplier()48 public ComponentSupplier componentSupplier() { 49 return componentSupplier; 50 } 51 52 /** Returns the {@link TestInjector}. */ testInjector()53 public TestInjector<Object> testInjector() { 54 return testInjector; 55 } 56 57 /** Returns the set of modules that Dagger cannot create instances of itself */ daggerRequiredModules()58 public Set<Class<?>> daggerRequiredModules() { 59 return daggerRequiredModules; 60 } 61 62 /** 63 * Returns a subset of {@link #daggerRequiredModules} that filters out the modules Hilt can 64 * instantiate itself. 65 */ hiltRequiredModules()66 public Set<Class<?>> hiltRequiredModules() { 67 return hiltRequiredModules; 68 } 69 70 /** Returns true if creation of the component needs to wait for bind() to be called. */ waitForBindValue()71 public boolean waitForBindValue() { 72 return waitForBindValue; 73 } 74 75 /** Returns the component using the given registered modules. */ 76 public interface ComponentSupplier { get(Map<Class<?>, ?> registeredModules, Object testInstance, Boolean autoAddModule)77 Object get(Map<Class<?>, ?> registeredModules, Object testInstance, Boolean autoAddModule); 78 } 79 } 80