Index: chrome/test/data/webui/webui_resource_test.js |
diff --git a/chrome/test/data/webui/webui_resource_test.js b/chrome/test/data/webui/webui_resource_test.js |
index 1cd3aececc419ad5a203ebd29e5c470051e5fc48..c98722527bab07010368e597c470e24c41a5b6bd 100644 |
--- a/chrome/test/data/webui/webui_resource_test.js |
+++ b/chrome/test/data/webui/webui_resource_test.js |
@@ -57,7 +57,7 @@ function assertNotEqual(reference, observed, opt_message) { |
} |
/** |
- * Verifies that a test evaluation results in an assertion failure. |
+ * Verifies that a test evaluation results in an exception. |
* @param {!Function} f The test function. |
*/ |
function assertThrows(f) { |
@@ -72,39 +72,103 @@ function assertThrows(f) { |
} |
/** |
- * Runs all functions starting with test and reports success or |
- * failure of the test suite. |
+ * Verifies that the contents of the expected and observed arrays match. |
+ * @param {Array} expected The expected result. |
+ * @param {Array} observed The actual result. |
*/ |
-function runTests() { |
- var tests = []; |
- |
- if (window.setUp) |
- window.setUp(); |
- |
- var success = true; |
- for (var name in window) { |
- if (typeof window[name] == 'function' && /^test/.test(name)) |
- tests.push(name); |
+function assertArrayEquals(expected, observed) { |
+ var v1 = Array.prototype.slice.call(expected); |
+ var v2 = Array.prototype.slice.call(observed); |
+ var equal = v1.length == v2.length; |
+ if (equal) { |
+ for (var i = 0; i < v1.length; i++) { |
+ if (v1[i] !== v2[i]) { |
+ equal = false; |
+ break; |
+ } |
+ } |
} |
- if (!tests.length) { |
- console.error('\nFailed to find test cases.'); |
- success = false; |
+ if (!equal) { |
+ var message = 'Assertion Failed\n Observed: ' + v1 + |
+ '\n Expected: ' + v2; |
+ throw new Error(message); |
} |
- for (var i = 0; i < tests.length; i++) { |
- try { |
- window[tests[i]](); |
- } catch (err) { |
- console.error('Failure in test ' + tests[i] + '\n' + err); |
- console.log(err.stack); |
- success = false; |
+} |
+ |
+/** |
+ * Defines runTests. |
+ */ |
+(function(exports) { |
+ /** |
+ * List of test cases. |
+ * @type {Array.<strring>} List of function names for tests to run. |
arv (Not doing code reviews)
2013/06/20 18:33:03
typo
arv (Not doing code reviews)
2013/06/20 18:33:03
Only one space after }
kevers
2013/06/20 18:52:12
Done.
kevers
2013/06/20 18:52:12
Fixed.
|
+ */ |
+ var testCases_ = []; |
Dan Beam
2013/06/20 18:31:46
nit: these aren't @private member variables, so th
arv (Not doing code reviews)
2013/06/20 18:33:03
no underscore
kevers
2013/06/20 18:52:12
Done.
|
+ |
+ /** |
+ * Indicates if all tests have run successfully. |
+ * @type {boolean} |
+ */ |
+ var cleanTestRun_ = true; |
+ |
+ /** |
+ * Armed during setup of a test to call the matching tear down code. |
+ * @type {?Function} |
arv (Not doing code reviews)
2013/06/20 18:33:03
@type {Function}
Function is already nullable
kevers
2013/06/20 18:52:12
Done.
|
+ */ |
+ var pendingTearDown_ = null; |
+ |
+ /** |
+ * Runs all functions starting with test and reports success or |
+ * failure of the test suite. |
+ */ |
+ function runTests_() { |
+ for (var name in window) { |
+ if (typeof window[name] == 'function' && /^test/.test(name)) |
+ testCases_.push(name); |
+ } |
+ if (!testCases_.length) { |
+ console.error('\nFailed to find test cases.'); |
+ cleanTestRun = false; |
} |
+ continueTesting_(); |
} |
- if (window.tearDown) |
- window.tearDown(); |
+ /** |
+ * Runs the next test in the queue. Reports the test results if the queue is |
+ * empty. |
+ */ |
+ function continueTesting_() { |
+ if (pendingTearDown_) { |
+ pendingTearDown_(); |
+ pendingTearDown_ = null; |
+ } |
+ if (testCases_.length > 0) { |
+ var fn = testCases_.pop(); |
+ var isAsyncTest = window[fn].length; |
+ try { |
+ if (window.setUp) |
+ window.setUp(); |
+ pendingTearDown_ = window.tearDown; |
+ window[fn](continueTesting_); |
+ } catch(err) { |
+ console.error('Failure in test ' + fn + '\n' + err); |
+ console.log(err.stack); |
+ cleanTestRun_ = false; |
+ } |
+ // Asynchronous tests must manually call continueTesting when complete. |
+ if (!isAsyncTest) |
+ continueTesting_(); |
+ } else { |
+ endTests(cleanTestRun_); |
+ } |
+ if (testCases_.length) { |
+ domAutomationController.setAutomationId(1); |
+ domAutomationController.send('PENDING'); |
+ } |
+ }; |
- endTests(success); |
-} |
+ exports.runTests = runTests_; |
+})(this); |
/** |
* Signals completion of a test. |