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 android.app.Activity; 20 import androidx.fragment.app.Fragment; 21 import dagger.Module; 22 import dagger.Provides; 23 import dagger.hilt.InstallIn; 24 import dagger.hilt.android.components.ActivityComponent; 25 import dagger.hilt.android.components.FragmentComponent; 26 import dagger.hilt.components.SingletonComponent; 27 import dagger.hilt.testing.TestInstallIn; 28 import javax.inject.Qualifier; 29 30 /** Modules and classes used in TestInstallInFooTest and TestInstallInBarTest. */ 31 final class TestInstallInModules { TestInstallInModules()32 private TestInstallInModules() {} 33 34 @Qualifier 35 public @interface ActivityLevel {} 36 37 @Qualifier 38 public @interface FragmentLevel {} 39 40 static class Foo { 41 Class<?> moduleClass; 42 Foo(Class<?> moduleClass)43 Foo(Class<?> moduleClass) { 44 this.moduleClass = moduleClass; 45 } 46 } 47 48 static class Bar { 49 Class<?> moduleClass; 50 Bar(Class<?> moduleClass)51 Bar(Class<?> moduleClass) { 52 this.moduleClass = moduleClass; 53 } 54 } 55 56 @Module 57 @InstallIn(SingletonComponent.class) 58 interface SingletonFooModule { 59 @Provides provideFoo()60 static Foo provideFoo() { 61 return new Foo(SingletonFooModule.class); 62 } 63 } 64 65 @Module 66 @InstallIn(SingletonComponent.class) 67 interface SingletonBarModule { 68 @Provides provideFoo()69 static Bar provideFoo() { 70 return new Bar(SingletonBarModule.class); 71 } 72 } 73 74 @Module 75 @TestInstallIn(components = SingletonComponent.class, replaces = SingletonFooModule.class) 76 interface SingletonFooTestModule { 77 @Provides provideFoo()78 static Foo provideFoo() { 79 return new Foo(SingletonFooTestModule.class); 80 } 81 } 82 83 @Module 84 @InstallIn(ActivityComponent.class) 85 interface ActivityFooModule { 86 @Provides 87 @ActivityLevel provideFoo()88 static Foo provideFoo() { 89 throw new AssertionError("This provides method should never be called."); 90 } 91 } 92 93 @Module 94 @InstallIn(ActivityComponent.class) 95 interface ActivityBarModule { 96 @Provides 97 @ActivityLevel 98 // Add an activity dependency to make sure Dagger adds this module to the correct component. provideFoo(@uppressWarnings"UnusedVariable") Activity activity)99 static Bar provideFoo(@SuppressWarnings("UnusedVariable") Activity activity) { 100 return new Bar(ActivityBarModule.class); 101 } 102 } 103 104 @Module 105 @TestInstallIn(components = ActivityComponent.class, replaces = ActivityFooModule.class) 106 interface ActivityFooTestModule { 107 @Provides 108 @ActivityLevel 109 // Add an activity dependency to make sure Dagger adds this module to the correct component. provideFoo(@uppressWarnings"UnusedVariable") Activity activity)110 static Foo provideFoo(@SuppressWarnings("UnusedVariable") Activity activity) { 111 return new Foo(ActivityFooTestModule.class); 112 } 113 } 114 115 @Module 116 @InstallIn(FragmentComponent.class) 117 interface FragmentFooModule { 118 @Provides 119 @FragmentLevel provideFoo()120 static Foo provideFoo() { 121 throw new AssertionError("This provides method should never be called."); 122 } 123 } 124 125 @Module 126 @InstallIn(FragmentComponent.class) 127 interface FragmentBarModule { 128 @Provides 129 @FragmentLevel 130 // Add a fragment dependency to make sure Dagger adds this module to the correct component. provideFoo(@uppressWarnings"UnusedVariable") Fragment fragment)131 static Bar provideFoo(@SuppressWarnings("UnusedVariable") Fragment fragment) { 132 return new Bar(FragmentBarModule.class); 133 } 134 } 135 136 @Module 137 @TestInstallIn(components = FragmentComponent.class, replaces = FragmentFooModule.class) 138 interface FragmentFooTestModule { 139 @Provides 140 @FragmentLevel 141 // Add a fragment dependency to make sure Dagger adds this module to the correct component. provideFoo(@uppressWarnings"UnusedVariable") Fragment fragment)142 static Foo provideFoo(@SuppressWarnings("UnusedVariable") Fragment fragment) { 143 return new Foo(FragmentFooTestModule.class); 144 } 145 } 146 } 147