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