Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.

Commit f47c84f

Browse files
eeejaymcomella
authored andcommitted
Add accessibility test example.
1 parent a2cb7f6 commit f47c84f

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

android/accessibility_guide.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,43 @@ This is both inefficient and confusing for the user, since they will encounter e
4242
## Automated testing
4343
You can regularly use [the Accessibility Scanner][scanner] to get a cursory overview of how the accessibility of your app is doing.
4444

45-
Unfortunately, we don't have much automated testing experience outside of this. However, this [official documentation](https://developer.android.com/training/accessibility/testing#automated) seems like a good starting point. If you find out more, please share with the team and consider updating this doc!
45+
When adding specialized accessibility logic, it is encouraged to add as much testing as possible since these code paths are not excercised in typical use and the chance of regression is high. Below is an example Mockito and Robolectric test that validates accessibility events that are resulted from a [loading progress update](https://github.com/mozilla-mobile/android-components/pull/2526).
46+
47+
```kotlin
48+
@Test
49+
fun `displayProgress will send accessibility events`() {
50+
val toolbar = BrowserToolbar(context)
51+
val root = mock(ViewParent::class.java)
52+
Shadows.shadowOf(toolbar).setMyParent(root)
53+
`when`(root.requestSendAccessibilityEvent(any(), any())).thenReturn(false)
54+
55+
Shadows.shadowOf(context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager).setEnabled(true)
56+
57+
toolbar.displayProgress(10)
58+
toolbar.displayProgress(50)
59+
toolbar.displayProgress(100)
60+
61+
val captor = ArgumentCaptor.forClass(AccessibilityEvent::class.java)
62+
63+
verify(root, times(4)).requestSendAccessibilityEvent(any(), captor.capture())
64+
65+
assertEquals(AccessibilityEvent.TYPE_ANNOUNCEMENT, captor.allValues[0].eventType)
66+
assertEquals(context.getString(R.string.mozac_browser_toolbar_progress_loading), captor.allValues[0].text[0])
67+
68+
assertEquals(AccessibilityEvent.TYPE_VIEW_SCROLLED, captor.allValues[1].eventType)
69+
assertEquals(10, captor.allValues[1].scrollY)
70+
assertEquals(100, captor.allValues[1].maxScrollY)
71+
72+
assertEquals(AccessibilityEvent.TYPE_VIEW_SCROLLED, captor.allValues[2].eventType)
73+
assertEquals(50, captor.allValues[2].scrollY)
74+
assertEquals(100, captor.allValues[2].maxScrollY)
75+
76+
assertEquals(AccessibilityEvent.TYPE_VIEW_SCROLLED, captor.allValues[3].eventType)
77+
assertEquals(100, captor.allValues[3].scrollY)
78+
assertEquals(100, captor.allValues[3].maxScrollY)
79+
}
80+
```
81+
82+
In addition the [official documentation](https://developer.android.com/training/accessibility/testing#automated) seems like a good starting point to learn more about accessibility validation in tests. We should look into integrating this into our CI.
4683

4784
[scanner]: https://support.google.com/accessibility/android/answer/6376570?hl=en

0 commit comments

Comments
 (0)