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. 45 * 46 * <p>Note that this test class exactly matches the simple name of {@link 47 * dagger.hilt.android.testsubpackage.UsesSharedComponent1Test}. This is intentional and used to 48 * verify generated code class names do not clash. 49 */ 50 @HiltAndroidTest 51 @RunWith(AndroidJUnit4.class) 52 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead 53 @Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class) 54 public final class UsesSharedComponent1Test { 55 56 @Rule public HiltAndroidRule rule = new HiltAndroidRule(this); 57 58 @Inject @UsesComponentQualifier String injectedString; 59 60 @Test testInject()61 public void testInject() { 62 rule.inject(); 63 assertThat(injectedString).isEqualTo("shared_string"); 64 } 65 66 @Test testSubcomponent()67 public void testSubcomponent() { 68 UsesComponentTestSubcomponent subcomponent = 69 EntryPoints.get( 70 getApplicationContext(), UsesComponentTestSubcomponentBuilderEntryPoint.class) 71 .mySubcomponentBuilder() 72 .id(123) 73 .build(); 74 75 Foo foo1 = EntryPoints.get(subcomponent, FooEntryPoint.class).foo(); 76 Foo foo2 = EntryPoints.get(subcomponent, FooEntryPoint.class).foo(); 77 78 assertThat(foo1).isNotNull(); 79 assertThat(foo2).isNotNull(); 80 assertThat(foo1).isSameInstanceAs(foo2); 81 } 82 83 @Test testActivity()84 public void testActivity() { 85 try (ActivityScenario<TestActivity> scenario = ActivityScenario.launch(TestActivity.class)) { 86 scenario.onActivity( 87 activity -> { 88 assertThat(activity.activityString).isEqualTo("shared_string"); 89 }); 90 } 91 } 92 93 @Test testUsesSharedComponent()94 public void testUsesSharedComponent() { 95 HiltTestApplication app = (HiltTestApplication) getApplicationContext(); 96 Object generatedComponent = 97 ((TestApplicationComponentManager) app.componentManager()).generatedComponent(); 98 assertThat(generatedComponent.getClass().getName()) 99 .isEqualTo(UsesComponentHelper.defaultComponentName()); 100 } 101 102 @Test testLocalComponentNotGenerated()103 public void testLocalComponentNotGenerated() { 104 assertThrows( 105 ClassNotFoundException.class, 106 () -> Class.forName(UsesComponentHelper.perTestComponentName(this))); 107 } 108 109 @AndroidEntryPoint(FragmentActivity.class) 110 public static final class TestActivity extends Hilt_UsesSharedComponent1Test_TestActivity { 111 @Inject @UsesComponentQualifier String activityString; 112 } 113 } 114