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 
75     @Override
onResume()76     protected void onResume() {
77         super.onResume();
78         Log.d(TAG, "onResume()");
79     }
80 
onCommit()81     private void onCommit() {
82         if (mClearFieldsOnSubmit) {
83             resetFields();
84         }
85         if (mAutoCommit) {
86             Log.d(TAG, "onCommit(): calling AFM.commit()");
87             getAutofillManager().commit();
88         } else {
89             Log.d(TAG, "onCommit(): NOT calling AFM.commit()");
90         }
91     }
92 
resetFields()93     private void resetFields() {
94         Log.d(TAG, "resetFields()");
95         mInput.setText("");
96         mPassword.setText("");
97     }
98 
99     /**
100      * Defines whether the activity should automatically call {@link AutofillManager#commit()} when
101      * the commit button is tapped.
102      */
setAutoCommit(boolean flag)103     public void setAutoCommit(boolean flag) {
104         mAutoCommit = flag;
105     }
106 
107     /**
108      * Defines whether the activity should automatically clear its fields when submit is clicked.
109      */
setClearFieldsOnSubmit(boolean flag)110     public void setClearFieldsOnSubmit(boolean flag) {
111         mClearFieldsOnSubmit = flag;
112     }
113 
114     /**
115      * Set the EditText input or password value and wait until text change.
116      */
setTextAndWaitTextChange(String input, String password)117     public void setTextAndWaitTextChange(String input, String password) throws Exception {
118         FillExpectation changeExpectation = expectInputPasswordTextChange(input, password);
119         syncRunOnUiThread(() -> {
120             if (input != null) {
121                 mInput.setText(input);
122             }
123             if (password != null) {
124                 mPassword.setText(password);
125             }
126         });
127         changeExpectation.assertTextChange();
128     }
129 
expectAutoFill(String input)130     public FillExpectation expectAutoFill(String input) {
131         final FillExpectation expectation = new FillExpectation(input, null);
132         mInput.addTextChangedListener(expectation.mInputWatcher);
133         return expectation;
134     }
135 
expectInputPasswordTextChange(String input, String password)136     public FillExpectation expectInputPasswordTextChange(String input, String password) {
137         final FillExpectation expectation = new FillExpectation(input, password);
138         if (expectation.mInputWatcher != null) {
139             mInput.addTextChangedListener(expectation.mInputWatcher);
140         }
141         if (expectation.mPasswordWatcher != null) {
142             mPassword.addTextChangedListener(expectation.mPasswordWatcher);
143         }
144         return expectation;
145     }
146 
expectAutoFill(String input, String password)147     public FillExpectation expectAutoFill(String input, String password) {
148         return expectInputPasswordTextChange(input, password);
149     }
150 
getInput()151     public EditText getInput() {
152         return mInput;
153     }
154 
155     public final class FillExpectation {
156         private final OneTimeTextWatcher mInputWatcher;
157         private final OneTimeTextWatcher mPasswordWatcher;
158 
FillExpectation(String input, String password)159         private FillExpectation(String input, String password) {
160             mInputWatcher = input == null ? null : new OneTimeTextWatcher("input", mInput, input);
161             mPasswordWatcher =
162                     password == null
163                             ? null
164                             : new OneTimeTextWatcher("password", mPassword, password);
165         }
166 
assertTextChange()167         public void assertTextChange() throws Exception {
168             assertAutoFilled();
169         }
170 
assertAutoFilled()171         public void assertAutoFilled() throws Exception {
172             if (mInputWatcher != null) {
173                 mInputWatcher.assertAutoFilled();
174             }
175             if (mPasswordWatcher != null) {
176                 mPasswordWatcher.assertAutoFilled();
177             }
178         }
179     }
180 }
181