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