[MUSIC PLAYING]
CAREN CHANG: There are several approaches
to testing to help ensure you're creating accessible
experiences within your app.
And in this episode, we'll be focusing
on utilizing automated tests with Espresso
and the Accessibility Test Framework.
By writing automated tests, you can identify opportunities
to improve your app's accessibility
during the development process and guard against regressions
during future development.
To understand how to write automated tests
with accessibility checks, you should first
be familiar with the Accessibility Test Framework.
The Accessibility Test Framework is a library
that includes a set of accessibility checks
that can be used in automated tests.
Let's take a look at an example of how
to integrate accessibility checks in an existing Espresso
test.
Here, we have a simple counter app
that displays a number and two buttons
to increase or decrease the number shown.
To test that the add button works properly,
we have an Espresso test that first
clicks on the increase button and then checks
that the display number has increased by 1.
With this existing Espresso test in place,
we can integrate Accessibility Testing Framework's checks
to also test for possible accessibility improvements.
Accessibility Test Framework is a library
that checks specific aspects of an Android UI
to identify opportunities for improving
an app's accessibility.
The Accessibility Test Framework performs the following checks--
presence of labels, which allow users of screen readers
to understand content within your app;
touch target size, because larger touch targets make
interaction easier for those with limited manual dexterity;
color contrast between text and images,
which can impact the legibility of your app's content;
and other properties of UI elements
to test whether Android's Accessibility Services can
properly convey your app's semantics to end users.
Now that you have a better understanding of what
the Accessibility Test Framework is and how it works,
let's go through the steps to enable accessibility checks
in the existing Espresso test.
First, you need to add the espresso-accessibility
dependency in your build.gradle file.
Then in the existing test class, call
AccessibilityChecks.enable() in the setup method.
Now let's run the test again to see what results are produced.
With AccessibilityChecks.enabled(),
there are now two accessibility errors reported.
The first error is that the add button
doesn't have a sufficiently large touch target size.
The second error is that the add button
doesn't have a label that could be read by a screen reader.
You might have noticed that both errors are
directed at the add button.
This is because, by default, accessibility checks are only
performed on Views that perform a ViewAction.
For example, accessibility checks would be skipped
for a test that directly invokes a View's perform() click()
method.
However, accessibility checks would run in a test that calls
perform(ViewActions.click()).
To improve on the accessibility checks for this test,
you can instead perform checks from a window's root view.
Instead of calling AccessibilityChecks.enable(),
you can call AccessibilityChe cks.enable().set
RunChecksFromRootView().
This will expand the scope of the accessibility testing
by checking the entire view hierarchy of the window that
receives the ViewAction, instead of only checking
the individual Views that receive the ViewActions.
If we fix the previously reported errors
and then run the same test again,
the result now flags an accessibility error
on the text view that displays the number,
because the text view doesn't have
the minimum suggested color contrast between the text
and the background.
And now you've seen how easy it is
to enable accessibility checks on your existing Espresso
tests.
However, it's important to remember
that if tests don't identify any accessibility suggestions, that
doesn't necessarily mean the app is accessible.
Automated tests should not be taken as a replacement
for manual testing.
If you want some hands-on practice
with integrating accessibility checks with an Espresso test,
be sure to check out the codelab linked in the video description
below.
To summarize, automated tests for accessibility
enable you to identify opportunities
to improve your app's accessibility
during the development process.
Accessibility Test Framework is a library
that uses a check-based system to assess
specific aspects of a Android UI and identify opportunities
for improving an app's accessibility.
And finally, you can integrate Accessibility Test Framework
checks into existing Espresso tests by utilizing
AccessibilityChecks.enable().
Thanks for tuning in to learn more about accessibility
testing with Espresso, and be on the lookout for more tips
on how to make your apps more accessible.
[MUSIC PLAYING]