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.android.processor.internal.androidentrypoint; 18 19 import static dagger.hilt.android.testing.compile.HiltCompilerTests.kotlinCompiler; 20 21 import com.google.common.collect.ImmutableList; 22 import com.google.common.collect.ImmutableMap; 23 import com.google.common.truth.Truth; 24 import com.tschuchort.compiletesting.KotlinCompilation; 25 import com.tschuchort.compiletesting.KotlinCompilation.ExitCode; 26 import com.tschuchort.compiletesting.SourceFile; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 @RunWith(JUnit4.class) 32 public class KotlinAndroidEntryPointProcessorTest { 33 @Test checkBaseClassConstructorHasNotDefaultParameters()34 public void checkBaseClassConstructorHasNotDefaultParameters() { 35 SourceFile fragmentSrc = SourceFile.Companion.kotlin("MyFragment.kt", 36 String.join("\n", 37 "package test", 38 "", 39 "import dagger.hilt.android.AndroidEntryPoint", 40 "", 41 "@AndroidEntryPoint", 42 "class MyFragment : BaseFragment()" 43 ), 44 false); 45 SourceFile baseFragmentSrc = SourceFile.Companion.kotlin("BaseFragment.kt", 46 String.join("\n", 47 "package test", 48 "", 49 "import androidx.fragment.app.Fragment", 50 "", 51 "abstract class BaseFragment(layoutId: Int = 0) : Fragment()" 52 ), 53 false); 54 KotlinCompilation compilation = kotlinCompiler(); 55 compilation.setSources(ImmutableList.of(fragmentSrc, baseFragmentSrc)); 56 compilation.setKaptArgs(ImmutableMap.of( 57 "dagger.hilt.android.internal.disableAndroidSuperclassValidation", "true")); 58 KotlinCompilation.Result result = compilation.compile(); 59 Truth.assertThat(result.getExitCode()).isEqualTo(ExitCode.COMPILATION_ERROR); 60 Truth.assertThat(result.getMessages()).contains("The base class, 'test.BaseFragment', of the " 61 + "@AndroidEntryPoint, 'test.MyFragment', contains a constructor with default parameters. " 62 + "This is currently not supported by the Gradle plugin. Either specify the base class as " 63 + "described at https://dagger.dev/hilt/gradle-setup#why-use-the-plugin or remove the " 64 + "default value declaration."); 65 } 66 } 67