blob: 091bede5f8282a51c18756b7a73f3fcd762d0ca9 [file] [log] [blame]
Justin Klaassen4d01eea2018-04-03 23:21:57 -04001/*
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
17package androidx.lifecycle;
18
19import static org.hamcrest.CoreMatchers.is;
20import static org.hamcrest.CoreMatchers.notNullValue;
21import static org.hamcrest.MatcherAssert.assertThat;
22
23import android.os.Bundle;
24import android.support.test.annotation.UiThreadTest;
25import android.support.test.filters.MediumTest;
26import android.support.test.rule.ActivityTestRule;
27import android.support.test.runner.AndroidJUnit4;
28
29import androidx.annotation.Nullable;
30import androidx.fragment.app.Fragment;
31import androidx.lifecycle.viewmodeltest.TestViewModel;
32
33import org.junit.Rule;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36
37@MediumTest
38@RunWith(AndroidJUnit4.class)
39public 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}