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; 18 19 import static androidx.test.core.app.ApplicationProvider.getApplicationContext; 20 import static com.google.common.truth.Truth.assertThat; 21 22 import android.app.Application; 23 import android.content.Context; 24 import android.os.Build; 25 import androidx.test.ext.junit.runners.AndroidJUnit4; 26 import dagger.hilt.EntryPoint; 27 import dagger.hilt.EntryPoints; 28 import dagger.hilt.InstallIn; 29 import dagger.hilt.android.EarlyEntryPointCustomApplicationClasses.EarlyFooEntryPoint; 30 import dagger.hilt.android.EarlyEntryPointCustomApplicationClasses.Foo; 31 import dagger.hilt.android.testing.CustomTestApplication; 32 import dagger.hilt.android.testing.HiltAndroidRule; 33 import dagger.hilt.android.testing.HiltAndroidTest; 34 import dagger.hilt.components.SingletonComponent; 35 import org.junit.Rule; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.robolectric.annotation.Config; 39 40 @CustomTestApplication(EarlyEntryPointCustomApplicationTest.BaseApplication.class) 41 @HiltAndroidTest 42 @RunWith(AndroidJUnit4.class) 43 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead 44 @Config( 45 sdk = Build.VERSION_CODES.P, 46 application = EarlyEntryPointCustomApplicationTest_Application.class) 47 public final class EarlyEntryPointCustomApplicationTest { 48 @EntryPoint 49 @InstallIn(SingletonComponent.class) 50 interface FooEntryPoint { foo()51 Foo foo(); 52 } 53 54 public static class BaseApplication extends Application { 55 Foo earlyFoo = null; 56 IllegalStateException entryPointsException = null; 57 58 @Override attachBaseContext(Context base)59 protected final void attachBaseContext(Context base) { 60 // This is a regression test for https://github.com/google/dagger/issues/3356. 61 // Test that EarlyEntryPoints works even before the base context is attached. 62 earlyFoo = EarlyEntryPoints.get(this, EarlyFooEntryPoint.class).foo(); 63 super.attachBaseContext(base); 64 } 65 66 @Override onCreate()67 public void onCreate() { 68 super.onCreate(); 69 70 // Test that calling EntryPoints fails if called before the test instance is created. 71 try { 72 EntryPoints.get(this, FooEntryPoint.class); 73 } catch (IllegalStateException e) { 74 entryPointsException = e; 75 } 76 } 77 } 78 79 @Rule public HiltAndroidRule rule = new HiltAndroidRule(this); 80 81 @Test testEarlyEntryPointsPasses()82 public void testEarlyEntryPointsPasses() throws Exception { 83 BaseApplication baseApplication = (BaseApplication) getApplicationContext(); 84 assertThat(baseApplication.earlyFoo).isNotNull(); 85 } 86 87 @Test testEntryPointsFails()88 public void testEntryPointsFails() throws Exception { 89 BaseApplication baseApplication = (BaseApplication) getApplicationContext(); 90 assertThat(baseApplication.entryPointsException).isNotNull(); 91 assertThat(baseApplication.entryPointsException) 92 .hasMessageThat() 93 .contains("The component was not created."); 94 } 95 } 96