1 /* 2 * Copyright 2018 The Android Open Source Project 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 androidx.fragment.app; 18 19 import static org.hamcrest.CoreMatchers.is; 20 import static org.hamcrest.CoreMatchers.notNullValue; 21 import static org.hamcrest.MatcherAssert.assertThat; 22 23 import android.os.Bundle; 24 import android.support.test.annotation.UiThreadTest; 25 import android.support.test.filters.MediumTest; 26 import android.support.test.rule.ActivityTestRule; 27 import android.support.test.runner.AndroidJUnit4; 28 29 import androidx.annotation.Nullable; 30 import androidx.fragment.app.test.EmptyFragmentTestActivity; 31 import androidx.fragment.app.test.TestViewModel; 32 import androidx.lifecycle.ViewModelProvider; 33 34 import org.junit.Rule; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 38 @MediumTest 39 @RunWith(AndroidJUnit4.class) 40 public class ViewModelTestInTransaction { 41 42 @Rule 43 public ActivityTestRule<EmptyFragmentTestActivity> mActivityRule = 44 new ActivityTestRule<>(EmptyFragmentTestActivity.class); 45 46 @Test 47 @UiThreadTest 48 public void testViewModelInTransactionActivity() { 49 EmptyFragmentTestActivity activity = mActivityRule.getActivity(); 50 TestFragment fragment = new TestFragment(); 51 activity.getSupportFragmentManager().beginTransaction().add(fragment, "tag").commitNow(); 52 ViewModelProvider viewModelProvider = new ViewModelProvider(activity, 53 new ViewModelProvider.NewInstanceFactory()); 54 TestViewModel viewModel = viewModelProvider.get(TestViewModel.class); 55 assertThat(viewModel, is(fragment.mViewModel)); 56 } 57 58 @Test 59 @UiThreadTest 60 public void testViewModelInTransactionFragment() { 61 EmptyFragmentTestActivity activity = mActivityRule.getActivity(); 62 ParentFragment parent = new ParentFragment(); 63 activity.getSupportFragmentManager().beginTransaction().add(parent, "parent").commitNow(); 64 assertThat(parent.mExecuted, is(true)); 65 } 66 67 68 public static class ParentFragment extends Fragment { 69 70 private boolean mExecuted; 71 72 @Override 73 public void onCreate(@Nullable Bundle savedInstanceState) { 74 super.onCreate(savedInstanceState); 75 TestFragment fragment = new TestFragment(); 76 getChildFragmentManager().beginTransaction().add(fragment, "tag").commitNow(); 77 ViewModelProvider viewModelProvider = new ViewModelProvider(this, 78 new ViewModelProvider.NewInstanceFactory()); 79 TestViewModel viewModel = viewModelProvider.get(TestViewModel.class); 80 assertThat(viewModel, is(fragment.mViewModel)); 81 mExecuted = true; 82 } 83 } 84 85 public static class TestFragment extends Fragment { 86 87 TestViewModel mViewModel; 88 89 @Override 90 public void onCreate(@Nullable Bundle savedInstanceState) { 91 super.onCreate(savedInstanceState); 92 Fragment parentFragment = getParentFragment(); 93 ViewModelProvider provider = new ViewModelProvider( 94 parentFragment != null ? parentFragment : getActivity(), 95 new ViewModelProvider.NewInstanceFactory()); 96 mViewModel = provider.get(TestViewModel.class); 97 assertThat(mViewModel, notNullValue()); 98 } 99 } 100 } 101