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.
This codelab intended to lead you to:
No prior knowledge of accessibility or automated testing is required to perform this codelab. However, we assume that you:
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.
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.
To add automated accessibility tests for the Counter app, follow these instructions:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
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'
...
}
MainActivityTest
. So you will know that this test class refers to MainActivity.With MainActivityTest
class generated and opened, start to set it up to run AATK tests.
RoboletricTestRunner
.AccessibilityTestRunner
.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);
}
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.
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:
@Test
public void mustUseAdequateContrastRatio(){
runner.runAccessibilityTest(rootView, new TestAdequateContrastRatio());
}
View
identification, the expected ratio and the current ratio.