Index: chrome/test/data/webui/test_api.js |
diff --git a/chrome/test/data/webui/test_api.js b/chrome/test/data/webui/test_api.js |
index 72de125013fd6830d488d5c1cb467aa5f679dd07..d310046ed0e77e89e5141a3683550fe9e2ed544c 100644 |
--- a/chrome/test/data/webui/test_api.js |
+++ b/chrome/test/data/webui/test_api.js |
@@ -18,6 +18,17 @@ var testing = {}; |
**/ |
var currentTestCase = null; |
+/** |
+ * @type {?string} The string representation of the currently running test |
+ * function. |
+ */ |
+var currentTestFunction = null; |
+ |
+/** |
+ * @type {?Array} The arguments of the currently running test. |
+ */ |
+var currentTestArguments = null; |
+ |
(function() { |
// Provide global objects for generation case. |
if (this['window'] === undefined) |
@@ -309,6 +320,50 @@ var currentTestCase = null; |
**/ |
var helper = new CallHelper(); |
+ /** |
+ * true when asyncTestDone has been called. |
+ * @type {boolean} |
+ */ |
+ var asyncTestIsDone = false; |
+ |
+ /** |
+ * Holds the errors, if any, caught by expects so that the test case can |
+ * fail. Cleared when results are reported from runTest() or asyncTestDone(). |
+ * @type {Array.<Error>} |
+ **/ |
+ var errors = []; |
+ |
+ /** |
+ * Notifies the running browser test that any async javascript is complete. |
+ * @param {Array.<boolean, string>=} result When passed, this is used for the |
+ * asyncTestResult message. |
+ **/ |
+ function asyncTestDone(result) { |
+ if (!asyncTestIsDone) { |
+ asyncTestIsDone = true; |
+ chrome.send('asyncTestResult', result ? result : testResult()); |
+ errors.splice(0, errors.length); |
+ } |
+ } |
+ |
+ /** |
+ * Returns [success, message] & clears |errors|. |
+ * @return {Array.<boolean, string>} |
+ **/ |
+ function testResult() { |
+ var result = [true, '']; |
+ if (errors.length) { |
+ var message = ''; |
+ for (var i = 0; i < errors.length; ++i) { |
+ message += 'Failed: ' + currentTestFunction + '(' + |
+ currentTestArguments.map(JSON.stringify) + |
+ ')\n' + errors[i].stack; |
+ } |
+ result = [false, message]; |
+ } |
+ return result; |
+ } |
+ |
// Asserts. |
// Use the following assertions to verify a condition within a test. |
// If assertion fails, the C++ backend will be immediately notified. |
mmenke
2011/08/10 17:23:40
This is not true for async tests.
Sheridan Rawlins
2011/08/11 01:58:44
Done.
|
@@ -445,12 +500,6 @@ var currentTestCase = null; |
} |
/** |
- * Holds the errors, if any, caught by expects so that the test case can fail. |
- * @type {Array.<Error>} |
- **/ |
- var errors = []; |
- |
- /** |
* Creates a function based upon a function that thows an exception on |
* failure. The new function stuffs any errors into the |errors| array for |
* checking by runTest. This allows tests to continue running other checks, |
@@ -476,7 +525,7 @@ var currentTestCase = null; |
/** |
* This is the starting point for tests run by WebUIBrowserTest. If an error |
* occurs, it reports a failure and a message created by joining individual |
- * error messages. |
+ * error messages. The |errors| is cleared before returning the results. |
* @param {string} testFunction The function name to call. |
* @param {Array} testArguments The arguments to call |testFunction| with. |
* @return {Array.<boolean, string>} [test-succeeded, message-if-failed] |
@@ -487,45 +536,36 @@ var currentTestCase = null; |
// Avoid eval() if at all possible, since it will not work on pages |
// that have enabled content-security-policy. |
var testBody = this[testFunction]; // global object -- not a method. |
- if (typeof testBody === "undefined") |
+ var testName = testFunction; |
+ if (typeof testBody === "undefined") { |
testBody = eval(testFunction); |
+ testName = testBody.toString(); |
+ } |
if (testBody != RUN_TEST_F) { |
- var testName = |
- testFunction.name ? testFunction.name : testBody.toString(); |
console.log('Running test ' + testName); |
} |
- return runTestFunction(testFunction, testBody, testArguments); |
+ var result = runTestFunction(testFunction, testBody, testArguments); |
+ errors.splice(0, errors.length); |
+ return result; |
} |
/** |
- * This is the guts of WebUIBrowserTest. It clears |errors|, runs the |
- * test surrounded by an expect to catch Errors. If |errors| is |
- * non-empty, it reports a failure and a message by joining |errors|. |
- * Consumers can use this to use assert/expect functions asynchronously, |
- * but are then responsible for reporting errors to the browser themselves. |
+ * This is the guts of WebUIBrowserTest. It runs the test surrounded by an |
+ * expect to catch Errors. If |errors| is non-empty, it reports a failure and |
+ * a message by joining |errors|. Consumers can use this to use assert/expect |
+ * functions asynchronously, but are then responsible for reporting errors to |
+ * the browser themselves. |
* @param {string} testFunction The function name to report on failure. |
* @param {Function} testBody The function to call. |
* @param {Array} testArguments The arguments to call |testBody| with. |
* @return {Array.<boolean, string>} [test-succeeded, message-if-failed] |
- * @see errors |
* @see createExpect |
mmenke
2011/08/10 17:23:40
nit: @see asyncTestDone?
Sheridan Rawlins
2011/08/11 01:58:44
Done.
|
**/ |
function runTestFunction(testFunction, testBody, testArguments) { |
- errors.splice(0, errors.length); |
+ currentTestFunction = testFunction; |
+ currentTestArguments = testArguments; |
createExpect(testBody).apply(null, testArguments); |
- |
- var result = [true]; |
- if (errors.length) { |
- var message = ''; |
- for (var i = 0; i < errors.length; ++i) { |
- message += 'Failed: ' + testFunction + '(' + |
- testArguments.map(JSON.stringify) + |
- ')\n' + errors[i].stack; |
- } |
- errors.splice(0, errors.length); |
- result = [false, message]; |
- } |
- return result; |
+ return testResult(); |
} |
/** |
@@ -710,6 +750,7 @@ var currentTestCase = null; |
// Exports. |
testing.Test = Test; |
+ window.asyncTestDone = asyncTestDone; |
window.assertTrue = assertTrue; |
window.assertFalse = assertFalse; |
window.assertGE = assertGE; |