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 static android.autofillservice.cts.testcore.Helper.ID_PASSWORD; 19 import static android.autofillservice.cts.testcore.Helper.ID_PASSWORD_LABEL; 20 import static android.autofillservice.cts.testcore.Helper.ID_USERNAME; 21 import static android.autofillservice.cts.testcore.Helper.ID_USERNAME_LABEL; 22 23 import static com.google.common.truth.Truth.assertWithMessage; 24 25 import android.autofillservice.cts.R; 26 import android.autofillservice.cts.activities.VirtualContainerView.Line; 27 import android.autofillservice.cts.activities.VirtualContainerView.Line.OneTimeLineWatcher; 28 import android.graphics.Canvas; 29 import android.os.Bundle; 30 import android.text.InputType; 31 import android.widget.EditText; 32 33 /** 34 * A custom activity that uses {@link Canvas} to draw the following fields: 35 * 36 * <ul> 37 * <li>Username 38 * <li>Password 39 * </ul> 40 */ 41 public class VirtualContainerActivity extends AbstractAutoFillActivity { 42 43 public static final String BLANK_VALUE = " "; 44 public static final String INITIAL_URL_BAR_VALUE = "ftp://dev.null/4/8/15/16/23/42"; 45 46 // TODO: Make these private and expose getters. 47 public EditText mUrlBar; 48 public EditText mUrlBar2; 49 public VirtualContainerView mCustomView; 50 51 public Line mUsername; 52 public Line mPassword; 53 54 private FillExpectation mExpectation; 55 getVirtualViewHolder()56 public VirtualContainerView getVirtualViewHolder() { 57 return mCustomView; 58 } 59 getUsernameVirtualId()60 public int getUsernameVirtualId() { 61 return mUsername.text.id; 62 } 63 64 @Override onCreate(Bundle savedInstanceState)65 protected void onCreate(Bundle savedInstanceState) { 66 super.onCreate(savedInstanceState); 67 68 setContentView(R.layout.virtual_container_activity); 69 70 mUrlBar = findViewById(R.id.my_url_bar); 71 mUrlBar2 = findViewById(R.id.my_url_bar2); 72 mCustomView = findViewById(R.id.virtual_container_view); 73 74 mUrlBar.setText(INITIAL_URL_BAR_VALUE); 75 mUsername = mCustomView.addLine(ID_USERNAME_LABEL, "Username", ID_USERNAME, BLANK_VALUE, 76 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL); 77 mPassword = mCustomView.addLine(ID_PASSWORD_LABEL, "Password", ID_PASSWORD, BLANK_VALUE, 78 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 79 } 80 81 /** 82 * Triggers manual autofill in a given line. 83 */ requestAutofill(Line line)84 public void requestAutofill(Line line) { 85 getAutofillManager().requestAutofill(mCustomView, line.text.id, line.bounds); 86 } 87 88 /** 89 * Sets the expectation for an auto-fill request, so it can be asserted through 90 * {@link #assertAutoFilled()} later. 91 */ expectAutoFill(String username, String password)92 public void expectAutoFill(String username, String password) { 93 mExpectation = new FillExpectation(username, password); 94 mUsername.setTextChangedListener(mExpectation.ccUsernameWatcher); 95 mPassword.setTextChangedListener(mExpectation.ccPasswordWatcher); 96 } 97 98 /** 99 * Asserts the activity was auto-filled with the values passed to 100 * {@link #expectAutoFill(String, String)}. 101 */ assertAutoFilled()102 public void assertAutoFilled() throws Exception { 103 assertWithMessage("expectAutoFill() not called").that(mExpectation).isNotNull(); 104 mExpectation.ccUsernameWatcher.assertAutoFilled(); 105 mExpectation.ccPasswordWatcher.assertAutoFilled(); 106 } 107 108 /** 109 * Holder for the expected auto-fill values. 110 */ 111 private final class FillExpectation { 112 private final OneTimeLineWatcher ccUsernameWatcher; 113 private final OneTimeLineWatcher ccPasswordWatcher; 114 FillExpectation(String username, String password)115 private FillExpectation(String username, String password) { 116 ccUsernameWatcher = mUsername.new OneTimeLineWatcher(username); 117 ccPasswordWatcher = mPassword.new OneTimeLineWatcher(password); 118 } 119 } 120 } 121