1 // Copyright (c) 2012 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 * Asserts the value is true. 7 * @param {*} x The value to assert. 8 */ 9 function assert(x) { 10 if (!x) 11 throw new Error(); 12 } 13 14 /** 15 * Asserts the values are equal. 16 * @param {*} x The value to assert. 17 */ 18 function assertEquals(x, y) { 19 if (x != y) 20 throw new Error(x + ' != ' + y); 21 } 22 23 /** 24 * Runs the given test. 25 * @param {function()} test The test to run. 26 * @param {function()} onPass The function to call if and when the 27 * function passes. 28 */ 29 function runTest(test, onPass) { 30 var shouldContinue = true; 31 var runner = { 32 waitForAsync: function(description) { 33 shouldContinue = false; 34 console.log('Waiting for ', description); 35 }, 36 37 continueTesting: function() { 38 shouldContinue = true; 39 window.setTimeout(function() { 40 if (shouldContinue) 41 onPass(); 42 }, 0); 43 } 44 }; 45 46 test(runner); 47 if (shouldContinue) 48 onPass(); 49 } 50 51 /** 52 * Runs all tests and reports the results via the console. 53 */ 54 function runTests() { 55 var tests = []; 56 for (var i in window) { 57 if (i.indexOf('test') == 0) 58 tests.push(window[i]); 59 } 60 console.log('Running %d tests...', tests.length); 61 62 var testNo = 0; 63 function runNextTest() { 64 if (testNo >= tests.length) { 65 console.log('All tests passed'); 66 return; 67 } 68 69 function onPass() { 70 testNo++; 71 runNextTest(); 72 } 73 74 var test = tests[testNo]; 75 console.log('Running (%d/%d) -- %s', testNo + 1, tests.length, test.name); 76 runTest(test, onPass); 77 } 78 runNextTest(); 79 } 80 81 window.addEventListener('load', function() { 82 runTests(); 83 }); 84