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 
17 package androidx.lifecycle.testapp;
18 
19 import static androidx.lifecycle.testapp.TestEvent.OWNER_CALLBACK;
20 
21 import android.os.Bundle;
22 import android.widget.FrameLayout;
23 
24 import androidx.fragment.app.Fragment;
25 import androidx.fragment.app.FragmentActivity;
26 import androidx.lifecycle.Lifecycle.Event;
27 
28 import kotlin.Pair;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.concurrent.CountDownLatch;
33 import java.util.concurrent.TimeUnit;
34 
35 /**
36  * LifecycleRegistryOwner that extends FragmentActivity.
37  */
38 public class CollectingSupportActivity extends FragmentActivity implements
39         CollectingLifecycleOwner {
40 
41     private final List<Pair<TestEvent, Event>> mCollectedEvents = new ArrayList<>();
42     private TestObserver mTestObserver = new TestObserver(mCollectedEvents);
43     private CountDownLatch mSavedStateLatch = new CountDownLatch(1);
44 
45     @Override
onCreate(Bundle savedInstanceState)46     protected void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48         FrameLayout layout = new FrameLayout(this);
49         layout.setId(R.id.fragment_container);
50         setContentView(layout);
51         mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_CREATE));
52         getLifecycle().addObserver(mTestObserver);
53     }
54 
55     /**
56      * replaces the main content fragment w/ the given fragment.
57      */
replaceFragment(Fragment fragment)58     public void replaceFragment(Fragment fragment) {
59         getSupportFragmentManager()
60                 .beginTransaction()
61                 .add(R.id.fragment_container, fragment)
62                 .commitNow();
63     }
64 
65     @Override
onStart()66     protected void onStart() {
67         super.onStart();
68         mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_START));
69     }
70 
71     @Override
onResume()72     protected void onResume() {
73         super.onResume();
74         mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_RESUME));
75     }
76 
77     @Override
onDestroy()78     protected void onDestroy() {
79         super.onDestroy();
80         mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_DESTROY));
81     }
82 
83     @Override
onStop()84     protected void onStop() {
85         super.onStop();
86         mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_STOP));
87     }
88 
89     @Override
onPause()90     protected void onPause() {
91         super.onPause();
92         mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_PAUSE));
93         // helps with less flaky API 16 tests.
94         overridePendingTransition(0, 0);
95     }
96 
97     @Override
copyCollectedEvents()98     public List<Pair<TestEvent, Event>> copyCollectedEvents() {
99         return new ArrayList<>(mCollectedEvents);
100     }
101 
102     @Override
onSaveInstanceState(Bundle outState)103     protected void onSaveInstanceState(Bundle outState) {
104         super.onSaveInstanceState(outState);
105         mSavedStateLatch.countDown();
106     }
107 
108     /**
109      * Waits for onSaveInstanceState to be called.
110      */
waitForStateSave(@uppressWarnings"SameParameterValue") int seconds)111     public boolean waitForStateSave(@SuppressWarnings("SameParameterValue") int seconds)
112             throws InterruptedException {
113         return mSavedStateLatch.await(seconds, TimeUnit.SECONDS);
114     }
115 }
116