aboutsummaryrefslogtreecommitdiff
path: root/javatests/dagger/hilt/android/MultiTestRoot2Test.java
blob: 39aecfa2e95433f3787ca2d330e3591fe471a485 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Copyright (C) 2020 The Dagger Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dagger.hilt.android;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import android.os.Build;
import androidx.activity.ComponentActivity;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import dagger.Module;
import dagger.Provides;
import dagger.hilt.EntryPoint;
import dagger.hilt.EntryPoints;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ActivityComponent;
import dagger.hilt.android.testing.BindValue;
import dagger.hilt.android.testing.HiltAndroidRule;
import dagger.hilt.android.testing.HiltAndroidTest;
import dagger.hilt.android.testing.HiltTestApplication;
import dagger.hilt.components.SingletonComponent;
import javax.inject.Inject;
import javax.inject.Named;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;

@HiltAndroidTest
@RunWith(AndroidJUnit4.class)
// Robolectric requires Java9 to run API 29 and above, so use API 28 instead
@Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class)
public final class MultiTestRoot2Test {
  private static final int INT_VALUE = 13;
  private static final String STR_VALUE = "MultiTestRoot2TestValue";
  private static final long LONG_VALUE = 17L;
  private static final String BIND_VALUE_STRING = "BIND_VALUE_STRING";
  private static final String TEST_QUALIFIER = "TEST_QUALIFIER";

  @EntryPoint
  @InstallIn(SingletonComponent.class)
  public interface BindValueEntryPoint {
    @Named(TEST_QUALIFIER)
    String bindValueString();
  }

  @AndroidEntryPoint(ComponentActivity.class)
  public static class TestActivity extends Hilt_MultiTestRoot2Test_TestActivity {
    @Inject Baz baz;
    @Inject @MultiTestRootExternalModules.External Long externalLongValue;
  }

  @Module
  @InstallIn(ActivityComponent.class)
  public interface TestActivityModule {
    @Provides
    static Baz provideBaz() {
      return new Baz(LONG_VALUE);
    }
  }

  @Module
  @InstallIn(SingletonComponent.class)
  interface PkgPrivateTestModule {
    @Provides
    static Qux provideQux() {
      return new Qux();
    }
  }

  @Module
  @InstallIn(SingletonComponent.class)
  public interface TestModule {
    @Provides
    static int provideInt() {
      return INT_VALUE;
    }

    @Provides
    static String provideString() {
      return STR_VALUE;
    }
  }

  public static final class Outer {
    @Module
    @InstallIn(SingletonComponent.class)
    public interface NestedTestModule {
      @Provides
      static long provideLong() {
        return LONG_VALUE;
      }
    }

    private Outer() {}
  }

  static class Foo {
    final int value;

    @Inject
    Foo(int value) {
      this.value = value;
    }
  }

  static class Bar {
    final String value;

    Bar(String value) {
      this.value = value;
    }
  }

  static class Baz {
    final long value;

    Baz(long value) {
      this.value = value;
    }
  }

  static class Qux {}

  @Module
  @InstallIn(SingletonComponent.class)
  public interface BarModule {
    @Provides
    static Bar provideBar(String value) {
      return new Bar(value);
    }
  }

  @EntryPoint
  @InstallIn(SingletonComponent.class)
  public interface BarEntryPoint {
    Bar getBar();
  }

  @EntryPoint
  @InstallIn(SingletonComponent.class)
  interface PkgPrivateQuxEntryPoint {
    Qux getQux();
  }

  @Rule public HiltAndroidRule rule = new HiltAndroidRule(this);

  @Inject Foo foo;
  @Inject Qux qux;
  @Inject String str;
  @Inject Long longValue;
  @Inject @MultiTestRootExternalModules.External String externalStrValue;

  @BindValue
  @Named(TEST_QUALIFIER)
  String bindValueString = BIND_VALUE_STRING;

  @Test
  public void testInjectFromTestModule() throws Exception {
    assertThat(foo).isNull();
    rule.inject();
    assertThat(foo).isNotNull();
    assertThat(foo.value).isEqualTo(INT_VALUE);
  }

  @Test
  public void testInjectFromTestModuleWithArgs() throws Exception {
    assertThat(str).isNull();
    rule.inject();
    assertThat(str).isNotNull();
    assertThat(str).isEqualTo(STR_VALUE);
  }

  @Test
  public void testInjectFromNestedTestModule() throws Exception {
    assertThat(longValue).isNull();
    rule.inject();
    assertThat(longValue).isNotNull();
    assertThat(longValue).isEqualTo(LONG_VALUE);
  }

  @Test
  public void testInjectFromPkgPrivateTestModule() throws Exception {
    assertThat(qux).isNull();
    rule.inject();
    assertThat(qux).isNotNull();
  }

  @Test
  public void testInjectFromExternalAppModule() throws Exception {
    assertThat(externalStrValue).isNull();
    rule.inject();
    assertThat(externalStrValue).isNotNull();
    assertThat(externalStrValue).isEqualTo(MultiTestRootExternalModules.EXTERNAL_STR_VALUE);
  }

  @Test
  public void testInjectFromExternalActivityModule() throws Exception {
    rule.inject();
    ActivityController<TestActivity> ac = Robolectric.buildActivity(TestActivity.class);
    assertThat(ac.get().externalLongValue).isNull();
    ac.create();
    assertThat(ac.get().externalLongValue).isNotNull();
    assertThat(ac.get().externalLongValue)
        .isEqualTo(MultiTestRootExternalModules.EXTERNAL_LONG_VALUE);
  }

  @Test
  public void testLocalEntryPoint() throws Exception {
    rule.inject();
    Bar bar = EntryPoints.get(getApplicationContext(), BarEntryPoint.class).getBar();
    assertThat(bar).isNotNull();
    assertThat(bar.value).isEqualTo(STR_VALUE);
  }

  @Test
  public void testLocalPkgPrivateEntryPoint() throws Exception {
    rule.inject();
    Qux qux = EntryPoints.get(getApplicationContext(), PkgPrivateQuxEntryPoint.class).getQux();
    assertThat(qux).isNotNull();
  }

  @Test
  public void testAndroidEntryPoint() throws Exception {
    rule.inject();
    ActivityController<TestActivity> ac = Robolectric.buildActivity(TestActivity.class);
    assertThat(ac.get().baz).isNull();
    ac.create();
    assertThat(ac.get().baz).isNotNull();
    assertThat(ac.get().baz.value).isEqualTo(LONG_VALUE);
  }

  @Test
  public void testMissingMultiTestRoot1EntryPoint() throws Exception {
    rule.inject();
    ClassCastException exception =
        assertThrows(
            ClassCastException.class,
            () -> EntryPoints.get(getApplicationContext(), MultiTestRoot1Test.BarEntryPoint.class));
    assertThat(exception)
          .hasMessageThat()
          .isEqualTo(
              "Cannot cast dagger.hilt.android.DaggerMultiTestRoot2Test_HiltComponents_SingletonC"
              + " to dagger.hilt.android.MultiTestRoot1Test$BarEntryPoint");
  }

  @Test
  public void testBindValueFieldIsProvided() throws Exception {
    rule.inject();
    assertThat(bindValueString).isEqualTo(BIND_VALUE_STRING);
    assertThat(getBinding()).isEqualTo(BIND_VALUE_STRING);
  }

  @Test
  public void testBindValueIsMutable() throws Exception {
    rule.inject();
    bindValueString = "newValue";
    assertThat(getBinding()).isEqualTo("newValue");
  }

  private static String getBinding() {
    return EntryPoints.get(getApplicationContext(), BindValueEntryPoint.class).bindValueString();
  }
}