Stay organized with collections Save and categorize content based on your preferences.

MonitoringInstrumentation

public class MonitoringInstrumentation

Known direct subclasses
AndroidJUnitRunner

An Instrumentation that runs JUnit3 and JUnit4 tests against an Android package (application).


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

public class MonitoringInstrumentation.ActivityFinisher implements Runnable

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

Public constructors

Public methods

void
callActivityOnCreate(Activity activity, Bundle bundle)
void
callActivityOnDestroy(Activity activity)
void
callActivityOnPause(Activity activity)
void
callActivityOnRestart(Activity activity)
void
callActivityOnResume(Activity activity)
void
callActivityOnStart(Activity activity)
void
callActivityOnStop(Activity activity)
void
callApplicationOnCreate(Application app)
void
execStartActivities(
    Context who,
    IBinder contextThread,
    IBinder token,
    Activity target,
    Intent[] intents,
    Bundle options
)
ActivityResult
execStartActivity(
    Context who,
    IBinder contextThread,
    IBinder token,
    Activity target,
    Intent intent,
    int requestCode,
    Bundle options,
    UserHandle user
)

This API was added in Android API 17 (JELLY_BEAN_MR1)

ActivityResult
execStartActivity(
    Context who,
    IBinder contextThread,
    IBinder token,
    Activity target,
    Intent intent,
    int requestCode,
    Bundle options
)
ActivityResult
execStartActivity(
    Context who,
    IBinder contextThread,
    IBinder token,
    Activity target,
    Intent intent,
    int requestCode
)
ActivityResult
execStartActivity(
    Context who,
    IBinder contextThread,
    IBinder token,
    Fragment target,
    Intent intent,
    int requestCode,
    Bundle options
)
ActivityResult
execStartActivity(
    Context who,
    IBinder contextThread,
    IBinder token,
    String target,
    Intent intent,
    int requestCode,
    Bundle options
)

This API was added in Android API 23 (M)

void
finish(int resultCode, Bundle results)

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

void
interceptActivityUsing(
    InterceptingActivityFactory interceptingActivityFactory
)

Use the given InterceptingActivityFactory to create Activity instance in newActivity.

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

Does initialization before creating the application.

void
onCreate(Bundle arguments)

Sets up lifecycle monitoring, and argument registry.

void
boolean
void

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

void

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

Activity
startActivitySync(Intent intent)
void

Use default mechanism of creating activity instance in newActivity

Public constructors

MonitoringInstrumentation

public MonitoringInstrumentation()

Public methods

callActivityOnCreate

public void callActivityOnCreate(Activity activity, Bundle bundle)

callActivityOnDestroy

public void callActivityOnDestroy(Activity activity)

callActivityOnPause

public void callActivityOnPause(Activity activity)

callActivityOnRestart

public void callActivityOnRestart(Activity activity)

callActivityOnResume

public void callActivityOnResume(Activity activity)

callActivityOnStart

public void callActivityOnStart(Activity activity)

callActivityOnStop

public void callActivityOnStop(Activity activity)

callApplicationOnCreate

public void callApplicationOnCreate(Application app)

execStartActivities

public void execStartActivities(
    Context who,
    IBinder contextThread,
    IBinder token,
    Activity target,
    Intent[] intents,
    Bundle options
)

execStartActivity

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

This API was added in Android API 17 (JELLY_BEAN_MR1)

execStartActivity

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

execStartActivity

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

execStartActivity

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

execStartActivity

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

This API was added in Android API 23 (M)

finish

public void finish(int resultCode, 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

public void interceptActivityUsing(
    InterceptingActivityFactory interceptingActivityFactory
)

Use the given InterceptingActivityFactory to create Activity instance in newActivity. 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
InterceptingActivityFactory interceptingActivityFactory

InterceptingActivityFactory to be used for creating activity instance in newActivity

newActivity

public Activity newActivity(
    Class<Object> clazz,
    Context context,
    IBinder token,
    Application application,
    Intent intent,
    ActivityInfo info,
    CharSequence title,
    Activity parent,
    String id,
    Object lastNonConfigurationInstance
)
Throws
java.lang.IllegalAccessException java.lang.IllegalAccessException
java.lang.InstantiationException java.lang.InstantiationException

newActivity

public Activity newActivity(ClassLoader cl, String className, Intent intent)
Throws
java.lang.ClassNotFoundException java.lang.ClassNotFoundException
java.lang.IllegalAccessException java.lang.IllegalAccessException
java.lang.InstantiationException java.lang.InstantiationException

newApplication

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

Does initialization before creating the application.

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

Throws
java.lang.ClassNotFoundException java.lang.ClassNotFoundException
java.lang.IllegalAccessException java.lang.IllegalAccessException
java.lang.InstantiationException java.lang.InstantiationException

onCreate

public void onCreate(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

public void onDestroy()

onException

public boolean onException(Object obj, Throwable e)

onStart

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

public void runOnMainSync(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
Runnable runnable

a runnable to be executed on the main thread

startActivitySync

public Activity startActivitySync(Intent intent)

useDefaultInterceptingActivityFactory

public void useDefaultInterceptingActivityFactory()

Use default mechanism of creating activity instance in newActivity