| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Mock tts class. | 6 * Mock tts class. |
| 7 * @constructor | 7 * @constructor |
| 8 * @extends {cvox.TtsInterface} | 8 * @extends {cvox.TtsInterface} |
| 9 */ | 9 */ |
| 10 var MockTts = function() { | 10 var MockTts = function() { |
| 11 /** | |
| 12 * The event handler for the most recent call to |speak|. | |
| 13 * @private | |
| 14 */ | |
| 15 this.onEvent_; | |
| 16 }; | 11 }; |
| 17 | 12 |
| 18 MockTts.prototype = { | 13 MockTts.prototype = { |
| 19 /** | 14 /** |
| 20 * A list of predicate, start, and end callbacks for a pending expectation. | 15 * A list of predicate, start, and end callbacks for a pending expectation. |
| 21 * @type {!Array<{{predicate: function(string) : boolean, | 16 * @type {!Array<{{predicate: function(string) : boolean, |
| 22 * startCallback: function() : void, | 17 * startCallback: function() : void, |
| 23 * endCallback: function() : void}>} | 18 * endCallback: function() : void}>} |
| 24 * @private | 19 * @private |
| 25 */ | 20 */ |
| 26 expectations_: [], | 21 expectations_: [], |
| 27 | 22 |
| 28 /** | 23 /** |
| 29 * A list of strings stored whenever there are no expectations. | 24 * A list of strings stored whenever there are no expectations. |
| 30 * @type {!Array<string} | 25 * @type {!Array<string} |
| 31 * @private | 26 * @private |
| 32 */ | 27 */ |
| 33 idleUtterances_: [], | 28 idleUtterances_: [], |
| 34 | 29 |
| 35 /** @override */ | 30 /** @override */ |
| 36 speak: function(textString, queueMode, properties) { | 31 speak: function(textString, queueMode, properties) { |
| 37 if (properties) | 32 this.process_(textString, false, properties); |
| 38 this.onEvent_ = properties['onEvent']; | |
| 39 | |
| 40 this.process_(textString); | |
| 41 }, | 33 }, |
| 42 | 34 |
| 43 /** | 35 /** |
| 44 * Adds an expectation for the given string to be spoken. If satisfied, | 36 * Adds an expectation for the given string to be spoken. If satisfied, |
| 45 * |opt_callback| is called. | 37 * |opt_callback| is called. |
| 46 * @param {string} expectedText | 38 * @param {string} expectedText |
| 47 * @param {function() : void=} opt_callback | 39 * @param {function() : void=} opt_callback |
| 48 * @param {boolean=} opt_exact Expect an exact match; defaults to false. | 40 * @param {boolean=} opt_exact Expect an exact match; defaults to false. |
| 49 */ | 41 */ |
| 50 expectSpeech: function(expectedText, opt_callback, opt_exact) { | 42 expectSpeech: function(expectedText, opt_callback, opt_exact) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 70 }, | 62 }, |
| 71 | 63 |
| 72 /** | 64 /** |
| 73 * Finishes expectations and calls testDone. | 65 * Finishes expectations and calls testDone. |
| 74 */ | 66 */ |
| 75 finishExpectations: function() { | 67 finishExpectations: function() { |
| 76 this.expectSpeechAfter('', testDone); | 68 this.expectSpeechAfter('', testDone); |
| 77 }, | 69 }, |
| 78 | 70 |
| 79 /** | 71 /** |
| 80 * Fakes an event to |onEvent|. | |
| 81 */ | |
| 82 sendStartEvent: function() { | |
| 83 if (this.onEvent_) | |
| 84 this.onEvent_({type: 'start'}); | |
| 85 }, | |
| 86 | |
| 87 /** | |
| 88 * Fakes an event to |onEvent|. | |
| 89 */ | |
| 90 sendEndEvent: function() { | |
| 91 if (this.onEvent_) | |
| 92 this.onEvent_({type: 'end'}); | |
| 93 }, | |
| 94 | |
| 95 /** | |
| 96 * @private | 72 * @private |
| 97 * @param {string} expectedText Text expected spoken. | 73 * @param {string} expectedText Text expected spoken. |
| 98 * @param {{startCallback: function() : void, | 74 * @param {{startCallback: function() : void, |
| 99 * endCallback: function() : void}=} opt_expectation | 75 * endCallback: function() : void}=} opt_expectation |
| 100 * @param {boolean=} opt_exact Expects an exact match. | 76 * @param {boolean=} opt_exact Expects an exact match. |
| 101 */ | 77 */ |
| 102 addExpectation_: function(expectedText, opt_expectation, opt_exact) { | 78 addExpectation_: function(expectedText, opt_expectation, opt_exact) { |
| 103 var expectation = opt_expectation ? opt_expectation : {}; | 79 var expectation = opt_expectation ? opt_expectation : {}; |
| 104 | 80 |
| 105 expectation.predicate = function(actualText) { | 81 expectation.predicate = function(actualText) { |
| 106 if (opt_exact) | 82 if (opt_exact) |
| 107 return actualText === expectedText; | 83 return actualText === expectedText; |
| 108 return actualText.indexOf(expectedText) != -1; | 84 return actualText.indexOf(expectedText) != -1; |
| 109 }; | 85 }; |
| 110 | 86 |
| 111 this.expectations_.push(expectation); | 87 this.expectations_.push(expectation); |
| 112 | 88 |
| 113 // Process any idleUtterances. | 89 // Process any idleUtterances. |
| 114 this.idleUtterances_.forEach(function(utterance) { | 90 this.idleUtterances_.forEach(function(utterance) { |
| 115 this.process_(utterance, true); | 91 this.process_(utterance.text, true, utterance.properties); |
| 116 }.bind(this)); | 92 }.bind(this)); |
| 117 }, | 93 }, |
| 118 | 94 |
| 119 /** | 95 /** |
| 120 * @param {string} textString Utterance to match against callbacks. | 96 * @param {string} textString Utterance to match against callbacks. |
| 121 * @param {boolean=} opt_manual True if called outside of tts.speak. | 97 * @param {boolean=} opt_manual True if called outside of tts.speak. |
| 98 * @param {!Object=} opt_properties |
| 122 * @private | 99 * @private |
| 123 */ | 100 */ |
| 124 process_: function(textString, opt_manual) { | 101 process_: function(textString, opt_manual, opt_properties) { |
| 102 var utterance = {text: textString, properties: opt_properties}; |
| 125 if (this.expectations_.length == 0) { | 103 if (this.expectations_.length == 0) { |
| 126 if (!opt_manual) | 104 if (!opt_manual) { |
| 127 this.idleUtterances_.push(textString); | 105 this.idleUtterances_.push(utterance); |
| 106 } |
| 128 return; | 107 return; |
| 129 } | 108 } |
| 130 | 109 |
| 131 var allUtterances = this.idleUtterances_.concat([textString]); | 110 var allUtterances = this.idleUtterances_.concat([utterance]); |
| 132 var targetExpectation = this.expectations_.shift(); | 111 var targetExpectation = this.expectations_.shift(); |
| 133 if (allUtterances.some(targetExpectation.predicate)) { | 112 allUtterances = allUtterances.filter(function(u) { |
| 113 return targetExpectation.predicate(u.text); |
| 114 }); |
| 115 if (allUtterances.length > 0) { |
| 116 var matchingProperties = allUtterances[0].properties; |
| 134 this.idleUtterances_.length = 0; | 117 this.idleUtterances_.length = 0; |
| 135 if (targetExpectation.endCallback) | 118 if (targetExpectation.endCallback) |
| 136 targetExpectation.endCallback(); | 119 targetExpectation.endCallback(); |
| 120 if (matchingProperties && matchingProperties.endCallback) { |
| 121 matchingProperties.endCallback(); |
| 122 } |
| 137 var nextExpectation = this.expectations_[0]; | 123 var nextExpectation = this.expectations_[0]; |
| 138 if (nextExpectation && nextExpectation.startCallback) | 124 if (nextExpectation && nextExpectation.startCallback) |
| 139 nextExpectation.startCallback(); | 125 nextExpectation.startCallback(); |
| 140 } else { | 126 } else { |
| 141 this.expectations_.unshift(targetExpectation); | 127 this.expectations_.unshift(targetExpectation); |
| 142 } | 128 } |
| 143 }, | 129 }, |
| 144 }; | 130 }; |
| OLD | NEW |