summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/chromevox/testing/mock_tts.js
blob: 3f32d0ca400195ba699af41f9c70f0463edc5d2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * Mock tts class.
 * @constructor
 * @extends {cvox.TtsInterface}
 */
var MockTts = function() {
};

MockTts.prototype = {
  /**
   * A list of predicate, start, and end callbacks for a pending expectation.
   * @type {!Array<{{predicate: function(string) : boolean,
   *     startCallback: function() : void,
   *     endCallback: function() : void}>}
   * @private
   */
  expectations_: [],

  /**
   * A list of strings stored whenever there are no expectations.
   * @type {!Array<string}
   * @private
   */
  idleUtterances_: [],

  /** @override */
  speak: function(textString, queueMode, properties) {
    this.process_(textString, false, properties);
  },

  /**
   * Adds an expectation for the given string to be spoken. If satisfied,
   * |opt_callback| is called.
   * @param {string} expectedText
   * @param {function() : void=} opt_callback
   * @param {boolean=} opt_exact Expect an exact match; defaults to false.
   */
  expectSpeech: function(expectedText, opt_callback, opt_exact) {
    var expectation = {};
    expectation.endCallback = opt_callback;
    this.addExpectation_(expectedText, expectation, opt_exact);
  },

  /**
   * Adds an expectation for the given string to be spoken after |opt_callback|
   * executes.
   * @param {string} expectedText
   * @param {function() : void=} opt_callback
   * @param {boolean=} opt_exact Expect an exact match; defaults to false.
   */
  expectSpeechAfter: function(expectedText, opt_callback, opt_exact) {
    var expectation = {};
    if (this.expectations_.length == 0 && opt_callback)
      opt_callback();
    else
      expectation.startCallback = opt_callback;
    this.addExpectation_(expectedText, expectation, opt_exact);
  },

  /**
   * Finishes expectations and calls {@code callback} afterwards.
   * @param {Function} callback
   */
  finishExpectations: function(callback) {
    this.expectSpeechAfter('', callback);
  },

  /**
   * @private
   * @param {string} expectedText Text expected spoken.
   * @param {{startCallback: function() : void,
   *     endCallback: function() : void}=} opt_expectation
   * @param {boolean=} opt_exact Expects an exact match.
   */
  addExpectation_: function(expectedText, opt_expectation, opt_exact) {
    var expectation = opt_expectation ? opt_expectation : {};

    expectation.predicate = function(actualText) {
      if (opt_exact)
        return actualText === expectedText;
      return actualText.indexOf(expectedText) != -1;
    };

    this.expectations_.push(expectation);

    // Process any idleUtterances.
    this.idleUtterances_.forEach(function(utterance) {
      this.process_(utterance.text, true, utterance.properties);
    }.bind(this));
  },

  /**
   * @param {string} textString Utterance to match against callbacks.
   * @param {boolean=} opt_manual True if called outside of tts.speak.
   * @param {!Object=} opt_properties
   * @private
   */
  process_: function(textString, opt_manual, opt_properties) {
    var utterance = {text: textString, properties: opt_properties};
    if (this.expectations_.length == 0) {
      if (!opt_manual) {
        this.idleUtterances_.push(utterance);
      }
      return;
    }

    var allUtterances = this.idleUtterances_.concat([utterance]);
    var targetExpectation = this.expectations_.shift();
    allUtterances = allUtterances.filter(function(u) {
      return targetExpectation.predicate(u.text);
    });
    if (allUtterances.length > 0) {
      var matchingProperties = allUtterances[0].properties;
      this.idleUtterances_.length = 0;
      if (targetExpectation.endCallback)
        targetExpectation.endCallback();
      if (matchingProperties && matchingProperties.endCallback) {
        matchingProperties.endCallback();
      }
      var nextExpectation = this.expectations_[0];
      if (nextExpectation && nextExpectation.startCallback)
        nextExpectation.startCallback();
    } else {
      this.expectations_.unshift(targetExpectation);
    }
  },
};