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.content.Intent;
21 import android.os.Bundle;
22 import android.widget.Button;
23 import android.widget.EditText;
24 import android.widget.TextView;
25 
26 /**
27  * A simple activity that upon submission launches {@link SimpleSaveActivity}.
28  */
29 public class PreSimpleSaveActivity extends AbstractAutoFillActivity {
30 
31     public static final String ID_PRE_LABEL = "preLabel";
32     public static final String ID_PRE_INPUT = "preInput";
33 
34     private static PreSimpleSaveActivity sInstance;
35 
36     public TextView mPreLabel;
37     public EditText mPreInput;
38     public Button mSubmit;
39 
getInstance()40     public static PreSimpleSaveActivity getInstance() {
41         return sInstance;
42     }
43 
PreSimpleSaveActivity()44     public PreSimpleSaveActivity() {
45         sInstance = this;
46     }
47 
48     @Override
onCreate(Bundle savedInstanceState)49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51 
52         setContentView(R.layout.pre_simple_save_activity);
53 
54         mPreLabel = findViewById(R.id.preLabel);
55         mPreInput = findViewById(R.id.preInput);
56         mSubmit = findViewById(R.id.submit);
57 
58         mSubmit.setOnClickListener((v) -> {
59             finish();
60             startActivity(new Intent(this, SimpleSaveActivity.class));
61         });
62     }
63 
64     /**
65      * Set the EditText input or password value and wait until text change.
66      */
setTextAndWaitTextChange(String input)67     public void setTextAndWaitTextChange(String input) throws Exception {
68         final FillExpectation expectation = expectInputTextChange(input);
69         syncRunOnUiThread(() -> mPreInput.setText(input));
70         expectation.assertTextChange();
71     }
72 
expectInputTextChange(String input)73     public FillExpectation expectInputTextChange(String input) {
74         final FillExpectation expectation = new FillExpectation(input);
75         mPreInput.addTextChangedListener(expectation.mInputWatcher);
76         return expectation;
77     }
78 
expectAutoFill(String input)79     public FillExpectation expectAutoFill(String input) {
80         return expectInputTextChange(input);
81     }
82 
getPreInput()83     public EditText getPreInput() {
84         return mPreInput;
85     }
86 
87     public final class FillExpectation {
88         private final OneTimeTextWatcher mInputWatcher;
89 
FillExpectation(String input)90         private FillExpectation(String input) {
91             mInputWatcher = new OneTimeTextWatcher("input", mPreInput, input);
92         }
93 
assertTextChange()94         public void assertTextChange() throws Exception {
95             mInputWatcher.assertAutoFilled();
96         }
97 
assertAutoFilled()98         public void assertAutoFilled() throws Exception {
99             mInputWatcher.assertAutoFilled();
100         }
101     }
102 }
103