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