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
3 // found in the LICENSE file.
6 * Tests that an observation matches the expected value.
7 * @param {Object} expected The expected value.
8 * @param {Object} observed The actual value.
9 * @param {string=} opt_message Optional message to include with a test
12 function assertEquals(expected
, observed
, opt_message
) {
13 if (observed
!== expected
) {
14 var message
= 'Assertion Failed\n Observed: ' + observed
+
15 '\n Expected: ' + expected
;
17 message
= message
+ '\n ' + opt_message
;
18 throw new Error(message
);
23 * Verifies that a test result is true.
24 * @param {boolean} observed The observed value.
25 * @param {string=} opt_message Optional message to include with a test
28 function assertTrue(observed
, opt_message
) {
29 assertEquals(true, observed
, opt_message
);
33 * Verifies that a test result is false.
34 * @param {boolean} observed The observed value.
35 * @param {string=} opt_message Optional message to include with a test
38 function assertFalse(observed
, opt_message
) {
39 assertEquals(false, observed
, opt_message
);
43 * Verifies that the observed and reference values differ.
44 * @param {Object} reference The target value for comparison.
45 * @param {Object} observed The test result.
46 * @param {string=} opt_message Optional message to include with a test
49 function assertNotEqual(reference
, observed
, opt_message
) {
50 if (observed
=== reference
) {
51 var message
= 'Assertion Failed\n Observed: ' + observed
+
52 '\n Reference: ' + reference
;
54 message
= message
+ '\n ' + opt_message
;
55 throw new Error(message
);
60 * Verifies that a test evaluation results in an exception.
61 * @param {!Function} f The test function.
63 function assertThrows(f
) {
64 var triggeredError
= false;
68 triggeredError
= true;
71 throw new Error('Assertion Failed: throw expected.');
75 * Verifies that the contents of the expected and observed arrays match.
76 * @param {!Array} expected The expected result.
77 * @param {!Array} observed The actual result.
79 function assertArrayEquals(expected
, observed
) {
80 var v1
= Array
.prototype.slice
.call(expected
);
81 var v2
= Array
.prototype.slice
.call(observed
);
82 var equal
= v1
.length
== v2
.length
;
84 for (var i
= 0; i
< v1
.length
; i
++) {
85 if (v1
[i
] !== v2
[i
]) {
93 ['Assertion Failed', 'Observed: ' + v2
, 'Expected: ' + v1
].join('\n ');
94 throw new Error(message
);
99 * Verifies that the expected and observed result have the same content.
100 * @param {*} expected The expected result.
101 * @param {*} observed The actual result.
103 function assertDeepEquals(expected
, observed
, opt_message
) {
104 if (typeof expected
== 'object' && expected
!= null) {
105 assertNotEqual(null, observed
);
106 for (var key
in expected
) {
107 assertTrue(key
in observed
, opt_message
);
108 assertDeepEquals(expected
[key
], observed
[key
], opt_message
);
110 for (var key
in observed
) {
111 assertTrue(key
in expected
, opt_message
);
114 assertEquals(expected
, observed
, opt_message
);
123 * List of test cases.
124 * @type {Array.<string>} List of function names for tests to run.
129 * Indicates if all tests have run successfully.
132 var cleanTestRun
= true;
135 * Armed during setup of a test to call the matching tear down code.
138 var pendingTearDown
= null;
141 * Runs all functions starting with test and reports success or
142 * failure of the test suite.
144 function runTests() {
145 for (var name
in window
) {
146 if (typeof window
[name
] == 'function' && /^test/.test(name
))
147 testCases
.push(name
);
149 if (!testCases
.length
) {
150 console
.error('Failed to find test cases.');
151 cleanTestRun
= false;
157 * Runs the next test in the queue. Reports the test results if the queue is
159 * @param {boolean=} opt_asyncTestFailure Optional parameter indicated if the
160 * last asynchronous test failed.
162 function continueTesting(opt_asyncTestFailure
) {
163 if (opt_asyncTestFailure
)
164 cleanTestRun
= false;
166 if (pendingTearDown
) {
168 pendingTearDown
= null;
170 if (testCases
.length
> 0) {
171 var fn
= testCases
.pop();
172 var isAsyncTest
= window
[fn
].length
;
176 pendingTearDown
= window
.tearDown
;
177 window
[fn
](continueTesting
);
179 console
.error('Failure in test ' + fn
+ '\n' + err
);
180 console
.log(err
.stack
);
181 cleanTestRun
= false;
183 // Asynchronous tests must manually call continueTesting when complete.
188 endTests(cleanTestRun
);
191 domAutomationController
.setAutomationId(1);
192 domAutomationController
.send('PENDING');
196 exports
.runTests
= runTests
;
200 * Signals completion of a test.
201 * @param {boolean} success Indicates if the test completed successfully.
203 function endTests(success
) {
204 domAutomationController
.setAutomationId(1);
205 domAutomationController
.send(success
? 'SUCCESS' : 'FAILURE');
208 window
.onerror = function() {