Summary: Nested Classes | Inherited Constants | Ctors | Methods | Protected Methods | Inherited Methods | [Expand All]

# MonitoringInstrumentation

`public class MonitoringInstrumentation`
`` `extends Instrumentation` ``

java.lang.Object
↳ | android.app.Instrumentation
| ↳ | android.support.test.runner.MonitoringInstrumentation

Known Direct Subclasses AndroidJUnitRunner
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 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`
`String` | `REPORT_KEY_IDENTIFIER`
`String` | `REPORT_KEY_STREAMRESULT`

### 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, Activity target, Intent intent, int requestCode, Bundle options)`
`Instrumentation.ActivityResult` | `execStartActivity(Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode)`
`Instrumentation.ActivityResult` | `execStartActivity(Context who, IBinder contextThread, IBinder token, Fragment target, Intent intent, int requestCode, Bundle options)`
`void` | `finish(int resultCode, Bundle results)` Ensures all activities launched in this instrumentation are finished before the instrumentation exits.
`Activity` | `newActivity(Class<?> clazz, Context context, IBinder token, Application application, Intent intent, ActivityInfo info, CharSequence title, Activity parent, String id, Object lastNonConfigurationInstance)`
`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)`

### Protected methods
`void` | `dumpThreadStateToOutputs(String outputFileName)`
`String` | `getThreadState()`
`final void` | `specifyDexMakerCacheProperty()`
`void` | `waitForActivitiesToComplete()` Ensures we've onStopped() all activities which were onStarted().

### Inherited methods
From class `android.app.Instrumentation`
`Instrumentation.ActivityMonitor` | `addMonitor(String arg0, Instrumentation.ActivityResult arg1, boolean arg2)`
`Instrumentation.ActivityMonitor` | `addMonitor(IntentFilter arg0, Instrumentation.ActivityResult arg1, boolean arg2)`
`void` | `addMonitor(Instrumentation.ActivityMonitor arg0)`
`void` | `callActivityOnCreate(Activity arg0, Bundle arg1)`
`void` | `callActivityOnCreate(Activity arg0, Bundle arg1, PersistableBundle arg2)`
`void` | `callActivityOnDestroy(Activity arg0)`
`void` | `callActivityOnNewIntent(Activity arg0, Intent arg1)`
`void` | `callActivityOnPause(Activity arg0)`
`void` | `callActivityOnPostCreate(Activity arg0, Bundle arg1, PersistableBundle arg2)`
`void` | `callActivityOnPostCreate(Activity arg0, Bundle arg1)`
`void` | `callActivityOnRestart(Activity arg0)`
`void` | `callActivityOnRestoreInstanceState(Activity arg0, Bundle arg1)`
`void` | `callActivityOnRestoreInstanceState(Activity arg0, Bundle arg1, PersistableBundle arg2)`
`void` | `callActivityOnResume(Activity arg0)`
`void` | `callActivityOnSaveInstanceState(Activity arg0, Bundle arg1, PersistableBundle arg2)`
`void` | `callActivityOnSaveInstanceState(Activity arg0, Bundle arg1)`
`void` | `callActivityOnStart(Activity arg0)`
`void` | `callActivityOnStop(Activity arg0)`
`void` | `callActivityOnUserLeaving(Activity arg0)`
`void` | `callApplicationOnCreate(Application arg0)`
`boolean` | `checkMonitorHit(Instrumentation.ActivityMonitor arg0, int arg1)`
`void` | `endPerformanceSnapshot()`
`void` | `finish(int arg0, Bundle arg1)`
`Bundle` | `getAllocCounts()`
`Bundle` | `getBinderCounts()`
`ComponentName` | `getComponentName()`
`Context` | `getContext()`
`Context` | `getTargetContext()`
`UiAutomation` | `getUiAutomation()`
`boolean` | `invokeContextMenuAction(Activity arg0, int arg1, int arg2)`
`boolean` | `invokeMenuActionSync(Activity arg0, int arg1, int arg2)`
`boolean` | `isProfiling()`
`Activity` | `newActivity(Class<?> arg0, Context arg1, IBinder arg2, Application arg3, Intent arg4, ActivityInfo arg5, CharSequence arg6, Activity arg7, String arg8, Object arg9)`
`Activity` | `newActivity(ClassLoader arg0, String arg1, Intent arg2)`
`Application` | `newApplication(ClassLoader arg0, String arg1, Context arg2)`
`static Application` | `newApplication(Class<?> arg0, Context arg1)`
`void` | `onCreate(Bundle arg0)`
`void` | `onDestroy()`
`boolean` | `onException(Object arg0, Throwable arg1)`
`void` | `onStart()`
`void` | `removeMonitor(Instrumentation.ActivityMonitor arg0)`
`void` | `runOnMainSync(Runnable arg0)`
`void` | `sendCharacterSync(int arg0)`
`void` | `sendKeyDownUpSync(int arg0)`
`void` | `sendKeySync(KeyEvent arg0)`
`void` | `sendPointerSync(MotionEvent arg0)`
`void` | `sendStatus(int arg0, Bundle arg1)`
`void` | `sendStringSync(String arg0)`
`void` | `sendTrackballEventSync(MotionEvent arg0)`
`void` | `setAutomaticPerformanceSnapshots()`
`void` | `setInTouchMode(boolean arg0)`
`void` | `start()`
`Activity` | `startActivitySync(Intent arg0)`
`void` | `startAllocCounting()`
`void` | `startPerformanceSnapshot()`
`void` | `startProfiling()`
`void` | `stopAllocCounting()`
`void` | `stopProfiling()`
`void` | `waitForIdle(Runnable arg0)`
`void` | `waitForIdleSync()`
`Activity` | `waitForMonitor(Instrumentation.ActivityMonitor arg0)`
`Activity` | `waitForMonitorWithTimeout(Instrumentation.ActivityMonitor arg0, long arg1)`
From class `java.lang.Object`
`Object` | `clone()`
`boolean` | `equals(Object arg0)`
`void` | `finalize()`
`final Class<?>` | `getClass()`
`int` | `hashCode()`
`final void` | `notify()`
`final void` | `notifyAll()`
`String` | `toString()`
`final void` | `wait(long arg0, int arg1)`
`final void` | `wait(long arg0)`
`final void` | `wait()`

## 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,
                    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)

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,
                    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` |

### 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`

### 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` |

### 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` |

## Protected methods

### dumpThreadStateToOutputs

    void dumpThreadStateToOutputs (String outputFileName)

Parameters
`outputFileName` | `String`

### getThreadState

    String getThreadState ()

Returns
`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.

- ## Classes

  - AndroidJUnit4
  - AndroidJUnitRunner
  - MonitoringInstrumentation
  - MonitoringInstrumentation.ActivityFinisher
  - UsageTrackerFacilitator