diff options
author | hoisie <hoisie@google.com> | 2020-09-22 16:30:12 -0700 |
---|---|---|
committer | Michael Hoisie <hoisie@google.com> | 2020-09-29 10:57:44 -0700 |
commit | 24c8bf78bac77d95166edeb29b145c2e0abc3b1a (patch) | |
tree | f4a119b121e4e49f02aa129b65628bda4b479c51 /plugins | |
parent | 96aedab88800adedc6fc5514a6b5654eab71472c (diff) | |
download | robolectric-24c8bf78bac77d95166edeb29b145c2e0abc3b1a.tar.gz |
Remove AccessibilityUtil from Robolectric
PiperOrigin-RevId: 333183848
Diffstat (limited to 'plugins')
4 files changed, 0 insertions, 511 deletions
diff --git a/plugins/accessibility-deprecated/build.gradle b/plugins/accessibility-deprecated/build.gradle deleted file mode 100644 index d9b018eae..000000000 --- a/plugins/accessibility-deprecated/build.gradle +++ /dev/null @@ -1,25 +0,0 @@ -apply plugin: org.robolectric.gradle.RoboJavaModulePlugin -apply plugin: org.robolectric.gradle.DeployedRoboJavaModulePlugin - -dependencies { - api project(":pluginapi") - api project(":utils") - api project(":shadows:framework") - - compileOnly(AndroidSdk.MAX_SDK.coordinates) { force = true } - api "com.google.android.apps.common.testing.accessibility.framework:accessibility-test-framework:2.1" - compileOnly "com.google.auto.service:auto-service-annotations:1.0-rc6" - annotationProcessor "com.google.auto.service:auto-service:1.0-rc6" - - testImplementation project(":robolectric") - testImplementation "junit:junit:4.12" - testImplementation "org.mockito:mockito-core:2.5.4" - testImplementation("androidx.test:core:1.3.0-rc03") - testImplementation("androidx.test:runner:1.3.0-rc03") - testImplementation("androidx.test.ext:junit:1.1.2-rc03") - - testCompileOnly AndroidSdk.MAX_SDK.coordinates // compile against latest Android SDK - testRuntime AndroidSdk.MAX_SDK.coordinates - testRuntime project(":shadows:supportv4") - testRuntime "com.android.support:support-v4:26.0.1" -} diff --git a/plugins/accessibility-deprecated/src/main/java/org/robolectric/android/AccessibilityUtil.java b/plugins/accessibility-deprecated/src/main/java/org/robolectric/android/AccessibilityUtil.java deleted file mode 100644 index 5afc35f56..000000000 --- a/plugins/accessibility-deprecated/src/main/java/org/robolectric/android/AccessibilityUtil.java +++ /dev/null @@ -1,291 +0,0 @@ -package org.robolectric.android; - -import android.view.View; -import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckPreset; -import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResult; -import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResult.AccessibilityCheckResultType; -import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResultUtils; -import com.google.android.apps.common.testing.accessibility.framework.AccessibilityViewCheckResult; -import com.google.android.apps.common.testing.accessibility.framework.DuplicateClickableBoundsViewCheck; -import com.google.android.apps.common.testing.accessibility.framework.TouchTargetSizeViewCheck; -import com.google.android.apps.common.testing.accessibility.framework.integrations.espresso.AccessibilityValidator; -import com.google.auto.service.AutoService; -import java.lang.annotation.Annotation; -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.List; -import org.hamcrest.Matcher; -import org.hamcrest.Matchers; -import org.robolectric.annotation.AccessibilityChecks; -import org.robolectric.annotation.AccessibilityChecks.ForRobolectricVersion; -import org.robolectric.pluginapi.AccessibilityChecker; - -/** - * Utility class for checking Views for accessibility. - * - * This class is used by {@code ShadowView.checkedPerformClick} to check for accessibility problems. - * There is some subtlety to checking a UI for accessibility when it hasn't been rendered. The - * better initialized the View, the more accurate the checking will be. At a minimum, the view - * should be attached to a proper view hierarchy similar to what's checked for in:q - * {@code ShadowView.checkedPerformClick}. - * - * Will be removed after Robolectric 4.4. - * - * @deprecated Use Espresso for view interactions. - */ -@Deprecated -@AutoService(AccessibilityChecker.class) -public class AccessibilityUtil implements AccessibilityChecker { - private static final String COMPAT_V4_CLASS_NAME = "android.support.v4.view.ViewCompat"; - /* The validator that this class configures and uses to run the checks */ - private static AccessibilityValidator validator; - - /* - * Slightly hacky way to deal with the legacy of allowing the annotation to configure the - * subset of checks to run from the annotation. {@code true} when a version set is - * specified by setRunChecksForRobolectricVersion. - */ - private static boolean forVersionSet = false; - - /* Flag indicating if the support library's presence has been verified */ - private static boolean v4SupportPresenceVerified = false; - - /** - * Check a hierarchy of {@code View}s for accessibility. Only performs checks if (in decreasing - * priority order) accessibility checking is enabled using an {@link AccessibilityChecks} - * annotation, if the system property {@code robolectric.accessibility.enablechecks} is set to - * {@code true}, or if the environment variable {@code robolectric.accessibility.enablechecks} - * is set to {@code true}. - * - * @param view The {@code View} to examine - * - * @return A list of results from the check. If there are no results or checking is disabled, - * the list is empty. - */ - public static List<AccessibilityViewCheckResult> checkViewIfCheckingEnabled(View view) { - AccessibilityChecks classChecksAnnotation = getAnnotation(); - if (!isAccessibilityCheckingEnabled(classChecksAnnotation)) { - return Collections.emptyList(); - } - - return checkView(view); - } - - /** - * Check a hierarchy of {@code View}s for accessibility, based on currently set options. - * - * @param view The {@code View} to examine - * - * @return A list of results from the check. If there are no results, the list is empty. - */ - public static List<AccessibilityViewCheckResult> checkView(View view) { - return checkView(view, getAnnotation()); - } - - /** - * Check a hierarchy of {@code View}s for accessibility. Only performs checks if (in decreasing - * priority order) accessibility checking is enabled using an {@link AccessibilityChecks} - * annotation, if the system property {@code robolectric.accessibility.enablechecks} is set to - * {@code true}, or if the environment variable {@code robolectric.accessibility.enablechecks} - * is set to {@code true}. - * - * Implicitly calls {code setThrowExceptionForErrors(false)} to disable exception throwing. This - * method is deprecated, both because of this side effect and because the other methods offer - * more control over execution. - * - * @param view The {@code View} to examine - * - * @return A list of results from the check. If there are no results or checking is disabled, - * the list is empty. - */ - @Deprecated - public static boolean passesAccessibilityChecksIfEnabled(View view) { - setThrowExceptionForErrors(false); - List<AccessibilityViewCheckResult> results = checkViewIfCheckingEnabled(view); - List<AccessibilityViewCheckResult> errors = AccessibilityCheckResultUtils.getResultsForType( - results, AccessibilityCheckResultType.ERROR); - return (errors.size() == 0); - } - - /** - * Specify that a specific subset of accessibility checks be run. The subsets are specified based - * on which Robolectric version particular checks were released with. By default, all checks are - * run {@link ForRobolectricVersion}. - * - * If you call this method, the value you pass will take precedence over any value in any - * annotations. - * - * @param forVersion The version of checks to run for. If {@code null}, throws away the current - * value and falls back on the annotation or default. - */ - public static void setRunChecksForRobolectricVersion(ForRobolectricVersion forVersion) { - initializeValidator(); - if (forVersion != null) { - validator.setCheckPreset(convertRoboVersionToA11yTestVersion(forVersion)); - forVersionSet = true; - } else { - forVersionSet = false; - } - } - - /** - * Specify that accessibility checks should be run for all views in the hierarchy whenever a - * single view's accessibility is asserted. - * - * @param runChecksFromRootView {@code true} if all views in the hierarchy should be checked. - */ - public static void setRunChecksFromRootView(boolean runChecksFromRootView) { - initializeValidator(); - validator.setRunChecksFromRootView(runChecksFromRootView); - } - - /** - * Suppress all results that match the given matcher. Suppressed results will not be included - * in any logs or cause any {@code Exception} to be thrown. This capability is useful if there - * are known issues, but checks should still look for regressions. - * - * @param matcher A matcher to match a {@link AccessibilityViewCheckResult}. {@code null} - * disables suppression and is the default. - */ - @SuppressWarnings("unchecked") // The generic passed to anyOf - public static void setSuppressingResultMatcher( - final Matcher<? super AccessibilityViewCheckResult> matcher) { - initializeValidator(); - /* Suppress all touch target results, since views all report size as 0x0 */ - Matcher<AccessibilityCheckResult> touchTargetResultMatcher = - AccessibilityCheckResultUtils.matchesChecks( - Matchers.equalTo(TouchTargetSizeViewCheck.class)); - Matcher<AccessibilityCheckResult> duplicateBoundsResultMatcher = - AccessibilityCheckResultUtils.matchesChecks( - Matchers.equalTo(DuplicateClickableBoundsViewCheck.class)); - if (matcher == null) { - validator.setSuppressingResultMatcher( - Matchers.anyOf(touchTargetResultMatcher, duplicateBoundsResultMatcher)); - } else { - validator.setSuppressingResultMatcher( - Matchers.anyOf(matcher, touchTargetResultMatcher, duplicateBoundsResultMatcher)); - } - } - - /** - * Control whether or not to throw exceptions when accessibility errors are found. - * - * @param throwExceptionForErrors {@code true} to throw an {@code AccessibilityViewCheckException} - * when there is at least one error result. Default: {@code true}. - */ - public static void setThrowExceptionForErrors(boolean throwExceptionForErrors) { - initializeValidator(); - validator.setThrowExceptionForErrors(throwExceptionForErrors); - } - - private static List<AccessibilityViewCheckResult> checkView(View view, - AccessibilityChecks classChecksAnnotation) { - /* - * Accessibility Checking requires the v4 support library. If the support library isn't present, - * throw a descriptive exception now. - */ - if (!v4SupportPresenceVerified) { - try { - View.class.getClassLoader().loadClass(COMPAT_V4_CLASS_NAME); - } catch (ClassNotFoundException e) { - throw new RuntimeException( - "Accessibility Checking requires the Android support library (v4).\n" - + "Either include it in the project or disable accessibility checking."); - } - v4SupportPresenceVerified = true; - } - - initializeValidator(); - if (!forVersionSet) { - if (classChecksAnnotation != null) { - validator.setCheckPreset( - convertRoboVersionToA11yTestVersion(classChecksAnnotation.forRobolectricVersion())); - } else { - validator.setCheckPreset(AccessibilityCheckPreset.LATEST); - } - } - return validator.checkAndReturnResults(view); - } - - private static boolean isAccessibilityCheckingEnabled(AccessibilityChecks classChecksAnnotation) { - boolean checksEnabled = false; - - String checksEnabledString = System.getenv("robolectric.accessibility.enablechecks"); - if (checksEnabledString != null) { - checksEnabled = checksEnabledString.equals("true"); - } - - /* Allow test arg to enable checking (and override environment variables) */ - checksEnabledString = System.getProperty("robolectric.accessibility.enablechecks"); - if (checksEnabledString != null) { - checksEnabled = checksEnabledString.equals("true"); - } - - if (classChecksAnnotation != null) { - checksEnabled = classChecksAnnotation.enabled(); - } - - return checksEnabled; - } - - private static AccessibilityChecks getAnnotation() { - AccessibilityChecks classChecksAnnotation = null; - StackTraceElement[] stack = new Throwable().fillInStackTrace().getStackTrace(); - for (StackTraceElement element : stack) { - /* Look for annotations on the method or the class */ - Class<?> clazz; - try { - clazz = Class.forName(element.getClassName()); - Method method; - method = clazz.getMethod(element.getMethodName()); - /* Assume the method is void, as that is the case for tests */ - classChecksAnnotation = method.getAnnotation(AccessibilityChecks.class); - if (classChecksAnnotation == null) { - classChecksAnnotation = clazz.getAnnotation(AccessibilityChecks.class); - } - /* Stop looking when we find an annotation */ - if (classChecksAnnotation != null) { - break; - } - - /* If we've crawled up the stack far enough to find the test, stop looking */ - for (Annotation annotation : clazz.getAnnotations()) { - if (annotation.annotationType().getName().equals("org.junit.Test")) { - break; - } - } - } - /* - * The reflective calls may throw exceptions if the stack trace elements - * don't look like junit test methods. In that case we simply go on - * to the next element - */ - catch (ClassNotFoundException | SecurityException | NoSuchMethodException e) {} - } - return classChecksAnnotation; - } - - private static void initializeValidator() { - if (validator == null) { - validator = new AccessibilityValidator(); - setSuppressingResultMatcher(null); - } - } - - private static AccessibilityCheckPreset convertRoboVersionToA11yTestVersion( - ForRobolectricVersion robolectricVersion) { - if (robolectricVersion == ForRobolectricVersion.LATEST) { - return AccessibilityCheckPreset.LATEST; - } - AccessibilityCheckPreset preset = AccessibilityCheckPreset.VERSION_1_0_CHECKS; - if (robolectricVersion.ordinal() >= ForRobolectricVersion.VERSION_3_1.ordinal()) { - preset = AccessibilityCheckPreset.VERSION_2_0_CHECKS; - } - return preset; - } - - @Override - public void checkViewAccessibility(Object realView) { - checkViewIfCheckingEnabled((View) realView); - } -} diff --git a/plugins/accessibility-deprecated/src/test/java/org/robolectric/android/AccessibilityShadowViewTest.java b/plugins/accessibility-deprecated/src/test/java/org/robolectric/android/AccessibilityShadowViewTest.java deleted file mode 100644 index 51752f8dc..000000000 --- a/plugins/accessibility-deprecated/src/test/java/org/robolectric/android/AccessibilityShadowViewTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.robolectric.android; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.robolectric.Shadows.shadowOf; - -import android.graphics.Rect; -import android.view.View; -import androidx.test.ext.junit.runners.AndroidJUnit4; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.annotation.AccessibilityChecks; - -@RunWith(AndroidJUnit4.class) -public class AccessibilityShadowViewTest { - - @Test - @AccessibilityChecks - public void checkedClick_withA11yChecksAnnotation_shouldThrow() throws Exception { - AccessibilityUtil.setThrowExceptionForErrors(true); - - View unlabeledView = new View(RuntimeEnvironment.application); - unlabeledView.setContentDescription(null); - unlabeledView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); - unlabeledView.setClickable(true); - Rect validViewBounds = new Rect(100, 100, 200, 200); - shadowOf(unlabeledView).setGlobalVisibleRect(validViewBounds); - - try { - shadowOf(unlabeledView).checkedPerformClick(); - fail("RuntimeException not thrown"); - } catch (RuntimeException e) { - // expected - assertContains("View is not visible and cannot be clicked", e.getMessage()); - } - } - - private void assertContains(String expectedText, String actualText) { - assertTrue("Expected <" + actualText + "> to contain <" - + expectedText + ">", actualText.contains(expectedText)); - } -} diff --git a/plugins/accessibility-deprecated/src/test/java/org/robolectric/android/AccessibilityUtilTest.java b/plugins/accessibility-deprecated/src/test/java/org/robolectric/android/AccessibilityUtilTest.java deleted file mode 100644 index 044a4d96b..000000000 --- a/plugins/accessibility-deprecated/src/test/java/org/robolectric/android/AccessibilityUtilTest.java +++ /dev/null @@ -1,152 +0,0 @@ -package org.robolectric.android; - -import static org.mockito.Mockito.mock; -import static org.robolectric.Shadows.shadowOf; - -import android.graphics.Rect; -import android.text.SpannableString; -import android.text.Spanned; -import android.text.style.ClickableSpan; -import android.view.View; -import android.widget.LinearLayout; -import android.widget.TextView; -import androidx.test.ext.junit.runners.AndroidJUnit4; -import com.google.android.apps.common.testing.accessibility.framework.integrations.AccessibilityViewCheckException; -import org.hamcrest.Matchers; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.annotation.AccessibilityChecks; -import org.robolectric.annotation.AccessibilityChecks.ForRobolectricVersion; - -/** - * Tests for accessibility checking. The checking relies on the Accessibility Test Framework for - * Android, which has support-v4 as a dependency, so these tests are included where the presence of - * that library is guaranteed. - */ -@RunWith(AndroidJUnit4.class) -public class AccessibilityUtilTest { - private static final String DUPLICATE_STRING = "Duplicate"; - private TextView textViewWithClickableSpan; - private LinearLayout parentLayout; - private View labeledView; - private View unlabeledView; - - @Before - public void setUp() throws Exception { - Rect validViewBounds = new Rect(100, 100, 200, 200); - - // Set the statics back to their default values - AccessibilityUtil.setRunChecksForRobolectricVersion(null); - AccessibilityUtil.setSuppressingResultMatcher(null); - AccessibilityUtil.setRunChecksFromRootView(false); - AccessibilityUtil.setThrowExceptionForErrors(true); - - labeledView = new View(RuntimeEnvironment.application); - labeledView.setContentDescription("Something"); - labeledView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); - labeledView.setClickable(true); - // Force the views on the screen so they'll be seen as visible even though they aren't - // part of a valid view hierarchy - shadowOf(labeledView).setGlobalVisibleRect(validViewBounds); - - unlabeledView = new View(RuntimeEnvironment.application); - unlabeledView.setContentDescription(null); - unlabeledView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); - unlabeledView.setClickable(true); - shadowOf(unlabeledView).setGlobalVisibleRect(validViewBounds); - - parentLayout = new LinearLayout(RuntimeEnvironment.application); - parentLayout.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); - parentLayout.addView(labeledView); - shadowOf(parentLayout).setGlobalVisibleRect(validViewBounds); - - textViewWithClickableSpan = new TextView(RuntimeEnvironment.application); - SpannableString spannableString = new SpannableString("Some text"); - ClickableSpan mockClickableSpan = mock(ClickableSpan.class); - spannableString.setSpan(mockClickableSpan, 0, 1, Spanned.SPAN_COMPOSING); - textViewWithClickableSpan.setText(spannableString); - shadowOf(textViewWithClickableSpan).setGlobalVisibleRect(validViewBounds); - } - - @Test(expected = AccessibilityViewCheckException.class) - public void checkUnlabeledView_shouldThrow() throws Exception { - AccessibilityUtil.checkView(unlabeledView); - } - - @Test - public void checkOKView_shouldNotThrow() throws Exception { - AccessibilityUtil.checkView(labeledView); - } - - @Test - public void default_viewWithSiblingIssue_shouldNotThrow() throws Exception { - parentLayout.addView(unlabeledView); - AccessibilityUtil.checkView(labeledView); - } - - @Test(expected = AccessibilityViewCheckException.class) - public void whenCheckingFromRoot_viewWithSiblingIssue_shouldThrow() throws Exception { - parentLayout.addView(unlabeledView); - AccessibilityUtil.setRunChecksFromRootView(true); - AccessibilityUtil.checkView(labeledView); - } - - @Test(expected = AccessibilityViewCheckException.class) - @AccessibilityChecks - public void whenAnnotationPresent_conditionalCheckRun() { - AccessibilityUtil.checkViewIfCheckingEnabled(unlabeledView); - } - - @Test - public void whenAnnotationNotPresent_conditionalCheckNotRun() { - AccessibilityUtil.checkViewIfCheckingEnabled(unlabeledView); - } - - @Test(expected = AccessibilityViewCheckException.class) - public void framework2pt0Error_byDefault_shouldThrow() throws Exception { - AccessibilityUtil.checkView(textViewWithClickableSpan); - } - - @Test - public void framework2pt0Error_whenCheckingForRL3pt0_shouldNotThrow() throws Exception { - AccessibilityUtil.setRunChecksForRobolectricVersion(ForRobolectricVersion.VERSION_3_0); - AccessibilityUtil.checkView(textViewWithClickableSpan); - } - - @Test - @AccessibilityChecks(forRobolectricVersion = ForRobolectricVersion.VERSION_3_0) - public void framework2pt0Error_annotationForRL3pt0_shouldNotThrow() throws Exception { - AccessibilityUtil.checkView(textViewWithClickableSpan); - } - - @Test(expected = AccessibilityViewCheckException.class) - @AccessibilityChecks(forRobolectricVersion = ForRobolectricVersion.VERSION_3_0) - public void framework2pt0Error_codeForcesRL3pt1_shouldThrow() throws Exception { - AccessibilityUtil.setRunChecksForRobolectricVersion(ForRobolectricVersion.VERSION_3_1); - AccessibilityUtil.checkView(textViewWithClickableSpan); - } - - @Test - public void whenSuppressingResults_shouldNotThrow() throws Exception { - AccessibilityUtil.setSuppressingResultMatcher(Matchers.anything()); - AccessibilityUtil.checkView(unlabeledView); - } - - @Test - public void whenOnlyPrintingResults_shouldNotThrow() throws Exception { - AccessibilityUtil.setThrowExceptionForErrors(false); - AccessibilityUtil.checkView(unlabeledView); - } - - @Test - public void warningIssue_shouldNotThrow() throws Exception { - labeledView.setContentDescription(DUPLICATE_STRING); - parentLayout.setContentDescription(DUPLICATE_STRING); - parentLayout.setClickable(true); - AccessibilityUtil.checkView(parentLayout); - } - -} - |