1 /* 2 * Copyright (C) 2024 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; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.os.Build; 22 import android.os.IBinder; 23 import androidx.test.core.app.ActivityScenario; 24 import androidx.test.core.app.ApplicationProvider; 25 import androidx.test.ext.junit.runners.AndroidJUnit4; 26 import dagger.hilt.android.testing.HiltAndroidRule; 27 import dagger.hilt.android.testing.HiltAndroidTest; 28 import dagger.hilt.android.testing.HiltTestApplication; 29 import dagger.hilt.android.testsubpackage.PackagePrivateConstructorTestClasses.BaseActivity; 30 import dagger.hilt.android.testsubpackage.PackagePrivateConstructorTestClasses.BaseBroadcastReceiver; 31 import dagger.hilt.android.testsubpackage.PackagePrivateConstructorTestClasses.BaseFragment; 32 import dagger.hilt.android.testsubpackage.PackagePrivateConstructorTestClasses.BaseIntentService; 33 import dagger.hilt.android.testsubpackage.PackagePrivateConstructorTestClasses.BaseService; 34 import dagger.hilt.android.testsubpackage.PackagePrivateConstructorTestClasses.BaseView; 35 import org.junit.Before; 36 import org.junit.Rule; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.robolectric.Robolectric; 40 import org.robolectric.annotation.Config; 41 42 /** Regression test for b/331280240. */ 43 @HiltAndroidTest 44 @RunWith(AndroidJUnit4.class) 45 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead 46 @Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class) 47 public final class PackagePrivateConstructorTest { 48 @Rule public final HiltAndroidRule rule = new HiltAndroidRule(this); 49 50 @AndroidEntryPoint(BaseActivity.class) 51 public static final class TestActivity extends Hilt_PackagePrivateConstructorTest_TestActivity { 52 } 53 54 @AndroidEntryPoint(BaseFragment.class) 55 public static final class TestFragment extends Hilt_PackagePrivateConstructorTest_TestFragment { 56 } 57 58 @AndroidEntryPoint(BaseView.class) 59 public static final class TestView extends Hilt_PackagePrivateConstructorTest_TestView { TestView(Context context)60 TestView(Context context) { 61 super(context); 62 } 63 } 64 65 @AndroidEntryPoint(BaseService.class) 66 public static final class TestService extends Hilt_PackagePrivateConstructorTest_TestService { 67 @Override onBind(Intent intent)68 public IBinder onBind(Intent intent) { 69 return null; 70 } 71 } 72 73 @AndroidEntryPoint(BaseIntentService.class) 74 public static final class TestIntentService 75 extends Hilt_PackagePrivateConstructorTest_TestIntentService { TestIntentService()76 public TestIntentService() { 77 super("TestIntentServiceName"); 78 } 79 80 @Override onHandleIntent(Intent intent)81 public void onHandleIntent(Intent intent) {} 82 } 83 84 @AndroidEntryPoint(BaseBroadcastReceiver.class) 85 public static final class TestBroadcastReceiver 86 extends Hilt_PackagePrivateConstructorTest_TestBroadcastReceiver { 87 } 88 89 @Before setup()90 public void setup() { 91 rule.inject(); 92 } 93 94 // Technically all the tests need to do is check for compilation, but might as well make sure the 95 // classes are usable 96 @Test testActivityFragmentView()97 public void testActivityFragmentView() throws Exception { 98 try (ActivityScenario<TestActivity> scenario = ActivityScenario.launch(TestActivity.class)) { 99 scenario.onActivity( 100 activity -> { 101 TestFragment fragment = new TestFragment(); 102 activity.getSupportFragmentManager().beginTransaction().add(fragment, "").commitNow(); 103 TestView unused = new TestView(fragment.getContext()); 104 }); 105 } 106 } 107 108 @Test testServices()109 public void testServices() throws Exception { 110 Robolectric.setupService(TestService.class); 111 Robolectric.setupService(TestIntentService.class); 112 } 113 114 @Test testBroadcastReceiver()115 public void testBroadcastReceiver() throws Exception { 116 TestBroadcastReceiver testBroadcastReceiver = new TestBroadcastReceiver(); 117 Intent intent = new Intent(); 118 testBroadcastReceiver.onReceive(ApplicationProvider.getApplicationContext(), intent); 119 } 120 } 121