Send emulator console commands during Espresso test

Issue

Google explains how to control many parameters of an Android emulator, like the current charge of the battery, the sensor input and so on here: https://developer.android.com/studio/run/emulator-console.html .

I would like to create an Espresso test that changes these parameters of an emulator during a test, is this possible? I know that there is the following method:

InstrumentationRegistry.getInstrumentation().uiAutomation
    .executeShellCommand("someShellCommand")

This method can be called during a test, but for changing system parameters of an emulator I first have to login via telnet localhost 5554 and only then can I manipulate the emulator parameters like, for example, so: power capacity 30, which changes the power of the battery to 30%. How can I do this during an Espresso test?

Solution

You can try

InstrumentationRegistry.getInstrumentation().getUiAutomation()
    .executeShellCommand("dumpsys battery set level 30");

to change the battery level. Other values or settings may require different commands.

Note: It is your responsibility to close the returned file descriptor once you are done reading.

Answered By – Diego Torres Milano

Leave a Comment