Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7563)

Unified Diff: chrome/test/data/webui/webui_resource_test.js

Issue 16831021: Convert asynchronous closure test in cr.ui framework to a browser test. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..fc0d3028d6b0a21f5cb3bc4c9999c1826dbe3b5e 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.<string>} List of function names for tests to run.
Dan Beam 2013/06/20 21:42:14 !Array, please read https://developers.google.com/
+ */
+ 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;
+
+ /**
+ * 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.');
Dan Beam 2013/06/20 21:42:14 remove '\n'
+ 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.

Powered by Google App Engine
This is Rietveld 408576698