• 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.testsubpackage;
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.AndroidEntryPoint;
29 import dagger.hilt.android.UsesComponentHelper;
30 import dagger.hilt.android.UsesComponentTestClasses.Foo;
31 import dagger.hilt.android.UsesComponentTestClasses.FooEntryPoint;
32 import dagger.hilt.android.UsesComponentTestClasses.UsesComponentQualifier;
33 import dagger.hilt.android.UsesComponentTestClasses.UsesComponentTestSubcomponent;
34 import dagger.hilt.android.UsesComponentTestClasses.UsesComponentTestSubcomponentBuilderEntryPoint;
35 import dagger.hilt.android.internal.testing.TestApplicationComponentManager;
36 import dagger.hilt.android.testing.HiltAndroidRule;
37 import dagger.hilt.android.testing.HiltAndroidTest;
38 import dagger.hilt.android.testing.HiltTestApplication;
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 /**
46  * A test that provides none of its own test bindings, and therefore uses the shared components.
47  *
48  * <p>Note that this test class exactly matches the simple name of {@link
49  * dagger.hilt.android.UsesSharedComponent1Test}. This is intentional and used to verify generated
50  * 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 UsesSharedComponent1Test {
57 
58   @Rule public HiltAndroidRule rule = new HiltAndroidRule(this);
59 
60   @Inject @UsesComponentQualifier String injectedString;
61 
62   @Test
testInject()63   public void testInject() {
64     rule.inject();
65     assertThat(injectedString).isEqualTo("shared_string");
66   }
67 
68   @Test
testSubcomponent()69   public void testSubcomponent() {
70     UsesComponentTestSubcomponent subcomponent =
71         EntryPoints.get(
72                 getApplicationContext(), UsesComponentTestSubcomponentBuilderEntryPoint.class)
73             .mySubcomponentBuilder()
74             .id(123)
75             .build();
76 
77     Foo foo1 = EntryPoints.get(subcomponent, FooEntryPoint.class).foo();
78     Foo foo2 = EntryPoints.get(subcomponent, FooEntryPoint.class).foo();
79 
80     assertThat(foo1).isNotNull();
81     assertThat(foo2).isNotNull();
82     assertThat(foo1).isSameInstanceAs(foo2);
83   }
84 
85   @Test
testActivity()86   public void testActivity() {
87     try (ActivityScenario<TestActivity> scenario = ActivityScenario.launch(TestActivity.class)) {
88       scenario.onActivity(
89           activity -> {
90             assertThat(activity.activityString).isEqualTo("shared_string");
91           });
92     }
93   }
94 
95   @Test
testUsesSharedComponent()96   public void testUsesSharedComponent() {
97     HiltTestApplication app = (HiltTestApplication) getApplicationContext();
98     Object generatedComponent =
99         ((TestApplicationComponentManager) app.componentManager()).generatedComponent();
100     assertThat(generatedComponent.getClass().getName())
101         .isEqualTo(UsesComponentHelper.defaultComponentName());
102   }
103 
104   @Test
testLocalComponentNotGenerated()105   public void testLocalComponentNotGenerated() {
106     assertThrows(
107         ClassNotFoundException.class,
108         () -> Class.forName(UsesComponentHelper.perTestComponentName(this)));
109   }
110 
111   @AndroidEntryPoint(FragmentActivity.class)
112   public static final class TestActivity extends Hilt_UsesSharedComponent1Test_TestActivity {
113     @Inject @UsesComponentQualifier String activityString;
114   }
115 }
116