MonitoringInstrumentation

public class MonitoringInstrumentation


An instrumentation that enables several advanced features and makes some hard guarantees about the state of the application under instrumentation.

A short list of these capabilities:

  • Forces Application.onCreate() to happen before Instrumentation.onStart() runs (ensuring your code always runs in a sane state).
  • Logs application death due to exceptions.
  • Allows tracking of activity lifecycle states.
  • Registers instrumentation arguments in an easy to access place.
  • Ensures your activities are creating themselves in reasonable amounts of time.
  • Provides facilities to dump current app threads to test outputs.
  • Ensures all activities finish before instrumentation exits.
This Instrumentation is *NOT* a test instrumentation (some of its subclasses are). It makes no assumptions about what the subclass wants to do.

Summary

Nested types

MonitoringInstrumentation.ActivityFinisher

Loops through all the activities that have not yet finished and explicitly calls finish on them.

Public constructors

Public methods

@NonNull void
callActivityOnCreate(@NonNull Activity activity, @NonNull Bundle bundle)
@NonNull void
callActivityOnDestroy(@NonNull Activity activity)
@NonNull void
callActivityOnPause(@NonNull Activity activity)
@NonNull void
callActivityOnRestart(@NonNull Activity activity)
@NonNull void
callActivityOnResume(@NonNull Activity activity)
@NonNull void
callActivityOnStart(@NonNull Activity activity)
@NonNull void
callActivityOnStop(@NonNull Activity activity)
@NonNull void
callApplicationOnCreate(@NonNull Application app)
@NonNull void
execStartActivities(
    @NonNull Context who,
    @NonNull IBinder contextThread,
    @NonNull IBinder token,
    @NonNull Activity target,
    @NonNull Array<@NonNull Intent> intents,
    @NonNull Bundle options
)
@NonNull ActivityResult
execStartActivity(
    @NonNull Context who,
    @NonNull IBinder contextThread,
    @NonNull IBinder token,
    @NonNull Activity target,
    @NonNull Intent intent,
    @NonNull int requestCode
)
@NonNull ActivityResult
execStartActivity(
    @NonNull Context who,
    @NonNull IBinder contextThread,
    @NonNull IBinder token,
    @NonNull Activity target,
    @NonNull Intent intent,
    @NonNull int requestCode,
    @NonNull Bundle options
)
@NonNull ActivityResult
execStartActivity(
    @NonNull Context who,
    @NonNull IBinder contextThread,
    @NonNull IBinder token,
    @NonNull String target,
    @NonNull Intent intent,
    @NonNull int requestCode,
    @NonNull Bundle options
)

This API was added in Android API 23 (M)

@NonNull ActivityResult
execStartActivity(
    @NonNull Context who,
    @NonNull IBinder contextThread,
    @NonNull IBinder token,
    @NonNull Activity target,
    @NonNull Intent intent,
    @NonNull int requestCode,
    @NonNull Bundle options,
    @NonNull UserHandle user
)

This API was added in Android API 17 (JELLY_BEAN_MR1)

@NonNull ActivityResult
execStartActivity(
    @NonNull Context who,
    @NonNull IBinder contextThread,
    @NonNull IBinder token,
    @NonNull Fragment target,
    @NonNull Intent intent,
    @NonNull int requestCode,
    @NonNull Bundle options
)
@NonNull void
finish(@NonNull int resultCode, @NonNull Bundle results)

Ensures all activities launched in this instrumentation are finished before the instrumentation exits.

@NonNull void
interceptActivityUsing(
    @NonNull InterceptingActivityFactory interceptingActivityFactory
)

Use the given InterceptingActivityFactory to create Activity instance in .

@NonNull Activity
newActivity(
    @NonNull Class<@NonNull Object> clazz,
    @NonNull Context context,
    @NonNull IBinder token,
    @NonNull Application application,
    @NonNull Intent intent,
    @NonNull ActivityInfo info,
    @NonNull CharSequence title,
    @NonNull Activity parent,
    @NonNull String id,
    @NonNull Object lastNonConfigurationInstance
)
@NonNull Activity
newActivity(
    @NonNull ClassLoader cl,
    @NonNull String className,
    @NonNull Intent intent
)
@NonNull Application
newApplication(
    @NonNull ClassLoader cl,
    @NonNull String className,
    @NonNull Context context
)

Does initialization before creating the application.

@NonNull void
onCreate(@NonNull Bundle arguments)

Sets up lifecycle monitoring, and argument registry.

@NonNull void
@NonNull boolean
@NonNull void

This implementation of onStart() will guarantee that the Application's onCreate method has completed when it returns.

@NonNull void

Posts a runnable to the main thread and blocks the caller's thread until the runnable is executed.

@NonNull Activity
startActivitySync(@NonNull Intent intent)
@NonNull void

Use default mechanism of creating activity instance in newActivity

Public constructors

MonitoringInstrumentation

public MonitoringInstrumentation()

Public methods

callActivityOnCreate

@NonNull
public void callActivityOnCreate(@NonNull Activity activity, @NonNull Bundle bundle)

callActivityOnDestroy

@NonNull
public void callActivityOnDestroy(@NonNull Activity activity)

callActivityOnPause

@NonNull
public void callActivityOnPause(@NonNull Activity activity)

callActivityOnRestart

@NonNull
public void callActivityOnRestart(@NonNull Activity activity)

callActivityOnResume

@NonNull
public void callActivityOnResume(@NonNull Activity activity)

callActivityOnStart

@NonNull
public void callActivityOnStart(@NonNull Activity activity)

callActivityOnStop

@NonNull
public void callActivityOnStop(@NonNull Activity activity)

callApplicationOnCreate

@NonNull
public void callApplicationOnCreate(@NonNull Application app)

execStartActivities

@NonNull
public void execStartActivities(
    @NonNull Context who,
    @NonNull IBinder contextThread,
    @NonNull IBinder token,
    @NonNull Activity target,
    @NonNull Array<@NonNull Intent> intents,
    @NonNull Bundle options
)

execStartActivity

@NonNull
public ActivityResult execStartActivity(
    @NonNull Context who,
    @NonNull IBinder contextThread,
    @NonNull IBinder token,
    @NonNull Activity target,
    @NonNull Intent intent,
    @NonNull int requestCode
)

execStartActivity

@NonNull
public ActivityResult execStartActivity(
    @NonNull Context who,
    @NonNull IBinder contextThread,
    @NonNull IBinder token,
    @NonNull Activity target,
    @NonNull Intent intent,
    @NonNull int requestCode,
    @NonNull Bundle options
)

execStartActivity

@NonNull
public ActivityResult execStartActivity(
    @NonNull Context who,
    @NonNull IBinder contextThread,
    @NonNull IBinder token,
    @NonNull String target,
    @NonNull Intent intent,
    @NonNull int requestCode,
    @NonNull Bundle options
)

This API was added in Android API 23 (M)

execStartActivity

@NonNull
public ActivityResult execStartActivity(
    @NonNull Context who,
    @NonNull IBinder contextThread,
    @NonNull IBinder token,
    @NonNull Activity target,
    @NonNull Intent intent,
    @NonNull int requestCode,
    @NonNull Bundle options,
    @NonNull UserHandle user
)

This API was added in Android API 17 (JELLY_BEAN_MR1)

execStartActivity

@NonNull
public ActivityResult execStartActivity(
    @NonNull Context who,
    @NonNull IBinder contextThread,
    @NonNull IBinder token,
    @NonNull Fragment target,
    @NonNull Intent intent,
    @NonNull int requestCode,
    @NonNull Bundle options
)

finish

@NonNull
public void finish(@NonNull int resultCode, @NonNull Bundle results)

Ensures all activities launched in this instrumentation are finished before the instrumentation exits.

Subclasses who override this method should do their finish processing and then call super.finish to invoke this logic. Not waiting for all activities to finish() before exiting can cause device wide instability.

interceptActivityUsing

@NonNull
public void interceptActivityUsing(
    @NonNull InterceptingActivityFactory interceptingActivityFactory
)

Use the given InterceptingActivityFactory to create Activity instance in . This can be used to override default behavior of activity in tests e.g. mocking startService() method in Activity under test, to avoid starting the real service and instead verifying that a particular service was started.

Parameters
@NonNull InterceptingActivityFactory interceptingActivityFactory

InterceptingActivityFactory to be used for creating activity instance in newActivity

newActivity

@NonNull
public Activity newActivity(
    @NonNull Class<@NonNull Object> clazz,
    @NonNull Context context,
    @NonNull IBinder token,
    @NonNull Application application,
    @NonNull Intent intent,
    @NonNull ActivityInfo info,
    @NonNull CharSequence title,
    @NonNull Activity parent,
    @NonNull String id,
    @NonNull Object lastNonConfigurationInstance
)

newActivity

@NonNull
public Activity newActivity(
    @NonNull ClassLoader cl,
    @NonNull String className,
    @NonNull Intent intent
)

newApplication

@NonNull
public Application newApplication(
    @NonNull ClassLoader cl,
    @NonNull String className,
    @NonNull Context context
)

Does initialization before creating the application.

Note, newApplication is called before on API >15. For API <= 15, onCreate is called first.

onCreate

@NonNull
public void onCreate(@NonNull Bundle arguments)

Sets up lifecycle monitoring, and argument registry.

Subclasses must call up to onCreate(). This onCreate method does not call start() it is the subclasses responsibility to call start if it desires.

onDestroy

@NonNull
public void onDestroy()

onException

@NonNull
public boolean onException(@NonNull Object obj, @NonNull Throwable e)

onStart

@NonNull
public void onStart()

This implementation of onStart() will guarantee that the Application's onCreate method has completed when it returns.

Subclasses should call super.onStart() before executing any code that touches the application and it's state.

runOnMainSync

@NonNull
public void runOnMainSync(@NonNull Runnable runnable)

Posts a runnable to the main thread and blocks the caller's thread until the runnable is executed. When a Throwable is thrown in the runnable, the exception is propagated back to the caller's thread. If it is an unchecked throwable, it will be rethrown as is. If it is a checked exception, it will be rethrown as a RuntimeException.

Parameters
@NonNull Runnable runnable

a runnable to be executed on the main thread

startActivitySync

@NonNull
public Activity startActivitySync(@NonNull Intent intent)

useDefaultInterceptingActivityFactory

@NonNull
public void useDefaultInterceptingActivityFactory()

Use default mechanism of creating activity instance in newActivity