1 /* 2 * Copyright (C) 2021 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 static androidx.test.core.app.ApplicationProvider.getApplicationContext; 20 import static com.google.common.truth.Truth.assertThat; 21 22 import android.content.Intent; 23 import android.os.Build; 24 import android.os.Bundle; 25 import androidx.fragment.app.FragmentActivity; 26 import androidx.lifecycle.SavedStateHandle; 27 import androidx.lifecycle.ViewModel; 28 import androidx.lifecycle.ViewModelProvider; 29 import androidx.test.core.app.ActivityScenario; 30 import androidx.test.ext.junit.runners.AndroidJUnit4; 31 import dagger.Module; 32 import dagger.Provides; 33 import dagger.hilt.InstallIn; 34 import dagger.hilt.android.components.ActivityComponent; 35 import dagger.hilt.android.testing.HiltAndroidRule; 36 import dagger.hilt.android.testing.HiltAndroidTest; 37 import dagger.hilt.android.testing.HiltTestApplication; 38 import javax.inject.Inject; 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.robolectric.annotation.Config; 43 44 @HiltAndroidTest 45 @RunWith(AndroidJUnit4.class) 46 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead 47 @Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class) 48 public class ActivityInjectedSavedStateViewModelTest { 49 50 private static final String DATA_KEY = "TEST_KEY"; 51 52 @Rule public final HiltAndroidRule rule = new HiltAndroidRule(this); 53 54 @Test memberInjectedViewModelWithSavedState()55 public void memberInjectedViewModelWithSavedState() { 56 Intent intent = new Intent(getApplicationContext(), TestActivity.class); 57 intent.putExtra(DATA_KEY, "test data"); 58 try (ActivityScenario<TestActivity> scenario = ActivityScenario.launch(intent)) { 59 scenario.onActivity( 60 activity -> { 61 String data = activity.myViewModel.handle.get(DATA_KEY); 62 assertThat(data).isEqualTo("test data"); 63 }); 64 } 65 } 66 67 // Note that assertion of object not being yet injected is in the SuperActivity, while the 68 // assertion in the scenario is confirming injection eventually does occur. 69 @Test notYetMemberInjectedSuperActivity()70 public void notYetMemberInjectedSuperActivity() { 71 try (ActivityScenario<TestActivityWithSuperActivity> scenario = 72 ActivityScenario.launch(TestActivityWithSuperActivity.class)) { 73 scenario.onActivity(activity -> assertThat(activity.someObject).isNotNull()); 74 } 75 } 76 77 @AndroidEntryPoint(FragmentActivity.class) 78 public static final class TestActivity 79 extends Hilt_ActivityInjectedSavedStateViewModelTest_TestActivity { 80 @Inject MyViewModel myViewModel; 81 } 82 83 @AndroidEntryPoint(SuperActivity.class) 84 public static final class TestActivityWithSuperActivity 85 extends Hilt_ActivityInjectedSavedStateViewModelTest_TestActivityWithSuperActivity {} 86 87 public static class SuperActivity extends FragmentActivity { 88 @Inject Object someObject; 89 90 @Override onCreate(Bundle savedInstanceState)91 protected void onCreate(Bundle savedInstanceState) { 92 assertThat(someObject).isNull(); // not yet injected 93 super.onCreate(savedInstanceState); 94 } 95 } 96 97 @Module 98 @InstallIn(ActivityComponent.class) 99 static final class MyViewModelModel { 100 @Provides provideModel(FragmentActivity activity)101 static MyViewModel provideModel(FragmentActivity activity) { 102 return new ViewModelProvider(activity).get(MyViewModel.class); 103 } 104 105 @Provides provideObject()106 static Object provideObject() { 107 return new Object(); 108 } 109 } 110 111 public static final class MyViewModel extends ViewModel { 112 final SavedStateHandle handle; 113 MyViewModel(SavedStateHandle handle)114 public MyViewModel(SavedStateHandle handle) { 115 this.handle = handle; 116 } 117 } 118 } 119