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.processor.internal.root; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static dagger.hilt.android.testing.compile.HiltCompilerTests.compileWithKapt; 21 import static dagger.hilt.android.testing.compile.HiltCompilerTests.javaSource; 22 23 import androidx.room.compiler.processing.util.DiagnosticMessage; 24 import androidx.room.compiler.processing.util.Source; 25 import com.google.common.collect.ImmutableCollection; 26 import com.google.common.collect.ImmutableList; 27 import com.google.common.collect.ImmutableMap; 28 import java.util.List; 29 import javax.tools.Diagnostic.Kind; 30 import org.junit.Rule; 31 import org.junit.Test; 32 import org.junit.rules.TemporaryFolder; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.Parameterized; 35 import org.junit.runners.Parameterized.Parameters; 36 37 @RunWith(Parameterized.class) 38 public final class MyTestPreviousCompilationTest { 39 40 @Parameters(name = "{0}") parameters()41 public static ImmutableCollection<Object[]> parameters() { 42 return ImmutableList.copyOf(new Object[][] {{true}, {false}}); 43 } 44 45 @Rule public TemporaryFolder tempFolderRule = new TemporaryFolder(); 46 47 private final boolean disableCrossCompilationRootValidation; 48 MyTestPreviousCompilationTest(boolean disableCrossCompilationRootValidation)49 public MyTestPreviousCompilationTest(boolean disableCrossCompilationRootValidation) { 50 this.disableCrossCompilationRootValidation = disableCrossCompilationRootValidation; 51 } 52 processorOptions()53 private ImmutableMap<String, String> processorOptions() { 54 return ImmutableMap.of( 55 "dagger.hilt.disableCrossCompilationRootValidation", 56 Boolean.toString(disableCrossCompilationRootValidation)); 57 } 58 59 @Test testRootTest()60 public void testRootTest() { 61 Source testRoot = 62 javaSource( 63 "test.TestRoot", 64 "package test;", 65 "", 66 "import dagger.hilt.android.testing.HiltAndroidTest;", 67 "", 68 "@HiltAndroidTest", 69 "public class TestRoot {}"); 70 71 // TODO(danysantiago): Add KSP test once b/288966076 is resolved. 72 compileWithKapt( 73 ImmutableList.of(testRoot), 74 processorOptions(), 75 tempFolderRule, 76 result -> { 77 if (disableCrossCompilationRootValidation) { 78 assertThat(result.getSuccess()).isTrue(); 79 } else { 80 List<DiagnosticMessage> errors = result.getDiagnostics().get(Kind.ERROR); 81 assertThat(errors).hasSize(1); 82 assertThat(errors.get(0).getMsg()) 83 .contains( 84 "Cannot process new roots when there are test roots from a previous " 85 + "compilation unit:\n" 86 + " Test roots from previous compilation unit: " 87 + "dagger.hilt.processor.internal.root.MyTestPreviousCompilation.MyTest\n" 88 + " All roots from this compilation unit: test.TestRoot"); 89 } 90 }); 91 } 92 93 @Test appRootTest()94 public void appRootTest() { 95 Source appRoot = 96 javaSource( 97 "test.AppRoot", 98 "package test;", 99 "", 100 "import android.app.Application;", 101 "import dagger.hilt.android.HiltAndroidApp;", 102 "", 103 "@HiltAndroidApp(Application.class)", 104 "public class AppRoot extends Hilt_AppRoot {}"); 105 106 // TODO(danysantiago): Add KSP test once b/288966076 is resolved. 107 compileWithKapt( 108 ImmutableList.of(appRoot), 109 processorOptions(), 110 tempFolderRule, 111 result -> { 112 if (disableCrossCompilationRootValidation) { 113 assertThat(result.getSuccess()).isTrue(); 114 } else { 115 List<DiagnosticMessage> errors = result.getDiagnostics().get(Kind.ERROR); 116 assertThat(errors).hasSize(1); 117 assertThat(errors.get(0).getMsg()) 118 .contains( 119 "Cannot process new roots when there are test roots from a previous " 120 + "compilation unit:\n" 121 + " Test roots from previous compilation unit: " 122 + "dagger.hilt.processor.internal.root.MyTestPreviousCompilation.MyTest\n" 123 + " All roots from this compilation unit: test.AppRoot"); 124 } 125 }); 126 } 127 } 128