mobile/android/base/tests/helpers/JavascriptMessageParser.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.tests.helpers;
michael@0 6
michael@0 7 import org.mozilla.gecko.Assert;
michael@0 8
michael@0 9 import junit.framework.AssertionFailedError;
michael@0 10
michael@0 11 import java.util.regex.Matcher;
michael@0 12 import java.util.regex.Pattern;
michael@0 13
michael@0 14 /**
michael@0 15 * Route messages from Javascript's head.js test framework into Java's
michael@0 16 * Mochitest framework.
michael@0 17 */
michael@0 18 public final class JavascriptMessageParser {
michael@0 19
michael@0 20 /**
michael@0 21 * The Javascript test harness sends test events to Java.
michael@0 22 * Each such test event is wrapped in a Robocop:JS event.
michael@0 23 */
michael@0 24 public static final String EVENT_TYPE = "Robocop:JS";
michael@0 25
michael@0 26 // Messages matching this pattern are handled specially. Messages not
michael@0 27 // matching this pattern are still printed. This pattern should be able
michael@0 28 // to handle having multiple lines in a message.
michael@0 29 private static final Pattern testMessagePattern =
michael@0 30 Pattern.compile("TEST-([A-Z\\-]+) \\| (.*?) \\| (.*)", Pattern.DOTALL);
michael@0 31
michael@0 32 private final Assert asserter;
michael@0 33 // Used to help print stack traces neatly.
michael@0 34 private String lastTestName = "";
michael@0 35 // Have we seen a message saying the test is finished?
michael@0 36 private boolean testFinishedMessageSeen = false;
michael@0 37 private final boolean endOnAssertionFailure;
michael@0 38
michael@0 39 /**
michael@0 40 * Constructs a message parser for test result messages sent from JavaScript. When seeing an
michael@0 41 * assertion failure, the message parser can use the given {@link org.mozilla.gecko.Assert}
michael@0 42 * instance to immediately end the test (typically if the underlying JS framework is not able
michael@0 43 * to end the test itself) or to swallow the Errors - this functionality is determined by the
michael@0 44 * <code>endOnAssertionFailure</code> parameter.
michael@0 45 *
michael@0 46 * @param asserter The Assert instance to which test results should be passed.
michael@0 47 * @param endOnAssertionFailure
michael@0 48 * true if the test should end if we see a JS assertion failure, false otherwise.
michael@0 49 */
michael@0 50 public JavascriptMessageParser(final Assert asserter, final boolean endOnAssertionFailure) {
michael@0 51 this.asserter = asserter;
michael@0 52 this.endOnAssertionFailure = endOnAssertionFailure;
michael@0 53 }
michael@0 54
michael@0 55 public boolean isTestFinished() {
michael@0 56 return testFinishedMessageSeen;
michael@0 57 }
michael@0 58
michael@0 59 public void logMessage(final String str) {
michael@0 60 final Matcher m = testMessagePattern.matcher(str.trim());
michael@0 61
michael@0 62 if (m.matches()) {
michael@0 63 final String type = m.group(1);
michael@0 64 final String name = m.group(2);
michael@0 65 final String message = m.group(3);
michael@0 66
michael@0 67 if ("INFO".equals(type)) {
michael@0 68 asserter.info(name, message);
michael@0 69 testFinishedMessageSeen = testFinishedMessageSeen ||
michael@0 70 "exiting test".equals(message);
michael@0 71 } else if ("PASS".equals(type)) {
michael@0 72 asserter.ok(true, name, message);
michael@0 73 } else if ("UNEXPECTED-FAIL".equals(type)) {
michael@0 74 try {
michael@0 75 asserter.ok(false, name, message);
michael@0 76 } catch (AssertionFailedError e) {
michael@0 77 // Above, we call the assert, allowing it to log.
michael@0 78 // Now we can end the test, if applicable.
michael@0 79 if (this.endOnAssertionFailure) {
michael@0 80 throw e;
michael@0 81 }
michael@0 82 // Otherwise, swallow the Error. The JS framework we're
michael@0 83 // logging messages from is likely capable of ending tests
michael@0 84 // when it needs to, and we want to see all of its failures,
michael@0 85 // not just the first one!
michael@0 86 }
michael@0 87 } else if ("KNOWN-FAIL".equals(type)) {
michael@0 88 asserter.todo(false, name, message);
michael@0 89 } else if ("UNEXPECTED-PASS".equals(type)) {
michael@0 90 asserter.todo(true, name, message);
michael@0 91 }
michael@0 92
michael@0 93 lastTestName = name;
michael@0 94 } else {
michael@0 95 // Generally, these extra lines are stack traces from failures,
michael@0 96 // so we print them with the name of the last test seen.
michael@0 97 asserter.info(lastTestName, str.trim());
michael@0 98 }
michael@0 99 }
michael@0 100 }

mercurial