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.testing; 18 19 import static dagger.hilt.internal.Preconditions.checkNotNull; 20 21 import dagger.hilt.android.internal.testing.MarkThatRulesRanRule; 22 import org.junit.rules.TestRule; 23 import org.junit.runner.Description; 24 import org.junit.runners.model.Statement; 25 26 /** 27 * A {@link TestRule} for Hilt that can be used with JVM or Instrumentation tests. 28 * 29 * <p>This rule is required. The Dagger component will not be created without this test rule. 30 */ 31 public final class HiltAndroidRule implements TestRule { 32 private final MarkThatRulesRanRule rule; 33 34 /** Creates a new instance of the rules. Tests should pass {@code this}. */ HiltAndroidRule(Object testInstance)35 public HiltAndroidRule(Object testInstance) { 36 this.rule = new MarkThatRulesRanRule(checkNotNull(testInstance)); 37 } 38 apply(Statement baseStatement, Description description)39 @Override public Statement apply(Statement baseStatement, Description description) { 40 return rule.apply(baseStatement, description); 41 } 42 43 /** 44 * Completes Dagger injection. Must be called before accessing inject types. Must be called after 45 * any non-static test module have been added. If {@link #delayComponentReady} was used, this must 46 * be called after {@link #componentReady}. 47 */ inject()48 public void inject() { 49 rule.inject(); 50 } 51 52 /** 53 * Delays creating the component until {@link #componentReady} is called. This is only necessary 54 * in the case that a dynamically bound value (e.g. configuring an @BindValue field in @Before 55 * or @Test method) is requested before test case execution begins. 56 * 57 * <p>Examples of early binding requests include an Activity launched by a test rule, or an entry 58 * points in a {@link OnComponentReadyRunner}. 59 * 60 * <p>If this method is called, {@link #componentReady} must be called before the test case 61 * finishes. 62 */ delayComponentReady()63 public HiltAndroidRule delayComponentReady() { 64 rule.delayComponentReady(); 65 return this; 66 } 67 68 /** 69 * Completes Dagger component creation if {@link delayComponentReady} was used. Binds the current 70 * value of {@link BindValue} fields. Normally this happens automatically. This method may only be 71 * called if {@link delayComponentReady} was used to delay value binding. 72 * 73 * @return an instance of the test rule for chaining 74 */ componentReady()75 public HiltAndroidRule componentReady() { 76 rule.componentReady(); 77 return this; 78 } 79 } 80