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 
17 package android.autofillservice.cts;
18 
19 import android.os.Bundle;
20 import androidx.annotation.Nullable;
21 
22 import java.util.concurrent.CountDownLatch;
23 import java.util.concurrent.TimeUnit;
24 
25 /**
26  * Activity containing an fragment
27  */
28 public class FragmentContainerActivity extends AbstractAutoFillActivity {
29     static final String FRAGMENT_TAG =
30             FragmentContainerActivity.class.getName() + "#FRAGMENT_TAG";
31     private CountDownLatch mResumed = new CountDownLatch(1);
32     private CountDownLatch mStopped = new CountDownLatch(0);
33 
34     @Override
onCreate(@ullable Bundle savedInstanceState)35     protected void onCreate(@Nullable Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37 
38         setContentView(R.layout.fragment_container);
39 
40         // have to manually add fragment as we cannot remove it otherwise
41         getFragmentManager().beginTransaction().add(R.id.rootContainer,
42                 new FragmentWithEditText(), FRAGMENT_TAG).commitNow();
43     }
44 
45     @Override
onStart()46     protected void onStart() {
47         super.onStart();
48 
49         mStopped = new CountDownLatch(1);
50     }
51 
52     @Override
onResume()53     protected void onResume() {
54         super.onResume();
55 
56         mResumed.countDown();
57     }
58 
59     @Override
onPause()60     protected void onPause() {
61         super.onPause();
62 
63         mResumed = new CountDownLatch(1);
64     }
65 
66     @Override
onStop()67     protected void onStop() {
68         super.onStop();
69 
70         mStopped.countDown();
71     }
72 
waitUntilResumed()73     public boolean waitUntilResumed() throws InterruptedException {
74         return mResumed.await(Timeouts.UI_TIMEOUT.ms(), TimeUnit.MILLISECONDS);
75     }
76 
waitUntilStopped()77     public boolean waitUntilStopped() throws InterruptedException {
78         return mStopped.await(Timeouts.UI_TIMEOUT.ms(), TimeUnit.MILLISECONDS);
79     }
80 }
81