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