• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.common.truth.Truth.assertThat;
20 import static dagger.hilt.android.testing.compile.HiltCompilerTests.compileWithKapt;
21 
22 import androidx.room.compiler.processing.util.DiagnosticMessage;
23 import androidx.room.compiler.processing.util.Source;
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ImmutableMap;
26 import java.util.List;
27 import javax.tools.Diagnostic.Kind;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.TemporaryFolder;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 @RunWith(JUnit4.class)
35 public class KotlinAndroidEntryPointProcessorTest {
36 
37   @Rule
38   public TemporaryFolder tempFolderRule = new TemporaryFolder();
39 
40   @Test
checkBaseClassConstructorHasNotDefaultParameters()41   public void checkBaseClassConstructorHasNotDefaultParameters() {
42     Source fragmentSrc = Source.Companion.kotlin("MyFragment.kt",
43         String.join("\n",
44             "package test",
45             "",
46             "import dagger.hilt.android.AndroidEntryPoint",
47             "",
48             "@AndroidEntryPoint",
49             "class MyFragment : BaseFragment()"
50         ));
51     Source baseFragmentSrc = Source.Companion.kotlin("BaseFragment.kt",
52         String.join("\n",
53             "package test",
54             "",
55             "import androidx.fragment.app.Fragment",
56             "",
57             "abstract class BaseFragment(layoutId: Int = 0) : Fragment()"
58         ));
59     compileWithKapt(
60         ImmutableList.of(fragmentSrc, baseFragmentSrc),
61         ImmutableMap.of(
62             "dagger.hilt.android.internal.disableAndroidSuperclassValidation", "true"),
63         tempFolderRule,
64         result -> {
65           assertThat(result.getSuccess()).isFalse();
66           List<DiagnosticMessage> errors = result.getDiagnostics().get(Kind.ERROR);
67           assertThat(errors).hasSize(1);
68           assertThat(errors.get(0).getMsg())
69               .contains("The base class, 'test.BaseFragment', of the "
70                   + "@AndroidEntryPoint, 'test.MyFragment', contains a constructor with default "
71                   + "parameters. This is currently not supported by the Gradle plugin.");
72         });
73   }
74 }
75