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