• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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.EarlyEntryPointCustomApplicationClasses.EarlyFooEntryPoint;
29 import dagger.hilt.android.EarlyEntryPointCustomApplicationClasses.Foo;
30 import dagger.hilt.android.testing.CustomTestApplication;
31 import dagger.hilt.android.testing.HiltAndroidRule;
32 import dagger.hilt.android.testing.HiltAndroidTest;
33 import dagger.hilt.components.SingletonComponent;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.robolectric.annotation.Config;
38 
39 @CustomTestApplication(EarlyEntryPointCustomApplicationTest.BaseApplication.class)
40 @HiltAndroidTest
41 @RunWith(AndroidJUnit4.class)
42 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead
43 @Config(
44     sdk = Build.VERSION_CODES.P,
45     application = EarlyEntryPointCustomApplicationTest_Application.class)
46 public final class EarlyEntryPointCustomApplicationTest {
47   @EntryPoint
48   @InstallIn(SingletonComponent.class)
49   interface FooEntryPoint {
foo()50     Foo foo();
51   }
52 
53   public static class BaseApplication extends Application {
54     Foo earlyFoo = null;
55     IllegalStateException entryPointsException = null;
56 
57     @Override
onCreate()58     public void onCreate() {
59       super.onCreate();
60 
61       // Test that calling EarlyEntryPoints works before the test instance is created.
62       earlyFoo = EarlyEntryPoints.get(this, EarlyFooEntryPoint.class).foo();
63 
64       // Test that calling EntryPoints fails if called before the test instance is created.
65       try {
66         EntryPoints.get(this, FooEntryPoint.class);
67       } catch (IllegalStateException e) {
68         entryPointsException = e;
69       }
70     }
71   }
72 
73   @Rule public HiltAndroidRule rule = new HiltAndroidRule(this);
74 
75   @Test
testEarlyEntryPointsPasses()76   public void testEarlyEntryPointsPasses() throws Exception {
77     BaseApplication baseApplication = (BaseApplication) getApplicationContext();
78     assertThat(baseApplication.earlyFoo).isNotNull();
79   }
80 
81   @Test
testEntryPointsFails()82   public void testEntryPointsFails() throws Exception {
83     BaseApplication baseApplication = (BaseApplication) getApplicationContext();
84     assertThat(baseApplication.entryPointsException).isNotNull();
85     assertThat(baseApplication.entryPointsException)
86         .hasMessageThat()
87         .contains("The component was not created.");
88   }
89 }
90