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 androidx.room.compiler.processing.XProcessingEnv.Backend; 20 import androidx.room.compiler.processing.util.Source; 21 import com.google.common.base.Joiner; 22 import com.google.common.truth.StringSubject; 23 import dagger.hilt.android.testing.compile.HiltCompilerTests; 24 import org.junit.Test; 25 import org.junit.runner.RunWith; 26 import org.junit.runners.JUnit4; 27 28 @RunWith(JUnit4.class) 29 public final class GeneratorsTest { 30 private static final Joiner JOINER = Joiner.on("\n"); 31 32 @Test copyConstructorParametersCopiesExternalNullables()33 public void copyConstructorParametersCopiesExternalNullables() { 34 Source baseActivity = 35 HiltCompilerTests.javaSource( 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 Source myActivity = 48 HiltCompilerTests.javaSource( 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 HiltCompilerTests.hiltCompiler(baseActivity, myActivity) 64 .compile( 65 subject -> { 66 subject.hasErrorCount(0); 67 StringSubject stringSubject = 68 subject.generatedSourceFileWithPath("test/Hilt_MyActivity.java"); 69 stringSubject.contains("package test;"); 70 stringSubject.contains("import androidx.annotation.Nullable;"); 71 stringSubject.contains( 72 JOINER.join( 73 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ActivityGenerator\")", 74 "abstract class Hilt_MyActivity extends BaseActivity implements " 75 + "GeneratedComponentManagerHolder {")); 76 stringSubject.contains( 77 JOINER.join( 78 " Hilt_MyActivity(@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 Source myView = 92 HiltCompilerTests.javaSource( 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 HiltCompilerTests.hiltCompiler(myView) 108 .compile( 109 subject -> { 110 subject.hasErrorCount(0); 111 StringSubject stringSubject = 112 subject.generatedSourceFileWithPath("test/Hilt_MyView.java"); 113 stringSubject.contains("import androidx.annotation.Nullable;"); 114 stringSubject.contains( 115 JOINER.join( 116 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ViewGenerator\")", 117 "abstract class Hilt_MyView extends View implements" 118 + " GeneratedComponentManagerHolder {")); 119 // TODO(kuanyingchou): Remove the condition once 120 // https://github.com/google/ksp/issues/1459 is fixed 121 if (HiltCompilerTests.backend(subject) == Backend.KSP) { 122 stringSubject.contains( 123 JOINER.join( 124 " Hilt_MyView(Context p0, @Nullable AttributeSet p1) {", 125 " super(p0, p1);", 126 " if(!isInEditMode()) {", 127 " inject();", 128 " }", 129 " }")); 130 } else { 131 stringSubject.contains( 132 JOINER.join( 133 " Hilt_MyView(Context context, @Nullable AttributeSet attrs) {", 134 " super(context, attrs);", 135 " if(!isInEditMode()) {", 136 " inject();", 137 " }", 138 " }")); 139 } 140 }); 141 } 142 143 // This is a regression test for b/382104423 144 @Test typeUseNullableCopiedFromSuperConstructor()145 public void typeUseNullableCopiedFromSuperConstructor() { 146 Source baseView = 147 HiltCompilerTests.javaSource( 148 "test.BaseView", 149 "package test;", 150 "", 151 "import android.content.Context;", 152 "import android.util.AttributeSet;", 153 "import android.view.View;", 154 "import org.jspecify.annotations.Nullable;", 155 "", 156 "public class BaseView extends View {", 157 " public BaseView(Context context, @Nullable AttributeSet attrs) {", 158 " super(context, attrs);", 159 " }", 160 "}"); 161 Source myView = 162 HiltCompilerTests.javaSource( 163 "test.MyView", 164 "package test;", 165 "", 166 "import android.content.Context;", 167 "import android.util.AttributeSet;", 168 "import android.view.View;", 169 "import dagger.hilt.android.AndroidEntryPoint;", 170 "import org.jspecify.annotations.Nullable;", 171 "", 172 "@AndroidEntryPoint(BaseView.class)", 173 "public class MyView extends Hilt_MyView {", 174 " public MyView(Context context, @Nullable AttributeSet attrs) {", 175 " super(context, attrs);", 176 " }", 177 "}"); 178 HiltCompilerTests.hiltCompiler(baseView, myView) 179 .compile( 180 subject -> { 181 subject.hasErrorCount(0); 182 StringSubject stringSubject = 183 subject.generatedSourceFileWithPath("test/Hilt_MyView.java"); 184 stringSubject.contains("org.jspecify.annotations.Nullable"); 185 }); 186 } 187 188 @Test hybridTypeUseAndDeclarationNullableNotDuplicated()189 public void hybridTypeUseAndDeclarationNullableNotDuplicated() { 190 Source hybridNullable = 191 HiltCompilerTests.javaSource( 192 "test.Nullable", 193 "package test;", 194 "", 195 "import static java.lang.annotation.ElementType.PARAMETER;", 196 "import static java.lang.annotation.ElementType.TYPE_USE;", 197 "", 198 "import java.lang.annotation.Target;", 199 "", 200 "@Target({TYPE_USE, PARAMETER})", 201 "public @interface Nullable {}"); 202 Source baseView = 203 HiltCompilerTests.javaSource( 204 "test.BaseView", 205 "package test;", 206 "", 207 "import android.content.Context;", 208 "import android.util.AttributeSet;", 209 "import android.view.View;", 210 "", 211 "public class BaseView extends View {", 212 " public BaseView(Context context, @Nullable AttributeSet attrs) {", 213 " super(context, attrs);", 214 " }", 215 "}"); 216 Source myView = 217 HiltCompilerTests.javaSource( 218 "test.MyView", 219 "package test;", 220 "", 221 "import android.content.Context;", 222 "import android.util.AttributeSet;", 223 "import android.view.View;", 224 "import dagger.hilt.android.AndroidEntryPoint;", 225 "", 226 "@AndroidEntryPoint(BaseView.class)", 227 "public class MyView extends Hilt_MyView {", 228 " public MyView(Context context, @Nullable AttributeSet attrs) {", 229 " super(context, attrs);", 230 " }", 231 "}"); 232 HiltCompilerTests.hiltCompiler(hybridNullable, baseView, myView) 233 .compile( 234 subject -> { 235 subject.hasErrorCount(0); 236 StringSubject stringSubject = 237 subject.generatedSourceFileWithPath("test/Hilt_MyView.java"); 238 stringSubject.contains("@Nullable"); 239 }); 240 } 241 242 // This is a regression test for https://github.com/google/dagger/issues/3296 243 @Test isRestrictedApiConstructorWithPrimitiveParameterTest()244 public void isRestrictedApiConstructorWithPrimitiveParameterTest() { 245 Source baseView = 246 HiltCompilerTests.javaSource( 247 "test.BaseView", 248 "package test;", 249 "", 250 "import android.content.Context;", 251 "import android.util.AttributeSet;", 252 "import android.view.View;", 253 "", 254 "public class BaseView extends View {", 255 " public BaseView(int i, int j, Context context, AttributeSet attrs) {", 256 " super(context, attrs);", 257 " }", 258 "}"); 259 Source myView = 260 HiltCompilerTests.javaSource( 261 "test.MyView", 262 "package test;", 263 "", 264 "import android.content.Context;", 265 "import android.util.AttributeSet;", 266 "import android.view.View;", 267 "import dagger.hilt.android.AndroidEntryPoint;", 268 "", 269 "@AndroidEntryPoint(BaseView.class)", 270 "public class MyView extends Hilt_MyView {", 271 " public MyView(int i, int j, Context context, AttributeSet attrs) {", 272 " super(i, j, context, attrs);", 273 " }", 274 "}"); 275 HiltCompilerTests.hiltCompiler(baseView, myView).compile(subject -> subject.hasErrorCount(0)); 276 } 277 278 // This is a regression test for https://github.com/google/dagger/issues/3296 279 @Test isRestrictedApiConstructorWithArrayParameterTest()280 public void isRestrictedApiConstructorWithArrayParameterTest() { 281 Source baseView = 282 HiltCompilerTests.javaSource( 283 "test.BaseView", 284 "package test;", 285 "", 286 "import android.content.Context;", 287 "import android.util.AttributeSet;", 288 "import android.view.View;", 289 "", 290 "public class BaseView extends View {", 291 " public BaseView(String[] strs, int i, Context context, AttributeSet attrs) {", 292 " super(context, attrs);", 293 " }", 294 "}"); 295 Source myView = 296 HiltCompilerTests.javaSource( 297 "test.MyView", 298 "package test;", 299 "", 300 "import android.content.Context;", 301 "import android.util.AttributeSet;", 302 "import android.view.View;", 303 "import dagger.hilt.android.AndroidEntryPoint;", 304 "", 305 "@AndroidEntryPoint(BaseView.class)", 306 "public class MyView extends Hilt_MyView {", 307 " public MyView(String[] strs, int i, Context context, AttributeSet attrs) {", 308 " super(strs, i, context, attrs);", 309 " }", 310 "}"); 311 HiltCompilerTests.hiltCompiler(baseView, myView).compile(subject -> subject.hasErrorCount(0)); 312 } 313 314 // This is a regression test for https://github.com/google/dagger/issues/3296 315 @Test isRestrictedApiConstructorWithTypeParameterTest()316 public void isRestrictedApiConstructorWithTypeParameterTest() { 317 Source baseView = 318 HiltCompilerTests.javaSource( 319 "test.BaseView", 320 "package test;", 321 "", 322 "import android.content.Context;", 323 "import android.util.AttributeSet;", 324 "import android.view.View;", 325 "", 326 "public class BaseView<T> extends View {", 327 " public BaseView(T t, int i, Context context, AttributeSet attrs) {", 328 " super(context, attrs);", 329 " }", 330 "}"); 331 Source myView = 332 HiltCompilerTests.javaSource( 333 "test.MyView", 334 "package test;", 335 "", 336 "import android.content.Context;", 337 "import android.util.AttributeSet;", 338 "import android.view.View;", 339 "import dagger.hilt.android.AndroidEntryPoint;", 340 "", 341 "@AndroidEntryPoint(BaseView.class)", 342 "public class MyView extends Hilt_MyView<String> {", 343 " public MyView(String str, int i, Context context, AttributeSet attrs) {", 344 " super(str, i, context, attrs);", 345 " }", 346 "}"); 347 HiltCompilerTests.hiltCompiler(baseView, myView).compile(subject -> subject.hasErrorCount(0)); 348 } 349 350 @Test copyTargetApiAnnotationActivity()351 public void copyTargetApiAnnotationActivity() { 352 Source myActivity = 353 HiltCompilerTests.javaSource( 354 "test.MyActivity", 355 "package test;", 356 "", 357 "import android.annotation.TargetApi;", 358 "import androidx.fragment.app.FragmentActivity;", 359 "import dagger.hilt.android.AndroidEntryPoint;", 360 "", 361 "@TargetApi(24)", 362 "@AndroidEntryPoint(FragmentActivity.class)", 363 "public class MyActivity extends Hilt_MyActivity {}"); 364 HiltCompilerTests.hiltCompiler(myActivity) 365 .compile( 366 subject -> { 367 subject.hasErrorCount(0); 368 StringSubject stringSubject = 369 subject.generatedSourceFileWithPath("test/Hilt_MyActivity.java"); 370 stringSubject.contains( 371 JOINER.join( 372 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ActivityGenerator\")", 373 "@TargetApi(24)", 374 "abstract class Hilt_MyActivity extends FragmentActivity" 375 + " implements GeneratedComponentManagerHolder {")); 376 }); 377 } 378 379 @Test copyTargetApiAnnotationOverView()380 public void copyTargetApiAnnotationOverView() { 381 Source myView = 382 HiltCompilerTests.javaSource( 383 "test.MyView", 384 "package test;", 385 "", 386 "import android.annotation.TargetApi;", 387 "import android.widget.LinearLayout;", 388 "import android.content.Context;", 389 "import android.util.AttributeSet;", 390 "import dagger.hilt.android.AndroidEntryPoint;", 391 "", 392 "@TargetApi(24)", 393 "@AndroidEntryPoint(LinearLayout.class)", 394 "public class MyView extends Hilt_MyView {", 395 " public MyView(Context context, AttributeSet attributeSet){", 396 " super(context, attributeSet);", 397 " }", 398 "}"); 399 HiltCompilerTests.hiltCompiler(myView) 400 .compile( 401 subject -> { 402 subject.hasErrorCount(0); 403 StringSubject stringSubject = 404 subject.generatedSourceFileWithPath("test/Hilt_MyView.java"); 405 stringSubject.contains("package test;"); 406 stringSubject.contains( 407 JOINER.join( 408 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ViewGenerator\")", 409 "@TargetApi(24)", 410 "abstract class Hilt_MyView extends LinearLayout implements" 411 + " GeneratedComponentManagerHolder {")); 412 }); 413 } 414 415 @Test copyTargetApiAnnotationApplication()416 public void copyTargetApiAnnotationApplication() { 417 Source myApplication = 418 HiltCompilerTests.javaSource( 419 "test.MyApplication", 420 "package test;", 421 "", 422 "import android.annotation.TargetApi;", 423 "import android.app.Application;", 424 "import dagger.hilt.android.HiltAndroidApp;", 425 "", 426 "@TargetApi(24)", 427 "@HiltAndroidApp(Application.class)", 428 "public class MyApplication extends Hilt_MyApplication {}"); 429 HiltCompilerTests.hiltCompiler(myApplication) 430 .compile( 431 subject -> { 432 subject.hasErrorCount(0); 433 StringSubject stringSubject = 434 subject.generatedSourceFileWithPath("test/Hilt_MyApplication.java"); 435 stringSubject.contains("package test;"); 436 stringSubject.contains( 437 JOINER.join( 438 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint." 439 + "ApplicationGenerator\")", 440 "@TargetApi(24)", 441 "abstract class Hilt_MyApplication extends Application implements" 442 + " GeneratedComponentManagerHolder {")); 443 }); 444 } 445 446 @Test copyTargetApiAnnotationFragment()447 public void copyTargetApiAnnotationFragment() { 448 Source myApplication = 449 HiltCompilerTests.javaSource( 450 "test.MyFragment", 451 "package test;", 452 "", 453 "import android.annotation.TargetApi;", 454 "import androidx.fragment.app.Fragment;", 455 "import dagger.hilt.android.AndroidEntryPoint;", 456 "", 457 "@TargetApi(24)", 458 "@AndroidEntryPoint(Fragment.class)", 459 "public class MyFragment extends Hilt_MyFragment {}"); 460 HiltCompilerTests.hiltCompiler(myApplication) 461 .compile( 462 subject -> { 463 subject.hasErrorCount(0); 464 StringSubject stringSubject = 465 subject.generatedSourceFileWithPath("test/Hilt_MyFragment.java"); 466 stringSubject.contains("package test;"); 467 stringSubject.contains( 468 JOINER.join( 469 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.FragmentGenerator\")", 470 "@TargetApi(24)", 471 "abstract class Hilt_MyFragment extends Fragment implements" 472 + " GeneratedComponentManagerHolder {")); 473 }); 474 } 475 476 @Test copyTargetApiBroadcastRecieverGenerator()477 public void copyTargetApiBroadcastRecieverGenerator() { 478 Source myBroadcastReceiver = 479 HiltCompilerTests.javaSource( 480 "test.MyBroadcastReceiver", 481 "package test;", 482 "", 483 "import android.content.BroadcastReceiver;", 484 "import android.annotation.TargetApi;", 485 "import dagger.hilt.android.AndroidEntryPoint;", 486 "", 487 "@TargetApi(24)", 488 "@AndroidEntryPoint(BroadcastReceiver.class)", 489 "public class MyBroadcastReceiver extends Hilt_MyBroadcastReceiver {}"); 490 HiltCompilerTests.hiltCompiler(myBroadcastReceiver) 491 .compile( 492 subject -> { 493 subject.hasErrorCount(0); 494 StringSubject stringSubject = 495 subject.generatedSourceFileWithPath("test/Hilt_MyBroadcastReceiver.java"); 496 stringSubject.contains("package test;"); 497 stringSubject.contains( 498 JOINER.join( 499 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.BroadcastReceiverGenerator\")", 500 "@TargetApi(24)", 501 "abstract class Hilt_MyBroadcastReceiver extends BroadcastReceiver {")); 502 }); 503 } 504 505 @Test copyTargetApiServiceGenerator()506 public void copyTargetApiServiceGenerator() { 507 Source myService = 508 HiltCompilerTests.javaSource( 509 "test.MyService", 510 "package test;", 511 "", 512 "import android.annotation.TargetApi;", 513 "import android.content.Intent;", 514 "import android.app.Service;", 515 "import android.os.IBinder;", 516 "import dagger.hilt.android.AndroidEntryPoint;", 517 "", 518 "@TargetApi(24)", 519 "@AndroidEntryPoint(Service.class)", 520 "public class MyService extends Hilt_MyService {", 521 " @Override", 522 " public IBinder onBind(Intent intent){", 523 " return null;", 524 " }", 525 "}"); 526 HiltCompilerTests.hiltCompiler(myService) 527 .compile( 528 subject -> { 529 subject.hasErrorCount(0); 530 StringSubject stringSubject = 531 subject.generatedSourceFileWithPath("test/Hilt_MyService.java"); 532 stringSubject.contains("package test;"); 533 stringSubject.contains( 534 JOINER.join( 535 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ServiceGenerator\")", 536 "@TargetApi(24)", 537 "abstract class Hilt_MyService extends Service implements" 538 + " GeneratedComponentManagerHolder {")); 539 }); 540 } 541 542 @Test copySuppressWarningsAnnotationActivity_annotationCopied()543 public void copySuppressWarningsAnnotationActivity_annotationCopied() { 544 Source myActivity = 545 HiltCompilerTests.javaSource( 546 "test.MyActivity", 547 "package test;", 548 "", 549 "import android.annotation.TargetApi;", 550 "import androidx.fragment.app.FragmentActivity;", 551 "import dagger.hilt.android.AndroidEntryPoint;", 552 "", 553 "@SuppressWarnings(\"deprecation\")", 554 "@AndroidEntryPoint(FragmentActivity.class)", 555 "public class MyActivity extends Hilt_MyActivity {}"); 556 HiltCompilerTests.hiltCompiler(myActivity) 557 .compile( 558 subject -> { 559 subject.hasErrorCount(0); 560 StringSubject stringSubject = 561 subject.generatedSourceFileWithPath("test/Hilt_MyActivity.java"); 562 stringSubject.contains("package test;"); 563 stringSubject.contains( 564 JOINER.join( 565 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ActivityGenerator\")", 566 "@SuppressWarnings(\"deprecation\")", 567 "abstract class Hilt_MyActivity extends FragmentActivity " 568 + "implements GeneratedComponentManagerHolder {")); 569 }); 570 } 571 572 @Test copySuppressWarningsAnnotation_onView_annotationCopied()573 public void copySuppressWarningsAnnotation_onView_annotationCopied() { 574 Source myView = 575 HiltCompilerTests.javaSource( 576 "test.MyView", 577 "package test;", 578 "", 579 "import android.annotation.TargetApi;", 580 "import android.widget.LinearLayout;", 581 "import android.content.Context;", 582 "import android.util.AttributeSet;", 583 "import dagger.hilt.android.AndroidEntryPoint;", 584 "", 585 "@SuppressWarnings(\"deprecation\")", 586 "@AndroidEntryPoint(LinearLayout.class)", 587 "public class MyView extends Hilt_MyView {", 588 " public MyView(Context context, AttributeSet attributeSet){", 589 " super(context, attributeSet);", 590 " }", 591 "}"); 592 HiltCompilerTests.hiltCompiler(myView) 593 .compile( 594 subject -> { 595 subject.hasErrorCount(0); 596 StringSubject stringSubject = 597 subject.generatedSourceFileWithPath("test/Hilt_MyView.java"); 598 stringSubject.contains("package test;"); 599 stringSubject.contains( 600 JOINER.join( 601 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ViewGenerator\")", 602 "@SuppressWarnings(\"deprecation\")", 603 "abstract class Hilt_MyView extends LinearLayout implements" 604 + " GeneratedComponentManagerHolder {")); 605 }); 606 } 607 608 @Test copySuppressWarningsAnnotation_onApplication_annotationCopied()609 public void copySuppressWarningsAnnotation_onApplication_annotationCopied() { 610 Source myApplication = 611 HiltCompilerTests.javaSource( 612 "test.MyApplication", 613 "package test;", 614 "", 615 "import android.annotation.TargetApi;", 616 "import android.app.Application;", 617 "import dagger.hilt.android.HiltAndroidApp;", 618 "", 619 "@SuppressWarnings(\"deprecation\")", 620 "@HiltAndroidApp(Application.class)", 621 "public class MyApplication extends Hilt_MyApplication {}"); 622 HiltCompilerTests.hiltCompiler(myApplication) 623 .compile( 624 subject -> { 625 subject.hasErrorCount(0); 626 StringSubject stringSubject = 627 subject.generatedSourceFileWithPath("test/Hilt_MyApplication.java"); 628 stringSubject.contains("package test;"); 629 stringSubject.contains( 630 JOINER.join( 631 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.ApplicationGenerator\")", 632 "@SuppressWarnings(\"deprecation\")", 633 "abstract class Hilt_MyApplication extends Application implements" 634 + " GeneratedComponentManagerHolder {")); 635 }); 636 } 637 638 @Test copySuppressWarningsAnnotation_onFragment_annotationCopied()639 public void copySuppressWarningsAnnotation_onFragment_annotationCopied() { 640 Source myApplication = 641 HiltCompilerTests.javaSource( 642 "test.MyFragment", 643 "package test;", 644 "", 645 "import android.annotation.TargetApi;", 646 "import androidx.fragment.app.Fragment;", 647 "import dagger.hilt.android.AndroidEntryPoint;", 648 "", 649 "@SuppressWarnings(\"rawtypes\")", 650 "@AndroidEntryPoint(Fragment.class)", 651 "public class MyFragment extends Hilt_MyFragment {}"); 652 HiltCompilerTests.hiltCompiler(myApplication) 653 .compile( 654 subject -> { 655 subject.hasErrorCount(0); 656 StringSubject stringSubject = 657 subject.generatedSourceFileWithPath("test/Hilt_MyFragment.java"); 658 stringSubject.contains("package test;"); 659 stringSubject.contains( 660 JOINER.join( 661 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint.FragmentGenerator\")", 662 "@SuppressWarnings(\"rawtypes\")", 663 "abstract class Hilt_MyFragment extends Fragment implements" 664 + " GeneratedComponentManagerHolder {")); 665 }); 666 } 667 668 @Test copySuppressWarnings_onBroadcastRecieverGenerator_annotationCopied()669 public void copySuppressWarnings_onBroadcastRecieverGenerator_annotationCopied() { 670 Source myBroadcastReceiver = 671 HiltCompilerTests.javaSource( 672 "test.MyBroadcastReceiver", 673 "package test;", 674 "", 675 "import android.content.BroadcastReceiver;", 676 "import android.annotation.TargetApi;", 677 "import dagger.hilt.android.AndroidEntryPoint;", 678 "", 679 "@SuppressWarnings(\"deprecation\")", 680 "@AndroidEntryPoint(BroadcastReceiver.class)", 681 "public class MyBroadcastReceiver extends Hilt_MyBroadcastReceiver {}"); 682 HiltCompilerTests.hiltCompiler(myBroadcastReceiver) 683 .compile( 684 subject -> { 685 subject.hasErrorCount(0); 686 StringSubject stringSubject = 687 subject.generatedSourceFileWithPath("test/Hilt_MyBroadcastReceiver.java"); 688 stringSubject.contains("package test;"); 689 stringSubject.contains( 690 JOINER.join( 691 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint." 692 + "BroadcastReceiverGenerator\")", 693 "@SuppressWarnings(\"deprecation\")", 694 "abstract class Hilt_MyBroadcastReceiver extends BroadcastReceiver {")); 695 }); 696 } 697 698 @Test copySuppressWarnings_onServiceGenerator_annotationCopied()699 public void copySuppressWarnings_onServiceGenerator_annotationCopied() { 700 Source myService = 701 HiltCompilerTests.javaSource( 702 "test.MyService", 703 "package test;", 704 "", 705 "import android.annotation.TargetApi;", 706 "import android.content.Intent;", 707 "import android.app.Service;", 708 "import android.os.IBinder;", 709 "import dagger.hilt.android.AndroidEntryPoint;", 710 "", 711 "@SuppressWarnings(\"deprecation\")", 712 "@AndroidEntryPoint(Service.class)", 713 "public class MyService extends Hilt_MyService {", 714 " @Override", 715 " public IBinder onBind(Intent intent){", 716 " return null;", 717 " }", 718 "}"); 719 HiltCompilerTests.hiltCompiler(myService) 720 .compile( 721 subject -> { 722 subject.hasErrorCount(0); 723 StringSubject stringSubject = 724 subject.generatedSourceFileWithPath("test/Hilt_MyService.java"); 725 stringSubject.contains("package test;"); 726 stringSubject.contains( 727 JOINER.join( 728 "@Generated(\"dagger.hilt.android.processor.internal.androidentrypoint." 729 + "ServiceGenerator\")", 730 "@SuppressWarnings(\"deprecation\")", 731 "abstract class Hilt_MyService extends Service implements" 732 + " GeneratedComponentManagerHolder {")); 733 }); 734 } 735 } 736