| OLD | NEW |
| (Empty) |
| 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 * Mock tts class. | |
| 7 * @constructor | |
| 8 * @extends {cvox.TtsInterface} | |
| 9 */ | |
| 10 var MockTts = function() { | |
| 11 }; | |
| 12 | |
| 13 MockTts.prototype = { | |
| 14 /** | |
| 15 * A list of predicate, start, and end callbacks for a pending expectation. | |
| 16 * @type {!Array<{{predicate: function(string) : boolean, | |
| 17 * startCallback: function() : void, | |
| 18 * endCallback: function() : void}>} | |
| 19 * @private | |
| 20 */ | |
| 21 expectations_: [], | |
| 22 | |
| 23 /** | |
| 24 * A list of strings stored whenever there are no expectations. | |
| 25 * @type {!Array<string} | |
| 26 * @private | |
| 27 */ | |
| 28 idleUtterances_: [], | |
| 29 | |
| 30 /** @override */ | |
| 31 speak: function(textString, queueMode, properties) { | |
| 32 this.process_(textString, false, properties); | |
| 33 }, | |
| 34 | |
| 35 /** | |
| 36 * Adds an expectation for the given string to be spoken. If satisfied, | |
| 37 * |opt_callback| is called. | |
| 38 * @param {string} expectedText | |
| 39 * @param {function() : void=} opt_callback | |
| 40 * @param {boolean=} opt_exact Expect an exact match; defaults to false. | |
| 41 */ | |
| 42 expectSpeech: function(expectedText, opt_callback, opt_exact) { | |
| 43 var expectation = {}; | |
| 44 expectation.endCallback = opt_callback; | |
| 45 this.addExpectation_(expectedText, expectation, opt_exact); | |
| 46 }, | |
| 47 | |
| 48 /** | |
| 49 * Adds an expectation for the given string to be spoken after |opt_callback| | |
| 50 * executes. | |
| 51 * @param {string} expectedText | |
| 52 * @param {function() : void=} opt_callback | |
| 53 * @param {boolean=} opt_exact Expect an exact match; defaults to false. | |
| 54 */ | |
| 55 expectSpeechAfter: function(expectedText, opt_callback, opt_exact) { | |
| 56 var expectation = {}; | |
| 57 if (this.expectations_.length == 0 && opt_callback) | |
| 58 opt_callback(); | |
| 59 else | |
| 60 expectation.startCallback = opt_callback; | |
| 61 this.addExpectation_(expectedText, expectation, opt_exact); | |
| 62 }, | |
| 63 | |
| 64 /** | |
| 65 * Finishes expectations and calls {@code callback} afterwards. | |
| 66 * @param {Function} callback | |
| 67 */ | |
| 68 finishExpectations: function(callback) { | |
| 69 this.expectSpeechAfter('', callback); | |
| 70 }, | |
| 71 | |
| 72 /** | |
| 73 * @private | |
| 74 * @param {string} expectedText Text expected spoken. | |
| 75 * @param {{startCallback: function() : void, | |
| 76 * endCallback: function() : void}=} opt_expectation | |
| 77 * @param {boolean=} opt_exact Expects an exact match. | |
| 78 */ | |
| 79 addExpectation_: function(expectedText, opt_expectation, opt_exact) { | |
| 80 var expectation = opt_expectation ? opt_expectation : {}; | |
| 81 | |
| 82 expectation.predicate = function(actualText) { | |
| 83 if (opt_exact) | |
| 84 return actualText === expectedText; | |
| 85 return actualText.indexOf(expectedText) != -1; | |
| 86 }; | |
| 87 | |
| 88 this.expectations_.push(expectation); | |
| 89 | |
| 90 // Process any idleUtterances. | |
| 91 this.idleUtterances_.forEach(function(utterance) { | |
| 92 this.process_(utterance.text, true, utterance.properties); | |
| 93 }.bind(this)); | |
| 94 }, | |
| 95 | |
| 96 /** | |
| 97 * @param {string} textString Utterance to match against callbacks. | |
| 98 * @param {boolean=} opt_manual True if called outside of tts.speak. | |
| 99 * @param {!Object=} opt_properties | |
| 100 * @private | |
| 101 */ | |
| 102 process_: function(textString, opt_manual, opt_properties) { | |
| 103 var utterance = {text: textString, properties: opt_properties}; | |
| 104 if (this.expectations_.length == 0) { | |
| 105 if (!opt_manual) { | |
| 106 this.idleUtterances_.push(utterance); | |
| 107 } | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 var allUtterances = this.idleUtterances_.concat([utterance]); | |
| 112 var targetExpectation = this.expectations_.shift(); | |
| 113 allUtterances = allUtterances.filter(function(u) { | |
| 114 return targetExpectation.predicate(u.text); | |
| 115 }); | |
| 116 if (allUtterances.length > 0) { | |
| 117 var matchingProperties = allUtterances[0].properties; | |
| 118 this.idleUtterances_.length = 0; | |
| 119 if (targetExpectation.endCallback) | |
| 120 targetExpectation.endCallback(); | |
| 121 if (matchingProperties && matchingProperties.endCallback) { | |
| 122 matchingProperties.endCallback(); | |
| 123 } | |
| 124 var nextExpectation = this.expectations_[0]; | |
| 125 if (nextExpectation && nextExpectation.startCallback) | |
| 126 nextExpectation.startCallback(); | |
| 127 } else { | |
| 128 this.expectations_.unshift(targetExpectation); | |
| 129 } | |
| 130 }, | |
| 131 }; | |
| OLD | NEW |