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

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: Use closure to hide variables used in runTests. 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
« no previous file with comments | « chrome/test/data/webui/webui_resource_browsertest.cc ('k') | content/public/test/browser_test_utils.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.
« no previous file with comments | « chrome/test/data/webui/webui_resource_browsertest.cc ('k') | content/public/test/browser_test_utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698