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