Skip to content

Commit

Permalink
Add support for 'debug' option
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
friederbluemle committed Oct 21, 2013
1 parent f1bfa94 commit c55f0d0
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
}

Expand Down Expand Up @@ -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));

Expand All @@ -142,6 +158,37 @@ public void onStart() {
}
}

/**
* Waits the specified time for a debugger to attach.
* <p />
* 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()) {
Expand Down

0 comments on commit c55f0d0

Please sign in to comment.