kailianc | 52f9121 | 2022-01-17 23:10:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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.previewview.utils; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.os.Bundle; |
| 21 | |
| 22 | import androidx.annotation.NonNull; |
| 23 | import androidx.annotation.Nullable; |
| 24 | import androidx.annotation.RequiresApi; |
| 25 | import androidx.test.espresso.idling.CountingIdlingResource; |
| 26 | |
| 27 | /** An empty activity that checks the activity's <em>main</em> window currently has window focus. */ |
| 28 | @RequiresApi(21) // TODO(b/200306659): Remove and replace with annotation on package-info.java |
| 29 | public class ForegroundTestActivity extends Activity { |
| 30 | |
| 31 | final CountingIdlingResource mViewReady = new CountingIdlingResource("ViewReady"); |
| 32 | |
| 33 | @NonNull |
| 34 | public CountingIdlingResource getViewReadyIdlingResource() { |
| 35 | return mViewReady; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 40 | super.onCreate(savedInstanceState); |
| 41 | mViewReady.increment(); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void onWindowFocusChanged(boolean hasFocus) { |
| 46 | super.onWindowFocusChanged(hasFocus); |
| 47 | if (hasFocus && !mViewReady.isIdleNow()) { |
| 48 | mViewReady.decrement(); |
| 49 | } |
| 50 | } |
| 51 | } |