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.processor.internal.root; 18 19 import androidx.room.compiler.processing.util.Source; 20 import com.google.common.base.Joiner; 21 import com.google.common.collect.ImmutableMap; 22 import com.google.common.truth.StringSubject; 23 import dagger.hilt.android.testing.compile.HiltCompilerTests; 24 import org.junit.Test; 25 import org.junit.runner.RunWith; 26 import org.junit.runners.JUnit4; 27 28 // This test makes sure we don't regress the formatting in the components file. 29 @RunWith(JUnit4.class) 30 public final class RootFileFormatterTest { 31 private static final Joiner JOINER = Joiner.on("\n"); 32 33 @Test testProdComponents()34 public void testProdComponents() { 35 HiltCompilerTests.hiltCompiler( 36 HiltCompilerTests.javaSource( 37 "test.TestApplication", 38 "package test;", 39 "", 40 "import android.app.Application;", 41 "import dagger.hilt.android.HiltAndroidApp;", 42 "", 43 "@HiltAndroidApp(Application.class)", 44 "public class TestApplication extends Hilt_TestApplication {}"), 45 entryPoint("SingletonComponent", "EntryPoint1"), 46 entryPoint("SingletonComponent", "EntryPoint2"), 47 entryPoint("ActivityComponent", "EntryPoint3"), 48 entryPoint("ActivityComponent", "EntryPoint4")) 49 .compile( 50 subject -> { 51 subject.hasErrorCount(0); 52 StringSubject stringSubject = 53 subject.generatedSourceFileWithPath("test/TestApplication_HiltComponents.java"); 54 stringSubject.contains( 55 JOINER.join( 56 " public abstract static class SingletonC implements" 57 + " HiltWrapper_ActivityRetainedComponentManager" 58 + "_ActivityRetainedComponentBuilderEntryPoint,", 59 " ServiceComponentManager.ServiceComponentBuilderEntryPoint,", 60 " SingletonComponent,", 61 " GeneratedComponent,", 62 " EntryPoint1,", 63 " EntryPoint2,", 64 " TestApplication_GeneratedInjector {")); 65 stringSubject.contains( 66 JOINER.join( 67 " public abstract static class ActivityC implements ActivityComponent,", 68 " DefaultViewModelFactories.ActivityEntryPoint,", 69 " HiltWrapper_HiltViewModelFactory_ActivityCreatorEntryPoint,", 70 " FragmentComponentManager.FragmentComponentBuilderEntryPoint,", 71 " ViewComponentManager.ViewComponentBuilderEntryPoint,", 72 " GeneratedComponent,", 73 " EntryPoint3,", 74 " EntryPoint4 {")); 75 }); 76 } 77 78 @Test testTestComponents()79 public void testTestComponents() { 80 HiltCompilerTests.hiltCompiler( 81 HiltCompilerTests.javaSource( 82 "test.MyTest", 83 "package test;", 84 "", 85 "import dagger.hilt.android.testing.HiltAndroidTest;", 86 "", 87 "@HiltAndroidTest", 88 "public class MyTest {}"), 89 entryPoint("SingletonComponent", "EntryPoint1"), 90 entryPoint("SingletonComponent", "EntryPoint2"), 91 entryPoint("ActivityComponent", "EntryPoint3"), 92 entryPoint("ActivityComponent", "EntryPoint4")) 93 .withProcessorOptions( 94 ImmutableMap.of("dagger.hilt.shareTestComponents", Boolean.toString(false))) 95 .compile( 96 subject -> { 97 subject.hasErrorCount(0); 98 StringSubject stringSubject = 99 subject.generatedSourceFileWithPath("test/MyTest_HiltComponents.java"); 100 stringSubject.contains( 101 JOINER.join( 102 " public abstract static class SingletonC implements" 103 + " HiltWrapper_ActivityRetainedComponentManager" 104 + "_ActivityRetainedComponentBuilderEntryPoint,", 105 " ServiceComponentManager.ServiceComponentBuilderEntryPoint,", 106 " SingletonComponent,", 107 " TestSingletonComponent,", 108 " EntryPoint1,", 109 " EntryPoint2,", 110 " MyTest_GeneratedInjector {")); 111 112 stringSubject.contains( 113 JOINER.join( 114 " public abstract static class ActivityC implements ActivityComponent,", 115 " DefaultViewModelFactories.ActivityEntryPoint,", 116 " HiltWrapper_HiltViewModelFactory_ActivityCreatorEntryPoint,", 117 " FragmentComponentManager.FragmentComponentBuilderEntryPoint,", 118 " ViewComponentManager.ViewComponentBuilderEntryPoint,", 119 " GeneratedComponent,", 120 " EntryPoint3,", 121 " EntryPoint4 {")); 122 }); 123 } 124 125 @Test testSharedTestComponents()126 public void testSharedTestComponents() { 127 HiltCompilerTests.hiltCompiler( 128 HiltCompilerTests.javaSource( 129 "test.MyTest", 130 "package test;", 131 "", 132 "import dagger.hilt.android.testing.HiltAndroidTest;", 133 "", 134 "@HiltAndroidTest", 135 "public class MyTest {}"), 136 entryPoint("SingletonComponent", "EntryPoint1")) 137 .withProcessorOptions( 138 ImmutableMap.of("dagger.hilt.shareTestComponents", Boolean.toString(true))) 139 .compile( 140 subject -> { 141 subject.hasErrorCount(0); 142 StringSubject stringSubject = 143 subject.generatedSourceFileWithPath( 144 "dagger/hilt/android/internal/testing/root/Default_HiltComponents.java"); 145 stringSubject.contains( 146 JOINER.join( 147 " public abstract static class SingletonC implements" 148 + " HiltWrapper_ActivityRetainedComponentManager" 149 + "_ActivityRetainedComponentBuilderEntryPoint,", 150 " ServiceComponentManager.ServiceComponentBuilderEntryPoint,", 151 " SingletonComponent,", 152 " TestSingletonComponent,", 153 " EntryPoint1,", 154 " MyTest_GeneratedInjector {")); 155 }); 156 } 157 entryPoint(String component, String name)158 private static Source entryPoint(String component, String name) { 159 return HiltCompilerTests.javaSource( 160 "test." + name, 161 "package test;", 162 "", 163 "import dagger.hilt.EntryPoint;", 164 "import dagger.hilt.InstallIn;", 165 component.equals("SingletonComponent") 166 ? "import dagger.hilt.components.SingletonComponent;" 167 : "import dagger.hilt.android.components." + component + ";", 168 "", 169 "@EntryPoint", 170 "@InstallIn(" + component + ".class)", 171 "public interface " + name + " {}"); 172 } 173 } 174