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 androidx.test.core.app.ApplicationProvider.getApplicationContext; 20 import static com.google.common.truth.Truth.assertThat; 21 import static org.junit.Assert.assertThrows; 22 23 import androidx.test.ext.junit.runners.AndroidJUnit4; 24 import dagger.hilt.EntryPoint; 25 import dagger.hilt.EntryPoints; 26 import dagger.hilt.InstallIn; 27 import dagger.hilt.components.SingletonComponent; 28 import javax.inject.Named; 29 import org.junit.Rule; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.robolectric.annotation.Config; 33 34 @HiltAndroidTest 35 @RunWith(AndroidJUnit4.class) 36 @Config(application = HiltTestApplication.class) 37 public final class BindValueTest { 38 private static final String BIND_VALUE_STRING1 = "BIND_VALUE_STRING1"; 39 private static final String BIND_VALUE_STRING2 = "BIND_VALUE_STRING2"; 40 private static final String TEST_QUALIFIER1 = "TEST_QUALIFIER1"; 41 private static final String TEST_QUALIFIER2 = "TEST_QUALIFIER2"; 42 43 @EntryPoint 44 @InstallIn(SingletonComponent.class) 45 public interface BindValueEntryPoint { 46 @Named(TEST_QUALIFIER1) bindValueString1()47 String bindValueString1(); 48 49 @Named(TEST_QUALIFIER2) bindValueString2()50 String bindValueString2(); 51 } 52 53 @Rule public final HiltAndroidRule rule = new HiltAndroidRule(this); 54 55 @BindValue 56 @Named(TEST_QUALIFIER1) 57 String bindValueString1 = BIND_VALUE_STRING1; 58 59 @BindValue 60 @Named(TEST_QUALIFIER2) 61 String bindValueString2 = BIND_VALUE_STRING2; 62 63 @Test testBindValueFieldIsProvided()64 public void testBindValueFieldIsProvided() throws Exception { 65 assertThat(bindValueString1).isEqualTo(BIND_VALUE_STRING1); 66 assertThat(getBinding1()).isEqualTo(BIND_VALUE_STRING1); 67 68 assertThat(bindValueString2).isEqualTo(BIND_VALUE_STRING2); 69 assertThat(getBinding2()).isEqualTo(BIND_VALUE_STRING2); 70 } 71 72 @Test testBindValueIsMutable()73 public void testBindValueIsMutable() throws Exception { 74 bindValueString1 = "newValue"; 75 assertThat(getBinding1()).isEqualTo("newValue"); 76 } 77 78 @Test testCallingComponentReadyWithoutDelayComponentReady_fails()79 public void testCallingComponentReadyWithoutDelayComponentReady_fails() throws Exception { 80 IllegalStateException expected = 81 assertThrows(IllegalStateException.class, rule::componentReady); 82 assertThat(expected) 83 .hasMessageThat() 84 .isEqualTo("Called componentReady(), even though delayComponentReady() was not used."); 85 } 86 getBinding1()87 private static String getBinding1() { 88 return EntryPoints.get(getApplicationContext(), BindValueEntryPoint.class).bindValueString1(); 89 } 90 getBinding2()91 private static String getBinding2() { 92 return EntryPoints.get(getApplicationContext(), BindValueEntryPoint.class).bindValueString2(); 93 } 94 } 95