MonitoringInstrumentation

public class MonitoringInstrumentation
extends ExposedInstrumentationApi

java.lang.Object
   ↳ android.app.Instrumentation
     ↳ android.support.test.internal.runner.hidden.ExposedInstrumentationApi
       ↳ android.support.test.runner.MonitoringInstrumentation
Known Direct Subclasses


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 classes

class MonitoringInstrumentation.ActivityFinisher

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

Inherited constants

From class android.app.Instrumentation

Public constructors

MonitoringInstrumentation()

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)

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

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

Instrumentation.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)

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

Instrumentation.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(ClassLoader, String, Intent).

Activity newActivity(Class<?> 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)
void onCreate(Bundle arguments)

Sets up lifecycle monitoring, and argument registry.

void onDestroy()
boolean onException(Object obj, Throwable e)
void onStart()

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

Activity startActivitySync(Intent intent)
void useDefaultInterceptingActivityFactory()

Use default mechanism of creating activity instance in newActivity(ClassLoader, String, Intent)

Protected methods

void dumpThreadStateToOutputs(String outputFileName)
String getThreadState()
void installMultidex()
void installOldMultiDex(Class<?> multidexClass)

Perform application MultiDex installation only when instrumentation installation is not available.

boolean isPrimaryInstrProcess(String argsProcessName)

Checks whether this instance of instrumentation should be considered as a primary instrumentation process.

final void setJsBridgeClassName(String className)
void specifyDexMakerCacheProperty()
void waitForActivitiesToComplete()

Ensures we've onStopped() all activities which were onStarted().

Inherited methods

From class android.support.test.internal.runner.hidden.ExposedInstrumentationApi
From class android.app.Instrumentation
From class java.lang.Object

Public constructors

MonitoringInstrumentation

MonitoringInstrumentation ()

Public methods

callActivityOnCreate

void callActivityOnCreate (Activity activity, 
                Bundle bundle)

Parameters
activity Activity

bundle Bundle

callActivityOnDestroy

void callActivityOnDestroy (Activity activity)

Parameters
activity Activity

callActivityOnPause

void callActivityOnPause (Activity activity)

Parameters
activity Activity

callActivityOnRestart

void callActivityOnRestart (Activity activity)

Parameters
activity Activity

callActivityOnResume

void callActivityOnResume (Activity activity)

Parameters
activity Activity

callActivityOnStart

void callActivityOnStart (Activity activity)

Parameters
activity Activity

callActivityOnStop

void callActivityOnStop (Activity activity)

Parameters
activity Activity

callApplicationOnCreate

void callApplicationOnCreate (Application app)

Parameters
app Application

execStartActivities

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

Parameters
who Context

contextThread IBinder

token IBinder

target Activity

intents Intent

options Bundle

execStartActivity

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

Parameters
who Context

contextThread IBinder

token IBinder

target Fragment

intent Intent

requestCode int

options Bundle

Returns
Instrumentation.ActivityResult

execStartActivity

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

Parameters
who Context

contextThread IBinder

token IBinder

target Activity

intent Intent

requestCode int

options Bundle

Returns
Instrumentation.ActivityResult

execStartActivity

Instrumentation.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)

Parameters
who Context

contextThread IBinder

token IBinder

target Activity

intent Intent

requestCode int

options Bundle

user UserHandle

Returns
Instrumentation.ActivityResult

execStartActivity

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

Parameters
who Context

contextThread IBinder

token IBinder

target Activity

intent Intent

requestCode int

Returns
Instrumentation.ActivityResult

execStartActivity

Instrumentation.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)

Parameters
who Context

contextThread IBinder

token IBinder

target String

intent Intent

requestCode int

options Bundle

Returns
Instrumentation.ActivityResult

finish

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.

Parameters
resultCode int

results Bundle

interceptActivityUsing

void interceptActivityUsing (InterceptingActivityFactory interceptingActivityFactory)

Use the given InterceptingActivityFactory to create Activity instance in newActivity(ClassLoader, String, Intent). 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(ClassLoader, String, Intent)

newActivity

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

Parameters
clazz Class

context Context

token IBinder

application Application

intent Intent

info ActivityInfo

title CharSequence

parent Activity

id String

lastNonConfigurationInstance Object

Returns
Activity

Throws
InstantiationException
IllegalAccessException

newActivity

Activity newActivity (ClassLoader cl, 
                String className, 
                Intent intent)

Parameters
cl ClassLoader

className String

intent Intent

Returns
Activity

Throws
InstantiationException
IllegalAccessException
ClassNotFoundException

onCreate

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.

Parameters
arguments Bundle

onDestroy

void onDestroy ()

onException

boolean onException (Object obj, 
                Throwable e)

Parameters
obj Object

e Throwable

Returns
boolean

onStart

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.

startActivitySync

Activity startActivitySync (Intent intent)

Parameters
intent Intent

Returns
Activity

useDefaultInterceptingActivityFactory

void useDefaultInterceptingActivityFactory ()

Use default mechanism of creating activity instance in newActivity(ClassLoader, String, Intent)

Protected methods

dumpThreadStateToOutputs

void dumpThreadStateToOutputs (String outputFileName)

Parameters
outputFileName String

getThreadState

String getThreadState ()

Returns
String

installMultidex

void installMultidex ()

installOldMultiDex

void installOldMultiDex (Class<?> multidexClass)

Perform application MultiDex installation only when instrumentation installation is not available. Called when MultiDex class is available but MultiDex.installInstrumentation is not.

Parameters
multidexClass Class

Throws
NoSuchMethodException
InvocationTargetException
IllegalAccessException

isPrimaryInstrProcess

boolean isPrimaryInstrProcess (String argsProcessName)

Checks whether this instance of instrumentation should be considered as a primary instrumentation process.

Parameters
argsProcessName String: the process name passed in via test runner args

Returns
boolean true if the given process is the primary instrumentation process

setJsBridgeClassName

void setJsBridgeClassName (String className)

Parameters
className String

specifyDexMakerCacheProperty

void specifyDexMakerCacheProperty ()

waitForActivitiesToComplete

void waitForActivitiesToComplete ()

Ensures we've onStopped() all activities which were onStarted().

According to Activity's contract, the process is not killable between onStart and onStop. Breaking this contract (which finish() will if you let it) can cause bad behaviour (including a full restart of system_server).

We give the app 2 seconds to stop all its activities, then we proceed.

This should never be run on the main thread.