public class TestButler
extends java.lang.Object
Contains a number of helper methods for safely communicating with the Test Butler app via RPC.
| Modifier and Type | Class and Description |
|---|---|
static interface |
TestButler.LocationMode |
static interface |
TestButler.Rotation
AOSP has an IntDef for this, but it's hidden, so we create our own here
|
| Constructor and Description |
|---|
TestButler() |
| Modifier and Type | Method and Description |
|---|---|
static void |
grantPermission(android.content.Context context,
java.lang.String permission)
A helper method for granting runtime permissions to apps under test on API 23+
|
static void |
setAccessibilityServiceState(boolean enabled)
Enable TestButler's dummy accessibility service.
|
static void |
setAlwaysFinishActivities(boolean enabled)
Enable or disable always finish activities setting on the emulator
|
static void |
setGsmState(boolean enabled)
Enable/disable the GSM connection on the emulator
|
static void |
setImmersiveModeConfirmation(boolean enabled)
Enable or disable immersive mode confirmation
|
static void |
setLocale(java.lang.String language,
java.lang.String country,
android.content.Context context)
Set the locale for the application under test.
|
static void |
setLocationMode(int locationMode)
Change the location services mode on the emulator
|
static void |
setRotation(int rotation)
Change the screen rotation of the emulator
|
static void |
setShowImeWithHardKeyboardState(boolean enabled)
Tell the system to prefer the hardware IME
This method has no effect on api levels below API 22
You must have your emulator configured with a hardware IME, or this method has no effect
|
static void |
setSpellCheckerState(boolean enabled)
Enable or disable the system spell checker
|
static void |
setup(android.content.Context context)
Start the remote ButlerService to prepare for running tests
|
static void |
setWifiState(boolean enabled)
Enable/disable the Wifi connection on the emulator
|
static void |
teardown(android.content.Context context)
Stop the remote ButlerService to indicate the test run has completed.
|
static void |
verifyAnimationsDisabled(android.content.Context context)
Validate that all animations have been disabled during the
setup(Context) call. |
public static void setup(@NonNull
android.content.Context context)
This method should be called from a subclass of Instrumentation.onStart(), BEFORE
calling super.onStart(). Often this will be a subclass of AndroidJUnitRunner.
This will handle disabling animations on the device, as well as disabling system popups when
apps crash or ANR on the emulator. NOTE: Calling this method will cause the system method
ActivityManager.isUserAMonkey() to start returning true. If your code uses this method,
you will need to create a helper method for detecting when your code is running under instrumentation
testing and not under monkey testing.
context - the "target context"; i.e. Context of the app under test (not the test apk context!)public static void teardown(@NonNull
android.content.Context context)
This method should be called from a subclass of Instrumentation.finish(int, Bundle), BEFORE
calling super.finish(). Often this will be a subclass of AndroidJUnitRunner.
This will handle re-enabling animations on the device, as well as allow system popups to be shown when apps crash or ANR on the emulator.
context - the "target context"; i.e. Context of the app under test (not the test apk context!)public static void verifyAnimationsDisabled(@NonNull
android.content.Context context)
setup(Context) call.
This method will fail with an AssertionFailedError if animations were not disabled
by the setup(Context) method. This is useful if you want to immediately halt test execution if
animations could not be disabled by Test Butler, rather than failing gracefully and continuing
to try and run your tests.
context - the "target context"; i.e. Context of the app under test (not the test apk context!)public static void setWifiState(boolean enabled)
enabled - true if wifi should be enabled, false otherwisepublic static void setGsmState(boolean enabled)
enabled - true if GSM should be enabled, false otherwisepublic static void setLocationMode(int locationMode)
locationMode - one of the TestButler.LocationMode IntDef valuespublic static void setLocale(@NonNull
java.lang.String language,
@NonNull
java.lang.String country,
@NonNull
android.content.Context context)
NOTE: this does not change the device locale! Changing the device locale on the emulator
has been slow/unreliable and led to flaky tests when the locale setting did not take effect in time.
Instead, this method uses the Configuration class to update the locale on the Resources
object of the Context from the application under test. As long as apps are checking the
current locale from the current Configuration, this approach will be reliable.
language - the language code for the new locale, as expected by Locale(String, String)country - the country code for the new locale, as expected by Locale(String, String)context - the "target context"; i.e. Context of the app under test (not the test apk context!)public static void setRotation(int rotation)
rotation - one of the TestButler.Rotation IntDef valuespublic static void setSpellCheckerState(boolean enabled)
enabled - What state the spell checker should be set topublic static void setShowImeWithHardKeyboardState(boolean enabled)
enabled - Whether to require the hardware keyboard or notpublic static void grantPermission(@NonNull
android.content.Context context,
@NonNull
java.lang.String permission)
Note: Before API 23, this method is a no-op
A common use case for this is when running tests from Android Studio where you can't (currently) pass the -g flag to adb install when installing the app to run tests.
context - the "target context"; i.e. Context of the app under test (not the test apk context!)public static void setImmersiveModeConfirmation(boolean enabled)
enabled - What state the immersive mode confirmation should be set topublic static void setAlwaysFinishActivities(boolean enabled)
enabled - What state the always finish activities setting should be set topublic static void setAccessibilityServiceState(boolean enabled)
enabled - True to enable the accessibility service, false to disable it.