2

How can I change my device or emulator's accessibility settings before running an Espresso test suite?

My aim is to use Spoon to capture app screenshots at a variety of different accessibility text zoom levels to verify quickly whether the layout is correct; rather than manually verifying each zoom level.

funkybro
  • 8,432
  • 6
  • 39
  • 52
  • Do you want to do it at start of whole test suite or some particular test, asking because test suite may contain tests that don't need accessibility settings tested – Amit Kaushik Jun 20 '17 at 06:21
  • Ideally whole test suite but either would be acceptable at this point... – funkybro Jun 20 '17 at 08:36
  • also if you want to capture spoon screenshots with different accessibility settings then you would ideally go for TestRule approach so that you can create different testRules for different setting like LargeFontTestRule, SmallFontTestRule etc. – Amit Kaushik Jun 20 '17 at 09:56

2 Answers2

2

You can run these adb shell commands either as part of the test, or in a script that runs before the tests are invoked, depending on your needs.

adb shell settings put system font_scale 1.0
adb shell wm density <n>

Setting font size is easy: possible values for font scale:

  • Small - 0.85
  • Default - 1.0
  • Large - 1.15
  • Largest - 1.30

Setting the device density is more complicated. You can use standard scaling factor for density, or choose something custom:

  • Small (scale: 0.85)
  • Default (scale: 1.00)
  • Large (scale: 1.1)
  • Larger (scale: 1.2)
  • Largest (missed on some devices, scale: 1.3)

For example, let's look at a Pixel 3.

> adb shell wm density
Physical density: 440
Override density: 572

This device has had the screen density dialed up to Largest. Let's set it to the 'Small' density.

(default density) * (scaling factor) = n 440 x .85 = 374

So the adb command to set the device density to 'Small' is

> adb shell wm density 374
> adb shell wm density
Physical density: 440
Override density: 374

And if we want to reset it to the default density without having to do any calculations, a handy shortcut is

adb shell wm density reset

Since the default/native density number is different for various devices, you must first retrieve the default density, and then multiply that by the scaling factor to calculate the target density.

This is a summary of the info I found over at https://alexzh.com/adb-commands-accessibility/#display-size

jerimiah797
  • 4,525
  • 1
  • 14
  • 13
0

For test level you can use custom TestRules to change font setting before running you test using UiAutomator framework.

For suite level you can use adb commands to change settings on your device before running test suite (before gradle command).

adb shell am start com.android.settings/.Settings

Then use these key event to go to which ever setting and change setting

Amit Kaushik
  • 642
  • 7
  • 13