Mobile device applications can help people perform everyday tasks. However, people with disabilities may face various barriers when using the features of these devices if they do not provide adequate accessibility.

Software developers play a crucial role in promoting digital accessibility improvements, and automated tests can help them.

The Automated accessibility tests kit for Android apps (AATK) consists of a collection of automated accessibility tests designed to run with Robolectric. This enables them to be executed as local tests, without the need for a physical or emulated device.

This kit was developed focusing on the most common accessibility issues and the most frequently used widgets, where many accessibility problems tend to arise.

What You'll Learn

This codelab intended to lead you to:

Prerequisites

No prior knowledge of accessibility or automated testing is required to perform this codelab. However, we assume that you:

The Counter App

In this codelab, you'll be working with an existing app, Counter, forked from Google Codelabs. This app allows users to track, increment, and decrement a numerical count. Even though the app is simple, you'll discover that it has some accessibility issues that make it hard for many users to properly interact with it.

We'll guide you to run thre tests from AATK to identify these issues quickly, and then fix them. Additionally, you can write and run other tests by your own.

Clone and open project

You can get the source code for the starting version of the app from GitHub here. Clone the repo, and open Counter in Android Studio.

Set it up to use AATK

To add automated accessibility tests for the Counter app, follow these instructions:

  1. Add it in your root build.gradle file, at the end of repositories.
    allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
    }
    
  2. Configure your app-level build.gradle file for Robolectric and AATK testing by updating the testOptions and adding the necessary dependencies. First, add these two lines inside testOptions inside android, like this:
    android{
        ...
        testOptions {
            // Used for Unit testing Android dependent elements in test folder
            unitTests.includeAndroidResources  = true
            unitTests.returnDefaultValues = true
        }
    }
    
    Then, add these two testImplementation dependencies:
    dependencies {
        ...
        testImplementation 'org.robolectric:robolectric:4.9'
        testImplementation 'com.github.andersongarcia:android-accessibility-test-kit:v1.0.0'
        ...
    }
    
  1. After making these changes, sync your project to ensure they take effect.

Create the Test Class

  1. In Android Studio, open up the Project pane and find this folder:
  1. Right click the counter folder and select New > Java Class
  2. Name it MainActivityTest. So you will know that this test class refers to MainActivity.

Set up the Test Class

With MainActivityTest class generated and opened, start to set it up to run AATK tests.

  1. Annotate the class scope to run with RoboletricTestRunner.
  2. Declare private field to keep de rootView and the AccessibilityTestRunner.
  3. Declare a public property to the ErrorCollector.
  4. Add a setUp method as follow:
    @Before
    public void setUp() {
        MainActivity activity = Robolectric.buildActivity(MainActivity.class).create().get();
    
        // Get the root node of the view hierarchy
        rootView = activity.getWindow().getDecorView().getRootView();
        runner = new AccessibilityTestRunner(collector);
    }
    
  5. At this point, your MainActivityTest should look like this:
    @RunWith(RobolectricTestRunner.class)
    public class MainActivityTest {
        private View rootView;
        private AccessibilityTestRunner runner;
    
        @Rule
        public ErrorCollector collector = new ErrorCollector();
    
        @Before
        public void setUp() {
            MainActivity activity = Robolectric.buildActivity(MainActivity.class).create().get();
    
            // Get the root node of the view hierarchy
            rootView = activity.getWindow().getDecorView().getRootView();
            runner = new AccessibilityTestRunner(collector);
        }
    }
    

Add a test method to each accessibility test you want to run. We'll start from the color contrast ratio check.

Create a test to check color contrast ratio

Proper contrast helps users better identify the content of the application. A contrast ratio of at least 4.5:1 should be used.

You can run the AATK TestAdequateContrastRatio as follow:

  1. Add a test method.
  2. Call the method runAccessibilityTest from the runner, passing as parameter the root view and a new instance of desired test.
        @Test
        public void mustUseAdequateContrastRatio(){
            runner.runAccessibilityTest(rootView, new TestAdequateContrastRatio());
        }
    
  3. Run your test. Right click on it and select Run MainActivityTest.mustUseAdequateContrastRatio
  4. In Run panel, double-click mustUseAdequateContrastRatio to see the the results. You'll notice the message error, the View identification, the expected ratio and the current ratio.
  5. Open res/layout/activity_main.xml, find the TextView and change android:textColor="@color/grey" to android:textColor="@color/darkGrey".
  6. Go back to item 3 to rerun the test and see it pass.