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