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..679773258f31c737056c39b760339226a4f1ad0f 100644 |
--- a/chrome/test/data/webui/webui_resource_test.js |
+++ b/chrome/test/data/webui/webui_resource_test.js |
@@ -3,6 +3,30 @@ |
// found in the LICENSE file. |
/** |
+ * List of test cases. |
+ * @type {Array.<strring>} List of function names for tests to run. |
Dan Beam
2013/06/20 18:31:46
!Array.<string>
|
+ */ |
+var testCases = []; |
+ |
+/** |
+ * 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} |
+ */ |
+var pendingTearDown = null; |
+ |
+/** |
+ * Indicates if running an asynchronous test. |
+ * @type {boolean} |
+ */ |
+var runningAsynchronousTest = false; |
Dan Beam
2013/06/20 18:31:46
nit: move right above continueTesting() IMO
|
+ |
+/** |
* Tests that an observation matches the expected value. |
* @param {Object} expected The expected value. |
* @param {Object} observed The actual value. |
@@ -57,7 +81,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,38 +96,77 @@ function assertThrows(f) { |
} |
/** |
+ * Verifies that the contents of the expected and observed arrays match. |
+ * @param {Array} expected The expected result. |
+ * @param {Array} observed The actual result. |
+ */ |
+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 (!equal) { |
+ var message = 'Assertion Failed\n Observed: ' + v1 + |
+ '\n Expected: ' + v2; |
+ throw new Error(message); |
+ } |
+} |
+ |
+/** |
* Runs all functions starting with test and reports success or |
* failure of the test suite. |
*/ |
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)) |
arv (Not doing code reviews)
2013/06/20 15:49:38
var isAsyncTest = window[name].length;
kevers
2013/06/20 17:34:18
Added isAyncTest in the continueTesting function b
|
- tests.push(name); |
+ testCases.push(name); |
} |
- if (!tests.length) { |
+ if (!testCases.length) { |
console.error('\nFailed to find test cases.'); |
- success = false; |
+ cleanTestRun = false; |
} |
- for (var i = 0; i < tests.length; i++) { |
+ continueTesting(); |
+} |
+ |
+/** |
+ * Runs the next test in the queue. Reports the test results if the queue is |
+ * empty. |
+ */ |
+function continueTesting() { |
+ runningAsynchronousTest = false; |
Dan Beam
2013/06/20 18:31:46
nit: \n
|
+ if (pendingTearDown) { |
+ pendingTearDown(); |
+ pendingTearDown = null; |
+ } |
Dan Beam
2013/06/20 18:31:46
nit: \n
|
+ if(testCases.length > 0) { |
Dan Beam
2013/06/20 18:31:46
if (
|
+ var fn = testCases.pop(); |
try { |
- window[tests[i]](); |
- } catch (err) { |
- console.error('Failure in test ' + tests[i] + '\n' + err); |
+ if (window.setUp) |
+ window.setUp(); |
Dan Beam
2013/06/20 18:31:46
nit: \n
|
+ pendingTearDown = window.tearDown; |
+ window[fn](); |
+ } catch(err) { |
+ console.error('Failure in test ' + fn + '\n' + err); |
console.log(err.stack); |
- success = false; |
+ cleanTestRun = false; |
} |
+ // Asynchronous tests must manually call continueTesting when complete. |
+ if (!runningAsynchronousTest) |
+ continueTesting(); |
+ } else { |
+ endTests(cleanTestRun); |
+ } |
Dan Beam
2013/06/20 18:31:46
nit: \n
|
+ if (testCases.length) { |
+ domAutomationController.setAutomationId(1); |
+ domAutomationController.send('PENDING'); |
} |
- |
- if (window.tearDown) |
- window.tearDown(); |
- |
- endTests(success); |
} |
/** |