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 17package androidx.fragment.app; 18 19import static org.hamcrest.CoreMatchers.is; 20import static org.hamcrest.MatcherAssert.assertThat; 21import static org.junit.Assert.assertTrue; 22 23import android.os.Build; 24import android.support.test.filters.MediumTest; 25import android.support.test.filters.SdkSuppress; 26import android.support.test.rule.ActivityTestRule; 27import android.support.test.runner.AndroidJUnit4; 28 29import androidx.fragment.app.test.NonConfigOnStopActivity; 30import androidx.testutils.FragmentActivityUtils; 31 32import org.junit.Rule; 33import org.junit.Test; 34import org.junit.runner.RunWith; 35 36@MediumTest 37@RunWith(AndroidJUnit4.class) 38public class FragmentManagerNonConfigTest { 39 40 @Rule 41 public ActivityTestRule<NonConfigOnStopActivity> mActivityRule = 42 new ActivityTestRule<>(NonConfigOnStopActivity.class); 43 44 /** 45 * When a fragment is added during onStop(), it shouldn't show up in non-config 46 * state when restored before P, because OnSaveInstanceState was already called. 47 */ 48 @Test 49 @SdkSuppress(maxSdkVersion = Build.VERSION_CODES.O_MR1) 50 public void nonConfigStop() throws Throwable { 51 FragmentActivity activity = FragmentActivityUtils.recreateActivity(mActivityRule, 52 mActivityRule.getActivity()); 53 54 // A fragment was added in onStop(), but we shouldn't see it here... 55 assertTrue(activity.getSupportFragmentManager().getFragments().isEmpty()); 56 } 57 58 /** 59 * When a fragment is added during onStop(), it shouldn't show up in non-config 60 * state when restored after (>=) P, because OnSaveInstanceState isn't yet called. 61 */ 62 @Test 63 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.P) 64 public void nonConfigStopSavingFragment() throws Throwable { 65 FragmentActivity activity = FragmentActivityUtils.recreateActivity(mActivityRule, 66 mActivityRule.getActivity()); 67 68 assertThat(activity.getSupportFragmentManager().getFragments().size(), is(1)); 69 } 70} 71