• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.testinstallin;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import androidx.test.ext.junit.runners.AndroidJUnit4;
22 import dagger.Module;
23 import dagger.Provides;
24 import dagger.hilt.InstallIn;
25 import dagger.hilt.android.testing.HiltAndroidRule;
26 import dagger.hilt.android.testing.HiltAndroidTest;
27 import dagger.hilt.android.testing.HiltTestApplication;
28 import dagger.hilt.android.testing.UninstallModules;
29 import dagger.hilt.android.testing.testinstallin.TestInstallInModules.Bar;
30 import dagger.hilt.android.testing.testinstallin.TestInstallInModules.Foo;
31 import dagger.hilt.android.testing.testinstallin.TestInstallInModules.SingletonBarModule;
32 import dagger.hilt.android.testing.testinstallin.TestInstallInModules.SingletonFooTestModule;
33 import dagger.hilt.components.SingletonComponent;
34 import javax.inject.Inject;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.robolectric.annotation.Config;
39 
40 /**
41  * Tests that Foo uses the global {@linkplain TestInstallIn} module and that Bar uses the local
42  * {@linkplain InstallIn} module due to {@linkplain UninstallModules}.
43  */
44 @UninstallModules(SingletonBarModule.class)
45 @HiltAndroidTest
46 @RunWith(AndroidJUnit4.class)
47 @Config(application = HiltTestApplication.class)
48 public final class TestInstallInBarTest {
49 
50   @Rule public HiltAndroidRule hiltRule = new HiltAndroidRule(this);
51 
52   @Module
53   @InstallIn(SingletonComponent.class)
54   interface LocalBarTestModule {
55     @Provides
provideBar()56     static Bar provideBar() {
57       return new Bar(LocalBarTestModule.class);
58     }
59   }
60 
61   @Inject Foo foo;
62   @Inject Bar bar;
63 
64   @Test
testFoo()65   public void testFoo() {
66     hiltRule.inject();
67     assertThat(foo.moduleClass).isEqualTo(SingletonFooTestModule.class);
68   }
69 
70   @Test
testBar()71   public void testBar() {
72     hiltRule.inject();
73     assertThat(bar.moduleClass).isEqualTo(LocalBarTestModule.class);
74   }
75 }
76