Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 702793965
DeviceInfra authored and copybara-github committed Dec 4, 2024
1 parent a590c38 commit ad3bda7
Showing 3 changed files with 101 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -504,63 +504,72 @@ private void syncRun(
}

// Checks the result.
StringBuilder errorMsg = new StringBuilder();
AndroidInstrumentationParser parser = new AndroidInstrumentationParser();
TestResult result = parser.parseOutput(output, errorMsg, mhException);
switch (result) {
case PASS:
hasPass = true;
break;
case FAIL:
hasFail = true;
failResultCause =
new MobileHarnessException(
AndroidErrorId.ANDROID_INSTRUMENTATION_TEST_FAILED,
"Instrumentation failures: "
+ errorMsg
+ "\nThis usually indicates validation errors of the test app, or "
+ "bugs of the app under test. You should be able to reproduce it with "
+ "\"adb shell am instrument ...\" with your local devices, without "
+ "Mobile Harness.",
mhException);
break;
case ERROR:
hasError = true;
errorResultCause =
new MobileHarnessException(
AndroidErrorId.ANDROID_INSTRUMENTATION_TEST_ERROR,
"Instrumentation start/finish unexpectedly: " + errorMsg,
mhException);
break;
case INFRA_ERROR:
hasInfraError = true;
infraErrorResultCause =
new MobileHarnessException(
AndroidErrorId.ANDROID_INSTRUMENTATION_TEST_INFRA_ERROR,
"Instrumentation start/finish unexpectedly: " + errorMsg,
mhException);
break;
case TIMEOUT:
MobileHarnessException timeoutCause =
new MobileHarnessException(
AndroidErrorId.ANDROID_INSTRUMENTATION_TEST_TIMEOUT,
String.format(
"Instrumentation timeout [please try to increase test_timeout_sec"
+ ", or instrument_timeout_sec if it has been set.]:%n%s",
errorMsg),
mhException);
boolean ignoreInstrumentOutput =
testInfo
.resultWithCause()
.setNonPassing(
com.google.devtools.mobileharness.api.model.proto.Test.TestResult.TIMEOUT,
timeoutCause);
throw timeoutCause;
default:
testInfo.result().set(TestResult.ERROR);
throw new MobileHarnessException(
AndroidErrorId.ANDROID_INSTRUMENTATION_TEST_RESULT_NOT_FOUND,
"Unexpected Test Result Found",
mhException);
.jobInfo()
.params()
.getBool(AndroidInstrumentationDriverSpec.PARAM_IGNORE_INSTRUMENT_OUTPUT, false);
TestResult result = null;

if (!ignoreInstrumentOutput) {
StringBuilder errorMsg = new StringBuilder();
AndroidInstrumentationParser parser = new AndroidInstrumentationParser();
result = parser.parseOutput(output, errorMsg, mhException);
switch (result) {
case PASS:
hasPass = true;
break;
case FAIL:
hasFail = true;
failResultCause =
new MobileHarnessException(
AndroidErrorId.ANDROID_INSTRUMENTATION_TEST_FAILED,
"Instrumentation failures: "
+ errorMsg
+ "\nThis usually indicates validation errors of the test app, or "
+ "bugs of the app under test. You should be able to reproduce it with "
+ "\"adb shell am instrument ...\" with your local devices, without "
+ "Mobile Harness.",
mhException);
break;
case ERROR:
hasError = true;
errorResultCause =
new MobileHarnessException(
AndroidErrorId.ANDROID_INSTRUMENTATION_TEST_ERROR,
"Instrumentation start/finish unexpectedly: " + errorMsg,
mhException);
break;
case INFRA_ERROR:
hasInfraError = true;
infraErrorResultCause =
new MobileHarnessException(
AndroidErrorId.ANDROID_INSTRUMENTATION_TEST_INFRA_ERROR,
"Instrumentation start/finish unexpectedly: " + errorMsg,
mhException);
break;
case TIMEOUT:
MobileHarnessException timeoutCause =
new MobileHarnessException(
AndroidErrorId.ANDROID_INSTRUMENTATION_TEST_TIMEOUT,
String.format(
"Instrumentation timeout [please try to increase test_timeout_sec"
+ ", or instrument_timeout_sec if it has been set.]:%n%s",
errorMsg),
mhException);
testInfo
.resultWithCause()
.setNonPassing(
com.google.devtools.mobileharness.api.model.proto.Test.TestResult.TIMEOUT,
timeoutCause);
throw timeoutCause;
default:
testInfo.result().set(TestResult.ERROR);
throw new MobileHarnessException(
AndroidErrorId.ANDROID_INSTRUMENTATION_TEST_RESULT_NOT_FOUND,
"Unexpected Test Result Found",
mhException);
}
}

// Extra adb shell commands or clean up after instrument.
@@ -597,8 +606,10 @@ private void syncRun(
}
if (hasSequentialOptionMaps) {
// Set result property.
properties.add(
AndroidInstrumentationDriverSpec.PROPERTY_RESULT + "_" + optionMapIdx, result.name());
if (result != null) {
properties.add(
AndroidInstrumentationDriverSpec.PROPERTY_RESULT + "_" + optionMapIdx, result.name());
}
try {
// Cleanup.
if (iterClearBuild) {
Original file line number Diff line number Diff line change
@@ -97,6 +97,14 @@ public interface AndroidInstrumentationDriverSpec {
+ "No effect if large than test timeout setting.")
String PARAM_INSTRUMENT_TIMEOUT_SEC = "instrument_timeout_sec";

@ParamAnnotation(
required = false,
help =
"Whether to skip instrumentation code parsing. By default, this is false. Use it"
+ " carefully to make sure you have plugins later to set the test result, otherwise"
+ " the test will fail due to no result provided at the end.")
String PARAM_IGNORE_INSTRUMENT_OUTPUT = "ignore_instrument_output";

/**
* adb shell command before each repeat, within each option map. The parameter name should be
* followed by the option map index.
Original file line number Diff line number Diff line change
@@ -251,6 +251,30 @@ public void run_noIsolatedStorage() throws Exception {
instrumentationLog);
}

@Test
public void run_ignoreInstrumentationOutput() throws Exception {
mockRunTestBasicSteps(TEST_NAME, OPTIONS, false, false);
jobInfo.params().add(AndroidInstrumentationDriverSpec.PARAM_IGNORE_INSTRUMENT_OUTPUT, "true");
String instrumentationLog = "INSTRUMENTATION_CODE: -1\n";

AndroidInstrumentationSetting setting =
mockInstrumentSetting(
TEST_NAME,
OPTION_MAP,
/* async= */ false,
/* showRawResults= */ false,
/* prefixAndroidTest= */ true,
/* noIsolatedStorage= */ true,
/* useTestStorageService= */ true);
when(androidInstrumentationUtil.instrument(
DEVICE_ID, DEFAULT_SDK_VERSION, setting, TEST_TIMEOUT))
.thenReturn(instrumentationLog);

driver.run(testInfo);

assertThat(testInfo.resultWithCause().get().type()).isEqualTo(TestResult.UNKNOWN);
}

@Test
public void run_showRawData_pass() throws Exception {
mockRunTestBasicSteps(TEST_PACKAGE + "." + TEST_CLASS_NAME, OPTIONS, true, false);

0 comments on commit ad3bda7

Please sign in to comment.