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.testing.compile.HiltCompilerTests.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 .withOptions("-Adagger.hilt.shareTestComponents=false") 87 .compile( 88 JavaFileObjects.forSourceLines( 89 "test.MyTest", 90 "package test;", 91 "", 92 "import dagger.hilt.android.testing.HiltAndroidTest;", 93 "", 94 "@HiltAndroidTest", 95 "public class MyTest {}"), 96 entryPoint("SingletonComponent", "EntryPoint1"), 97 entryPoint("SingletonComponent", "EntryPoint2"), 98 entryPoint("ActivityComponent", "EntryPoint3"), 99 entryPoint("ActivityComponent", "EntryPoint4")); 100 assertThat(compilation).succeeded(); 101 assertThat(compilation) 102 .generatedSourceFile("test/MyTest_HiltComponents") 103 .contentsAsUtf8String() 104 .contains( 105 JOINER.join( 106 " public abstract static class SingletonC implements" 107 + " HiltWrapper_ActivityRetainedComponentManager" 108 + "_ActivityRetainedComponentBuilderEntryPoint,", 109 " ServiceComponentManager.ServiceComponentBuilderEntryPoint,", 110 " SingletonComponent,", 111 " TestSingletonComponent,", 112 " EntryPoint1,", 113 " EntryPoint2,", 114 " MyTest_GeneratedInjector {")); 115 116 assertThat(compilation) 117 .generatedSourceFile("test/MyTest_HiltComponents") 118 .contentsAsUtf8String() 119 .contains( 120 JOINER.join( 121 " public abstract static class ActivityC implements ActivityComponent,", 122 " FragmentComponentManager.FragmentComponentBuilderEntryPoint,", 123 " ViewComponentManager.ViewComponentBuilderEntryPoint,", 124 " GeneratedComponent,", 125 " EntryPoint3,", 126 " EntryPoint4 {")); 127 } 128 129 @Test testSharedTestComponents()130 public void testSharedTestComponents() { 131 Compilation compilation = 132 compiler() 133 .withOptions("-Adagger.hilt.shareTestComponents=true") 134 .compile( 135 JavaFileObjects.forSourceLines( 136 "test.MyTest", 137 "package test;", 138 "", 139 "import dagger.hilt.android.testing.HiltAndroidTest;", 140 "", 141 "@HiltAndroidTest", 142 "public class MyTest {}"), 143 entryPoint("SingletonComponent", "EntryPoint1")); 144 assertThat(compilation).succeeded(); 145 assertThat(compilation) 146 .generatedSourceFile("dagger/hilt/android/internal/testing/root/Default_HiltComponents") 147 .contentsAsUtf8String() 148 .contains( 149 JOINER.join( 150 " public abstract static class SingletonC implements" 151 + " HiltWrapper_ActivityRetainedComponentManager" 152 + "_ActivityRetainedComponentBuilderEntryPoint,", 153 " ServiceComponentManager.ServiceComponentBuilderEntryPoint,", 154 " SingletonComponent,", 155 " TestSingletonComponent,", 156 " EntryPoint1,", 157 " MyTest_GeneratedInjector {")); 158 } 159 entryPoint(String component, String name)160 private static JavaFileObject entryPoint(String component, String name) { 161 return JavaFileObjects.forSourceLines( 162 "test." + name, 163 "package test;", 164 "", 165 "import dagger.hilt.EntryPoint;", 166 "import dagger.hilt.InstallIn;", 167 component.equals("SingletonComponent") ? "import dagger.hilt.components.SingletonComponent;" 168 : "import dagger.hilt.android.components." + component + ";", 169 "", 170 "@EntryPoint", 171 "@InstallIn(" + component + ".class)", 172 "public interface " + name + " {}"); 173 } 174 } 175