xref: /aosp_15_r20/external/dagger2/javatests/dagger/hilt/android/UsesSharedComponent2Test.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 import static org.junit.Assert.assertThrows;
22 
23 import android.os.Build;
24 import androidx.fragment.app.FragmentActivity;
25 import androidx.test.core.app.ActivityScenario;
26 import androidx.test.ext.junit.runners.AndroidJUnit4;
27 import dagger.hilt.EntryPoints;
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 javax.inject.Inject;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.robolectric.annotation.Config;
42 
43 /**
44  * A test that provides none of its own test bindings, and therefore uses the shared components. A
45  * duplicate of {@link SharedComponent1Test} to test that multiple test roots with shared components
46  * can be compiled in the same target.
47  */
48 @HiltAndroidTest
49 @RunWith(AndroidJUnit4.class)
50 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead
51 @Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class)
52 public final class UsesSharedComponent2Test {
53 
54   @Rule public HiltAndroidRule rule = new HiltAndroidRule(this);
55 
56   @Inject @UsesComponentQualifier String injectedString;
57 
58   @Test
testInject()59   public void testInject() {
60     rule.inject();
61     assertThat(injectedString).isEqualTo("shared_string");
62   }
63 
64   @Test
testSubcomponent()65   public void testSubcomponent() {
66     UsesComponentTestSubcomponent subcomponent =
67         EntryPoints.get(
68                 getApplicationContext(), UsesComponentTestSubcomponentBuilderEntryPoint.class)
69             .mySubcomponentBuilder()
70             .id(123)
71             .build();
72 
73     Foo foo1 = EntryPoints.get(subcomponent, FooEntryPoint.class).foo();
74     Foo foo2 = EntryPoints.get(subcomponent, FooEntryPoint.class).foo();
75 
76     assertThat(foo1).isNotNull();
77     assertThat(foo2).isNotNull();
78     assertThat(foo1).isSameInstanceAs(foo2);
79   }
80 
81   @Test
testActivity()82   public void testActivity() {
83     try (ActivityScenario<TestActivity> scenario = ActivityScenario.launch(TestActivity.class)) {
84       scenario.onActivity(
85           activity -> {
86             assertThat(activity.activityString).isEqualTo("shared_string");
87           });
88     }
89   }
90 
91   @Test
testUsesSharedComponent()92   public void testUsesSharedComponent() {
93     HiltTestApplication app = (HiltTestApplication) getApplicationContext();
94     Object generatedComponent =
95         ((TestApplicationComponentManager) app.componentManager()).generatedComponent();
96     assertThat(generatedComponent.getClass().getName())
97         .isEqualTo(UsesComponentHelper.defaultComponentName());
98   }
99 
100   @Test
testLocalComponentNotGenerated()101   public void testLocalComponentNotGenerated() {
102     assertThrows(
103         ClassNotFoundException.class,
104         () -> Class.forName(UsesComponentHelper.perTestComponentName(this)));
105   }
106 
107   @AndroidEntryPoint(FragmentActivity.class)
108   public static final class TestActivity extends Hilt_UsesSharedComponent2Test_TestActivity {
109     @Inject @UsesComponentQualifier String activityString;
110   }
111 }
112