chromevox_unittest_base.js revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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} 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 */ 30function 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 */ 49function 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 */ 70function assertEqualsJSON(obj1, obj2) { 71 if (!eqJSON(obj1, obj2)) { 72 throw new Error('Expected ' + JSON.stringify(obj1) + 73 ', got ' + JSON.stringify(obj2)); 74 } 75} 76 77assertSame = assertEquals; 78assertNotSame = assertNotEquals; 79 80/** 81 * Base test fixture for ChromeVox unit tests. 82 * 83 * Note that while conceptually these are unit tests, these tests need 84 * to run in a full web page, so they're actually run as WebUI browser 85 * tests. 86 * 87 * @constructor 88 * @extends {testing.Test} 89 */ 90function ChromeVoxUnitTestBase() {} 91 92ChromeVoxUnitTestBase.prototype = { 93 __proto__: testing.Test.prototype, 94 95 /** @override */ 96 browsePreload: DUMMY_URL, 97 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 /** 106 * Loads some inlined html into the current document, replacing 107 * whatever was there previously. 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: 120 * 121 * this.loadDoc(function() {/*! 122 * <p>Html goes here</p> 123 * * /}); 124 * 125 * @param {Function} commentEncodedHtml The html to load, embedded as a 126 * comment inside an anonymous function - see example, above. 127 */ 128 loadDoc: function(commentEncodedHtml) { 129 var html = commentEncodedHtml.toString(). 130 replace(/^[^\/]+\/\*!?/, ''). 131 replace(/\*\/[^\/]+$/, ''); 132 this.loadHtml(html); 133 } 134}; 135