1 /* 2 * Copyright (C) 2021 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.processor.internal.aggregateddeps; 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.testing.compile.Compilation; 23 import com.google.testing.compile.JavaFileObjects; 24 import javax.tools.JavaFileObject; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 @RunWith(JUnit4.class) 30 public class EarlyEntryPointProcessorTest { 31 32 @Test testUsedWithEntryPoint_fails()33 public void testUsedWithEntryPoint_fails() { 34 JavaFileObject entryPoint = 35 JavaFileObjects.forSourceLines( 36 "test.UsedWithEntryPoint", 37 "package test;", 38 "", 39 "import dagger.hilt.android.EarlyEntryPoint;", 40 "import dagger.hilt.EntryPoint;", 41 "import dagger.hilt.InstallIn;", 42 "import dagger.hilt.components.SingletonComponent;", 43 "", 44 "@EarlyEntryPoint", 45 "@EntryPoint", 46 "@InstallIn(SingletonComponent.class)", 47 "public interface UsedWithEntryPoint {}"); 48 Compilation compilation = compiler().compile(entryPoint); 49 50 assertThat(compilation).failed(); 51 assertThat(compilation) 52 .hadErrorContaining( 53 "Only one of the following annotations can be used on test.UsedWithEntryPoint: " 54 + "[dagger.hilt.EntryPoint, dagger.hilt.android.EarlyEntryPoint]") 55 .inFile(entryPoint) 56 .onLine(11); 57 } 58 59 @Test testNotSingletonComponent_fails()60 public void testNotSingletonComponent_fails() { 61 JavaFileObject entryPoint = 62 JavaFileObjects.forSourceLines( 63 "test.NotSingletonComponent", 64 "package test;", 65 "", 66 "import dagger.hilt.android.EarlyEntryPoint;", 67 "import dagger.hilt.android.components.ActivityComponent;", 68 "import dagger.hilt.EntryPoint;", 69 "import dagger.hilt.InstallIn;", 70 "", 71 "@EarlyEntryPoint", 72 "@InstallIn(ActivityComponent.class)", 73 "public interface NotSingletonComponent {}"); 74 75 Compilation compilation = compiler().compile(entryPoint); 76 77 assertThat(compilation).failed(); 78 assertThat(compilation) 79 .hadErrorContaining( 80 "@EarlyEntryPoint can only be installed into the SingletonComponent. " 81 + "Found: [dagger.hilt.android.components.ActivityComponent]") 82 .inFile(entryPoint) 83 .onLine(10); 84 } 85 86 @Test testThatTestInstallInCannotOriginateFromTest()87 public void testThatTestInstallInCannotOriginateFromTest() { 88 JavaFileObject test = 89 JavaFileObjects.forSourceLines( 90 "test.MyTest", 91 "package test;", 92 "", 93 "import dagger.hilt.EntryPoint;", 94 "import dagger.hilt.InstallIn;", 95 "import dagger.hilt.android.EarlyEntryPoint;", 96 "import dagger.hilt.android.testing.HiltAndroidTest;", 97 "import dagger.hilt.components.SingletonComponent;", 98 "", 99 "@HiltAndroidTest", 100 "public class MyTest {", 101 " @EarlyEntryPoint", 102 " @InstallIn(SingletonComponent.class)", 103 " interface NestedEarlyEntryPoint {}", 104 "}"); 105 Compilation compilation = compiler().compile(test); 106 assertThat(compilation).failed(); 107 assertThat(compilation).hadErrorCount(1); 108 assertThat(compilation) 109 .hadErrorContaining( 110 "@EarlyEntryPoint-annotated entry point, test.MyTest.NestedEarlyEntryPoint, cannot " 111 + "be nested in (or originate from) a @HiltAndroidTest-annotated class, " 112 + "test.MyTest.") 113 .inFile(test) 114 .onLine(13); 115 } 116 } 117