Test Butler


Source link: https://github.com/linkedin/test-butler

Test Butler

Reliable Android testing, at your service.

Motivation

Test Butler was inspired by the Google presentation " Going Green: Cleaning up the Toxic Mobile Environment". For more background, read the Test Butler announcement blog post.

What does it do?

  • Stabilizes the Android emulator, to help prevent test failures caused by emulator issues.
    • Disables animations so Espresso tests can run properly.
    • Disables crash & ANR dialogs so a system app misbehaving won't trigger a popup that causes your UI tests to fail.
    • Locks the keyguard, WiFi radio, and CPU to ensure they don't go to sleep unexpectedly while tests are running.
    • Disables system spell checker to avoid redline highlights and text auto correction.
  • Handles changing global emulator settings and holds relevant permissions so your app doesn't have to.
    • Enable/disable WiFi: Test Butler allows tests to simulate a situation where WiFi is not connected or changes availability at some point.
    • Change device orientation: Tests can manually set the orientation of the device during test execution.
    • Set location services mode: Test Butler lets your code simulate different location services modes, like battery saver or high accuracy.
    • Set application locale: Tests can set a custom Locale object for their application to simulate running the app in another language.
    • Grant runtime permissions: Tests can grant Marshmallow's runtime permissions to their application directly from test code.
    • Use hardware IME: Tests can tell the system to prefer the hardware IME.
    • Control immersive mode confirmation: Tests can enable or disable immersive mode confirmation.

How does it work?

Test Butler is a two-part project. It includes an Android library that your test code can depend on, as well as a companion Android app apk that must be installed on your Android emulator before using Test Butler. You can build the Test Butler APK yourself from source, or download the binary from Bintray

The Test Butler library is a thin wrapper around an AIDL interface to give your tests a safe way to talk to the Test Butler app's service running in the background on the emulator.

The Test Butler app is signed using the system keystore for the Android emulator, so it is automatically granted signature-level permissions when installed. This means granting permissions via adb is not necessary. It also means that this app can only be installed on emulators that use the stock Android keystore!

Being a system app makes Test Butler much easier to use than existing Android solutions for changing emulator settings. To disable animations, you just need a single line of code in your app; no extra permissions in your debug manifest, no granting permissions via adb, no Gradle plugin to integrate.

Test Butler can even use permissions that can't be granted via adb, like the SET_ACTIVITY_WATCHER permission, which lets Test Butler disable crash & ANR dialogs during tests.

Any "gotchas" to look out for?

Only a couple (and they're minor). Test Butler adds a custom IActivityController to the system to be able to suppress crash & ANR dialogs. This technique is also used internally by the Monkey tool. Unfortunately, the implementation of the isUserAMonkey() method takes advantage of the fact that the Monkey class is the only thing inside Android that sets an IActivityController and returns true whenever one is set.

This means that isUserAMonkey() will return true while Test Butler is running! If your app uses this method to invoke different behavior during actual monkey testing, you may encounter issues while running tests with Test Butler. An easy fix is to create a helper method in your app to call instead of isUserAMonkey(), which returns false while instrumentation tests are running and calls through to the real isUserAMonkey() when the app is not being instrumented.

Also, you can use methods in the TestButler class in the @BeforeClass & @AfterClass of a test class. However, be warned that the test suite will not report that the Test Butler app is not installed, instead it will skip the test. This is due to a bug in Android testing support.

Download

Download the latest .apk and .aar via Maven:

 <dependency>

 <groupId>com.linkedin.testbutler</groupId>

 <artifactId>test-butler-library</artifactId>

 <version>1.3.1</version>

 <type>pom</type>
  </dependency>
  <dependency>

 <groupId>com.linkedin.testbutler</groupId>

 <artifactId>test-butler-app</artifactId>

 <version>1.3.1</version>

 <type>pom</type>
  </dependency>

or Gradle:


 androidTestCompile 'com.linkedin.testbutler:test-butler-library:1.3.1' 

You can also download the apk file manually from Bintray if you prefer.

Getting Started

Install the Test Butler apk on your emulator prior to running tests, then add the following to your test runner class:

public class ExampleTestRunner extends AndroidJUnitRunner {

@Override
public void onStart() {

 TestButler.setup(InstrumentationRegistry.getTargetContext());

 super.onStart();

}

 @Override
public void finish(int resultCode, Bundle results) {

 TestButler.teardown(InstrumentationRegistry.getTargetContext());

 super.finish(resultCode, results);

}
 
}

To change settings on the device from your tests, just use the methods in the TestButler class. For example:

@Before public void setup() {

TestButler.setLocationMode(Settings.Secure.LOCATION_MODE_OFF);
 
}
  @Test public void verifyBehaviorWithNoLocation() {

// ... 
}
  @After public void teardown() {

TestButler.setLocationMode(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
 
}

NB: See gotchyas above to see why @BeforeClass & @AfterClass aren't used here.

Resources

Custom media controller view.

Inspired by stock android RatingBar. Simpler, has features that original lacks.

Clean up the intellij Android studio files.

View inspection toolbar for android development.

This plugin will scan and adapt your source files to include a provided header, e.g. a LICENSE file. By default it will scan every source set and report warnings. It will also create format tasks, which will properly format and apply the specified header.

Widget for RecyclerView fast scrolling, like Android's built-in FastScroller for AbsListView.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes