Skip to content

Commit 847760c

Browse files
Introduce instrumentation params proxying
* Previously, the orchestrator would pass instrumentation params as bundle params (eg: `--no-hidden-api-checks`) would be used to execute the orchestrator itself and not to execute the APK under test which made it impossible to run the APK under test with special instrumentation params. * Introduce a new flag that allows user to specify which instrumentation params they wish to proxy to the APK under test, the new flag would accept a comma delimited string that would simply remove trailing & leading spaces then pass the strings as-is to the instrumentation. This is implemented this way to minimize touching flags passed from the user as much as possible. PiperOrigin-RevId: 610486983
1 parent 2ce243d commit 847760c

File tree

5 files changed

+87
-12
lines changed

5 files changed

+87
-12
lines changed

runner/android_test_orchestrator/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88

99
**New Features**
1010

11+
* Introduce Instrumentation Params Proxying which allows the user to proxy
12+
instrumentation arguments to the APK under test (eg: --no-hidden-api-checks).
13+
1114
**Breaking Changes**
1215

1316
**API Changes**
1417

1518
**Breaking API Changes**
1619

1720
**Known Issues**
21+

runner/android_test_orchestrator/java/androidx/test/orchestrator/AndroidTestOrchestrator.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@
118118
*
119119
* <p>Pass {@code -e orchestratorDebug} flag if you need to debug orchestrator itself. Note, to
120120
* debug test code you still need to pass {@code -e debug}.
121+
*
122+
* <p>Pass {@code -e orchestratorInstrumentationArgs "params"} to pass {@code params} to the
123+
* instrumentation invocation directly instead of adding them to the bundle, {@code params} are
124+
* expected to be comma delimited parameters (eg: {@code -e orchestratorInstrumentationArgs
125+
* "--no-hidden-api-checks, --no-isolated-storage"})
121126
*/
122127
public final class AndroidTestOrchestrator extends android.app.Instrumentation
123128
implements RunFinishedListener {
@@ -452,17 +457,16 @@ private Bundle createResultBundle() {
452457
@Override
453458
public void finish(int resultCode, Bundle results) {
454459

455-
try {
456-
super.finish(resultCode, results);
457-
} catch (SecurityException e) {
458-
Log.e(TAG, "Security exception thrown on shutdown", e);
459-
// On API Level 18 a security exception can be occasionally thrown when calling finish
460-
// with a result bundle taken from a remote message. Recreating the result bundle and
461-
// retrying finish has a high probability of suppressing the flake.
462-
results = createResultBundle();
463-
super.finish(resultCode, results);
464-
}
465-
460+
try {
461+
super.finish(resultCode, results);
462+
} catch (SecurityException e) {
463+
Log.e(TAG, "Security exception thrown on shutdown", e);
464+
// On API Level 18 a security exception can be occasionally thrown when calling finish
465+
// with a result bundle taken from a remote message. Recreating the result bundle and
466+
// retrying finish has a high probability of suppressing the flake.
467+
results = createResultBundle();
468+
super.finish(resultCode, results);
469+
}
466470
}
467471

468472
@Override

runner/android_test_orchestrator/java/androidx/test/orchestrator/OrchestratorConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ final class OrchestratorConstants {
3030
static final String AJUR_COVERAGE = "coverage";
3131
static final String AJUR_COVERAGE_FILE = "coverageFile";
3232

33+
// Holds a comma separated listed of args that will be passed to the instrumentation invocation
34+
// directly.
35+
static final String ORCHESTRATOR_FORWARDED_INSTRUMENTATION_ARGS =
36+
"orchestratorInstrumentationArgs";
37+
3338
private OrchestratorConstants() {
3439
// Do not initialize
3540
}

runner/android_test_orchestrator/java/androidx/test/orchestrator/TestRunnable.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static androidx.test.orchestrator.OrchestratorConstants.AJUR_CLASS_ARGUMENT;
1919
import static androidx.test.orchestrator.OrchestratorConstants.AJUR_LIST_TESTS_ARGUMENT;
2020
import static androidx.test.orchestrator.OrchestratorConstants.ISOLATED_ARGUMENT;
21+
import static androidx.test.orchestrator.OrchestratorConstants.ORCHESTRATOR_FORWARDED_INSTRUMENTATION_ARGS;
2122
import static androidx.test.orchestrator.OrchestratorConstants.TARGET_INSTRUMENTATION_ARGUMENT;
2223

2324
import android.content.Context;
@@ -184,19 +185,36 @@ private Bundle getTargetInstrumentationArguments() {
184185
return targetArgs;
185186
}
186187

188+
/**
189+
* Instrumentation params are delimited by comma, each param is stripped from leading and trailing
190+
* whitespace.
191+
*/
192+
private List<String> getInstrumentationParamsAndRemoveBundleArgs(Bundle arguments) {
193+
List<String> cleanedParams = new ArrayList<>();
194+
String forwardedArgs = arguments.getString(ORCHESTRATOR_FORWARDED_INSTRUMENTATION_ARGS);
195+
if (forwardedArgs != null) {
196+
for (String param : forwardedArgs.split(",")) {
197+
cleanedParams.add(param.strip());
198+
}
199+
arguments.remove(ORCHESTRATOR_FORWARDED_INSTRUMENTATION_ARGS);
200+
}
201+
return cleanedParams;
202+
}
203+
187204
private List<String> buildShellParams(Bundle arguments) throws IOException, ClientNotConnected {
188205
List<String> params = new ArrayList<>();
189206
params.add("instrument");
190207
params.add("-w");
191208
params.add("-r");
192209

210+
params.addAll(getInstrumentationParamsAndRemoveBundleArgs(arguments));
211+
193212
for (String key : arguments.keySet()) {
194213
params.add("-e");
195214
params.add(key);
196215
params.add(arguments.getString(key));
197216
}
198217
params.add(getTargetInstrumentation());
199-
200218
return params;
201219
}
202220

runner/android_test_orchestrator/javatests/androidx/test/orchestrator/TestRunnableTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,50 @@ public void testRun_buildsParams_givenNullClassNameAndMethodForTestCollection()
168168
"-e class com.google.android.example.MyClass");
169169
}
170170

171+
@Test
172+
public void testRun_buildsParams_givenInstrumentationParamsAreHandledCorrectlyWhenTrue() {
173+
arguments.putString(
174+
"orchestratorInstrumentationArgs", "--no-hidden-api-checks, --no-isolated-storage");
175+
FakeListener listener = new FakeListener();
176+
FakeTestRunnable runnable =
177+
new FakeTestRunnable(context, "secret", arguments, outputStream, listener, null, true);
178+
runnable.run();
179+
assertContainsRunnerArgs(runnable.params, "-no-hidden-api-checks --no-isolated-storage");
180+
}
181+
182+
@Test
183+
public void testRun_buildsParams_givenInstrumentationParamsAreHandledCorrectlyWithSpace() {
184+
arguments.putString(
185+
"orchestratorInstrumentationArgs",
186+
"--no-hidden-api-checks, --no-isolated-storage, --abi x86_64");
187+
FakeListener listener = new FakeListener();
188+
FakeTestRunnable runnable =
189+
new FakeTestRunnable(context, "secret", arguments, outputStream, listener, null, true);
190+
runnable.run();
191+
assertContainsRunnerArgs(
192+
runnable.params, "-no-hidden-api-checks --no-isolated-storage --abi x86_64");
193+
}
194+
195+
@Test
196+
public void testRun_buildsParams_givenInstrumentationParamsAreHandledCorrectlySingleParam() {
197+
arguments.putString("orchestratorInstrumentationArgs", "--no-hidden-api-checks");
198+
FakeListener listener = new FakeListener();
199+
FakeTestRunnable runnable =
200+
new FakeTestRunnable(context, "secret", arguments, outputStream, listener, null, true);
201+
runnable.run();
202+
assertContainsRunnerArgs(runnable.params, "--no-hidden-api-checks");
203+
}
204+
205+
@Test
206+
public void testRun_buildsParams_givenInstrumentationParamsAreHandledCorrectlyMultipleParam() {
207+
arguments.putString("orchestratorInstrumentationArgs", "--A,--B,--C,--key value");
208+
FakeListener listener = new FakeListener();
209+
FakeTestRunnable runnable =
210+
new FakeTestRunnable(context, "secret", arguments, outputStream, listener, null, true);
211+
runnable.run();
212+
assertContainsRunnerArgs(runnable.params, "--A --B --C --key value");
213+
}
214+
171215
private static void assertContainsRunnerArgs(List<String> params, String... containsArgs) {
172216
String cmdArgs = Joiner.on(" ").join(params);
173217
assertThat(cmdArgs, startsWith("instrument -w -r"));

0 commit comments

Comments
 (0)