xref: /aosp_15_r20/external/dagger2/javatests/dagger/hilt/android/ModuleTest.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
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;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.os.Build;
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.android.testing.HiltAndroidRule;
28 import dagger.hilt.android.testing.HiltAndroidTest;
29 import dagger.hilt.android.testing.HiltTestApplication;
30 import dagger.hilt.components.SingletonComponent;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import org.junit.Before;
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 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead
42 @Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class)
43 public final class ModuleTest {
44   public static class Dep1 {}
45   public static class Dep2 {}
46   public static class Dep3 {}
Dep4()47   public static class Dep4 { @Inject Dep4() {}}
Dep5()48   public static class Dep5 { @Inject Dep5() {}}
49   public static class Dep6 {}
50   public static class Dep7 {}
51 
52   @Rule public HiltAndroidRule rule = new HiltAndroidRule(this);
53 
54   // Test that modules with only static methods can have private constructors.
55   @Module
56   @InstallIn(SingletonComponent.class)
57   static final class TestModule1 {
TestModule1()58     private TestModule1() {} // This is fine because Dagger doesn't need an instance.
59 
60     @Provides
provide()61     static Dep1 provide() {
62       return new Dep1();
63     }
64   }
65 
66   // Test that modules with only static methods can have constructors with parameters.
67   @Module
68   @InstallIn(SingletonComponent.class)
69   static final class TestModule2 {
TestModule2(String str)70     TestModule2(String str) {} // This is fine because Dagger doesn't need an instance.
71 
72     @Provides
provide()73     static Dep2 provide() {
74       return new Dep2();
75     }
76   }
77 
78   // Test that modules with non-static methods can have constructors with parameters if no-arg
79   // constructor exists.
80   @Module
81   @InstallIn(SingletonComponent.class)
82   public static final class TestModule3 {
TestModule3()83     TestModule3() {
84       this("");
85     }
86 
TestModule3(String str)87     TestModule3(String str) {} // This is fine because Dagger can use the other constructor
88 
89     @Provides
provide()90     Dep3 provide() {
91       return new Dep3();
92     }
93   }
94 
95   // Test that modules with only abstract methods can have private constructors.
96   @Module
97   @InstallIn(SingletonComponent.class)
98   @SuppressWarnings("ClassCanBeStatic") // purposely testing non-static class here
99   abstract class TestModule4 {
TestModule4()100     private TestModule4() {}  // This is fine because Dagger doesn't need an instance.
101 
bind(Dep4 impl)102     @Binds @Named("Dep4") abstract Object bind(Dep4 impl);
103   }
104 
105   // Test that modules with only abstract methods can have constructors with parameters.
106   @Module
107   @InstallIn(SingletonComponent.class)
108   @SuppressWarnings("ClassCanBeStatic") // purposely testing non-static class here
109   abstract class TestModule5 {
TestModule5(String str)110     TestModule5(String str) {} // This is fine because Dagger doesn't need an instance.
111 
bind(Dep5 impl)112     @Binds @Named("Dep5") abstract Object bind(Dep5 impl);
113   }
114 
115   // Test that static modules with no methods can have private constructors.
116   @Module
117   @InstallIn(SingletonComponent.class)
118   static final class TestModule6 {
TestModule6()119     private TestModule6() {} // This is fine because Dagger doesn't need an instance.
120   }
121 
122   // Test that static modules with no methods can have constructors with parameters.
123   @Module
124   @InstallIn(SingletonComponent.class)
125   static final class TestModule7 {
TestModule7(String str)126     TestModule7(String str) {} // This is fine because Dagger doesn't need an instance.
127   }
128 
129   // Test that abstract modules with no methods can have private constructors.
130   @Module
131   @InstallIn(SingletonComponent.class)
132   @SuppressWarnings("ClassCanBeStatic") // purposely testing non-static class here
133   abstract class TestModule8 {
TestModule8()134     private TestModule8() {} // This is fine because Dagger doesn't need an instance.
135   }
136 
137   // Test that abstract modules with no methods can have constructors with parameters.
138   @Module
139   @InstallIn(SingletonComponent.class)
140   @SuppressWarnings("ClassCanBeStatic") // purposely testing non-static class here
141   abstract class TestModule9 {
TestModule9(String str)142     TestModule9(String str) {} // This is fine because Dagger doesn't need an instance.
143   }
144 
145   @Inject Dep1 dep1;
146   @Inject Dep2 dep2;
147   @Inject Dep5 dep3;
148   @Inject @Named("Dep4") Object dep4;
149   @Inject @Named("Dep5") Object dep5;
150 
151   @Before
setup()152   public void setup() {
153     rule.inject();
154   }
155 
156   @Test
testDep1()157   public void testDep1() throws Exception {
158     assertThat(dep1).isNotNull();
159   }
160 
161   @Test
testDep2()162   public void testDep2() throws Exception {
163     assertThat(dep2).isNotNull();
164   }
165 
166   @Test
testDep3()167   public void testDep3() throws Exception {
168     assertThat(dep3).isNotNull();
169   }
170 
171   @Test
testDep4()172   public void testDep4() throws Exception {
173     assertThat(dep4).isNotNull();
174     assertThat(dep4).isInstanceOf(Dep4.class);
175   }
176 
177   @Test
testDep5()178   public void testDep5() throws Exception {
179     assertThat(dep5).isNotNull();
180     assertThat(dep5).isInstanceOf(Dep5.class);
181   }
182 }
183