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 
17 package android.autofillservice.cts.activities;
18 
19 import static android.autofillservice.cts.testcore.Helper.findViewByAutofillHint;
20 import static android.view.View.IMPORTANT_FOR_AUTOFILL_AUTO;
21 import static android.view.View.IMPORTANT_FOR_AUTOFILL_NO;
22 import static android.view.View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS;
23 import static android.view.View.IMPORTANT_FOR_AUTOFILL_YES;
24 import static android.view.View.IMPORTANT_FOR_AUTOFILL_YES_EXCLUDE_DESCENDANTS;
25 
26 import static com.google.common.truth.Truth.assertThat;
27 
28 import android.autofillservice.cts.R;
29 import android.autofillservice.cts.testcore.Visitor;
30 import android.content.Context;
31 import android.os.Bundle;
32 import android.util.AttributeSet;
33 import android.view.View;
34 import android.widget.EditText;
35 import android.widget.ImageView;
36 import android.widget.LinearLayout;
37 
38 /**
39  * An activity containing mostly widgets that should be removed from an auto-fill structure to
40  * optimize it.
41  */
42 public class FatActivity extends AbstractAutoFillActivity {
43 
44     public static final String ID_CAPTCHA = "captcha";
45     public static final String ID_INPUT = "input";
46     public static final String ID_INPUT_CONTAINER = "input_container";
47     public static final String ID_IMAGE = "image";
48     public static final String ID_IMPORTANT_IMAGE = "important_image";
49     public static final String ID_ROOT = "root";
50 
51     public static final String ID_NOT_IMPORTANT_CONTAINER_EXCLUDING_DESCENDANTS =
52             "not_important_container_excluding_descendants";
53     public static final String ID_NOT_IMPORTANT_CONTAINER_EXCLUDING_DESCENDANTS_CHILD =
54             "not_important_container_excluding_descendants_child";
55     public static final String ID_NOT_IMPORTANT_CONTAINER_EXCLUDING_DESCENDANTS_GRAND_CHILD =
56             "not_important_container_excluding_descendants_grand_child";
57 
58     public static final String ID_IMPORTANT_CONTAINER_EXCLUDING_DESCENDANTS =
59             "important_container_excluding_descendants";
60     public static final String ID_IMPORTANT_CONTAINER_EXCLUDING_DESCENDANTS_CHILD =
61             "important_container_excluding_descendants_child";
62     public static final String ID_IMPORTANT_CONTAINER_EXCLUDING_DESCENDANTS_GRAND_CHILD =
63             "important_container_excluding_descendants_grand_child";
64 
65     public static final String ID_NOT_IMPORTANT_CONTAINER_MIXED_DESCENDANTS =
66             "not_important_container_mixed_descendants";
67     public static final String ID_NOT_IMPORTANT_CONTAINER_MIXED_DESCENDANTS_CHILD =
68             "not_important_container_mixed_descendants_child";
69     public static final String ID_NOT_IMPORTANT_CONTAINER_MIXED_DESCENDANTS_GRAND_CHILD =
70             "not_important_container_mixed_descendants_grand_child";
71 
72     private LinearLayout mRoot;
73     private EditText mCaptcha;
74     private EditText mInput;
75     private ImageView mImage;
76     private ImageView mImportantImage;
77 
78     private View mNotImportantContainerExcludingDescendants;
79     private View mNotImportantContainerExcludingDescendantsChild;
80     private View mNotImportantContainerExcludingDescendantsGrandChild;
81 
82     private View mImportantContainerExcludingDescendants;
83     private View mImportantContainerExcludingDescendantsChild;
84     private View mImportantContainerExcludingDescendantsGrandChild;
85 
86     private View mNotImportantContainerMixedDescendants;
87     private View mNotImportantContainerMixedDescendantsChild;
88     private View mNotImportantContainerMixedDescendantsGrandChild;
89 
90     private MyView mViewWithAutofillHints;
91 
92     @Override
onCreate(Bundle savedInstanceState)93     protected void onCreate(Bundle savedInstanceState) {
94         super.onCreate(savedInstanceState);
95 
96         setContentView(R.layout.fat_activity);
97 
98         mRoot = findViewById(R.id.root);
99         mCaptcha = findViewById(R.id.captcha);
100         mInput = findViewById(R.id.input);
101         mImage = findViewById(R.id.image);
102         mImportantImage = findViewById(R.id.important_image);
103 
104         mNotImportantContainerExcludingDescendants = findViewById(
105                 R.id.not_important_container_excluding_descendants);
106         mNotImportantContainerExcludingDescendantsChild = findViewById(
107                 R.id.not_important_container_excluding_descendants_child);
108         mNotImportantContainerExcludingDescendantsGrandChild = findViewById(
109                 R.id.not_important_container_excluding_descendants_grand_child);
110 
111         mImportantContainerExcludingDescendants = findViewById(
112                 R.id.important_container_excluding_descendants);
113         mImportantContainerExcludingDescendantsChild = findViewById(
114                 R.id.important_container_excluding_descendants_child);
115         mImportantContainerExcludingDescendantsGrandChild = findViewById(
116                 R.id.important_container_excluding_descendants_grand_child);
117 
118         mNotImportantContainerMixedDescendants = findViewById(
119                 R.id.not_important_container_mixed_descendants);
120         mNotImportantContainerMixedDescendantsChild = findViewById(
121                 R.id.not_important_container_mixed_descendants_child);
122         mNotImportantContainerMixedDescendantsGrandChild = findViewById(
123                 R.id.not_important_container_mixed_descendants_grand_child);
124 
125         mViewWithAutofillHints = (MyView) findViewByAutofillHint(this, "importantAmI");
126         assertThat(mViewWithAutofillHints).isNotNull();
127 
128         // Validation check for importantForAutofill modes
129         assertThat(mRoot.getImportantForAutofill()).isEqualTo(IMPORTANT_FOR_AUTOFILL_AUTO);
130         assertThat(mInput.getImportantForAutofill()).isEqualTo(IMPORTANT_FOR_AUTOFILL_YES);
131         assertThat(mCaptcha.getImportantForAutofill()).isEqualTo(IMPORTANT_FOR_AUTOFILL_NO);
132         assertThat(mImage.getImportantForAutofill()).isEqualTo(IMPORTANT_FOR_AUTOFILL_NO);
133         assertThat(mImportantImage.getImportantForAutofill()).isEqualTo(IMPORTANT_FOR_AUTOFILL_YES);
134 
135         assertThat(mNotImportantContainerExcludingDescendants.getImportantForAutofill())
136                 .isEqualTo(IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
137         assertThat(mNotImportantContainerExcludingDescendantsChild.getImportantForAutofill())
138                 .isEqualTo(IMPORTANT_FOR_AUTOFILL_YES);
139         assertThat(mNotImportantContainerExcludingDescendantsGrandChild.getImportantForAutofill())
140                 .isEqualTo(IMPORTANT_FOR_AUTOFILL_AUTO);
141 
142         assertThat(mImportantContainerExcludingDescendants.getImportantForAutofill())
143                 .isEqualTo(IMPORTANT_FOR_AUTOFILL_YES_EXCLUDE_DESCENDANTS);
144         assertThat(mImportantContainerExcludingDescendantsChild.getImportantForAutofill())
145                 .isEqualTo(IMPORTANT_FOR_AUTOFILL_YES);
146         assertThat(mImportantContainerExcludingDescendantsGrandChild.getImportantForAutofill())
147                 .isEqualTo(IMPORTANT_FOR_AUTOFILL_AUTO);
148 
149         assertThat(mNotImportantContainerMixedDescendants.getImportantForAutofill())
150                 .isEqualTo(IMPORTANT_FOR_AUTOFILL_NO);
151         assertThat(mNotImportantContainerMixedDescendantsChild.getImportantForAutofill())
152                 .isEqualTo(IMPORTANT_FOR_AUTOFILL_YES);
153         assertThat(mNotImportantContainerMixedDescendantsGrandChild.getImportantForAutofill())
154                 .isEqualTo(IMPORTANT_FOR_AUTOFILL_NO);
155 
156         assertThat(mViewWithAutofillHints.getImportantForAutofill())
157                 .isEqualTo(IMPORTANT_FOR_AUTOFILL_AUTO);
158         assertThat(mViewWithAutofillHints.isImportantForAutofill()).isTrue();
159     }
160 
161     /**
162      * Visits the {@code input} in the UiThread.
163      */
onInput(Visitor<EditText> v)164     public void onInput(Visitor<EditText> v) {
165         syncRunOnUiThread(() -> {
166             v.visit(mInput);
167         });
168     }
169 
170     /**
171      * Custom view that defines an autofill type so autofill hints are set on {@code ViewNode}.
172      */
173     public static class MyView extends View {
MyView(Context context, AttributeSet attrs)174         public MyView(Context context, AttributeSet attrs) {
175             super(context, attrs);
176         }
177 
178         @Override
getAutofillType()179         public int getAutofillType() {
180             return AUTOFILL_TYPE_TEXT;
181         }
182     }
183 }
184