1 /* 2 * Copyright (C) 2017 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.lifecycle; 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.Fragment; 31 import androidx.lifecycle.viewmodeltest.TestViewModel; 32 33 import org.junit.Rule; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 @MediumTest 38 @RunWith(AndroidJUnit4.class) 39 public class ViewModelTestInTransaction { 40 41 @Rule 42 public ActivityTestRule<EmptyActivity> mActivityRule = 43 new ActivityTestRule<>(EmptyActivity.class); 44 45 @Test 46 @UiThreadTest 47 public void testViewModelInTransactionActivity() { 48 EmptyActivity activity = mActivityRule.getActivity(); 49 TestFragment fragment = new TestFragment(); 50 activity.getSupportFragmentManager().beginTransaction().add(fragment, "tag").commitNow(); 51 TestViewModel viewModel = ViewModelProviders.of(activity).get(TestViewModel.class); 52 assertThat(viewModel, is(fragment.mViewModel)); 53 } 54 55 @Test 56 @UiThreadTest 57 public void testViewModelInTransactionFragment() { 58 EmptyActivity activity = mActivityRule.getActivity(); 59 ParentFragment parent = new ParentFragment(); 60 activity.getSupportFragmentManager().beginTransaction().add(parent, "parent").commitNow(); 61 assertThat(parent.mExecuted, is(true)); 62 } 63 64 65 public static class ParentFragment extends Fragment { 66 67 private boolean mExecuted; 68 69 @Override 70 public void onCreate(@Nullable Bundle savedInstanceState) { 71 super.onCreate(savedInstanceState); 72 TestFragment fragment = new TestFragment(); 73 getChildFragmentManager().beginTransaction().add(fragment, "tag").commitNow(); 74 TestViewModel viewModel = ViewModelProviders.of(this).get(TestViewModel.class); 75 assertThat(viewModel, is(fragment.mViewModel)); 76 mExecuted = true; 77 } 78 } 79 80 public static class TestFragment extends Fragment { 81 82 TestViewModel mViewModel; 83 84 @Override 85 public void onCreate(@Nullable Bundle savedInstanceState) { 86 super.onCreate(savedInstanceState); 87 Fragment parentFragment = getParentFragment(); 88 ViewModelProvider provider = parentFragment != null 89 ? ViewModelProviders.of(parentFragment) : ViewModelProviders.of(getActivity()); 90 mViewModel = provider.get(TestViewModel.class); 91 assertThat(mViewModel, notNullValue()); 92 } 93 } 94 } 95