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 }; | 11 }; |
12 | 12 |
13 MockTts.prototype = { | 13 MockTts.prototype = { |
14 /** | 14 /** |
15 * 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. |
16 * @type {!Array<{{predicate: function(string) : boolean, | 16 * @type {!Array<{{predicate: function(string) : boolean, |
17 * startCallback: function() : void, | 17 * startCallback: function() : void, |
18 * endCallback: function() : void}>} | 18 * endCallback: function() : void}>} |
19 * @private | 19 * @private |
20 */ | 20 */ |
21 expectations_: [], | 21 expectations_: [], |
22 | 22 |
23 /** | 23 /** |
24 * A list of strings stored whenever there are no expectations. | 24 * A list of strings stored whenever there are no expectations. |
25 * @type {!Array<string} | 25 * @type {!Array<string} |
26 * @private | 26 * @private |
27 */ | 27 */ |
28 idleUtterances_: [], | 28 idleUtterances_: [], |
29 | 29 |
30 /** | |
31 * Whether we should skip non-matching utterances. Initially it's set | |
32 * to true, but after the first matching utterance it's set to false | |
33 * so that we can assert that no extra unrelated speech is generated. | |
David Tseng
2015/08/19 17:19:25
I think this is called a strict mock.
| |
34 */ | |
David Tseng
2015/08/19 17:19:25
nit: @private
| |
35 skipNonMatchingUtterances_: true, | |
Peter Lundblad
2015/08/20 08:30:06
I support this in principal, but it has the potent
| |
36 | |
30 /** @override */ | 37 /** @override */ |
31 speak: function(textString, queueMode, properties) { | 38 speak: function(textString, queueMode, properties) { |
32 this.process_(textString, false, properties); | 39 this.process_(textString, false, properties); |
33 }, | 40 }, |
34 | 41 |
35 /** | 42 /** |
36 * Adds an expectation for the given string to be spoken. If satisfied, | 43 * Adds an expectation for the given string to be spoken. If satisfied, |
37 * |opt_callback| is called. | 44 * |opt_callback| is called. |
38 * @param {string} expectedText | 45 * @param {string} expectedText |
39 * @param {function() : void=} opt_callback | 46 * @param {function() : void=} opt_callback |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 /** | 79 /** |
73 * @private | 80 * @private |
74 * @param {string} expectedText Text expected spoken. | 81 * @param {string} expectedText Text expected spoken. |
75 * @param {{startCallback: function() : void, | 82 * @param {{startCallback: function() : void, |
76 * endCallback: function() : void}=} opt_expectation | 83 * endCallback: function() : void}=} opt_expectation |
77 * @param {boolean=} opt_exact Expects an exact match. | 84 * @param {boolean=} opt_exact Expects an exact match. |
78 */ | 85 */ |
79 addExpectation_: function(expectedText, opt_expectation, opt_exact) { | 86 addExpectation_: function(expectedText, opt_expectation, opt_exact) { |
80 var expectation = opt_expectation ? opt_expectation : {}; | 87 var expectation = opt_expectation ? opt_expectation : {}; |
81 | 88 |
89 expectation.text = expectedText; | |
82 expectation.predicate = function(actualText) { | 90 expectation.predicate = function(actualText) { |
83 if (opt_exact) | 91 if (opt_exact) |
84 return actualText === expectedText; | 92 return actualText === expectedText; |
85 return actualText.indexOf(expectedText) != -1; | 93 return actualText.indexOf(expectedText) != -1; |
86 }; | 94 }; |
87 | 95 |
88 this.expectations_.push(expectation); | 96 this.expectations_.push(expectation); |
89 | 97 |
90 // Process any idleUtterances. | 98 // Process any idleUtterances. |
91 this.idleUtterances_.forEach(function(utterance) { | 99 this.idleUtterances_.forEach(function(utterance) { |
(...skipping 11 matching lines...) Expand all Loading... | |
103 var utterance = {text: textString, properties: opt_properties}; | 111 var utterance = {text: textString, properties: opt_properties}; |
104 if (this.expectations_.length == 0) { | 112 if (this.expectations_.length == 0) { |
105 if (!opt_manual) { | 113 if (!opt_manual) { |
106 this.idleUtterances_.push(utterance); | 114 this.idleUtterances_.push(utterance); |
107 } | 115 } |
108 return; | 116 return; |
109 } | 117 } |
110 | 118 |
111 var allUtterances = this.idleUtterances_.concat([utterance]); | 119 var allUtterances = this.idleUtterances_.concat([utterance]); |
112 var targetExpectation = this.expectations_.shift(); | 120 var targetExpectation = this.expectations_.shift(); |
113 allUtterances = allUtterances.filter(function(u) { | 121 |
114 return targetExpectation.predicate(u.text); | 122 if (this.skipNonMatchingUtterances_) { |
115 }); | 123 allUtterances = allUtterances.filter(function(u) { |
124 return targetExpectation.predicate(u.text); | |
125 }); | |
126 } else if (allUtterances.length > 0 && | |
127 !targetExpectation.predicate(allUtterances[0].text)) { | |
128 throw new Error('Mock TTS expected ' + targetExpectation.text + | |
129 ' but got ' + allUtterances[0].text); | |
130 return; | |
131 } | |
132 | |
116 if (allUtterances.length > 0) { | 133 if (allUtterances.length > 0) { |
134 // We got a match. Don't skip any more non-matching utterances | |
135 // anymore. | |
Peter Lundblad
2015/08/20 08:30:06
nit: any more, anymore
| |
136 this.skipNonMatchingUtterances_ = false; | |
137 | |
117 var matchingProperties = allUtterances[0].properties; | 138 var matchingProperties = allUtterances[0].properties; |
118 this.idleUtterances_.length = 0; | 139 this.idleUtterances_.length = 0; |
119 if (targetExpectation.endCallback) | 140 if (targetExpectation.endCallback) |
120 targetExpectation.endCallback(); | 141 targetExpectation.endCallback(); |
121 if (matchingProperties && matchingProperties.endCallback) { | 142 if (matchingProperties && matchingProperties.endCallback) { |
122 matchingProperties.endCallback(); | 143 matchingProperties.endCallback(); |
123 } | 144 } |
124 var nextExpectation = this.expectations_[0]; | 145 var nextExpectation = this.expectations_[0]; |
125 if (nextExpectation && nextExpectation.startCallback) | 146 if (nextExpectation && nextExpectation.startCallback) |
126 nextExpectation.startCallback(); | 147 nextExpectation.startCallback(); |
127 } else { | 148 } else { |
128 this.expectations_.unshift(targetExpectation); | 149 this.expectations_.unshift(targetExpectation); |
129 } | 150 } |
130 }, | 151 }, |
131 }; | 152 }; |
OLD | NEW |