1 /*
2  * Copyright (C) 2017 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 package androidx.lifecycle.testapp;
17 
18 import static androidx.lifecycle.testapp.TestEvent.OWNER_CALLBACK;
19 
20 import android.annotation.SuppressLint;
21 import android.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.FrameLayout;
26 
27 import androidx.fragment.app.Fragment;
28 import androidx.lifecycle.Lifecycle;
29 
30 import kotlin.Pair;
31 
32 import org.jspecify.annotations.Nullable;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 /**
38  * A support fragment that collects all of its events.
39  */
40 @SuppressLint("ValidFragment")
41 public class CollectingSupportFragment extends Fragment implements CollectingLifecycleOwner {
42     private final List<Pair<TestEvent, Lifecycle.Event>> mCollectedEvents =
43             new ArrayList<>();
44     private TestObserver mTestObserver = new TestObserver(mCollectedEvents);
45 
46     @Override
onCreate(Bundle savedInstanceState)47     public void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_CREATE));
50         getLifecycle().addObserver(mTestObserver);
51     }
52 
53     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)54     public @Nullable View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
55             @Nullable Bundle savedInstanceState) {
56         if (container == null) throw new IllegalStateException();
57         FrameLayout layout = new FrameLayout(container.getContext());
58         layout.setId(R.id.child_fragment_container);
59         return layout;
60     }
61 
62     /**
63      * Runs a replace fragment transaction with 'fragment' on this Fragment.
64      */
replaceFragment(Fragment fragment)65     public void replaceFragment(Fragment fragment) {
66         getChildFragmentManager()
67                 .beginTransaction()
68                 .add(R.id.child_fragment_container, fragment)
69                 .commitNow();
70     }
71 
72     @Override
onStart()73     public void onStart() {
74         super.onStart();
75         mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_START));
76     }
77 
78     @Override
onResume()79     public void onResume() {
80         super.onResume();
81         mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_RESUME));
82     }
83 
84     @Override
onDestroy()85     public void onDestroy() {
86         super.onDestroy();
87         mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_DESTROY));
88     }
89 
90     @Override
onStop()91     public void onStop() {
92         super.onStop();
93         mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_STOP));
94     }
95 
96     @Override
onPause()97     public void onPause() {
98         super.onPause();
99         mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_PAUSE));
100     }
101 
102     @Override
copyCollectedEvents()103     public List<Pair<TestEvent, Lifecycle.Event>> copyCollectedEvents() {
104         return new ArrayList<>(mCollectedEvents);
105     }
106 }
107