• 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;
18 
19 import static com.google.testing.compile.CompilationSubject.assertThat;
20 import static dagger.hilt.android.processor.AndroidCompilers.compiler;
21 
22 import com.google.testing.compile.Compilation;
23 import com.google.testing.compile.JavaFileObjects;
24 import javax.tools.JavaFileObject;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 @RunWith(JUnit4.class)
30 public final class GeneratorsTest {
31 
32   @Test
copyConstructorParametersCopiesExternalNullables()33   public void copyConstructorParametersCopiesExternalNullables() {
34     JavaFileObject baseActivity =
35         JavaFileObjects.forSourceLines(
36             "test.BaseActivity",
37             "package test;",
38             "",
39             "import androidx.fragment.app.FragmentActivity;",
40             "",
41             "public abstract class BaseActivity extends FragmentActivity {",
42             "  protected BaseActivity(",
43             "      @androidx.annotation.Nullable String supportNullable,",
44             "      @androidx.annotation.Nullable String androidxNullable,",
45             "      @javax.annotation.Nullable String javaxNullable) { }",
46             "}");
47     JavaFileObject myActivity =
48         JavaFileObjects.forSourceLines(
49             "test.MyActivity",
50             "package test;",
51             "",
52             "import dagger.hilt.android.AndroidEntryPoint;",
53             "",
54             "@AndroidEntryPoint(BaseActivity.class)",
55             "public class MyActivity extends Hilt_MyActivity {",
56             "  public MyActivity(",
57             "      String supportNullable,",
58             "      String androidxNullable,",
59             "      String javaxNullable) {",
60             "    super(supportNullable, androidxNullable, javaxNullable);",
61             "  }",
62             "}");
63     Compilation compilation = compiler().compile(baseActivity, myActivity);
64     assertThat(compilation).succeeded();
65     assertThat(compilation)
66         .generatedSourceFile("test/Hilt_MyActivity")
67         .containsElementsIn(
68             JavaFileObjects.forSourceLines(
69                 "test.Hilt_MyActivity",
70                 "package test;",
71                 "",
72                 "import androidx.annotation.Nullable;",
73                 "",
74                 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ActivityGenerator\")",
75                 "abstract class Hilt_MyActivity extends BaseActivity implements",
76                 "    GeneratedComponentManagerHolder {",
77                 "  Hilt_MyActivity(",
78                 "      @Nullable String supportNullable,",
79                 "      @Nullable String androidxNullable,",
80                 "      @javax.annotation.Nullable String javaxNullable) {",
81                 "    super(supportNullable, androidxNullable, javaxNullable);",
82                 "    _initHiltInternal();",
83                 "  }",
84                 "}"));
85   }
86 
87   @Test
copyConstructorParametersConvertsAndroidInternalNullableToExternal()88   public void copyConstructorParametersConvertsAndroidInternalNullableToExternal() {
89     // Relies on View(Context, AttributeSet), which has android-internal
90     // @android.annotation.Nullable on AttributeSet.
91     JavaFileObject myView =
92         JavaFileObjects.forSourceLines(
93             "test.MyView",
94             "package test;",
95             "",
96             "import android.content.Context;",
97             "import android.util.AttributeSet;",
98             "import android.view.View;",
99             "import dagger.hilt.android.AndroidEntryPoint;",
100             "",
101             "@AndroidEntryPoint(View.class)",
102             "public class MyView extends Hilt_MyView {",
103             "  public MyView(Context context, AttributeSet attrs) {",
104             "    super(context, attrs);",
105             "  }",
106             "}");
107     Compilation compilation = compiler().compile(myView);
108     assertThat(compilation).succeeded();
109     assertThat(compilation)
110         .generatedSourceFile("test/Hilt_MyView")
111         .containsElementsIn(
112             JavaFileObjects.forSourceLines(
113                 "test.Hilt_MyView",
114                 "package test;",
115                 "",
116                 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ViewGenerator\")",
117                 "abstract class Hilt_MyView extends View implements",
118                 "GeneratedComponentManagerHolder {",
119                 // The generated parameter names are copied from the base class. Since we only have
120                 // the jar and not the source for these base classes the parameter names are missing
121                 "  Hilt_MyView(Context arg0, @Nullable AttributeSet arg1) {",
122                 "    super(arg0, arg1);",
123                 "    inject();",
124                 "  }",
125                 "}"));
126   }
127 
128   @Test
copyTargetApiAnnotationActivity()129   public void copyTargetApiAnnotationActivity() {
130     JavaFileObject myActivity =
131         JavaFileObjects.forSourceLines(
132             "test.MyActivity",
133             "package test;",
134             "",
135             "import android.annotation.TargetApi;",
136             "import androidx.fragment.app.FragmentActivity;",
137             "import dagger.hilt.android.AndroidEntryPoint;",
138             "",
139             "@TargetApi(24)",
140             "@AndroidEntryPoint(FragmentActivity.class)",
141             "public class MyActivity extends Hilt_MyActivity {}");
142     Compilation compilation = compiler().compile(myActivity);
143     assertThat(compilation).succeeded();
144     assertThat(compilation)
145         .generatedSourceFile("test/Hilt_MyActivity")
146         .containsElementsIn(
147             JavaFileObjects.forSourceLines(
148                 "test.Hilt_MyActivity",
149                 " package test;",
150                 "",
151                 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ActivityGenerator\")",
152                 "@TargetApi(24)",
153                 "abstract class Hilt_MyActivity extends FragmentActivity ",
154                 "implements GeneratedComponentManagerHolder {",
155                 "}"));
156   }
157 
158   @Test
copyTargetApiAnnotationOverView()159   public void copyTargetApiAnnotationOverView() {
160     JavaFileObject myView =
161         JavaFileObjects.forSourceLines(
162             "test.MyView",
163             "package test;",
164             "",
165             "import android.annotation.TargetApi;",
166             "import android.widget.LinearLayout;",
167             "import android.content.Context;",
168             "import android.util.AttributeSet;",
169             "import dagger.hilt.android.AndroidEntryPoint;",
170             "",
171             "@TargetApi(24)",
172             "@AndroidEntryPoint(LinearLayout.class)",
173             "public class MyView extends Hilt_MyView {",
174             " public MyView(Context context, AttributeSet attributeSet){",
175             "   super(context, attributeSet);",
176             " }",
177             "",
178             "}");
179     Compilation compilation = compiler().compile(myView);
180     assertThat(compilation).succeeded();
181     assertThat(compilation)
182         .generatedSourceFile("test/Hilt_MyView")
183         .containsElementsIn(
184             JavaFileObjects.forSourceLines(
185                 "test.Hilt_MyView",
186                 "",
187                 "package test;",
188                 "",
189                 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ViewGenerator\")",
190                 "@TargetApi(24)",
191                 "abstract class Hilt_MyView extends LinearLayout implements"
192                     + " GeneratedComponentManagerHolder {",
193                 "}"));
194   }
195 
196   @Test
copyTargetApiAnnotationApplication()197   public void copyTargetApiAnnotationApplication() {
198     JavaFileObject myApplication =
199         JavaFileObjects.forSourceLines(
200             "test.MyApplication",
201             "package test;",
202             "",
203             "import android.annotation.TargetApi;",
204             "import android.app.Application;",
205             "import dagger.hilt.android.HiltAndroidApp;",
206             "",
207             "@TargetApi(24)",
208             "@HiltAndroidApp(Application.class)",
209             "public class MyApplication extends Hilt_MyApplication {}");
210     Compilation compilation = compiler().compile(myApplication);
211     assertThat(compilation).succeeded();
212     assertThat(compilation)
213         .generatedSourceFile("test/Hilt_MyApplication")
214         .containsElementsIn(
215             JavaFileObjects.forSourceLines(
216                 "test.Hilt_MyApplication",
217                 " package test;",
218                 "",
219                 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ApplicationGenerator\")",
220                 "@TargetApi(24)",
221                 "abstract class Hilt_MyApplication extends Application implements"
222                     + " GeneratedComponentManagerHolder {}"));
223   }
224 
225   @Test
copyTargetApiAnnotationFragment()226   public void copyTargetApiAnnotationFragment() {
227     JavaFileObject myApplication =
228         JavaFileObjects.forSourceLines(
229             "test.MyFragment",
230             "package test;",
231             "",
232             "import android.annotation.TargetApi;",
233             "import androidx.fragment.app.Fragment;",
234             "import dagger.hilt.android.AndroidEntryPoint;",
235             "",
236             "@TargetApi(24)",
237             "@AndroidEntryPoint(Fragment.class)",
238             "public class MyFragment extends Hilt_MyFragment {}");
239     Compilation compilation = compiler().compile(myApplication);
240     assertThat(compilation).succeeded();
241     assertThat(compilation)
242         .generatedSourceFile("test/Hilt_MyFragment")
243         .containsElementsIn(
244             JavaFileObjects.forSourceLines(
245                 "test.Hilt_MyFragment",
246                 "package test;",
247                 "",
248                 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.FragmentGenerator\")",
249                 "@TargetApi(24)",
250                 "@SuppressWarnings(\"deprecation\")",
251                 "abstract class Hilt_MyFragment extends Fragment implements"
252                     + " GeneratedComponentManagerHolder {}"));
253   }
254 
255   @Test
copyTargetApiBroadcastRecieverGenerator()256   public void copyTargetApiBroadcastRecieverGenerator() {
257     JavaFileObject myBroadcastReceiver =
258         JavaFileObjects.forSourceLines(
259             "test.MyBroadcastReceiver",
260             "package test;",
261             "",
262             "import android.content.BroadcastReceiver;",
263             "import android.annotation.TargetApi;",
264             "import dagger.hilt.android.AndroidEntryPoint;",
265             "",
266             "@TargetApi(24)",
267             "@AndroidEntryPoint(BroadcastReceiver.class)",
268             "public class MyBroadcastReceiver extends Hilt_MyBroadcastReceiver {}");
269     Compilation compilation = compiler().compile(myBroadcastReceiver);
270     assertThat(compilation).succeeded();
271     assertThat(compilation)
272         .generatedSourceFile("test/Hilt_MyBroadcastReceiver")
273         .containsElementsIn(
274             JavaFileObjects.forSourceLines(
275                 "test.Hilt_MyBroadcastReceiver",
276                 "package test;",
277                 "",
278                 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.BroadcastReceiverGenerator\")",
279                 "@TargetApi(24)",
280                 "abstract class Hilt_MyBroadcastReceiver extends BroadcastReceiver {}"));
281   }
282 
283   @Test
copyTargetApiServiceGenerator()284   public void copyTargetApiServiceGenerator() {
285     JavaFileObject myService =
286         JavaFileObjects.forSourceLines(
287             "test.MyService",
288             "package test;",
289             "",
290             "import android.annotation.TargetApi;",
291             "import android.content.Intent;",
292             "import android.app.Service;",
293             "import android.os.IBinder;",
294             "import dagger.hilt.android.AndroidEntryPoint;",
295             "",
296             "@TargetApi(24)",
297             "@AndroidEntryPoint(Service.class)",
298             "public class MyService extends Hilt_MyService {",
299             "   @Override",
300             "   public IBinder onBind(Intent intent){",
301             "     return null;",
302             "   }",
303             "}");
304     Compilation compilation = compiler().compile(myService);
305     assertThat(compilation).succeeded();
306     assertThat(compilation)
307         .generatedSourceFile("test/Hilt_MyService")
308         .containsElementsIn(
309             JavaFileObjects.forSourceLines(
310                 "test.Hilt_MyService",
311                 "package test;",
312                 "",
313                 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ServiceGenerator\")",
314                 "@TargetApi(24)",
315                 "abstract class Hilt_MyService extends Service implements"
316                     + " GeneratedComponentManagerHolder{}"));
317   }
318 }
319