Skip to content

Commit 863d6aa

Browse files
leticiarossidrchen
authored andcommitted
[TextInputLayout][a11y] Added more a11y tests for text fields and exposed dropdown menu. Also updated robolectric version
PiperOrigin-RevId: 441190540
1 parent b6b895a commit 863d6aa

File tree

4 files changed

+151
-5
lines changed

4 files changed

+151
-5
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ ext {
4646
espressoVersion = '3.1.0'
4747
mockitoCoreVersion = '2.25.0'
4848
truthVersion = '0.45'
49-
robolectricVersion = '4.7.3'
49+
robolectricVersion = '4.8-alpha-1'
5050

5151
// Enforce the use of prebuilt dependencies in all sub-projects. This is
5252
// required for the doclava dependency.

lib/javatests/com/google/android/material/textfield/ExposedDropdownMenuAccessibilityTest.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import android.content.Context;
2626
import androidx.appcompat.app.AppCompatActivity;
2727
import android.view.View;
28+
import android.view.ViewGroup;
2829
import android.view.accessibility.AccessibilityManager;
2930
import androidx.test.core.app.ApplicationProvider;
3031
import org.junit.Before;
@@ -54,25 +55,55 @@ public void themeApplicationContext() {
5455

5556
private void inflateLayout() {
5657
View inflated = activity.getLayoutInflater().inflate(R.layout.test_exposed_dropdown_menu, null);
58+
((ViewGroup) activity.findViewById(android.R.id.content)).addView(inflated);
5759
dropdownEditable = inflated.findViewById(R.id.dropdown_editable);
5860
dropdownNonEditable = inflated.findViewById(R.id.dropdown_noneditable);
5961
}
6062

6163
@Test
62-
public void testEndIconInNonEditableMenu_inA11yMode_notImportantForA11y() {
63-
accessibilityManager.setTouchExplorationEnabled(true);
64+
public void testEndIconInNonEditableMenu_isImportantForA11y() {
65+
accessibilityManager.setEnabled(true);
66+
67+
inflateLayout();
68+
69+
assertThat(dropdownNonEditable.getEndIconView().isImportantForAccessibility(), is(true));
70+
}
71+
72+
@Test
73+
public void testEndIconInEditableMenu_isImportantForA11y() {
74+
accessibilityManager.setEnabled(true);
6475

6576
inflateLayout();
6677

67-
assertThat(dropdownNonEditable.getEndIconView().isImportantForAccessibility(), is(false));
78+
assertThat(dropdownEditable.getEndIconView().isImportantForAccessibility(), is(true));;
6879
}
6980

7081
@Test
71-
public void testEndIconInEditableMenu_inA11yMode_isImportantForA11y() {
82+
public void testEndIconInNonEditableMenu_inTouchExplorationMode_notImportantForA11y() {
83+
inflateLayout();
84+
7285
accessibilityManager.setTouchExplorationEnabled(true);
86+
boolean importantForA11yInTouchExplMode =
87+
dropdownNonEditable.getEndIconView().isImportantForAccessibility();
88+
accessibilityManager.setTouchExplorationEnabled(false);
89+
accessibilityManager.setEnabled(false);
90+
91+
// Assert it was false in touch exploration mode.
92+
assertThat(importantForA11yInTouchExplMode, is(false));
93+
// Assert it switches back to true.
94+
assertThat(dropdownNonEditable.getEndIconView().isImportantForAccessibility(), is(true));
95+
}
7396

97+
@Test
98+
public void testEndIconInEditableMenu_inTouchExplorationMode_alwaysImportantForA11y() {
7499
inflateLayout();
75100

101+
accessibilityManager.setTouchExplorationEnabled(true);
102+
boolean importantForA11yInTouchExplMode =
103+
dropdownEditable.getEndIconView().isImportantForAccessibility();
104+
accessibilityManager.setTouchExplorationEnabled(false);
105+
106+
assertThat(importantForA11yInTouchExplMode, is(true));
76107
assertThat(dropdownEditable.getEndIconView().isImportantForAccessibility(), is(true));
77108
}
78109
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (C) 2022 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 com.google.android.material.textfield;
18+
19+
import com.google.android.material.R;
20+
21+
import static com.google.android.material.textfield.TextInputLayout.END_ICON_CUSTOM;
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
import static org.hamcrest.core.Is.is;
24+
25+
import android.graphics.Color;
26+
import android.graphics.drawable.ColorDrawable;
27+
import androidx.appcompat.app.AppCompatActivity;
28+
import android.view.View;
29+
import android.view.View.OnClickListener;
30+
import android.view.ViewGroup;
31+
import androidx.test.core.app.ApplicationProvider;
32+
import org.junit.Before;
33+
import org.junit.Test;
34+
import org.junit.runner.RunWith;
35+
import org.robolectric.Robolectric;
36+
import org.robolectric.RobolectricTestRunner;
37+
38+
@RunWith(RobolectricTestRunner.class)
39+
public final class TextInputLayoutAccessibilityTest {
40+
41+
private TextInputLayout textInputLayout;
42+
private final OnClickListener onClickListener =
43+
new OnClickListener() {
44+
@Override
45+
public void onClick(View v) {
46+
/* Do something */
47+
}
48+
};
49+
50+
@Before
51+
public void themeApplicationContext() {
52+
ApplicationProvider.getApplicationContext()
53+
.setTheme(R.style.Theme_Material3_DayNight_NoActionBar);
54+
AppCompatActivity activity = Robolectric.buildActivity(AppCompatActivity.class).setup().get();
55+
View inflated = activity.getLayoutInflater().inflate(R.layout.test_text_input_layout, null);
56+
((ViewGroup) activity.findViewById(android.R.id.content)).addView(inflated);
57+
textInputLayout = inflated.findViewById(R.id.text_input_layout);
58+
}
59+
60+
@Test
61+
public void testPasswordEndIcon_importantForA11y() {
62+
textInputLayout.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);
63+
64+
assertThat(textInputLayout.getEndIconView().isImportantForAccessibility(), is(true));
65+
}
66+
67+
@Test
68+
public void testClearTextEndIcon_importantForA11y() {
69+
textInputLayout.setEndIconMode(TextInputLayout.END_ICON_CLEAR_TEXT);
70+
71+
assertThat(textInputLayout.getEndIconView().isImportantForAccessibility(), is(true));
72+
}
73+
74+
@Test
75+
public void testClickableCustomEndIcon_importantForA11y() {
76+
textInputLayout.setEndIconMode(END_ICON_CUSTOM);
77+
textInputLayout.setEndIconDrawable(new ColorDrawable(Color.GREEN));
78+
textInputLayout.setEndIconOnClickListener(onClickListener);
79+
80+
assertThat(textInputLayout.getEndIconView().isImportantForAccessibility(), is(true));
81+
}
82+
83+
@Test
84+
public void testNonClickableCustomEndIcon_notImportantForA11y() {
85+
textInputLayout.setEndIconMode(END_ICON_CUSTOM);
86+
textInputLayout.setEndIconDrawable(new ColorDrawable(Color.GREEN));
87+
88+
assertThat(textInputLayout.getEndIconView().isImportantForAccessibility(), is(false));
89+
}
90+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright (C) 2022 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+
<com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
17+
android:id="@+id/text_input_layout"
18+
style="?attr/textInputOutlinedStyle"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:hint="@string/label">
22+
<com.google.android.material.textfield.TextInputEditText
23+
android:layout_width="match_parent"
24+
android:layout_height="wrap_content"/>
25+
</com.google.android.material.textfield.TextInputLayout>

0 commit comments

Comments
 (0)