From c55f0d039a115ab3ae3712cf520f9d6e95f47bb8 Mon Sep 17 00:00:00 2001 From: Frieder Bluemle Date: Fri, 11 Oct 2013 19:00:37 -0700 Subject: [PATCH] Add support for 'debug' option This adds support to wait for a debugger to attach at instrumentation start. 'debug' can either be set to true or a number which represents the timeout in milliseconds. Default timeout is 10 seconds. --- .../api/android/CucumberInstrumentation.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/android/src/main/java/cucumber/api/android/CucumberInstrumentation.java b/android/src/main/java/cucumber/api/android/CucumberInstrumentation.java index a9f5c948c4..9731851d1e 100644 --- a/android/src/main/java/cucumber/api/android/CucumberInstrumentation.java +++ b/android/src/main/java/cucumber/api/android/CucumberInstrumentation.java @@ -36,9 +36,11 @@ public class CucumberInstrumentation extends Instrumentation { public static final String REPORT_VALUE_ID = "CucumberInstrumentation"; public static final String REPORT_KEY_NUM_TOTAL = "numtests"; + public static final int DEFAULT_DEBUGGER_TIMEOUT = 10000; public static final String TAG = "cucumber-android"; private final Bundle results = new Bundle(); + private int debuggerTimeout; private boolean justCount; private int testCount; @@ -54,6 +56,16 @@ public void onCreate(Bundle arguments) { super.onCreate(arguments); if (arguments != null) { + String debug = arguments.getString("debug"); + if (debug != null) { + try { + debuggerTimeout = Integer.parseInt(debug); + } catch (NumberFormatException e) { + if (Boolean.parseBoolean(debug)) { + debuggerTimeout = DEFAULT_DEBUGGER_TIMEOUT; + } + } + } justCount = getBooleanArgument(arguments, "count"); } @@ -123,6 +135,10 @@ public void onStart() { results.putInt(REPORT_KEY_NUM_TOTAL, testCount); finish(Activity.RESULT_OK, results); } else { + if (debuggerTimeout != 0) { + waitForDebugger(debuggerTimeout); + } + runtimeOptions.getFormatters().add(new AndroidInstrumentationReporter(runtime, this, testCount)); runtimeOptions.getFormatters().add(new AndroidLogcatReporter(TAG)); @@ -142,6 +158,37 @@ public void onStart() { } } + /** + * Waits the specified time for a debugger to attach. + *

+ * For some reason {@link Debug#waitForDebugger()} is not blocking and thinks a debugger is + * attached when there isn't. + * + * @param timeout the time in milliseconds to wait + */ + private void waitForDebugger(int timeout) { + System.out.println("waiting " + timeout + "ms for debugger to attach."); + long elapsed = 0; + while (!Debug.isDebuggerConnected() && elapsed < timeout) { + try { + System.out.println("waiting for debugger to attach..."); + Thread.sleep(1000); + elapsed += 1000; + } catch (InterruptedException ie) { + } + } + if (Debug.isDebuggerConnected()) { + System.out.println("waiting for debugger to settle..."); + try { + Thread.sleep(1300); + } catch (InterruptedException e) { + } + System.out.println("debugger connected."); + } else { + System.out.println("no debugger connected."); + } + } + private void printSummary() { // TODO move this stuff into the AndroidLogcatReporter for (Throwable t : runtime.getErrors()) {