1 /* 2 * Copyright (C) 2020 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.testing; 18 19 import static java.lang.annotation.RetentionPolicy.RUNTIME; 20 import static org.junit.Assert.assertEquals; 21 22 import androidx.test.ext.junit.runners.AndroidJUnit4; 23 import dagger.Binds; 24 import dagger.Module; 25 import dagger.Provides; 26 import dagger.hilt.InstallIn; 27 import dagger.hilt.components.SingletonComponent; 28 import java.lang.annotation.ElementType; 29 import java.lang.annotation.Retention; 30 import java.lang.annotation.Target; 31 import javax.inject.Inject; 32 import javax.inject.Qualifier; 33 import javax.inject.Singleton; 34 import org.junit.Rule; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.robolectric.annotation.Config; 38 39 @HiltAndroidTest 40 @RunWith(AndroidJUnit4.class) 41 @Config(application = HiltTestApplication.class) 42 public final class TestRootModulesTest { 43 @Rule public final HiltAndroidRule rules = new HiltAndroidRule(this); 44 45 @Retention(RUNTIME) 46 @Qualifier 47 @Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD}) 48 public @interface TestQualifier { value()49 int value(); 50 } 51 52 @Inject 53 @TestQualifier(0) 54 String testString0; 55 56 @Inject 57 @TestQualifier(1) 58 String testString1; 59 60 @Inject 61 @TestQualifier(2) 62 String testString2; 63 64 @Inject 65 @TestQualifier(3) 66 String testString3; 67 68 @Inject 69 @TestQualifier(4) 70 String testString4; 71 72 @Inject FooImpl fooImpl; 73 @Inject Foo foo; 74 75 @Module 76 @InstallIn(SingletonComponent.class) 77 public final class NonStaticModuleNonStaticProvidesDefaultConstructor { 78 @Provides 79 @TestQualifier(0) provideString()80 String provideString() { 81 return "0"; 82 } 83 NonStaticModuleNonStaticProvidesDefaultConstructor()84 NonStaticModuleNonStaticProvidesDefaultConstructor() {} 85 } 86 87 @Module 88 @InstallIn(SingletonComponent.class) 89 public final class NonStaticModuleNonStaticProvides { 90 @Provides 91 @TestQualifier(1) provideString()92 String provideString() { 93 return "1"; 94 } 95 } 96 97 @Module 98 @InstallIn(SingletonComponent.class) 99 public static final class StaticModuleStaticProvides { 100 @Provides 101 @TestQualifier(2) provideString()102 static String provideString() { 103 return "2"; 104 } 105 StaticModuleStaticProvides()106 private StaticModuleStaticProvides() {} 107 } 108 109 @Module 110 @InstallIn(SingletonComponent.class) 111 public static final class StaticModuleNonStaticProvidesDefaultConstructor { 112 @Provides 113 @TestQualifier(3) provideString()114 String provideString() { 115 return "3"; 116 } 117 } 118 119 @Module 120 @InstallIn(SingletonComponent.class) 121 public abstract static class AbstractModuleStaticProvides { 122 @Provides 123 @TestQualifier(4) provideString()124 static String provideString() { 125 return "4"; 126 } 127 AbstractModuleStaticProvides()128 private AbstractModuleStaticProvides() {} 129 } 130 131 @Module 132 @InstallIn(SingletonComponent.class) 133 public abstract static class AbstractModuleBindsMethod { 134 @Binds foo(FooImpl fooImpl)135 abstract Foo foo(FooImpl fooImpl); 136 } 137 138 interface Foo {} 139 140 @Singleton 141 static final class FooImpl implements Foo { 142 @Inject FooImpl()143 FooImpl() {} 144 } 145 146 @Test testInjection()147 public void testInjection() throws Exception { 148 rules.inject(); 149 assertEquals("0", testString0); 150 assertEquals("1", testString1); 151 assertEquals("2", testString2); 152 assertEquals("3", testString3); 153 assertEquals("4", testString4); 154 assertEquals(fooImpl, foo); 155 } 156 } 157