public final class

AccessibilityChecks

extends java.lang.Object

 java.lang.Object

↳androidx.test.espresso.accessibility.AccessibilityChecks

Gradle dependencies

compile group: 'androidx.test.espresso', name: 'espresso-accessibility', version: '3.6.1'

  • groupId: androidx.test.espresso
  • artifactId: espresso-accessibility
  • version: 3.6.1

Artifact androidx.test.espresso:espresso-accessibility:3.6.1 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test.espresso:espresso-accessibility com.android.support.test.espresso:espresso-accessibility

Androidx class mapping:

androidx.test.espresso.accessibility.AccessibilityChecks android.support.test.espresso.accessibility.AccessibilityChecks

Overview

A class to enable automated accessibility checks in Espresso tests. These checks will run as a global ViewAssertion, and cover a variety of accessibility issues (see AccessibilityCheckPreset#LATEST to see which checks are run).

Summary

Methods
public static ViewAssertionaccessibilityAssertion()

public static voiddisable()

Disables accessibility checking as a global ViewAssertion in ViewActions.

public static AccessibilityValidatorenable()

Enables accessibility checking as a global ViewAssertion in ViewActions.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static AccessibilityValidator enable()

Enables accessibility checking as a global ViewAssertion in ViewActions.

Returns:

the backing , on which options for check execution can be set

public static void disable()

Disables accessibility checking as a global ViewAssertion in ViewActions.

public static ViewAssertion accessibilityAssertion()

Returns:

the backing ViewAssertion that can be used to explicitly check accessibility

Source

/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.test.espresso.accessibility;

import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import androidx.test.espresso.NoMatchingViewException;
import androidx.test.espresso.ViewAssertion;
import androidx.test.espresso.action.ViewActions;
import androidx.test.espresso.util.HumanReadables;
import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResultDescriptor;
import com.google.android.apps.common.testing.accessibility.framework.integrations.espresso.AccessibilityValidator;

/**
 * A class to enable automated accessibility checks in Espresso tests. These checks will run as a
 * global {@code ViewAssertion}, and cover a variety of accessibility issues (see <a
 * href="https://github.com/google/Accessibility-Test-Framework-for-Android/blob/master/src/main/java/com/google/android/apps/common/testing/accessibility/framework/AccessibilityCheckPreset.java">AccessibilityCheckPreset#LATEST</a>
 * to see which checks are run).
 */
public final class AccessibilityChecks {

  private static final AccessibilityValidator CHECK_EXECUTOR =
      new AccessibilityValidator()
          .setResultDescriptor(
              new AccessibilityCheckResultDescriptor() {
                @Override
                public String describeView(View view) {
                  return HumanReadables.describe(view);
                }
              });

  private static final ViewAssertion ACCESSIBILITY_CHECK_ASSERTION =
      new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException noViewFoundException) {
          if (noViewFoundException != null) {
            Log.e(
                TAG,
                String.format(
                    "'accessibility checks could not be performed because view '%s' was not"
                        + "found.\n",
                    noViewFoundException.getViewMatcherDescription()));
            throw noViewFoundException;
          }
          if (view == null) {
            throw new NullPointerException();
          }
          StrictMode.ThreadPolicy originalPolicy = StrictMode.allowThreadDiskWrites();
          try {
            CHECK_EXECUTOR.check(view);
          } finally {
            StrictMode.setThreadPolicy(originalPolicy);
          }
        }
      };

  private static boolean checksEnabled = false;
  private static final String TAG = "AccessibilityChecks";

  private AccessibilityChecks() {}

  /**
   * Enables accessibility checking as a global ViewAssertion in {@link ViewActions}.
   *
   * @return the backing {@link AccessibilityValidator}, on which options for check execution can be
   *     set
   */
  public static AccessibilityValidator enable() {
    if (checksEnabled) {
      Log.w(TAG, "Accessibility checks already enabled.");
    } else {
      checksEnabled = true;
      ViewActions.addGlobalAssertion("Accessibility Checks", ACCESSIBILITY_CHECK_ASSERTION);
    }
    return CHECK_EXECUTOR;
  }

  /**
   * Disables accessibility checking as a global ViewAssertion in {@link ViewActions}.
   *
   * @throws IllegalStateException if accessibility checking in not enabled.
   */
  public static void disable() {
    if (!checksEnabled) {
      throw new IllegalStateException("Accessibility checks not enabled!");
    }
    checksEnabled = false;
    ViewActions.removeGlobalAssertion(ACCESSIBILITY_CHECK_ASSERTION);
  }

  /**
   * @return the backing {@link ViewAssertion} that can be used to explicitly check accessibility
   */
  public static ViewAssertion accessibilityAssertion() {
    return ACCESSIBILITY_CHECK_ASSERTION;
  }
}