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