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 android.fragment.cts; 17 18 import static org.junit.Assert.assertTrue; 19 20 import android.app.Activity; 21 import android.os.Bundle; 22 23 import androidx.test.rule.ActivityTestRule; 24 25 import java.util.concurrent.CountDownLatch; 26 import java.util.concurrent.TimeUnit; 27 28 public class RecreatedActivity extends FragmentTestActivity { 29 // These must be cleared after each test using clearState() 30 public static RecreatedActivity sActivity; 31 public static CountDownLatch sResumed; 32 public static CountDownLatch sDestroyed; 33 private boolean mIsResumed; 34 clearState()35 public static void clearState() { 36 sActivity = null; 37 sResumed = null; 38 sDestroyed = null; 39 } 40 41 @Override onCreate(Bundle savedInstanceState)42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 sActivity = this; 45 } 46 47 @Override onResume()48 protected void onResume() { 49 super.onResume(); 50 mIsResumed = true; 51 if (sResumed != null) { 52 sResumed.countDown(); 53 } 54 } 55 56 @Override onPause()57 protected void onPause() { 58 super.onPause(); 59 mIsResumed = false; 60 } 61 62 @Override onDestroy()63 protected void onDestroy() { 64 super.onDestroy(); 65 if (sDestroyed != null) { 66 sDestroyed.countDown(); 67 } 68 } 69 waitForResume(ActivityTestRule<? extends Activity> rule)70 public void waitForResume(ActivityTestRule<? extends Activity> rule) throws Throwable { 71 if (mIsResumed) { 72 return; 73 } 74 if (sResumed != null) { 75 assertTrue(sResumed.await(1, TimeUnit.SECONDS)); 76 } else { 77 rule.runOnUiThread(() -> { 78 if (!mIsResumed) { 79 sResumed = new CountDownLatch(1); 80 } 81 }); 82 if (sResumed != null) { 83 assertTrue(sResumed.await(1, TimeUnit.SECONDS)); 84 sResumed = null; 85 } 86 } 87 } 88 } 89