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.
8 * @extends {cvox.TtsInterface}
10 var MockTts = function() {
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}>}
24 * A list of strings stored whenever there are no expectations.
25 * @type {!Array<string}
31 speak: function(textString
, queueMode
, properties
) {
32 this.process_(textString
, false, properties
);
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.
42 expectSpeech: function(expectedText
, opt_callback
, opt_exact
) {
44 expectation
.endCallback
= opt_callback
;
45 this.addExpectation_(expectedText
, expectation
, opt_exact
);
49 * Adds an expectation for the given string to be spoken after |opt_callback|
51 * @param {string} expectedText
52 * @param {function() : void=} opt_callback
53 * @param {boolean=} opt_exact Expect an exact match; defaults to false.
55 expectSpeechAfter: function(expectedText
, opt_callback
, opt_exact
) {
57 if (this.expectations_
.length
== 0 && opt_callback
)
60 expectation
.startCallback
= opt_callback
;
61 this.addExpectation_(expectedText
, expectation
, opt_exact
);
65 * Finishes expectations and calls {@code callback} afterwards.
66 * @param {Function} callback
68 finishExpectations: function(callback
) {
69 this.expectSpeechAfter('', callback
);
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.
79 addExpectation_: function(expectedText
, opt_expectation
, opt_exact
) {
80 var expectation
= opt_expectation
? opt_expectation
: {};
82 expectation
.predicate = function(actualText
) {
84 return actualText
=== expectedText
;
85 return actualText
.indexOf(expectedText
) != -1;
88 this.expectations_
.push(expectation
);
90 // Process any idleUtterances.
91 this.idleUtterances_
.forEach(function(utterance
) {
92 this.process_(utterance
.text
, true, utterance
.properties
);
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
102 process_: function(textString
, opt_manual
, opt_properties
) {
103 var utterance
= {text
: textString
, properties
: opt_properties
};
104 if (this.expectations_
.length
== 0) {
106 this.idleUtterances_
.push(utterance
);
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
);
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();
124 var nextExpectation
= this.expectations_
[0];
125 if (nextExpectation
&& nextExpectation
.startCallback
)
126 nextExpectation
.startCallback();
128 this.expectations_
.unshift(targetExpectation
);