Bug 988553 - Move JavascriptTestMessageParser into helper class; r=nalexander

This commit is contained in:
Jim Chen 2014-03-31 14:03:34 -04:00
parent 15a6da798e
commit ef15d29539
2 changed files with 79 additions and 77 deletions

View File

@ -1,14 +1,10 @@
package org.mozilla.gecko.tests;
import org.mozilla.gecko.*;
import org.mozilla.gecko.tests.helpers.JavascriptMessageParser;
import android.util.Log;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import junit.framework.AssertionFailedError;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -29,68 +25,6 @@ public class JavascriptTest extends BaseTest {
return TEST_MOCHITEST;
}
/**
* Route messages from Javascript's head.js test framework into Java's
* Mochitest framework.
*/
protected static class JavascriptTestMessageParser {
// Messages matching this pattern are handled specially. Messages not
// matching this pattern are still printed.
private static final Pattern testMessagePattern =
Pattern.compile("\n+TEST-(.*) \\| (.*) \\| (.*)\n*");
private final Assert mAsserter;
// Used to help print stack traces neatly.
private String lastTestName = "";
// Have we seen a message saying the test is finished?
private boolean testFinishedMessageSeen = false;
public JavascriptTestMessageParser(final Assert asserter) {
this.mAsserter = asserter;
}
private boolean testIsFinished() {
return testFinishedMessageSeen;
}
private void logMessage(String str) {
Matcher m = testMessagePattern.matcher(str);
if (m.matches()) {
String type = m.group(1);
String name = m.group(2);
String message = m.group(3);
if ("INFO".equals(type)) {
mAsserter.info(name, message);
testFinishedMessageSeen = testFinishedMessageSeen ||
"exiting test".equals(message);
} else if ("PASS".equals(type)) {
mAsserter.ok(true, name, message);
} else if ("UNEXPECTED-FAIL".equals(type)) {
try {
mAsserter.ok(false, name, message);
} catch (junit.framework.AssertionFailedError e) {
// Swallow this exception. We want to see all the
// Javascript failures, not die on the very first one!
}
} else if ("KNOWN-FAIL".equals(type)) {
mAsserter.todo(false, name, message);
} else if ("UNEXPECTED-PASS".equals(type)) {
mAsserter.todo(true, name, message);
}
lastTestName = name;
} else {
// Generally, these extra lines are stack traces from failures,
// so we print them with the name of the last test seen.
mAsserter.info(lastTestName, str.trim());
}
}
}
public void testJavascript() throws Exception {
blockForGeckoReady();
@ -105,11 +39,9 @@ public class JavascriptTest extends BaseTest {
loadUrl(url);
final JavascriptTestMessageParser testMessageParser =
new JavascriptTestMessageParser(mAsserter);
final JavascriptMessageParser testMessageParser = new JavascriptMessageParser(mAsserter);
try {
while (true) {
while (!testMessageParser.isTestFinished()) {
if (Log.isLoggable(LOGTAG, Log.VERBOSE)) {
Log.v(LOGTAG, "Waiting for Robocop:Status");
}
@ -131,13 +63,10 @@ public class JavascriptTest extends BaseTest {
}
testMessageParser.logMessage(message);
}
if (testMessageParser.testIsFinished()) {
if (Log.isLoggable(LOGTAG, Log.DEBUG)) {
Log.d(LOGTAG, "Got test finished message");
}
break;
}
if (Log.isLoggable(LOGTAG, Log.DEBUG)) {
Log.d(LOGTAG, "Got test finished message");
}
} finally {
expecter.unregisterListener();

View File

@ -0,0 +1,73 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.tests.helpers;
import org.mozilla.gecko.Assert;
import junit.framework.AssertionFailedError;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Route messages from Javascript's head.js test framework into Java's
* Mochitest framework.
*/
public final class JavascriptMessageParser {
// Messages matching this pattern are handled specially. Messages not
// matching this pattern are still printed.
private static final Pattern testMessagePattern =
Pattern.compile("\n+TEST-(.*) \\| (.*) \\| (.*)\n*");
private final Assert asserter;
// Used to help print stack traces neatly.
private String lastTestName = "";
// Have we seen a message saying the test is finished?
private boolean testFinishedMessageSeen = false;
public JavascriptMessageParser(final Assert asserter) {
this.asserter = asserter;
}
public boolean isTestFinished() {
return testFinishedMessageSeen;
}
public void logMessage(final String str) {
final Matcher m = testMessagePattern.matcher(str);
if (m.matches()) {
final String type = m.group(1);
final String name = m.group(2);
final String message = m.group(3);
if ("INFO".equals(type)) {
asserter.info(name, message);
testFinishedMessageSeen = testFinishedMessageSeen ||
"exiting test".equals(message);
} else if ("PASS".equals(type)) {
asserter.ok(true, name, message);
} else if ("UNEXPECTED-FAIL".equals(type)) {
try {
asserter.ok(false, name, message);
} catch (AssertionFailedError e) {
// Swallow this exception. We want to see all the
// Javascript failures, not die on the very first one!
}
} else if ("KNOWN-FAIL".equals(type)) {
asserter.todo(false, name, message);
} else if ("UNEXPECTED-PASS".equals(type)) {
asserter.todo(true, name, message);
}
lastTestName = name;
} else {
// Generally, these extra lines are stack traces from failures,
// so we print them with the name of the last test seen.
asserter.info(lastTestName, str.trim());
}
}
}