xref: /aosp_15_r20/external/dagger2/javatests/dagger/hilt/android/UsesLocalComponentTestBindingsTest.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
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.components.SingletonComponent;
38 import javax.inject.Inject;
39 import javax.inject.Qualifier;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.robolectric.annotation.Config;
44 
45 /**
46  * A test that provides its own test bindings, and therefore cannot use the shared components.
47  *
48  * <p>Note that this test class exactly matches the simple name of {@link
49  * dagger.hilt.android.testsubpackage.UsesLocalComponentTestBindingsTest}. This is intentional and
50  * used to verify generated code class names do not clash.
51  */
52 @HiltAndroidTest
53 @RunWith(AndroidJUnit4.class)
54 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead
55 @Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class)
56 public final class UsesLocalComponentTestBindingsTest {
57 
58   @Rule public HiltAndroidRule rule = new HiltAndroidRule(this);
59 
60   @Qualifier
61   @interface TestQualifier {}
62 
63   @Inject @UsesComponentQualifier String injectedString;
64   @Inject @TestQualifier String localString;
65 
66   @Module
67   @InstallIn(SingletonComponent.class)
68   public static final class TestModule {
69     @Provides
70     @TestQualifier
provideString()71     static String provideString() {
72       return "local_string";
73     }
74 
TestModule()75     private TestModule() {}
76   }
77 
78   @Test
testInject()79   public void testInject() {
80     rule.inject();
81     assertThat(injectedString).isEqualTo("shared_string");
82     assertThat(localString).isEqualTo("local_string");
83   }
84 
85   @Test
testSubcomponent()86   public void testSubcomponent() {
87     UsesComponentTestSubcomponent subcomponent =
88         EntryPoints.get(
89                 getApplicationContext(), UsesComponentTestSubcomponentBuilderEntryPoint.class)
90             .mySubcomponentBuilder()
91             .id(123)
92             .build();
93 
94     Foo foo1 = EntryPoints.get(subcomponent, FooEntryPoint.class).foo();
95     Foo foo2 = EntryPoints.get(subcomponent, FooEntryPoint.class).foo();
96 
97     assertThat(foo1).isNotNull();
98     assertThat(foo2).isNotNull();
99     assertThat(foo1).isSameInstanceAs(foo2);
100   }
101 
102   @Test
testUsesLocalComponent()103   public void testUsesLocalComponent() {
104     HiltTestApplication app = (HiltTestApplication) getApplicationContext();
105     Object generatedComponent =
106         ((TestApplicationComponentManager) app.componentManager()).generatedComponent();
107     assertThat(generatedComponent.getClass().getName())
108         .isEqualTo(UsesComponentHelper.perTestComponentNameWithDedupePrefix("dha_", this));
109   }
110 }
111