OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 * Shortcut for document.getElementById. | 6 * Shortcut for document.getElementById. |
7 * @param {string} id of the element. | 7 * @param {string} id of the element. |
8 * @return {HTMLElement} with the id. | 8 * @return {HTMLElement} with the id. |
9 */ | 9 */ |
10 function $(id) { | 10 function $(id) { |
11 return document.getElementById(id); | 11 return document.getElementById(id); |
12 } | 12 } |
13 | 13 |
14 /** | 14 /** |
| 15 * Asserts that a given argument's value is undefined. |
| 16 * @param {object} a The argument to check. |
| 17 */ |
| 18 function assertUndefined(a) { |
| 19 if (a !== undefined) { |
| 20 throw new Error('Assertion failed: expected undefined'); |
| 21 } |
| 22 } |
| 23 |
| 24 /** |
| 25 * Asserts that a given function call raises an exception. |
| 26 * @param {string} error The name of the exception we expect to throw. |
| 27 * @param {Function} fn The function to call. |
| 28 * @return {boolean} True if it throws the exception. |
| 29 */ |
| 30 function assertException(error, fn) { |
| 31 try { |
| 32 fn(); |
| 33 } catch (e) { |
| 34 if (error && e.name != error) { |
| 35 throw new Error('Expected to throw ' + error + ' but threw ' + e.name); |
| 36 } |
| 37 |
| 38 return true; |
| 39 } |
| 40 |
| 41 throw new Error('Expected to throw exception'); |
| 42 } |
| 43 |
| 44 /** |
| 45 * Asserts that two arrays of strings are equal. |
| 46 * @param {Array.<string>} array1 The expected array. |
| 47 * @param {Array.<string>} array2 The test array. |
| 48 */ |
| 49 function assertEqualStringArrays(array1, array2) { |
| 50 var same = true; |
| 51 if (array1.length != array2.length) { |
| 52 same = false; |
| 53 } |
| 54 for (var i = 0; i < Math.min(array1.length, array2.length); i++) { |
| 55 if (array1[i].trim() != array2[i].trim()) { |
| 56 same = false; |
| 57 } |
| 58 } |
| 59 if (!same) { |
| 60 throw new Error('Expected ' + JSON.stringify(array1) + |
| 61 ', got ' + JSON.stringify(array2)); |
| 62 } |
| 63 } |
| 64 |
| 65 /** |
| 66 * Asserts that two objects have the same JSON serialization. |
| 67 * @param {Object} obj1 The expected object. |
| 68 * @param {Object} obj2 The actual object. |
| 69 */ |
| 70 function assertEqualsJSON(obj1, obj2) { |
| 71 if (!eqJSON(obj1, obj2)) { |
| 72 throw new Error('Expected ' + JSON.stringify(obj1) + |
| 73 ', got ' + JSON.stringify(obj2)); |
| 74 } |
| 75 } |
| 76 |
| 77 assertSame = assertEquals; |
| 78 assertNotSame = assertNotEquals; |
| 79 |
| 80 /** |
15 * Base test fixture for ChromeVox unit tests. | 81 * Base test fixture for ChromeVox unit tests. |
16 * | 82 * |
17 * Note that while conceptually these are unit tests, these tests need | 83 * Note that while conceptually these are unit tests, these tests need |
18 * to run in a full web page, so they're actually run as WebUI browser | 84 * to run in a full web page, so they're actually run as WebUI browser |
19 * tests. | 85 * tests. |
20 * | 86 * |
21 * @constructor | 87 * @constructor |
22 * @extends {testing.Test} | 88 * @extends {testing.Test} |
23 */ | 89 */ |
24 function ChromeVoxUnitTestBase() {} | 90 function ChromeVoxUnitTestBase() {} |
25 | 91 |
26 ChromeVoxUnitTestBase.prototype = { | 92 ChromeVoxUnitTestBase.prototype = { |
27 __proto__: testing.Test.prototype, | 93 __proto__: testing.Test.prototype, |
28 | 94 |
29 /** @override */ | 95 /** @override */ |
30 browsePreload: DUMMY_URL, | 96 browsePreload: DUMMY_URL, |
31 | 97 |
32 /** | 98 /** |
| 99 * @override |
| 100 * It doesn't make sense to run the accessibility audit on these tests, |
| 101 * since many of them are deliberately testing inaccessible html. |
| 102 */ |
| 103 runAccessibilityChecks: false, |
| 104 |
| 105 /** |
33 * Loads some inlined html into the current document, replacing | 106 * Loads some inlined html into the current document, replacing |
34 * whatever was there previously. To use it, call it with the html | 107 * whatever was there previously. |
35 * inside an inline comment, like this: | 108 * @param {string} html The html to load as a string. |
| 109 */ |
| 110 loadHtml: function(html) { |
| 111 document.open(); |
| 112 document.write(html); |
| 113 document.close(); |
| 114 }, |
| 115 |
| 116 /** |
| 117 * Loads some inlined html into the current document, replacing |
| 118 * whatever was there previously. This version takes the html |
| 119 * encoded as a comment inside a function, so you can use it like this: |
36 * | 120 * |
37 * this.loadDoc(function() {/*! | 121 * this.loadDoc(function() {/*! |
38 * <p>Html goes here</p> | 122 * <p>Html goes here</p> |
39 * * /}); | 123 * * /}); |
| 124 * |
| 125 * @param {Function} commentEncodedHtml The html to load, embedded as a |
| 126 * comment inside an anonymous function - see example, above. |
40 */ | 127 */ |
41 loadDoc: function(commentEncodedHtml) { | 128 loadDoc: function(commentEncodedHtml) { |
42 var html = commentEncodedHtml.toString(). | 129 var html = commentEncodedHtml.toString(). |
43 replace(/^[^\/]+\/\*!?/, ''). | 130 replace(/^[^\/]+\/\*!?/, ''). |
44 replace(/\*\/[^\/]+$/, ''); | 131 replace(/\*\/[^\/]+$/, ''); |
45 document.open(); | 132 this.loadHtml(html); |
46 document.write(html); | |
47 document.close(); | |
48 } | 133 } |
49 }; | 134 }; |
OLD | NEW |