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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * Tests that an observation matches the expected value. 6 * Tests that an observation matches the expected value.
7 * @param {Object} expected The expected value. 7 * @param {Object} expected The expected value.
8 * @param {Object} observed The actual value. 8 * @param {Object} observed The actual value.
9 * @param {string=} opt_message Optional message to include with a test 9 * @param {string=} opt_message Optional message to include with a test
10 * failure. 10 * failure.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 if (observed === reference) { 50 if (observed === reference) {
51 var message = 'Assertion Failed\n Observed: ' + observed + 51 var message = 'Assertion Failed\n Observed: ' + observed +
52 '\n Reference: ' + reference; 52 '\n Reference: ' + reference;
53 if (opt_message) 53 if (opt_message)
54 message = message + '\n ' + opt_message; 54 message = message + '\n ' + opt_message;
55 throw new Error(message); 55 throw new Error(message);
56 } 56 }
57 } 57 }
58 58
59 /** 59 /**
60 * Verifies that a test evaluation results in an assertion failure. 60 * Verifies that a test evaluation results in an exception.
61 * @param {!Function} f The test function. 61 * @param {!Function} f The test function.
62 */ 62 */
63 function assertThrows(f) { 63 function assertThrows(f) {
64 var triggeredError = false; 64 var triggeredError = false;
65 try { 65 try {
66 f(); 66 f();
67 } catch(err) { 67 } catch(err) {
68 triggeredError = true; 68 triggeredError = true;
69 } 69 }
70 if (!triggeredError) 70 if (!triggeredError)
71 throw new Error('Assertion Failed: throw expected.'); 71 throw new Error('Assertion Failed: throw expected.');
72 } 72 }
73 73
74 /** 74 /**
75 * Runs all functions starting with test and reports success or 75 * Verifies that the contents of the expected and observed arrays match.
76 * failure of the test suite. 76 * @param {Array} expected The expected result.
77 * @param {Array} observed The actual result.
77 */ 78 */
78 function runTests() { 79 function assertArrayEquals(expected, observed) {
79 var tests = []; 80 var v1 = Array.prototype.slice.call(expected);
80 81 var v2 = Array.prototype.slice.call(observed);
81 if (window.setUp) 82 var equal = v1.length == v2.length;
82 window.setUp(); 83 if (equal) {
83 84 for (var i = 0; i < v1.length; i++) {
84 var success = true; 85 if (v1[i] !== v2[i]) {
85 for (var name in window) { 86 equal = false;
86 if (typeof window[name] == 'function' && /^test/.test(name)) 87 break;
87 tests.push(name); 88 }
88 }
89 if (!tests.length) {
90 console.error('\nFailed to find test cases.');
91 success = false;
92 }
93 for (var i = 0; i < tests.length; i++) {
94 try {
95 window[tests[i]]();
96 } catch (err) {
97 console.error('Failure in test ' + tests[i] + '\n' + err);
98 console.log(err.stack);
99 success = false;
100 } 89 }
101 } 90 }
102 91 if (!equal) {
103 if (window.tearDown) 92 var message = 'Assertion Failed\n Observed: ' + v1 +
104 window.tearDown(); 93 '\n Expected: ' + v2;
105 94 throw new Error(message);
106 endTests(success); 95 }
107 } 96 }
108 97
109 /** 98 /**
99 * Defines runTests.
100 */
101 (function(exports) {
102 /**
103 * List of test cases.
104 * @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.
105 */
106 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.
107
108 /**
109 * Indicates if all tests have run successfully.
110 * @type {boolean}
111 */
112 var cleanTestRun_ = true;
113
114 /**
115 * Armed during setup of a test to call the matching tear down code.
116 * @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.
117 */
118 var pendingTearDown_ = null;
119
120 /**
121 * Runs all functions starting with test and reports success or
122 * failure of the test suite.
123 */
124 function runTests_() {
125 for (var name in window) {
126 if (typeof window[name] == 'function' && /^test/.test(name))
127 testCases_.push(name);
128 }
129 if (!testCases_.length) {
130 console.error('\nFailed to find test cases.');
131 cleanTestRun = false;
132 }
133 continueTesting_();
134 }
135
136 /**
137 * Runs the next test in the queue. Reports the test results if the queue is
138 * empty.
139 */
140 function continueTesting_() {
141 if (pendingTearDown_) {
142 pendingTearDown_();
143 pendingTearDown_ = null;
144 }
145 if (testCases_.length > 0) {
146 var fn = testCases_.pop();
147 var isAsyncTest = window[fn].length;
148 try {
149 if (window.setUp)
150 window.setUp();
151 pendingTearDown_ = window.tearDown;
152 window[fn](continueTesting_);
153 } catch(err) {
154 console.error('Failure in test ' + fn + '\n' + err);
155 console.log(err.stack);
156 cleanTestRun_ = false;
157 }
158 // Asynchronous tests must manually call continueTesting when complete.
159 if (!isAsyncTest)
160 continueTesting_();
161 } else {
162 endTests(cleanTestRun_);
163 }
164 if (testCases_.length) {
165 domAutomationController.setAutomationId(1);
166 domAutomationController.send('PENDING');
167 }
168 };
169
170 exports.runTests = runTests_;
171 })(this);
172
173 /**
110 * Signals completion of a test. 174 * Signals completion of a test.
111 * @param {boolean} success Indicates if the test completed successfully. 175 * @param {boolean} success Indicates if the test completed successfully.
112 */ 176 */
113 function endTests(success) { 177 function endTests(success) {
114 domAutomationController.setAutomationId(1); 178 domAutomationController.setAutomationId(1);
115 domAutomationController.send(success ? 'SUCCESS' : 'FAILURE'); 179 domAutomationController.send(success ? 'SUCCESS' : 'FAILURE');
116 } 180 }
117 181
118 window.onerror = function() { 182 window.onerror = function() {
119 endTests(false); 183 endTests(false);
120 }; 184 };
OLDNEW
« 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