1 /* 2 * Copyright (C) 2021 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 android.server.wm.jetpack.utils; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.view.View; 22 23 import androidx.annotation.Nullable; 24 25 import java.util.concurrent.CountDownLatch; 26 import java.util.concurrent.TimeUnit; 27 28 /** 29 * Test activity that can verify whether the layout changes. Copied from 30 * androidx.window.TestActivity. 31 */ 32 public class TestActivity extends Activity implements View.OnLayoutChangeListener { 33 private CountDownLatch mLayoutLatch; 34 private static CountDownLatch sResumeLatch = new CountDownLatch(1); 35 36 @Override onCreate(@ullable Bundle savedInstanceState)37 public void onCreate(@Nullable Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 40 resetLayoutCounter(); 41 getWindow().getDecorView().addOnLayoutChangeListener(this); 42 } 43 44 @Override onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)45 public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, 46 int oldTop, int oldRight, int oldBottom) { 47 mLayoutLatch.countDown(); 48 } 49 50 @Override onResume()51 protected void onResume() { 52 super.onResume(); 53 sResumeLatch.countDown(); 54 } 55 56 /** 57 * Resets layout counter when waiting for a layout to happen before calling 58 * {@link #waitForLayout()}. 59 */ resetLayoutCounter()60 public void resetLayoutCounter() { 61 mLayoutLatch = new CountDownLatch(1); 62 } 63 64 /** 65 * Blocks and waits for the next layout to happen. {@link #resetLayoutCounter()} must be called 66 * before calling this method. 67 * @return {@code true} if the layout happened before the timeout count reached zero and 68 * {@code false} if the waiting time elapsed before the layout happened. 69 */ waitForLayout()70 public boolean waitForLayout() { 71 try { 72 return mLayoutLatch.await(3, TimeUnit.SECONDS); 73 } catch (InterruptedException e) { 74 return false; 75 } 76 } 77 78 /** 79 * Resets layout counter when waiting for a layout to happen before calling 80 * {@link #waitForOnResume()}. 81 */ resetResumeCounter()82 public static void resetResumeCounter() { 83 sResumeLatch = new CountDownLatch(1); 84 } 85 86 /** 87 * Same as {@link #waitForLayout()}, but waits for onResume() to be called for any activity of 88 * this class. This can be used to track activity re-creation. 89 * @return {@code true} if the onResume() happened before the timeout count reached zero and 90 * {@code false} if the waiting time elapsed before the onResume() happened. 91 */ waitForOnResume()92 public static boolean waitForOnResume() { 93 try { 94 return sResumeLatch.await(3, TimeUnit.SECONDS); 95 } catch (InterruptedException e) { 96 return false; 97 } 98 } 99 } 100