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 18package androidx.fragment.app; 19 20import android.os.Bundle; 21import android.support.test.annotation.UiThreadTest; 22import android.support.test.filters.MediumTest; 23import android.support.test.rule.ActivityTestRule; 24import android.support.test.runner.AndroidJUnit4; 25import android.view.LayoutInflater; 26import android.view.View; 27import android.view.ViewGroup; 28import android.widget.TextView; 29 30import androidx.annotation.Nullable; 31import androidx.fragment.app.test.FragmentTestActivity; 32import androidx.fragment.test.R; 33 34import org.junit.Rule; 35import org.junit.Test; 36import org.junit.runner.RunWith; 37 38@RunWith(AndroidJUnit4.class) 39@MediumTest 40public class NestedInflatedFragmentTest { 41 private static final String TAG = "NestedInflatedFragmentTest"; 42 43 @Rule 44 public ActivityTestRule<FragmentTestActivity> mActivityRule = 45 new ActivityTestRule<>(FragmentTestActivity.class); 46 47 @Test 48 @UiThreadTest 49 public void inflatedChildFragment() throws Throwable { 50 final FragmentTestActivity activity = mActivityRule.getActivity(); 51 final FragmentManager fm = activity.getSupportFragmentManager(); 52 53 ParentFragment parentFragment = new ParentFragment(); 54 fm.beginTransaction().add(android.R.id.content, parentFragment).commitNow(); 55 56 fm.beginTransaction().replace(android.R.id.content, new SimpleFragment()) 57 .addToBackStack(null).commit(); 58 fm.executePendingTransactions(); 59 60 fm.popBackStackImmediate(); 61 } 62 63 /** 64 * This mimics the behavior of FragmentStatePagerAdapter jumping between pages 65 */ 66 @Test 67 @UiThreadTest 68 public void nestedSetUserVisibleHint() throws Throwable { 69 FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager(); 70 71 // Add a UserVisibleHintParentFragment 72 UserVisibleHintParentFragment fragment = new UserVisibleHintParentFragment(); 73 fm.beginTransaction().add(android.R.id.content, fragment).commit(); 74 fm.executePendingTransactions(); 75 76 fragment.setUserVisibleHint(false); 77 78 Fragment.SavedState state = fm.saveFragmentInstanceState(fragment); 79 fm.beginTransaction().remove(fragment).commit(); 80 fm.executePendingTransactions(); 81 82 fragment = new UserVisibleHintParentFragment(); 83 fragment.setInitialSavedState(state); 84 fragment.setUserVisibleHint(true); 85 86 fm.beginTransaction().add(android.R.id.content, fragment).commit(); 87 fm.executePendingTransactions(); 88 } 89 90 public static class ParentFragment extends Fragment { 91 @Nullable 92 @Override 93 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 94 @Nullable Bundle savedInstanceState) { 95 return inflater.inflate(R.layout.nested_inflated_fragment_parent, container, false); 96 } 97 } 98 99 public static class UserVisibleHintParentFragment extends ParentFragment { 100 @Override 101 public void setUserVisibleHint(boolean isVisibleToUser) { 102 super.setUserVisibleHint(isVisibleToUser); 103 if (getHost() != null) { 104 for (Fragment fragment : getChildFragmentManager().getFragments()) { 105 fragment.setUserVisibleHint(isVisibleToUser); 106 } 107 } 108 } 109 110 @Override 111 public void onAttachFragment(Fragment childFragment) { 112 super.onAttachFragment(childFragment); 113 childFragment.setUserVisibleHint(getUserVisibleHint()); 114 } 115 } 116 117 public static class InflatedChildFragment extends Fragment { 118 @Nullable 119 @Override 120 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 121 @Nullable Bundle savedInstanceState) { 122 return inflater.inflate(R.layout.nested_inflated_fragment_child, container, false); 123 } 124 } 125 126 public static class SimpleFragment extends Fragment { 127 @Nullable 128 @Override 129 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 130 @Nullable Bundle savedInstanceState) { 131 TextView textView = new TextView(inflater.getContext()); 132 textView.setText("Simple fragment"); 133 return textView; 134 } 135 } 136} 137