• 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 androidx.room.compiler.processing.util.Source;
20 import dagger.hilt.android.testing.compile.HiltCompilerTests;
21 import dagger.testing.golden.GoldenFileRule;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.JUnit4;
26 
27 @RunWith(JUnit4.class)
28 public class ActivityGeneratorTest {
29   @Rule public GoldenFileRule goldenFileRule = new GoldenFileRule();
30 
31   @Test
generate_componentActivity()32   public void generate_componentActivity() {
33     Source myActivity =
34         HiltCompilerTests.javaSource(
35             "test.MyActivity",
36             "package test;",
37             "",
38             "import androidx.activity.ComponentActivity;",
39             "import dagger.hilt.android.AndroidEntryPoint;",
40             "",
41             "@AndroidEntryPoint(ComponentActivity.class)",
42             "public class MyActivity extends Hilt_MyActivity {",
43             "}");
44     HiltCompilerTests.hiltCompiler(myActivity).compile(subject -> subject.hasErrorCount(0));
45   }
46 
47   @Test
generate_baseHiltComponentActivity()48   public void generate_baseHiltComponentActivity() {
49     Source baseActivity =
50         HiltCompilerTests.javaSource(
51             "test.BaseActivity",
52             "package test;",
53             "",
54             "import androidx.activity.ComponentActivity;",
55             "import dagger.hilt.android.AndroidEntryPoint;",
56             "",
57             "@AndroidEntryPoint(ComponentActivity.class)",
58             "public class BaseActivity extends Hilt_BaseActivity {",
59             "}");
60     Source myActivity =
61         HiltCompilerTests.javaSource(
62             "test.MyActivity",
63             "package test;",
64             "",
65             "import androidx.activity.ComponentActivity;",
66             "import dagger.hilt.android.AndroidEntryPoint;",
67             "",
68             "@AndroidEntryPoint(BaseActivity.class)",
69             "public class MyActivity extends Hilt_MyActivity {",
70             "}");
71     HiltCompilerTests.hiltCompiler(baseActivity, myActivity)
72         .compile(subject -> subject.hasErrorCount(0));
73   }
74 
75   @Test
baseActivityHasFinalOnDestroy_fails()76   public void baseActivityHasFinalOnDestroy_fails() {
77     Source myActivity =
78         HiltCompilerTests.javaSource(
79             "test.MyActivity",
80             "package test;",
81             "",
82             "import dagger.hilt.android.AndroidEntryPoint;",
83             "",
84             "@AndroidEntryPoint(BaseActivity.class)",
85             "public class MyActivity extends Hilt_MyActivity {}");
86     Source baseActivity =
87         HiltCompilerTests.javaSource(
88             "test.BaseActivity",
89             "package test;",
90             "",
91             "import androidx.activity.ComponentActivity;",
92             "",
93             "public class BaseActivity extends ComponentActivity {",
94             "   @Override public final void onDestroy() {}",
95             "}");
96     HiltCompilerTests.hiltCompiler(myActivity, baseActivity)
97         .compile(
98             subject -> {
99               // TODO(b/319663779) make error count consistent.
100               // subject.hasErrorCount(1);
101               subject.hasErrorContaining(
102                   "Do not mark onDestroy as final in base Activity class, as Hilt needs to override"
103                       + " it to clean up SavedStateHandle");
104             });
105   }
106 
107   @Test
baseActivityHasFinalOnCreate_fails()108   public void baseActivityHasFinalOnCreate_fails() {
109     Source myActivity =
110         HiltCompilerTests.javaSource(
111             "test.MyActivity",
112             "package test;",
113             "",
114             "import dagger.hilt.android.AndroidEntryPoint;",
115             "",
116             "@AndroidEntryPoint(BaseActivity.class)",
117             "public class MyActivity extends Hilt_MyActivity {}");
118     Source baseActivity =
119         HiltCompilerTests.javaSource(
120             "test.BaseActivity",
121             "package test;",
122             "",
123             "import android.os.Bundle;",
124             "import androidx.activity.ComponentActivity;",
125             "",
126             "public class BaseActivity extends ComponentActivity {",
127             "   @Override public final void onCreate(Bundle bundle) {}",
128             "}");
129     HiltCompilerTests.hiltCompiler(myActivity, baseActivity)
130         .compile(
131             subject -> {
132               // TODO(b/319663779) make error count consistent.
133               // subject.hasErrorCount(1);
134               subject.hasErrorContaining(
135                   "Do not mark onCreate as final in base Activity class, as Hilt needs to override"
136                       + " it to inject SavedStateHandle");
137             });
138   }
139 
140   @Test
secondBaseActivityHasFinalOnCreate_fails()141   public void secondBaseActivityHasFinalOnCreate_fails() {
142     Source myActivity =
143         HiltCompilerTests.javaSource(
144             "test.MyActivity",
145             "package test;",
146             "",
147             "import dagger.hilt.android.AndroidEntryPoint;",
148             "",
149             "@AndroidEntryPoint(BaseActivity.class)",
150             "public class MyActivity extends Hilt_MyActivity {}");
151     Source baseActivity =
152         HiltCompilerTests.javaSource(
153             "test.BaseActivity",
154             "package test;",
155             "",
156             "public class BaseActivity extends BaseActivity2 {}");
157     Source baseActivity2 =
158         HiltCompilerTests.javaSource(
159             "test.BaseActivity2",
160             "package test;",
161             "",
162             "import android.os.Bundle;",
163             "import androidx.activity.ComponentActivity;",
164             "",
165             "public class BaseActivity2 extends ComponentActivity {",
166             "   @Override public final void onCreate(Bundle bundle) {}",
167             "}");
168     HiltCompilerTests.hiltCompiler(myActivity, baseActivity, baseActivity2)
169         .compile(
170             subject -> {
171               // TODO(b/319663779) make error count consistent.
172               // subject.hasErrorCount(1);
173               subject.hasErrorContaining(
174                   "Do not mark onCreate as final in base Activity class, as Hilt needs to override"
175                       + " it to inject SavedStateHandle");
176             });
177   }
178 }
179