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