aboutsummaryrefslogtreecommitdiff
path: root/javatests/dagger/hilt/android/InjectionTest.java
blob: 7c3427919a8dbb25fb640b9bda9aa3a6b3a2bea2 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
 * 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 java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.IntentService;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ActivityComponent;
import dagger.hilt.android.components.FragmentComponent;
import dagger.hilt.android.components.ServiceComponent;
import dagger.hilt.android.testing.HiltAndroidRule;
import dagger.hilt.android.testing.HiltAndroidTest;
import dagger.hilt.android.testing.HiltTestApplication;
import dagger.hilt.components.SingletonComponent;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.concurrent.atomic.AtomicLong;
import javax.inject.Inject;
import javax.inject.Qualifier;
import javax.inject.Singleton;
import org.junit.Before;
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 InjectionTest {
  private static final String APP_BINDING = "APP_BINDING";
  private static final String ACTIVITY_BINDING = "ACTIVIY_BINDING";
  private static final String FRAGMENT_BINDING = "FRAGMENT_BINDING";
  private static final String SERVICE_BINDING = "SERVICE_BINDING";

  @Retention(RUNTIME)
  @Qualifier
  @Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
  @interface ApplicationLevel {}

  @Retention(RUNTIME)
  @Qualifier
  @Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
  @interface ActivityLevel {}

  @Retention(RUNTIME)
  @Qualifier
  @Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
  @interface FragmentLevel {}

  @Retention(RUNTIME)
  @Qualifier
  @Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
  @interface ServiceLevel {}

  /** Application level bindings */
  @Module
  @InstallIn(SingletonComponent.class)
  static final class AppModule {
    @Provides
    @ApplicationLevel
    static String providesAppBinding() {
      return APP_BINDING;
    }

    @Provides
    @Singleton
    static AtomicLong provideCounter() {
      return new AtomicLong();
    }

    @Provides
    static Long provideCount(AtomicLong counter) {
      return counter.incrementAndGet();
    }
  }

  /** Activity level bindings */
  @Module
  @InstallIn(ActivityComponent.class)
  static final class ActivityModule {
    @Provides
    @ActivityLevel
    static String providesActivityBinding() {
      return ACTIVITY_BINDING;
    }
  }

  /** Fragment level bindings */
  @Module
  @InstallIn(FragmentComponent.class)
  static final class FragmentModule {
    @Provides
    @FragmentLevel
    static String providesFragmentBinding() {
      return FRAGMENT_BINDING;
    }
  }

  /** Service level bindings */
  @Module
  @InstallIn(ServiceComponent.class)
  static final class ServiceModule {
    @Provides
    @ServiceLevel
    static String providesServiceBinding() {
      return SERVICE_BINDING;
    }
  }

  /** Hilt Activity */
  @AndroidEntryPoint(FragmentActivity.class)
  public static final class TestActivity extends Hilt_InjectionTest_TestActivity {
    @Inject @ApplicationLevel String appBinding;
    @Inject @ActivityLevel String activityBinding;
    boolean onCreateCalled;

    @Override
    public void onCreate(Bundle onSavedInstanceState) {
      assertThat(appBinding).isNull();
      assertThat(activityBinding).isNull();

      super.onCreate(onSavedInstanceState);

      assertThat(appBinding).isEqualTo(APP_BINDING);
      assertThat(activityBinding).isEqualTo(ACTIVITY_BINDING);

      onCreateCalled = true;
    }
  }

  /** Non-Hilt Activity */
  public static final class NonHiltActivity extends FragmentActivity {}

  /** Hilt Fragment */
  @AndroidEntryPoint(Fragment.class)
  public static final class TestFragment extends Hilt_InjectionTest_TestFragment {
    @Inject @ApplicationLevel String appBinding;
    @Inject @ActivityLevel String activityBinding;
    @Inject @FragmentLevel String fragmentBinding;
    boolean onAttachContextCalled;
    boolean onAttachActivityCalled;

    @Override
    public void onAttach(Context context) {
      preInjectionAssert();
      super.onAttach(context);
      postInjectionAssert();
      onAttachContextCalled = true;
    }

    @Override
    public void onAttach(Activity activity) {
      preInjectionAssert();
      super.onAttach(activity);
      postInjectionAssert();
      onAttachActivityCalled = true;
    }

    private void preInjectionAssert() {
      assertThat(appBinding).isNull();
      assertThat(activityBinding).isNull();
      assertThat(fragmentBinding).isNull();
    }

    private void postInjectionAssert() {
      assertThat(appBinding).isEqualTo(APP_BINDING);
      assertThat(activityBinding).isEqualTo(ACTIVITY_BINDING);
      assertThat(fragmentBinding).isEqualTo(FRAGMENT_BINDING);
    }
  }

  /** Non-Hilt Fragment */
  public static final class NonHiltFragment extends Fragment {}

  /** Hilt extends parameterized fragment. */
  @AndroidEntryPoint(ParameterizedFragment.class)
  public static final class TestParameterizedFragment
      extends Hilt_InjectionTest_TestParameterizedFragment<Integer> {
    @Inject @ApplicationLevel String appBinding;
    @Inject @ActivityLevel String activityBinding;
    @Inject @FragmentLevel String fragmentBinding;
    boolean onAttachContextCalled;
    boolean onAttachActivityCalled;

    @Override
    public void onAttach(Context context) {
      preInjectionAssert();
      super.onAttach(context);
      postInjectionAssert();
      onAttachContextCalled = true;
    }

    @Override
    public void onAttach(Activity activity) {
      preInjectionAssert();
      super.onAttach(activity);
      postInjectionAssert();
      onAttachActivityCalled = true;
    }

    private void preInjectionAssert() {
      assertThat(appBinding).isNull();
      assertThat(activityBinding).isNull();
      assertThat(fragmentBinding).isNull();
    }

    private void postInjectionAssert() {
      assertThat(appBinding).isEqualTo(APP_BINDING);
      assertThat(activityBinding).isEqualTo(ACTIVITY_BINDING);
      assertThat(fragmentBinding).isEqualTo(FRAGMENT_BINDING);
    }
  }

  /** Non-Hilt parameterized fragment */
  public static class ParameterizedFragment<T> extends Fragment {}

  /** Hilt View */
  @AndroidEntryPoint(LinearLayout.class)
  public static final class TestView extends Hilt_InjectionTest_TestView {
    @Inject @ApplicationLevel String appBinding;
    @Inject @ActivityLevel String activityBinding;

    TestView(Context context) {
      super(context);
    }

    TestView(Context context, AttributeSet attrs) {
      super(context, attrs);
    }

    TestView(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
    }

    @TargetApi(21)
    TestView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
      super(context, attrs, defStyleAttr, defStyleRes);
    }
  }

  /** Hilt View (With Fragment bindings) */
  @WithFragmentBindings
  @AndroidEntryPoint(LinearLayout.class)
  public static final class TestViewWithFragmentBindings
      extends Hilt_InjectionTest_TestViewWithFragmentBindings {
    @Inject @ApplicationLevel String appBinding;
    @Inject @ActivityLevel String activityBinding;
    @Inject @FragmentLevel String fragmentBinding;

    TestViewWithFragmentBindings(Context context) {
      super(context);
    }
  }

  @AndroidEntryPoint(Service.class)
  public static final class TestService extends Hilt_InjectionTest_TestService {
    @Inject @ApplicationLevel String appBinding;
    @Inject @ServiceLevel String serviceBinding;

    @Override
    public IBinder onBind(Intent intent) {
      return null;
    }
  }

  @AndroidEntryPoint(IntentService.class)
  public static final class TestIntentService extends Hilt_InjectionTest_TestIntentService {
    private static final String NAME = "TestIntentServiceName";
    @Inject @ApplicationLevel String appBinding;
    @Inject @ServiceLevel String serviceBinding;

    TestIntentService() {
      super(NAME);
    }

    @Override
    public void onHandleIntent(Intent intent) {}
  }

  @AndroidEntryPoint(BroadcastReceiver.class)
  public static final class TestBroadcastReceiver extends Hilt_InjectionTest_TestBroadcastReceiver {
    @Inject @ApplicationLevel String appBinding;
    Intent lastIntent = null;

    @Override
    public void onReceive(Context context, Intent intent) {
      super.onReceive(context, intent);
      lastIntent = intent;
    }
  }

  @AndroidEntryPoint(BaseBroadcastReceiver.class)
  public static final class TestBroadcastReceiverWithBaseImplementingOnReceive
      extends Hilt_InjectionTest_TestBroadcastReceiverWithBaseImplementingOnReceive {
    @Inject @ApplicationLevel String appBinding;
    Intent baseLastIntent = null;

    @Override
    public void onReceive(Context context, Intent intent) {
      super.onReceive(context, intent);
      baseLastIntent = intent;
    }
  }

  abstract static class BaseBroadcastReceiver extends BroadcastReceiver {
    Intent lastIntent = null;

    @Override
    public void onReceive(Context context, Intent intent) {
      lastIntent = intent;
    }
  }

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

  @Inject @ApplicationLevel String appBinding;

  @Before
  public void setup() {
    rule.inject();
  }

  @Test
  public void testAppInjection() throws Exception {
    assertThat(appBinding).isEqualTo(APP_BINDING);
  }

  @Test
  public void testActivityInjection() throws Exception {
    ActivityController<TestActivity> controller = Robolectric.buildActivity(TestActivity.class);

    assertThat(controller.get().onCreateCalled).isFalse();
    controller.create();
    assertThat(controller.get().onCreateCalled).isTrue();
  }

  @Test
  public void testFragmentInjection() throws Exception {
    TestFragment fragment = new TestFragment();
    assertThat(fragment.onAttachContextCalled).isFalse();
    assertThat(fragment.onAttachActivityCalled).isFalse();
    setupFragment(TestActivity.class, fragment);
    assertThat(fragment.onAttachContextCalled).isTrue();
    assertThat(fragment.onAttachActivityCalled).isTrue();
  }

  @Test
  public void testParameterizedFragmentInjection() throws Exception {
    TestParameterizedFragment fragment = new TestParameterizedFragment();
    assertThat(fragment.onAttachContextCalled).isFalse();
    assertThat(fragment.onAttachActivityCalled).isFalse();
    setupFragment(TestActivity.class, fragment);
    assertThat(fragment.onAttachContextCalled).isTrue();
    assertThat(fragment.onAttachActivityCalled).isTrue();
  }

  @Test
  public void testViewNoFragmentBindingsWithActivity() throws Exception {
    TestActivity activity = Robolectric.setupActivity(TestActivity.class);
    TestView view = new TestView(activity);
    assertThat(view.appBinding).isEqualTo(APP_BINDING);
    assertThat(view.activityBinding).isEqualTo(ACTIVITY_BINDING);
  }

  @Test
  public void testViewNoFragmentBindingsWithFragment() throws Exception {
    TestFragment fragment = setupFragment(TestActivity.class, new TestFragment());
    TestView view = new TestView(fragment.getContext());
    assertThat(view.appBinding).isEqualTo(APP_BINDING);
    assertThat(view.activityBinding).isEqualTo(ACTIVITY_BINDING);
  }

  @Test
  public void testViewNoFragmentBindingsWithFragment_secondConstructor() throws Exception {
    TestFragment fragment = setupFragment(TestActivity.class, new TestFragment());
    TestView view = new TestView(fragment.getContext(), /* attrs= */ null);
    assertThat(view.appBinding).isEqualTo(APP_BINDING);
    assertThat(view.activityBinding).isEqualTo(ACTIVITY_BINDING);
  }

  @Test
  public void testViewNoFragmentBindingsWithFragment_thirdConstructor() throws Exception {
    TestFragment fragment = setupFragment(TestActivity.class, new TestFragment());
    TestView view = new TestView(fragment.getContext(), /* attrs= */ null, /* defStyleAttr= */ 0);
    assertThat(view.appBinding).isEqualTo(APP_BINDING);
    assertThat(view.activityBinding).isEqualTo(ACTIVITY_BINDING);
  }

  @Test
  @Config(sdk = 21)
  public void testViewNoFragmentBindingsWithFragment_fourthConstructor_presentOnTwentyOne()
      throws Exception {
    TestFragment fragment = setupFragment(TestActivity.class, new TestFragment());
    TestView view =
        new TestView(
            fragment.getContext(), /* attrs= */ null, /* defStyleAttr= */ 0, /* defStyleRes= */ 0);
    assertThat(view.appBinding).isEqualTo(APP_BINDING);
    assertThat(view.activityBinding).isEqualTo(ACTIVITY_BINDING);
  }

  @Test
  @Config(sdk = 19)
  public void testViewNoFragmentBindingsWithFragment_fourthConstructor_notPresentOnTwenty() {
    TestFragment fragment = setupFragment(TestActivity.class, new TestFragment());

    assertThrows(
        NoSuchMethodError.class,
        () ->
            new TestView(
                fragment.getContext(),
                /* attrs= */ null,
                /* defStyleAttr= */ 0,
                /* defStyleRes= */ 0));
  }

  @Test
  public void testServiceInjection() throws Exception {
    TestService testService = Robolectric.setupService(TestService.class);
    assertThat(testService.appBinding).isEqualTo(APP_BINDING);
    assertThat(testService.serviceBinding).isEqualTo(SERVICE_BINDING);
  }

  @Test
  public void testIntentServiceInjection() throws Exception {
    TestIntentService testIntentService = Robolectric.setupService(TestIntentService.class);
    assertThat(testIntentService.appBinding).isEqualTo(APP_BINDING);
    assertThat(testIntentService.serviceBinding).isEqualTo(SERVICE_BINDING);
  }

  @Test
  public void testBroadcastReceiverInjection() throws Exception {
    TestBroadcastReceiver testBroadcastReceiver = new TestBroadcastReceiver();
    Intent intent = new Intent();
    testBroadcastReceiver.onReceive(getApplicationContext(), intent);
    assertThat(testBroadcastReceiver.appBinding).isEqualTo(APP_BINDING);
    assertThat(testBroadcastReceiver.lastIntent).isSameInstanceAs(intent);
  }

  @Test
  public void testBroadcastReceiverWithBaseImplementingOnReceiveInjection() throws Exception {
    TestBroadcastReceiverWithBaseImplementingOnReceive testBroadcastReceiver =
        new TestBroadcastReceiverWithBaseImplementingOnReceive();
    Intent intent = new Intent();
    testBroadcastReceiver.onReceive(getApplicationContext(), intent);
    assertThat(testBroadcastReceiver.appBinding).isEqualTo(APP_BINDING);
    assertThat(testBroadcastReceiver.lastIntent).isSameInstanceAs(intent);
    assertThat(testBroadcastReceiver.baseLastIntent).isSameInstanceAs(intent);
  }

  @Test
  public void testViewWithFragmentBindingsWithFragment() throws Exception {
    TestFragment fragment = setupFragment(TestActivity.class, new TestFragment());

    Context fragmentContext = fragment.getContext();
    TestViewWithFragmentBindings view = new TestViewWithFragmentBindings(fragmentContext);
    assertThat(view.appBinding).isEqualTo(APP_BINDING);
    assertThat(view.activityBinding).isEqualTo(ACTIVITY_BINDING);
    assertThat(view.fragmentBinding).isEqualTo(FRAGMENT_BINDING);
  }

  @Test
  public void testViewWithFragmentBindingsFailsWithActivity() throws Exception {
    TestActivity activity = Robolectric.setupActivity(TestActivity.class);
    try {
      new TestViewWithFragmentBindings(activity);
      fail("Expected test to fail but it passes!");
    } catch (IllegalStateException e) {
      assertThat(e)
          .hasMessageThat()
      .contains(
      "@WithFragmentBindings Hilt view must be attached to an @AndroidEntryPoint Fragment");
    }
  }

  @Test
  public void testFragmentAttachedToNonHiltActivityFails() throws Exception {
    NonHiltActivity activity = Robolectric.setupActivity(NonHiltActivity.class);
    try {
      activity
          .getSupportFragmentManager()
          .beginTransaction()
          .add(new TestFragment(), null)
          .commitNow();
      fail("Expected test to fail but it passes!");
    } catch (IllegalStateException e) {
      assertThat(e)
          .hasMessageThat()
      .contains("Hilt Fragments must be attached to an @AndroidEntryPoint Activity");
    }
  }

  @Test
  public void testViewAttachedToNonHiltActivityFails() throws Exception {
    NonHiltActivity activity = Robolectric.setupActivity(NonHiltActivity.class);
    try {
      new TestView(activity);
      fail("Expected test to fail but it passes!");
    } catch (IllegalStateException e) {
      assertThat(e)
          .hasMessageThat()
      .contains("Hilt view must be attached to an @AndroidEntryPoint Fragment or Activity");
    }
  }

  @Test
  public void testViewAttachedToNonHiltFragmentFails() throws Exception {
    NonHiltActivity activity = Robolectric.setupActivity(NonHiltActivity.class);
    NonHiltFragment fragment = new NonHiltFragment();
    activity.getSupportFragmentManager().beginTransaction().add(fragment, null).commitNow();
    Context nonHiltContext = fragment.getContext();
    try {
      new TestView(nonHiltContext);
      fail("Expected test to fail but it passes!");
    } catch (IllegalStateException e) {
      assertThat(e)
          .hasMessageThat()
      .contains("Hilt view must be attached to an @AndroidEntryPoint Fragment or Activity");
    }
  }

  @Test
  public void testViewAttachedToApplicationContextFails() throws Exception {
    try {
      new TestView(getApplicationContext());
      fail("Expected test to fail but it passes!");
    } catch (IllegalStateException e) {
      assertThat(e)
          .hasMessageThat()
      .contains(
          "Hilt view cannot be created using the application context. "
              + "Use a Hilt Fragment or Activity context");
    }
  }

  /** Hilt Activity that manually calls inject(). */
  @AndroidEntryPoint(FragmentActivity.class)
  public static final class DoubleInjectActivity extends Hilt_InjectionTest_DoubleInjectActivity {
    @Inject Long counter;

    @Override
    public void onCreate(Bundle onSavedInstanceState) {
      inject();
      super.onCreate(onSavedInstanceState);
    }
  }

  @Test
  public void testActivityDoesNotInjectTwice() throws Exception {
    ActivityController<DoubleInjectActivity> controller =
        Robolectric.buildActivity(DoubleInjectActivity.class);
    controller.create();
    assertThat(controller.get().counter).isEqualTo(1L);
  }

  /** Hilt Fragment that manually calls inject(). */
  @AndroidEntryPoint(Fragment.class)
  public static final class DoubleInjectFragment extends Hilt_InjectionTest_DoubleInjectFragment {
    @Inject Long counter;

    @Override
    public void onAttach(Context context) {
      inject();
      super.onAttach(context);
    }

    @Override
    public void onAttach(Activity activity) {
      inject();
      super.onAttach(activity);
    }
  }

  @Test
  public void testFragmentDoesNotInjectTwice() throws Exception {
    DoubleInjectFragment fragment = setupFragment(TestActivity.class, new DoubleInjectFragment());
    assertThat(fragment.counter).isEqualTo(1L);
  }

  /** Hilt View that manually calls inject(). */
  @AndroidEntryPoint(LinearLayout.class)
  public static final class DoubleInjectView extends Hilt_InjectionTest_DoubleInjectView {
    @Inject Long counter;

    DoubleInjectView(Context context) {
      super(context);
      inject();
    }

    DoubleInjectView(Context context, AttributeSet attrs) {
      super(context, attrs);
      inject();
    }

    DoubleInjectView(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      inject();
    }

    @TargetApi(21)
    DoubleInjectView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
      super(context, attrs, defStyleAttr, defStyleRes);
      inject();
    }
  }

  @Test
  public void testViewDoesNotInjectTwice() throws Exception {
    TestActivity activity = Robolectric.setupActivity(TestActivity.class);
    DoubleInjectView view = new DoubleInjectView(activity);
    assertThat(view.counter).isEqualTo(1L);
  }

  /** Hilt Service that manually calls inject(). */
  @AndroidEntryPoint(Service.class)
  public static final class DoubleInjectService extends Hilt_InjectionTest_DoubleInjectService {
    @Inject Long counter;

    @Override public void onCreate() {
      inject();
      super.onCreate();
    }

    @Override
    public IBinder onBind(Intent intent) {
      return null;
    }
  }

  @Test
  public void testServiceDoesNotInjectTwice() throws Exception {
    DoubleInjectService testService = Robolectric.setupService(DoubleInjectService.class);
    assertThat(testService.counter).isEqualTo(1L);
  }

  /** Hilt BroadcastReceiver that manually calls inject(). */
  @AndroidEntryPoint(BroadcastReceiver.class)
  public static final class DoubleInjectBroadcastReceiver
      extends Hilt_InjectionTest_DoubleInjectBroadcastReceiver {
    @Inject Long counter;

    @Override
    public void onReceive(Context context, Intent intent) {
      inject(context);
      super.onReceive(context, intent);
    }
  }

  @Test
  public void testBroadcastReceiverDoesNotInjectTwice() throws Exception {
    DoubleInjectBroadcastReceiver testBroadcastReceiver = new DoubleInjectBroadcastReceiver();
    Intent intent = new Intent();
    testBroadcastReceiver.onReceive(getApplicationContext(), intent);
    assertThat(testBroadcastReceiver.counter).isEqualTo(1L);
  }

  private static <T extends Fragment> T setupFragment(
      Class<? extends FragmentActivity> activityClass, T fragment) {
    FragmentActivity activity = Robolectric.setupActivity(activityClass);
    attachFragment(activity, fragment);
    return fragment;
  }

  private static void attachFragment(FragmentActivity activity, Fragment fragment) {
    activity.getSupportFragmentManager().beginTransaction().add(fragment, "").commitNow();
  }
}