Implement issue #30977956: Enable Instrumentation tests for multi-process apps

New android:targetProcess attribute on <instrumentation> allows you to
specify the processes the instrumentation will run in.

This reworks how instrumentation is run in the activity manager to better
formalize its state and semantics, allowing us to keep track of it across
multiple processes.  This also clearly defines what happens when multiple
instrumentations are running at the same time, which is useful for writing
CTS tests that test the instrumentation APIs themselves.

Adds a couple new APIs to Instrumentation that helps with the new
situation where instrumentation can run concurrently in multiple processes.

Test: new CTS tests added (textXxxProcessInstrumentation in
ActivityManagerTest.java in cts/tests/app/src)

Change-Id: I2811e6c75bc98d4856045b2f0a45fb24af5d366f
This commit is contained in:
Dianne Hackborn
2017-01-31 15:27:13 -08:00
parent 7b6bcb6005
commit 340417356d
14 changed files with 376 additions and 82 deletions

View File

@@ -145,6 +145,7 @@ interface IActivityManager {
int flags, in Bundle arguments, in IInstrumentationWatcher watcher,
in IUiAutomationConnection connection, int userId,
in String abiOverride);
void addInstrumentationResults(in IApplicationThread target, in Bundle results);
void finishInstrumentation(in IApplicationThread target, int resultCode,
in Bundle results);
/**

View File

@@ -186,11 +186,25 @@ public class Instrumentation {
}
}
}
/**
* Report some results in the middle of instrumentation execution. Later results (including
* those provided by {@link #finish}) will be combined with {@link Bundle#putAll}.
*/
public void addResults(Bundle results) {
IActivityManager am = ActivityManager.getService();
try {
am.addInstrumentationResults(mThread.getApplicationThread(), results);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Terminate instrumentation of the application. This will cause the
* application process to exit, removing this instrumentation from the next
* time the application is started.
* time the application is started. If multiple processes are currently running
* for this instrumentation, all of those processes will be killed.
*
* @param resultCode Overall success/failure of instrumentation.
* @param results Any results to send back to the code that started the
@@ -277,6 +291,18 @@ public class Instrumentation {
return mAppContext;
}
/**
* Return the name of the process this instrumentation is running in. Note this should
* only be used for testing and debugging. If you are thinking about using this to,
* for example, conditionalize what is initialized in an Application class, it is strongly
* recommended to instead use lazy initialization (such as a getter for the state that
* only creates it when requested). This can greatly reduce the work your process does
* when created for secondary things, such as to receive a broadcast.
*/
public String getProcessName() {
return mThread.getProcessName();
}
/**
* Check whether this instrumentation was started with profiling enabled.
*

View File

@@ -33,6 +33,13 @@ public class InstrumentationInfo extends PackageItemInfo implements Parcelable {
*/
public String targetPackage;
/**
* Names of the process(es) this instrumentation will run in. If not specified, only
* runs in the main process of the targetPackage. Can either be a comma-separated list
* of process names or '*' for any process that launches to run targetPackage code.
*/
public String targetProcess;
/**
* Full path to the base APK for this application.
*/
@@ -113,6 +120,7 @@ public class InstrumentationInfo extends PackageItemInfo implements Parcelable {
public InstrumentationInfo(InstrumentationInfo orig) {
super(orig);
targetPackage = orig.targetPackage;
targetProcess = orig.targetProcess;
sourceDir = orig.sourceDir;
publicSourceDir = orig.publicSourceDir;
splitNames = orig.splitNames;
@@ -141,6 +149,7 @@ public class InstrumentationInfo extends PackageItemInfo implements Parcelable {
public void writeToParcel(Parcel dest, int parcelableFlags) {
super.writeToParcel(dest, parcelableFlags);
dest.writeString(targetPackage);
dest.writeString(targetProcess);
dest.writeString(sourceDir);
dest.writeString(publicSourceDir);
dest.writeStringArray(splitNames);
@@ -170,6 +179,7 @@ public class InstrumentationInfo extends PackageItemInfo implements Parcelable {
private InstrumentationInfo(Parcel source) {
super(source);
targetPackage = source.readString();
targetProcess = source.readString();
sourceDir = source.readString();
publicSourceDir = source.readString();
splitNames = source.readStringArray();

View File

@@ -1623,6 +1623,7 @@ public class PackageParser {
return parseApkLite(apkPath, parser, attrs, flags, signatures, certificates);
} catch (XmlPullParserException | IOException | RuntimeException e) {
Slog.w(TAG, "Failed to parse " + apkPath, e);
throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION,
"Failed to parse " + apkPath, e);
} finally {
@@ -3160,6 +3161,10 @@ public class PackageParser {
com.android.internal.R.styleable.AndroidManifestInstrumentation_targetPackage);
a.info.targetPackage = str != null ? str.intern() : null;
str = sa.getNonResourceString(
com.android.internal.R.styleable.AndroidManifestInstrumentation_targetProcess);
a.info.targetProcess = str != null ? str.intern() : null;
a.info.handleProfiling = sa.getBoolean(
com.android.internal.R.styleable.AndroidManifestInstrumentation_handleProfiling,
false);