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