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 108 /** 109 * Set the EditText input or password value and wait until text change. 110 */ setTextAndWaitTextChange(String input, String password)111 public void setTextAndWaitTextChange(String input, String password) throws Exception { 112 FillExpectation changeExpectation = expectInputPasswordTextChange(input, password); 113 syncRunOnUiThread(() -> { 114 if (input != null) { 115 mInput.setText(input); 116 } 117 if (password != null) { 118 mPassword.setText(password); 119 } 120 }); 121 changeExpectation.assertTextChange(); 122 } 123 expectAutoFill(String input)124 public FillExpectation expectAutoFill(String input) { 125 final FillExpectation expectation = new FillExpectation(input, null); 126 mInput.addTextChangedListener(expectation.mInputWatcher); 127 return expectation; 128 } 129 expectInputPasswordTextChange(String input, String password)130 public FillExpectation expectInputPasswordTextChange(String input, String password) { 131 final FillExpectation expectation = new FillExpectation(input, password); 132 if (expectation.mInputWatcher != null) { 133 mInput.addTextChangedListener(expectation.mInputWatcher); 134 } 135 if (expectation.mPasswordWatcher != null) { 136 mPassword.addTextChangedListener(expectation.mPasswordWatcher); 137 } 138 return expectation; 139 } 140 expectAutoFill(String input, String password)141 public FillExpectation expectAutoFill(String input, String password) { 142 return expectInputPasswordTextChange(input, password); 143 } 144 getInput()145 public EditText getInput() { 146 return mInput; 147 } 148 149 public final class FillExpectation { 150 private final OneTimeTextWatcher mInputWatcher; 151 private final OneTimeTextWatcher mPasswordWatcher; 152 FillExpectation(String input, String password)153 private FillExpectation(String input, String password) { 154 mInputWatcher = input == null 155 ? null 156 : new OneTimeTextWatcher("input", mInput, input); 157 mPasswordWatcher = password == null 158 ? null 159 : new OneTimeTextWatcher("password", mPassword, password); 160 } 161 assertTextChange()162 public void assertTextChange() throws Exception { 163 assertAutoFilled(); 164 } 165 assertAutoFilled()166 public void assertAutoFilled() throws Exception { 167 if (mInputWatcher != null) { 168 mInputWatcher.assertAutoFilled(); 169 } 170 if (mPasswordWatcher != null) { 171 mPasswordWatcher.assertAutoFilled(); 172 } 173 } 174 } 175 } 176