Bug 996313 - Add boolean for ending test on assertion failure in JavascriptMessageParser. r=nalexander

This commit is contained in:
Michael Comella 2014-04-24 12:23:20 -07:00
Родитель 313392a942
Коммит de7cef208f
3 изменённых файлов: 28 добавлений и 5 удалений

Просмотреть файл

@ -37,7 +37,8 @@ public class JavascriptTest extends BaseTest {
mAsserter.dumpLog("Loading JavaScript test from " + url); mAsserter.dumpLog("Loading JavaScript test from " + url);
loadUrl(url); loadUrl(url);
final JavascriptMessageParser testMessageParser = new JavascriptMessageParser(mAsserter); final JavascriptMessageParser testMessageParser =
new JavascriptMessageParser(mAsserter, false);
try { try {
while (!testMessageParser.isTestFinished()) { while (!testMessageParser.isTestFinished()) {
if (Log.isLoggable(LOGTAG, Log.VERBOSE)) { if (Log.isLoggable(LOGTAG, Log.VERBOSE)) {

Просмотреть файл

@ -110,8 +110,10 @@ public final class JavascriptBridge {
public JavascriptBridge(final Object target) { public JavascriptBridge(final Object target) {
mTarget = target; mTarget = target;
mMethods = target.getClass().getMethods(); mMethods = target.getClass().getMethods();
mLogParser = new JavascriptMessageParser(sAsserter);
mExpecter = sActions.expectGeckoEvent(EVENT_TYPE); mExpecter = sActions.expectGeckoEvent(EVENT_TYPE);
// The JS here is unrelated to a test harness, so we
// have our message parser end on assertion failure.
mLogParser = new JavascriptMessageParser(sAsserter, true);
} }
/** /**

Просмотреть файл

@ -34,9 +34,22 @@ public final class JavascriptMessageParser {
private String lastTestName = ""; private String lastTestName = "";
// Have we seen a message saying the test is finished? // Have we seen a message saying the test is finished?
private boolean testFinishedMessageSeen = false; private boolean testFinishedMessageSeen = false;
private final boolean endOnAssertionFailure;
public JavascriptMessageParser(final Assert asserter) { /**
* Constructs a message parser for test result messages sent from JavaScript. When seeing an
* assertion failure, the message parser can use the given {@link org.mozilla.gecko.Assert}
* instance to immediately end the test (typically if the underlying JS framework is not able
* to end the test itself) or to swallow the Errors - this functionality is determined by the
* <code>endOnAssertionFailure</code> parameter.
*
* @param asserter The Assert instance to which test results should be passed.
* @param endOnAssertionFailure
* true if the test should end if we see a JS assertion failure, false otherwise.
*/
public JavascriptMessageParser(final Assert asserter, final boolean endOnAssertionFailure) {
this.asserter = asserter; this.asserter = asserter;
this.endOnAssertionFailure = endOnAssertionFailure;
} }
public boolean isTestFinished() { public boolean isTestFinished() {
@ -61,8 +74,15 @@ public final class JavascriptMessageParser {
try { try {
asserter.ok(false, name, message); asserter.ok(false, name, message);
} catch (AssertionFailedError e) { } catch (AssertionFailedError e) {
// Swallow this exception. We want to see all the // Above, we call the assert, allowing it to log.
// Javascript failures, not die on the very first one! // Now we can end the test, if applicable.
if (this.endOnAssertionFailure) {
throw e;
}
// Otherwise, swallow the Error. The JS framework we're
// logging messages from is likely capable of ending tests
// when it needs to, and we want to see all of its failures,
// not just the first one!
} }
} else if ("KNOWN-FAIL".equals(type)) { } else if ("KNOWN-FAIL".equals(type)) {
asserter.todo(false, name, message); asserter.todo(false, name, message);