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.os.Build;
23 import androidx.test.ext.junit.runners.AndroidJUnit4;
24 import dagger.Module;
25 import dagger.Provides;
26 import dagger.hilt.EntryPoints;
27 import dagger.hilt.InstallIn;
28 import dagger.hilt.android.UsesComponentTestClasses.Foo;
29 import dagger.hilt.android.UsesComponentTestClasses.FooEntryPoint;
30 import dagger.hilt.android.UsesComponentTestClasses.UsesComponentQualifier;
31 import dagger.hilt.android.UsesComponentTestClasses.UsesComponentTestSubcomponent;
32 import dagger.hilt.android.UsesComponentTestClasses.UsesComponentTestSubcomponentBuilderEntryPoint;
33 import dagger.hilt.android.internal.testing.TestApplicationComponentManager;
34 import dagger.hilt.android.testing.HiltAndroidRule;
35 import dagger.hilt.android.testing.HiltAndroidTest;
36 import dagger.hilt.android.testing.HiltTestApplication;
37 import dagger.hilt.android.testing.UninstallModules;
38 import dagger.hilt.components.SingletonComponent;
39 import javax.inject.Inject;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.robolectric.annotation.Config;
44 
45 @HiltAndroidTest
46 @RunWith(AndroidJUnit4.class)
47 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead
48 @Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class)
49 @UninstallModules(UsesComponentTestModule.class)
50 public final class UsesLocalComponentUninstallModuleTest {
51 
52   @Rule public HiltAndroidRule rule = new HiltAndroidRule(this);
53 
54   @Inject @UsesComponentQualifier String injectedString;
55 
56   @Module
57   @InstallIn(SingletonComponent.class)
58   public interface LocalTestModule {
59 
60     @Provides
61     @UsesComponentQualifier
provideString()62     static String provideString() {
63       return "local_string";
64     }
65   }
66 
67   @Test
testInject()68   public void testInject() {
69     rule.inject();
70     assertThat(injectedString).isEqualTo("local_string");
71   }
72 
73   @Test
testSubcomponent()74   public void testSubcomponent() {
75     UsesComponentTestSubcomponent subcomponent =
76         EntryPoints.get(
77                 getApplicationContext(), UsesComponentTestSubcomponentBuilderEntryPoint.class)
78             .mySubcomponentBuilder()
79             .id(123)
80             .build();
81 
82     Foo foo1 = EntryPoints.get(subcomponent, FooEntryPoint.class).foo();
83     Foo foo2 = EntryPoints.get(subcomponent, FooEntryPoint.class).foo();
84 
85     assertThat(foo1).isNotNull();
86     assertThat(foo2).isNotNull();
87     assertThat(foo1).isSameInstanceAs(foo2);
88   }
89 
90   @Test
testUsesLocalComponent()91   public void testUsesLocalComponent() {
92     HiltTestApplication app = (HiltTestApplication) getApplicationContext();
93     Object generatedComponent =
94         ((TestApplicationComponentManager) app.componentManager()).generatedComponent();
95     assertThat(generatedComponent.getClass().getName())
96         .isEqualTo(UsesComponentHelper.perTestComponentName(this));
97   }
98 }
99