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
) {
147 if (typeof window
[name
] == 'function' && /^test/.test(name
))
148 testCases
.push(name
);
150 if (location
.protocol
== 'data:' && e
.name
== 'SecurityError') {
151 // Sometimes this file gets loaded as a data: URI. That causes issues
152 // when it touches window.caches or window.cookie.
158 if (!testCases
.length
) {
159 console
.error('Failed to find test cases.');
160 cleanTestRun
= false;
163 if (window
.setUpPage
)
166 cleanTestRun
= false;
172 * Runs the next test in the queue. Reports the test results if the queue is
174 * @param {boolean=} opt_asyncTestFailure Optional parameter indicated if the
175 * last asynchronous test failed.
177 function continueTesting(opt_asyncTestFailure
) {
178 if (opt_asyncTestFailure
)
179 cleanTestRun
= false;
181 if (pendingTearDown
) {
183 pendingTearDown
= null;
185 if (testCases
.length
> 0) {
186 var fn
= testCases
.pop();
187 var isAsyncTest
= window
[fn
].length
;
191 pendingTearDown
= window
.tearDown
;
192 window
[fn
](continueTesting
);
194 console
.error('Failure in test ' + fn
+ '\n' + err
);
195 console
.log(err
.stack
);
196 cleanTestRun
= false;
198 // Asynchronous tests must manually call continueTesting when complete.
203 endTests(cleanTestRun
);
206 domAutomationController
.setAutomationId(1);
207 domAutomationController
.send('PENDING');
211 exports
.runTests
= runTests
;
215 * Signals completion of a test.
216 * @param {boolean} success Indicates if the test completed successfully.
218 function endTests(success
) {
219 domAutomationController
.setAutomationId(1);
220 domAutomationController
.send(success
? 'SUCCESS' : 'FAILURE');
223 window
.onerror = function() {