Home | History | Annotate | Download | only in app
      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 
     18 package androidx.fragment.app;
     19 
     20 import static org.junit.Assert.assertNotNull;
     21 import static org.junit.Assert.assertNotSame;
     22 import static org.junit.Assert.assertTrue;
     23 
     24 import android.content.Context;
     25 import android.support.test.filters.SmallTest;
     26 import android.support.test.rule.ActivityTestRule;
     27 import android.support.test.runner.AndroidJUnit4;
     28 
     29 import androidx.fragment.app.test.FragmentTestActivity;
     30 import androidx.fragment.app.test.FragmentTestActivity.ChildFragment;
     31 import androidx.fragment.app.test.FragmentTestActivity.ChildFragment.OnAttachListener;
     32 import androidx.fragment.app.test.FragmentTestActivity.ParentFragment;
     33 
     34 import org.junit.Rule;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 
     38 import java.util.concurrent.CountDownLatch;
     39 import java.util.concurrent.TimeUnit;
     40 
     41 @RunWith(AndroidJUnit4.class)
     42 public class NestedFragmentRestoreTest {
     43 
     44     @Rule
     45     public ActivityTestRule<FragmentTestActivity> mActivityRule = new ActivityTestRule<>(
     46             FragmentTestActivity.class);
     47 
     48     public NestedFragmentRestoreTest() {
     49     }
     50 
     51     @Test
     52     @SmallTest
     53     public void recreateActivity() throws Throwable {
     54         final FragmentTestActivity activity = mActivityRule.getActivity();
     55         mActivityRule.runOnUiThread(new Runnable() {
     56             @Override
     57             public void run() {
     58                 FragmentTestActivity.ParentFragment parent = new ParentFragment();
     59                 parent.setRetainChildInstance(true);
     60 
     61                 activity.getSupportFragmentManager().beginTransaction()
     62                         .add(parent, "parent")
     63                         .commitNow();
     64             }
     65         });
     66 
     67         FragmentManager fm = activity.getSupportFragmentManager();
     68         ParentFragment parent = (ParentFragment) fm.findFragmentByTag("parent");
     69         ChildFragment child = parent.getChildFragment();
     70 
     71         final Context[] attachedTo = new Context[1];
     72         final CountDownLatch latch = new CountDownLatch(1);
     73         child.setOnAttachListener(new OnAttachListener() {
     74             @Override
     75             public void onAttach(Context activity, ChildFragment fragment) {
     76                 attachedTo[0] = activity;
     77                 latch.countDown();
     78             }
     79         });
     80 
     81         mActivityRule.runOnUiThread(new Runnable() {
     82             @Override
     83             public void run() {
     84                 activity.recreate();
     85             }
     86         });
     87 
     88         assertTrue("timeout waiting for recreate", latch.await(10, TimeUnit.SECONDS));
     89 
     90         assertNotNull("attached as part of recreate", attachedTo[0]);
     91         assertNotSame("attached to new context", activity, attachedTo[0]);
     92         assertNotSame("attached to new parent fragment", parent, child);
     93     }
     94 }
     95