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