Skip to content

Commit

Permalink
Show more stack trace in instrumentation tests.
Browse files Browse the repository at this point in the history
When unknown exceptions are raised during instrumentation tests,
catch the exception and output its stack trace as part of the log
rather than just displaying 'CRASHED' or 'UNKNOWN' statuses.

This should also show up in buildbot logs.

BUG=567841

Review URL: https://codereview.chromium.org/1514453007

Cr-Commit-Position: refs/heads/master@{#365586}
  • Loading branch information
wnwen authored and Commit bot committed Dec 16, 2015
1 parent 57ff58f commit e6e09ec
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ def testGenerateTestResults_testFailed(self):
self.assertEqual(1, len(results))
self.assertEqual(base_test_result.ResultType.FAIL, results[0].GetType())

def testGenerateTestResults_testUnknownException(self):
stacktrace = 'long\nstacktrace'
statuses = [
(1, {
'class': 'test.package.TestClass',
'test': 'testMethod',
}),
(-1, {
'class': 'test.package.TestClass',
'test': 'testMethod',
'stack': stacktrace,
}),
]
results = instrumentation_test_instance.GenerateTestResults(
None, None, statuses, 0, 1000)
self.assertEqual(1, len(results))
self.assertEqual(base_test_result.ResultType.FAIL, results[0].GetType())
self.assertEqual(stacktrace, results[0].GetLog())


if __name__ == '__main__':
unittest.main(verbosity=2)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public static void configureStrictMode() {
if ("death".equals(commandLine.getSwitchValue(ChromeSwitches.STRICT_MODE))) {
threadPolicy = threadPolicy.penaltyDeath();
vmPolicy = vmPolicy.penaltyDeath();
} else if ("testing".equals(commandLine.getSwitchValue(ChromeSwitches.STRICT_MODE))) {
threadPolicy = threadPolicy.penaltyDeath();
// Currently VmDeathPolicy kills the process, and is not visible on bot test output.
}
StrictMode.setThreadPolicy(threadPolicy.build());
StrictMode.setVmPolicy(vmPolicy.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Browser;
import android.test.InstrumentationTestRunner;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
Expand Down Expand Up @@ -118,19 +120,40 @@ public ChromeActivityTestCaseBase(Class<T> activityClass) {
protected boolean mSkipClearAppData = false;
protected boolean mSkipCheckHttpServer = false;

private Thread.UncaughtExceptionHandler mDefaultUncaughtExceptionHandler;

private class ChromeUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
String stackTrace = Log.getStackTraceString(e);
if (e.getClass().getName().endsWith("StrictModeViolation")) {
stackTrace += "\nSearch logcat for \"StrictMode policy violation\" for full stack.";
}
Bundle resultsBundle = new Bundle();
resultsBundle.putString(InstrumentationTestRunner.REPORT_KEY_NAME_CLASS,
getClass().getName());
resultsBundle.putString(InstrumentationTestRunner.REPORT_KEY_NAME_TEST, getName());
resultsBundle.putString(InstrumentationTestRunner.REPORT_KEY_STACK, stackTrace);
getInstrumentation().sendStatus(-1, resultsBundle);
mDefaultUncaughtExceptionHandler.uncaughtException(t, e);
}
}

@Override
protected void setUp() throws Exception {
super.setUp();
mDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new ChromeUncaughtExceptionHandler());
ApplicationTestUtils.setUp(
getInstrumentation().getTargetContext(), !mSkipClearAppData, !mSkipCheckHttpServer);

setActivityInitialTouchMode(false);
startMainActivity();
}

@Override
protected void tearDown() throws Exception {
ApplicationTestUtils.tearDown(getInstrumentation().getTargetContext());
Thread.setDefaultUncaughtExceptionHandler(mDefaultUncaughtExceptionHandler);
super.tearDown();
}

Expand Down

0 comments on commit e6e09ec

Please sign in to comment.