MonitoringInstrumentation
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.
Summary
Nested types |
|
---|---|
MonitoringInstrumentation.ActivityFinisher |
Loops through all the activities that have not yet finished and explicitly calls finish on them. |
Public constructors |
|
---|---|
Public functions |
|
---|---|
Unit |
callActivityOnCreate(activity: Activity, bundle: Bundle) |
Unit |
callActivityOnDestroy(activity: Activity) |
Unit |
callActivityOnPause(activity: Activity) |
Unit |
callActivityOnRestart(activity: Activity) |
Unit |
callActivityOnResume(activity: Activity) |
Unit |
callActivityOnStart(activity: Activity) |
Unit |
callActivityOnStop(activity: Activity) |
Unit |
callApplicationOnCreate(app: Application) |
Unit |
execStartActivities( |
ActivityResult |
execStartActivity( |
ActivityResult |
execStartActivity( |
ActivityResult |
execStartActivity( This API was added in Android API 23 (M) |
ActivityResult |
execStartActivity( This API was added in Android API 17 (JELLY_BEAN_MR1) |
ActivityResult |
execStartActivity( |
Unit |
Ensures all activities launched in this instrumentation are finished before the instrumentation exits. |
Unit |
interceptActivityUsing( Use the given InterceptingActivityFactory to create Activity instance in |
Activity |
newActivity( |
Activity |
newActivity(cl: ClassLoader, className: String, intent: Intent) |
Application |
newApplication(cl: ClassLoader, className: String, context: Context) Does initialization before creating the application. |
Unit |
onCreate(arguments: Bundle) Sets up lifecycle monitoring, and argument registry. |
Unit |
|
Boolean |
onException(obj: Any, e: Throwable) |
Unit |
onStart() This implementation of onStart() will guarantee that the Application's onCreate method has completed when it returns. |
Unit |
runOnMainSync(runnable: Runnable) Posts a runnable to the main thread and blocks the caller's thread until the runnable is executed. |
Activity |
startActivitySync(intent: Intent) |
Unit |
Use default mechanism of creating activity instance in |
Public constructors
Public functions
execStartActivities
fun execStartActivities(
who: Context,
contextThread: IBinder,
token: IBinder,
target: Activity,
intents: Array<Intent>,
options: Bundle
): Unit
execStartActivity
fun execStartActivity(
who: Context,
contextThread: IBinder,
token: IBinder,
target: Activity,
intent: Intent,
requestCode: Int
): ActivityResult
execStartActivity
fun execStartActivity(
who: Context,
contextThread: IBinder,
token: IBinder,
target: Activity,
intent: Intent,
requestCode: Int,
options: Bundle
): ActivityResult
execStartActivity
fun execStartActivity(
who: Context,
contextThread: IBinder,
token: IBinder,
target: String,
intent: Intent,
requestCode: Int,
options: Bundle
): ActivityResult
This API was added in Android API 23 (M)
execStartActivity
fun execStartActivity(
who: Context,
contextThread: IBinder,
token: IBinder,
target: Activity,
intent: Intent,
requestCode: Int,
options: Bundle,
user: UserHandle
): ActivityResult
This API was added in Android API 17 (JELLY_BEAN_MR1)
execStartActivity
fun execStartActivity(
who: Context,
contextThread: IBinder,
token: IBinder,
target: Fragment,
intent: Intent,
requestCode: Int,
options: Bundle
): ActivityResult
finish
fun finish(resultCode: Int, results: Bundle): Unit
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
fun interceptActivityUsing(
interceptingActivityFactory: InterceptingActivityFactory
): Unit
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 | |
---|---|
interceptingActivityFactory: InterceptingActivityFactory |
InterceptingActivityFactory to be used for creating activity instance in |
newActivity
fun newActivity(
clazz: Class<Any>,
context: Context,
token: IBinder,
application: Application,
intent: Intent,
info: ActivityInfo,
title: CharSequence,
parent: Activity,
id: String,
lastNonConfigurationInstance: Any
): Activity
newApplication
fun newApplication(cl: ClassLoader, className: String, context: Context): Application
Does initialization before creating the application.
Note, newApplication
is called before on API >15. For API <= 15,
onCreate
is called first.
onCreate
fun onCreate(arguments: Bundle): Unit
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.
onStart
fun onStart(): Unit
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
fun runOnMainSync(runnable: Runnable): Unit
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 |
useDefaultInterceptingActivityFactory
fun useDefaultInterceptingActivityFactory(): Unit
Use default mechanism of creating activity instance in newActivity