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 static android.autofillservice.cts.Timeouts.FILL_TIMEOUT;
     20 
     21 import static com.google.common.truth.Truth.assertWithMessage;
     22 
     23 import android.text.Editable;
     24 import android.text.TextWatcher;
     25 import android.util.Log;
     26 import android.widget.EditText;
     27 
     28 import java.util.concurrent.CountDownLatch;
     29 import java.util.concurrent.TimeUnit;
     30 
     31 /**
     32  * Custom {@link TextWatcher} used to assert a {@link EditText} was set multiple times.
     33  */
     34 class MultipleTimesTextWatcher implements TextWatcher {
     35     private static final String TAG = "MultipleTimesTextWatcher";
     36 
     37     private final String mName;
     38     private final CountDownLatch mLatch;
     39     private final EditText mEditText;
     40     private final CharSequence mExpected;
     41 
     42     MultipleTimesTextWatcher(String name, int times, EditText editText,
     43             CharSequence expectedAutofillValue) {
     44         this.mName = name;
     45         this.mEditText = editText;
     46         this.mExpected = expectedAutofillValue;
     47         this.mLatch = new CountDownLatch(times);
     48     }
     49 
     50     @Override
     51     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
     52     }
     53 
     54     @Override
     55     public void onTextChanged(CharSequence s, int start, int before, int count) {
     56         Log.v(TAG, "onTextChanged(" + mLatch.getCount() + "): " + mName + " = " + s);
     57         mLatch.countDown();
     58     }
     59 
     60     @Override
     61     public void afterTextChanged(Editable s) {
     62     }
     63 
     64     void assertAutoFilled() throws Exception {
     65         final boolean set = mLatch.await(FILL_TIMEOUT.ms(), TimeUnit.MILLISECONDS);
     66         if (!set) {
     67             throw new RetryableException(FILL_TIMEOUT, "Timeout (%s ms) on EditText %s",
     68                     FILL_TIMEOUT.ms(), mName);
     69         }
     70         final String actual = mEditText.getText().toString();
     71         assertWithMessage("Wrong auto-fill value on EditText %s", mName)
     72                 .that(actual).isEqualTo(mExpected.toString());
     73     }
     74 }
     75