webspeech.js (3583B)
1 var DELAY = 500; // In milliseconds. 2 var TIMEOUT = 2000; // In milliseconds. Used for most tests. 3 if (typeof(TIMEOUT_OVERRIDE) != 'undefined') { 4 TIMEOUT = TIMEOUT_OVERRIDE; 5 } 6 GLOBAL_TIMEOUT = TIMEOUT + 2000; // In milliseconds. 7 setup({timeout: GLOBAL_TIMEOUT}); 8 var onstarted = false; 9 var neverFireTest = async_test('Events that should not fire'); 10 var onstartTest = async_test('onstart'); 11 var reco = new SpeechRecognition(); 12 13 reco.onstart = onstartTest.step_func(function(event) { 14 assert_false(onstarted, 'onstart should only fire once.'); 15 onstarted = true; 16 onstartTest.done(); 17 beginTest(); 18 }); 19 20 reco.onend = function() { 21 neverFireTest.done(); 22 for (var i = 0; i < doneOnEndTestList.length; i++) { 23 doneOnEndTestList[i].done(); 24 } 25 }; 26 27 function neverFireEvent(name) { 28 return neverFireTest.step_func(function(event) { 29 assert_unreached(name + ' should not fire.'); 30 }); 31 } 32 33 var doneOnEndTestList = []; // List of all test objects to call done at onend. 34 35 // Tally calls to count() and test against min/max when test ends. 36 // A max value of 0 indicates no maximum. 37 function CountTest(name, min, max) { 38 doneOnEndTestList.push(this); 39 this.name = name; 40 this.min = min; 41 this.max = max; 42 this.sum = 0; 43 this.asyncTest = async_test(name); 44 45 this.count = function(increment) { this.sum += increment; }; 46 47 this.test = function() { return this.asyncTest; }; 48 49 this.done = function() { 50 var cTest = this; 51 this.asyncTest.step(function() { 52 notes.innerHTML += cTest.name + ' occurred ' + cTest.sum + ' times.<br>'; 53 if (cTest.min == cTest.max) { 54 assert_equals(cTest.sum, cTest.min, cTest.name + ' occurred ' + 55 cTest.sum + ' times and should have occurred ' + 56 cTest.min + ' times.'); 57 } else { 58 assert_true(cTest.sum >= cTest.min, cTest.name + ' occurred ' + 59 cTest.sum + ' times and should have occurred at least ' + 60 cTest.min + ' times.'); 61 assert_true(cTest.max == 0 || cTest.sum <= cTest.max, cTest.name + 62 ' occurred ' + cTest.sum + 63 ' times and should have occurred at most ' + cTest.max + ' times.'); 64 } 65 if (cTest.whenDone) { 66 cTest.whenDone(); 67 } 68 }); 69 this.asyncTest.done(); 70 }; 71 } 72 73 // Test for proper cycling of startEvent followed by endEvent. 74 function CycleTest(name) { 75 doneOnEndTestList.push(this); 76 this.name = name; 77 this.count = 0; // Counts number of start / end cycles. 78 this.started = false; // Tracks whether last event was a start or end event. 79 this.test = async_test(name + ' start/stop'); 80 81 this.startEvent = function() { 82 var cycle = this; 83 return this.test.step_func(function(event) { 84 assert_true(onstarted, cycle.name + 'start fired before onstart.'); 85 assert_false(cycle.started, cycle.name + 'start fired twice without ' + 86 cycle.name + 'stop.'); 87 cycle.started = true; 88 }); 89 }; 90 91 this.endEvent = function() { 92 var cycle = this; 93 return this.test.step_func(function(event) { 94 assert_true(cycle.started, cycle.name + 'end fired before ' + 95 cycle.name + 'start.'); 96 cycle.started = false; 97 cycle.count += 1; 98 }); 99 }; 100 101 this.done = function() { 102 var cycle = this; 103 this.test.step(function() { 104 assert_false(cycle.started, cycle.name + 'start fired but not ' + 105 cycle.name + 'end.'); 106 assert_true(cycle.count > 0, cycle.name + 'start never fired.'); 107 notes.innerHTML += cycle.name + ' cycled ' + cycle.count + ' times.<br>'; 108 }); 109 this.test.done(); 110 }; 111 }