137149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell/* 2ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas * Copyright 2018 The Android Open Source Project 337149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell * 437149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell * Licensed under the Apache License, Version 2.0 (the "License"); 537149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell * you may not use this file except in compliance with the License. 637149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell * You may obtain a copy of the License at 737149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell * 837149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell * http://www.apache.org/licenses/LICENSE-2.0 937149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell * 1037149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell * Unless required by applicable law or agreed to in writing, software 1137149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell * distributed under the License is distributed on an "AS IS" BASIS, 1237149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1337149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell * See the License for the specific language governing permissions and 1437149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell * limitations under the License. 1537149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell */ 1637149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell 1737149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell 18ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.fragment.app; 1937149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell 2037149f137aad1b2ec06df63807cef6713da3ca2fAdam Powellimport android.os.Bundle; 2137149f137aad1b2ec06df63807cef6713da3ca2fAdam Powellimport android.support.test.annotation.UiThreadTest; 2237149f137aad1b2ec06df63807cef6713da3ca2fAdam Powellimport android.support.test.filters.MediumTest; 2337149f137aad1b2ec06df63807cef6713da3ca2fAdam Powellimport android.support.test.rule.ActivityTestRule; 2437149f137aad1b2ec06df63807cef6713da3ca2fAdam Powellimport android.support.test.runner.AndroidJUnit4; 2537149f137aad1b2ec06df63807cef6713da3ca2fAdam Powellimport android.view.LayoutInflater; 2637149f137aad1b2ec06df63807cef6713da3ca2fAdam Powellimport android.view.View; 2737149f137aad1b2ec06df63807cef6713da3ca2fAdam Powellimport android.view.ViewGroup; 2837149f137aad1b2ec06df63807cef6713da3ca2fAdam Powellimport android.widget.TextView; 2937149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell 30320113721c2e14bbc2403809046fa2959a665c11Aurimas Liutikasimport androidx.annotation.Nullable; 31320113721c2e14bbc2403809046fa2959a665c11Aurimas Liutikasimport androidx.fragment.app.test.FragmentTestActivity; 32320113721c2e14bbc2403809046fa2959a665c11Aurimas Liutikasimport androidx.fragment.test.R; 33320113721c2e14bbc2403809046fa2959a665c11Aurimas Liutikas 3437149f137aad1b2ec06df63807cef6713da3ca2fAdam Powellimport org.junit.Rule; 3537149f137aad1b2ec06df63807cef6713da3ca2fAdam Powellimport org.junit.Test; 3637149f137aad1b2ec06df63807cef6713da3ca2fAdam Powellimport org.junit.runner.RunWith; 3737149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell 3837149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell@RunWith(AndroidJUnit4.class) 3937149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell@MediumTest 4037149f137aad1b2ec06df63807cef6713da3ca2fAdam Powellpublic class NestedInflatedFragmentTest { 4137149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell private static final String TAG = "NestedInflatedFragmentTest"; 4237149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell 4337149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell @Rule 4437149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell public ActivityTestRule<FragmentTestActivity> mActivityRule = 4537149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell new ActivityTestRule<>(FragmentTestActivity.class); 4637149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell 4737149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell @Test 4837149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell @UiThreadTest 4937149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell public void inflatedChildFragment() throws Throwable { 5037149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell final FragmentTestActivity activity = mActivityRule.getActivity(); 5137149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell final FragmentManager fm = activity.getSupportFragmentManager(); 5237149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell 5337149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell ParentFragment parentFragment = new ParentFragment(); 5437149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell fm.beginTransaction().add(android.R.id.content, parentFragment).commitNow(); 5537149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell 5637149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell fm.beginTransaction().replace(android.R.id.content, new SimpleFragment()) 5737149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell .addToBackStack(null).commit(); 5837149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell fm.executePendingTransactions(); 5937149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell 6037149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell fm.popBackStackImmediate(); 6137149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell } 6237149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell 63261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake /** 64261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake * This mimics the behavior of FragmentStatePagerAdapter jumping between pages 65261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake */ 66261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake @Test 67261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake @UiThreadTest 68261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake public void nestedSetUserVisibleHint() throws Throwable { 69261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager(); 70261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake 71261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake // Add a UserVisibleHintParentFragment 72261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake UserVisibleHintParentFragment fragment = new UserVisibleHintParentFragment(); 73261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake fm.beginTransaction().add(android.R.id.content, fragment).commit(); 74261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake fm.executePendingTransactions(); 75261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake 76261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake fragment.setUserVisibleHint(false); 77261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake 78261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake Fragment.SavedState state = fm.saveFragmentInstanceState(fragment); 79261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake fm.beginTransaction().remove(fragment).commit(); 80261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake fm.executePendingTransactions(); 81261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake 82261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake fragment = new UserVisibleHintParentFragment(); 83261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake fragment.setInitialSavedState(state); 84261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake fragment.setUserVisibleHint(true); 85261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake 86261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake fm.beginTransaction().add(android.R.id.content, fragment).commit(); 87261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake fm.executePendingTransactions(); 88261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake } 89261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake 9037149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell public static class ParentFragment extends Fragment { 9137149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell @Nullable 9237149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell @Override 9337149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 9437149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell @Nullable Bundle savedInstanceState) { 9537149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell return inflater.inflate(R.layout.nested_inflated_fragment_parent, container, false); 9637149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell } 9737149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell } 9837149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell 99261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake public static class UserVisibleHintParentFragment extends ParentFragment { 100261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake @Override 101261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake public void setUserVisibleHint(boolean isVisibleToUser) { 102261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake super.setUserVisibleHint(isVisibleToUser); 103261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake if (getHost() != null) { 104261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake for (Fragment fragment : getChildFragmentManager().getFragments()) { 105261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake fragment.setUserVisibleHint(isVisibleToUser); 106261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake } 107261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake } 108261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake } 109261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake 110261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake @Override 111261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake public void onAttachFragment(Fragment childFragment) { 112261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake super.onAttachFragment(childFragment); 113261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake childFragment.setUserVisibleHint(getUserVisibleHint()); 114261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake } 115261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake } 116261fa1b6dce85674f9a6fbe8a48851c80701b1dbIan Lake 11737149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell public static class InflatedChildFragment extends Fragment { 11837149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell @Nullable 11937149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell @Override 12037149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 12137149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell @Nullable Bundle savedInstanceState) { 12237149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell return inflater.inflate(R.layout.nested_inflated_fragment_child, container, false); 12337149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell } 12437149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell } 12537149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell 12637149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell public static class SimpleFragment extends Fragment { 12737149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell @Nullable 12837149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell @Override 12937149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 13037149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell @Nullable Bundle savedInstanceState) { 13137149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell TextView textView = new TextView(inflater.getContext()); 13237149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell textView.setText("Simple fragment"); 13337149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell return textView; 13437149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell } 13537149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell } 13637149f137aad1b2ec06df63807cef6713da3ca2fAdam Powell} 137