Home | History | Annotate | Download | only in cts
      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 android.widget.FrameLayout;
     21 
     22 import androidx.annotation.Nullable;
     23 
     24 import java.util.concurrent.CountDownLatch;
     25 import java.util.concurrent.TimeUnit;
     26 
     27 /**
     28  * Activity containing an fragment
     29  */
     30 public class FragmentContainerActivity extends AbstractAutoFillActivity {
     31     static final String FRAGMENT_TAG =
     32             FragmentContainerActivity.class.getName() + "#FRAGMENT_TAG";
     33     private CountDownLatch mResumed = new CountDownLatch(1);
     34     private CountDownLatch mStopped = new CountDownLatch(0);
     35     private FrameLayout mRootContainer;
     36 
     37     @Override
     38     protected void onCreate(@Nullable Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40 
     41         setContentView(R.layout.fragment_container);
     42 
     43         mRootContainer = findViewById(R.id.rootContainer);
     44 
     45         // have to manually add fragment as we cannot remove it otherwise
     46         getFragmentManager().beginTransaction().add(R.id.rootContainer,
     47                 new FragmentWithEditText(), FRAGMENT_TAG).commitNow();
     48     }
     49 
     50     @Override
     51     protected void onStart() {
     52         super.onStart();
     53 
     54         mStopped = new CountDownLatch(1);
     55     }
     56 
     57     @Override
     58     protected void onResume() {
     59         super.onResume();
     60 
     61         mResumed.countDown();
     62     }
     63 
     64     @Override
     65     protected void onPause() {
     66         super.onPause();
     67 
     68         mResumed = new CountDownLatch(1);
     69     }
     70 
     71     @Override
     72     protected void onStop() {
     73         super.onStop();
     74 
     75         mStopped.countDown();
     76     }
     77 
     78     /**
     79      * Sets whether the root container is focusable or not.
     80      *
     81      * <p>It's initially set as {@code trye} in the XML layout so autofill is not automatically
     82      * triggered in the edit text before the service is prepared to handle it.
     83      */
     84     public void setRootContainerFocusable(boolean focusable) {
     85         mRootContainer.setFocusable(focusable);
     86         mRootContainer.setFocusableInTouchMode(focusable);
     87     }
     88 
     89     public boolean waitUntilResumed() throws InterruptedException {
     90         return mResumed.await(Timeouts.UI_TIMEOUT.ms(), TimeUnit.MILLISECONDS);
     91     }
     92 
     93     public boolean waitUntilStopped() throws InterruptedException {
     94         return mStopped.await(Timeouts.UI_TIMEOUT.ms(), TimeUnit.MILLISECONDS);
     95     }
     96 }
     97