1 /* 2 * Copyright (C) 2021 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.simple; 18 19 import static androidx.test.core.app.ApplicationProvider.getApplicationContext; 20 import static com.google.common.truth.Truth.assertThat; 21 22 import android.content.Context; 23 import android.os.Build; 24 import androidx.test.ext.junit.runners.AndroidJUnit4; 25 import dagger.hilt.EntryPoint; 26 import dagger.hilt.EntryPoints; 27 import dagger.hilt.InstallIn; 28 import dagger.hilt.android.EarlyEntryPoints; 29 import dagger.hilt.android.simple.EarlyEntryPointWithBindValueObjects.Foo; 30 import dagger.hilt.android.simple.EarlyEntryPointWithBindValueObjects.FooEarlyEntryPoint; 31 import dagger.hilt.android.testing.BindValue; 32 import dagger.hilt.android.testing.HiltAndroidRule; 33 import dagger.hilt.android.testing.HiltAndroidTest; 34 import dagger.hilt.android.testing.HiltTestApplication; 35 import dagger.hilt.components.SingletonComponent; 36 import org.junit.Rule; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.robolectric.annotation.Config; 40 41 @HiltAndroidTest 42 @RunWith(AndroidJUnit4.class) 43 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead 44 @Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class) 45 public final class EarlyEntryPointWithBindValueTest { 46 @EntryPoint 47 @InstallIn(SingletonComponent.class) 48 interface FooEntryPoint { foo()49 Foo foo(); 50 } 51 52 @Rule public HiltAndroidRule rule = new HiltAndroidRule(this); 53 54 @BindValue String bindString = "TestValue"; 55 56 @Test testBindValue()57 public void testBindValue() throws Exception { 58 rule.inject(); 59 assertThat(bindString).isNotNull(); 60 assertThat(bindString).isEqualTo("TestValue"); 61 } 62 63 @Test testEarlyEntryPoint()64 public void testEarlyEntryPoint() throws Exception { 65 Context appContext = getApplicationContext(); 66 67 // Assert that all scoped Foo instances from EarlyEntryPoint are equal 68 Foo earlyFoo1 = EarlyEntryPoints.get(appContext, FooEarlyEntryPoint.class).getEarlyFoo(); 69 Foo earlyFoo2 = EarlyEntryPoints.get(appContext, FooEarlyEntryPoint.class).getEarlyFoo(); 70 assertThat(earlyFoo1).isNotNull(); 71 assertThat(earlyFoo2).isNotNull(); 72 assertThat(earlyFoo1).isEqualTo(earlyFoo2); 73 74 // Assert that all scoped Foo instances from EntryPoint are equal 75 Foo foo1 = EntryPoints.get(appContext, FooEntryPoint.class).foo(); 76 Foo foo2 = EntryPoints.get(appContext, FooEntryPoint.class).foo(); 77 assertThat(foo1).isNotNull(); 78 assertThat(foo2).isNotNull(); 79 assertThat(foo1).isEqualTo(foo2); 80 81 // Assert that scoped Foo instances from EarlyEntryPoint and EntryPoint are not equal 82 assertThat(foo1).isNotEqualTo(earlyFoo1); 83 } 84 } 85