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() {
12 * The event handler for the most recent call to |speak|.
20 * A list of predicate, start, and end callbacks for a pending expectation.
21 * @type {!Array.<{{predicate: function(string) : boolean,
22 * startCallback: function() : void,
23 * endCallback: function() : void}>}
29 * A list of strings stored whenever there are no expectations.
30 * @type {!Array.<string}
36 speak: function(textString, queueMode, properties) {
38 this.onEvent_ = properties['onEvent'];
40 this.process_(textString);
44 * Adds an expectation for the given string to be spoken. If satisfied,
45 * |opt_callback| is called.
46 * @param {string} expectedText
47 * @param {function() : void=} opt_callback
48 * @param {boolean=} opt_exact Expect an exact match; defaults to false.
50 expectSpeech: function(expectedText, opt_callback, opt_exact) {
52 expectation.endCallback = opt_callback;
53 this.addExpectation_(expectedText, expectation, opt_exact);
57 * Adds an expectation for the given string to be spoken after |opt_callback|
59 * @param {string} expectedText
60 * @param {function() : void=} opt_callback
61 * @param {boolean=} opt_exact Expect an exact match; defaults to false.
63 expectSpeechAfter: function(expectedText, opt_callback, opt_exact) {
65 if (this.expectations_.length == 0 && opt_callback)
68 expectation.startCallback = opt_callback;
69 this.addExpectation_(expectedText, expectation, opt_exact);
73 * Finishes expectations and calls testDone.
75 finishExpectations: function() {
76 this.expectSpeechAfter('', testDone);
80 * Fakes an event to |onEvent|.
82 sendStartEvent: function() {
84 this.onEvent_({type: 'start'});
88 * Fakes an event to |onEvent|.
90 sendEndEvent: function() {
92 this.onEvent_({type: 'end'});
97 * @param {string} expectedText Text expected spoken.
98 * @param {{startCallback: function() : void,
99 * endCallback: function() : void}=} opt_expectation
100 * @param {boolean=} opt_exact Expects an exact match.
102 addExpectation_: function(expectedText, opt_expectation, opt_exact) {
103 var expectation = opt_expectation ? opt_expectation : {};
105 expectation.predicate = function(actualText) {
107 return actualText === expectedText;
108 return actualText.indexOf(expectedText) != -1;
111 this.expectations_.push(expectation);
113 // Process any idleUtterances.
114 this.idleUtterances_.forEach(function(utterance) {
115 this.process_(utterance, true);
120 * @param {string} textString Utterance to match against callbacks.
121 * @param {boolean=} opt_manual True if called outside of tts.speak.
124 process_: function(textString, opt_manual) {
125 if (this.expectations_.length == 0) {
127 this.idleUtterances_.push(textString);
131 var allUtterances = this.idleUtterances_.concat([textString]);
132 var targetExpectation = this.expectations_.shift();
133 if (allUtterances.some(targetExpectation.predicate)) {
134 this.idleUtterances_.length = 0;
135 if (targetExpectation.endCallback)
136 targetExpectation.endCallback();
137 var nextExpectation = this.expectations_[0];
138 if (nextExpectation && nextExpectation.startCallback)
139 nextExpectation.startCallback();
141 this.expectations_.unshift(targetExpectation);