• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.View;
23 import android.view.autofill.AutofillManager;
24 import android.widget.Button;
25 import android.widget.EditText;
26 import android.widget.TextView;
27 
28 /**
29  * Simple activity that has an edit text and buttons to cancel or commit the autofill context.
30  */
31 public class SimpleSaveActivity extends AbstractAutoFillActivity {
32 
33     private static final String TAG = "AutofillSimpleSaveActivity";
34 
35     public static final String ID_LABEL = "label";
36     public static final String ID_INPUT = "input";
37     public static final String ID_PASSWORD = "password";
38     public static final String ID_COMMIT = "commit";
39     public static final String TEXT_LABEL = "Label:";
40 
41     private static SimpleSaveActivity sInstance;
42 
43     public TextView mLabel;
44     public EditText mInput;
45     public EditText mPassword;
46     public Button mCancel;
47     public Button mCommit;
48     public Button mInvisibleButton;
49 
50     private boolean mAutoCommit = true;
51     private boolean mClearFieldsOnSubmit = false;
52 
getInstance()53     public static SimpleSaveActivity getInstance() {
54         return sInstance;
55     }
56 
SimpleSaveActivity()57     public SimpleSaveActivity() {
58         sInstance = this;
59     }
60 
61     @Override
onCreate(Bundle savedInstanceState)62     protected void onCreate(Bundle savedInstanceState) {
63         super.onCreate(savedInstanceState);
64 
65         setContentView(R.layout.simple_save_activity);
66 
67         mLabel = findViewById(R.id.label);
68         mInput = findViewById(R.id.input);
69         mPassword = findViewById(R.id.password);
70         mCancel = findViewById(R.id.cancel);
71         mCommit = findViewById(R.id.commit);
72         mInvisibleButton = findViewById(R.id.make_views_invisible);
73 
74         mCancel.setOnClickListener((v) -> getAutofillManager().cancel());
75         mCommit.setOnClickListener((v) -> onCommit());
76         mInvisibleButton.setOnClickListener((v) -> makeEditTextViewsInvisible());
77     }
78 
79     @Override
onResume()80     protected void onResume() {
81         super.onResume();
82         Log.d(TAG, "onResume()");
83     }
84 
onCommit()85     private void onCommit() {
86         if (mClearFieldsOnSubmit) {
87             resetFields();
88         }
89         if (mAutoCommit) {
90             Log.d(TAG, "onCommit(): calling AFM.commit()");
91             getAutofillManager().commit();
92         } else {
93             Log.d(TAG, "onCommit(): NOT calling AFM.commit()");
94         }
95     }
96 
makeEditTextViewsInvisible()97     private void makeEditTextViewsInvisible() {
98         // Make the views invisible
99         Log.v(TAG, "makeEditTextViewsInvisible() onClick()");
100         mInput.setVisibility(View.INVISIBLE);
101         mPassword.setVisibility(View.INVISIBLE);
102         Log.v(TAG, "makeEditTextViewsInvisible() username and password views are invisible");
103     }
104 
resetFields()105     private void resetFields() {
106         Log.d(TAG, "resetFields()");
107         mInput.setText("");
108         mPassword.setText("");
109     }
110 
111     /**
112      * Defines whether the activity should automatically call {@link AutofillManager#commit()} when
113      * the commit button is tapped.
114      */
setAutoCommit(boolean flag)115     public void setAutoCommit(boolean flag) {
116         mAutoCommit = flag;
117     }
118 
119     /**
120      * Defines whether the activity should automatically clear its fields when submit is clicked.
121      */
setClearFieldsOnSubmit(boolean flag)122     public void setClearFieldsOnSubmit(boolean flag) {
123         mClearFieldsOnSubmit = flag;
124     }
125 
126     /**
127      * Set the EditText input or password value and wait until text change.
128      */
setTextAndWaitTextChange(String input, String password)129     public void setTextAndWaitTextChange(String input, String password) throws Exception {
130         FillExpectation changeExpectation = expectInputPasswordTextChange(input, password);
131         syncRunOnUiThread(() -> {
132             if (input != null) {
133                 mInput.setText(input);
134             }
135             if (password != null) {
136                 mPassword.setText(password);
137             }
138         });
139         changeExpectation.assertTextChange();
140     }
141 
expectAutoFill(String input)142     public FillExpectation expectAutoFill(String input) {
143         final FillExpectation expectation = new FillExpectation(input, null);
144         mInput.addTextChangedListener(expectation.mInputWatcher);
145         return expectation;
146     }
147 
expectInputPasswordTextChange(String input, String password)148     public FillExpectation expectInputPasswordTextChange(String input, String password) {
149         final FillExpectation expectation = new FillExpectation(input, password);
150         if (expectation.mInputWatcher != null) {
151             mInput.addTextChangedListener(expectation.mInputWatcher);
152         }
153         if (expectation.mPasswordWatcher != null) {
154             mPassword.addTextChangedListener(expectation.mPasswordWatcher);
155         }
156         return expectation;
157     }
158 
expectAutoFill(String input, String password)159     public FillExpectation expectAutoFill(String input, String password) {
160         return expectInputPasswordTextChange(input, password);
161     }
162 
getInput()163     public EditText getInput() {
164         return mInput;
165     }
166 
167     public final class FillExpectation {
168         private final OneTimeTextWatcher mInputWatcher;
169         private final OneTimeTextWatcher mPasswordWatcher;
170 
FillExpectation(String input, String password)171         private FillExpectation(String input, String password) {
172             mInputWatcher = input == null ? null : new OneTimeTextWatcher("input", mInput, input);
173             mPasswordWatcher =
174                     password == null
175                             ? null
176                             : new OneTimeTextWatcher("password", mPassword, password);
177         }
178 
assertTextChange()179         public void assertTextChange() throws Exception {
180             assertAutoFilled();
181         }
182 
assertAutoFilled()183         public void assertAutoFilled() throws Exception {
184             if (mInputWatcher != null) {
185                 mInputWatcher.assertAutoFilled();
186             }
187             if (mPasswordWatcher != null) {
188                 mPasswordWatcher.assertAutoFilled();
189             }
190         }
191     }
192 }
193