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; 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 androidx.fragment.app.FragmentActivity; 25 import androidx.lifecycle.ViewModel; 26 import androidx.lifecycle.ViewModelProvider; 27 import androidx.test.core.app.ActivityScenario; 28 import androidx.test.ext.junit.runners.AndroidJUnit4; 29 import dagger.Module; 30 import dagger.Provides; 31 import dagger.hilt.InstallIn; 32 import dagger.hilt.android.components.ActivityComponent; 33 import dagger.hilt.android.testing.HiltAndroidRule; 34 import dagger.hilt.android.testing.HiltAndroidTest; 35 import dagger.hilt.android.testing.HiltTestApplication; 36 import javax.inject.Inject; 37 import org.junit.Rule; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.robolectric.annotation.Config; 41 42 /** 43 * Tests that the Hilt view model factory doesn't cause errors with non-Hilt view models. This was 44 * a problem at one point because if the non-Hilt view model is accessed before saved state 45 * restoration happens, even if the non-Hilt view model doesn't use saved state, it can cause an 46 * error because the default Hilt view model factory does use saved state. This test ensures we 47 * still allow non-Hilt view models without saved state to be used before saved state is restored. 48 */ 49 @HiltAndroidTest 50 @RunWith(AndroidJUnit4.class) 51 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead 52 @Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class) 53 public class ActivityInjectedViewModelTest { 54 55 private static final String DATA_KEY = "TEST_KEY"; 56 57 @Rule public final HiltAndroidRule rule = new HiltAndroidRule(this); 58 59 @Test memberInjectedViewModelWithSavedState()60 public void memberInjectedViewModelWithSavedState() { 61 final Intent intent = new Intent(getApplicationContext(), TestActivity.class); 62 intent.putExtra(DATA_KEY, "test data"); 63 try (ActivityScenario<TestActivity> scenario = ActivityScenario.launch(intent)) { 64 scenario.onActivity(activity -> assertThat(activity.myViewModel).isNotNull()); 65 } 66 } 67 68 @AndroidEntryPoint(FragmentActivity.class) 69 public static final class TestActivity extends Hilt_ActivityInjectedViewModelTest_TestActivity { 70 @Inject MyViewModel myViewModel; 71 } 72 73 @Module 74 @InstallIn(ActivityComponent.class) 75 static final class MyViewModelModel { 76 @Provides provideModel(FragmentActivity activity)77 static MyViewModel provideModel(FragmentActivity activity) { 78 return new ViewModelProvider(activity).get(MyViewModel.class); 79 } 80 } 81 82 public static final class MyViewModel extends ViewModel {} 83 } 84