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 package android.autofillservice.cts;
     17 
     18 import android.os.Bundle;
     19 import android.util.Log;
     20 import android.view.autofill.AutofillManager;
     21 import android.widget.Button;
     22 import android.widget.EditText;
     23 import android.widget.TextView;
     24 
     25 /**
     26  * Simple activity that has an edit text and buttons to cancel or commit the autofill context.
     27  */
     28 public class SimpleSaveActivity extends AbstractAutoFillActivity {
     29 
     30     private static final String TAG = "SimpleSaveActivity";
     31 
     32     public static final String ID_LABEL = "label";
     33     public static final String ID_INPUT = "input";
     34     public static final String ID_PASSWORD = "password";
     35     public static final String ID_COMMIT = "commit";
     36     public static final String TEXT_LABEL = "Label:";
     37 
     38     private static SimpleSaveActivity sInstance;
     39 
     40     TextView mLabel;
     41     EditText mInput;
     42     EditText mPassword;
     43     Button mCancel;
     44     Button mCommit;
     45 
     46     private boolean mAutoCommit = true;
     47     private boolean mClearFieldsOnSubmit = false;
     48 
     49     public static SimpleSaveActivity getInstance() {
     50         return sInstance;
     51     }
     52 
     53     public SimpleSaveActivity() {
     54         sInstance = this;
     55     }
     56 
     57     @Override
     58     protected void onCreate(Bundle savedInstanceState) {
     59         super.onCreate(savedInstanceState);
     60 
     61         setContentView(R.layout.simple_save_activity);
     62 
     63         mLabel = findViewById(R.id.label);
     64         mInput = findViewById(R.id.input);
     65         mPassword = findViewById(R.id.password);
     66         mCancel = findViewById(R.id.cancel);
     67         mCommit = findViewById(R.id.commit);
     68 
     69         mCancel.setOnClickListener((v) -> getAutofillManager().cancel());
     70         mCommit.setOnClickListener((v) -> onCommit());
     71     }
     72 
     73     private void onCommit() {
     74         if (mClearFieldsOnSubmit) {
     75             resetFields();
     76         }
     77         if (mAutoCommit) {
     78             Log.d(TAG, "onCommit(): calling AFM.commit()");
     79             getAutofillManager().commit();
     80         } else {
     81             Log.d(TAG, "onCommit(): NOT calling AFM.commit()");
     82         }
     83     }
     84 
     85     private void resetFields() {
     86         Log.d(TAG, "resetFields()");
     87         mInput.setText("");
     88         mPassword.setText("");
     89     }
     90 
     91     /**
     92      * Defines whether the activity should automatically call {@link AutofillManager#commit()} when
     93      * the commit button is tapped.
     94      */
     95     void setAutoCommit(boolean flag) {
     96         mAutoCommit = flag;
     97     }
     98 
     99     /**
    100      * Defines whether the activity should automatically clear its fields when submit is clicked.
    101      */
    102     void setClearFieldsOnSubmit(boolean flag) {
    103         mClearFieldsOnSubmit = flag;
    104     }
    105 
    106     public FillExpectation expectAutoFill(String input) {
    107         final FillExpectation expectation = new FillExpectation(input, null);
    108         mInput.addTextChangedListener(expectation.mInputWatcher);
    109         return expectation;
    110     }
    111 
    112     public FillExpectation expectAutoFill(String input, String password) {
    113         final FillExpectation expectation = new FillExpectation(input, password);
    114         mInput.addTextChangedListener(expectation.mInputWatcher);
    115         mPassword.addTextChangedListener(expectation.mPasswordWatcher);
    116         return expectation;
    117     }
    118 
    119     public EditText getInput() {
    120         return mInput;
    121     }
    122 
    123     public final class FillExpectation {
    124         private final OneTimeTextWatcher mInputWatcher;
    125         private final OneTimeTextWatcher mPasswordWatcher;
    126 
    127         private FillExpectation(String input, String password) {
    128             mInputWatcher = new OneTimeTextWatcher("input", mInput, input);
    129             mPasswordWatcher = password == null
    130                     ? null
    131                     : new OneTimeTextWatcher("password", mPassword, password);
    132         }
    133 
    134         public void assertAutoFilled() throws Exception {
    135             mInputWatcher.assertAutoFilled();
    136             if (mPasswordWatcher != null) {
    137                 mPasswordWatcher.assertAutoFilled();
    138             }
    139         }
    140     }
    141 }
    142