1 /* 2 * Copyright 2023 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.camera.testing.impl.activity; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 22 import androidx.test.espresso.idling.CountingIdlingResource; 23 24 import org.jspecify.annotations.NonNull; 25 import org.jspecify.annotations.Nullable; 26 27 /** An empty activity that checks the activity's <em>main</em> window currently has window focus. */ 28 public class ForegroundTestActivity extends Activity { 29 30 final CountingIdlingResource mViewReady = new CountingIdlingResource("ViewReady"); 31 getViewReadyIdlingResource()32 public @NonNull CountingIdlingResource getViewReadyIdlingResource() { 33 return mViewReady; 34 } 35 36 @Override onCreate(@ullable Bundle savedInstanceState)37 protected void onCreate(@Nullable Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 mViewReady.increment(); 40 } 41 42 @Override onWindowFocusChanged(boolean hasFocus)43 public void onWindowFocusChanged(boolean hasFocus) { 44 super.onWindowFocusChanged(hasFocus); 45 if (hasFocus && !mViewReady.isIdleNow()) { 46 mViewReady.decrement(); 47 } 48 } 49 } 50