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