• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 android.fragment.cts;
19 
20 import android.app.Fragment;
21 import android.app.FragmentManager;
22 import android.os.Bundle;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.TextView;
27 
28 import androidx.annotation.Nullable;
29 import androidx.test.filters.MediumTest;
30 import androidx.test.rule.ActivityTestRule;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 
37 @RunWith(AndroidJUnit4.class)
38 @MediumTest
39 public class NestedInflatedFragmentTest {
40     private static final String TAG = "NestedInflatedFragmentTest";
41 
42     @Rule
43     public ActivityTestRule<FragmentTestActivity> mActivityRule =
44             new ActivityTestRule<>(FragmentTestActivity.class);
45 
46     @Test
inflatedChildFragment()47     public void inflatedChildFragment() throws Throwable {
48         mActivityRule.runOnUiThread(new Runnable() {
49             @Override
50             public void run() {
51                 final FragmentTestActivity activity = mActivityRule.getActivity();
52                 final FragmentManager fm = activity.getFragmentManager();
53 
54                 ParentFragment parentFragment = new ParentFragment();
55                 fm.beginTransaction().add(android.R.id.content, parentFragment).commitNow();
56 
57                 fm.beginTransaction().replace(android.R.id.content, new SimpleFragment())
58                         .addToBackStack(null).commit();
59                 fm.executePendingTransactions();
60 
61                 fm.popBackStackImmediate();
62             }
63         });
64     }
65 
66     public static class ParentFragment extends Fragment {
67         @Nullable
68         @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)69         public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
70                 @Nullable Bundle savedInstanceState) {
71             return inflater.inflate(R.layout.nested_inflated_fragment_parent, container, false);
72         }
73     }
74 
75     public static class InflatedChildFragment extends Fragment {
76         @Nullable
77         @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)78         public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
79                 @Nullable Bundle savedInstanceState) {
80             return inflater.inflate(R.layout.nested_inflated_fragment_child, container, false);
81         }
82     }
83 
84     public static class SimpleFragment extends Fragment {
85         @Nullable
86         @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)87         public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
88                 @Nullable Bundle savedInstanceState) {
89             TextView textView = new TextView(inflater.getContext());
90             textView.setText("Simple fragment");
91             return textView;
92         }
93     }
94 }
95