tts.js revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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. 4 5/** 6 * @fileoverview Dummy implementation of TTS for testing. 7 * 8 */ 9 10goog.provide('cvox.TestTts'); 11 12goog.require('cvox.AbstractTts'); 13goog.require('cvox.DomUtil'); 14goog.require('cvox.HostFactory'); 15 16 17 18/** 19 * @constructor 20 * @extends {cvox.AbstractTts} 21 */ 22cvox.TestTts = function() { 23 cvox.AbstractTts.call(this); 24 this.utterances_ = []; 25}; 26goog.inherits(cvox.TestTts, cvox.AbstractTts); 27 28 29/** 30 * @type {string} 31 * @private 32 */ 33cvox.TestTts.prototype.sentinelText_ = '@@@STOP@@@'; 34 35 36/** 37 * @override 38 */ 39cvox.TestTts.prototype.speak = function(text, queueMode, opt_properties) { 40 this.utterances_.push({text: text, queueMode: queueMode}); 41 if (opt_properties && opt_properties['endCallback'] != undefined) { 42 var len = this.utterances_.length; 43 // 'After' is a sentinel value in the tests that tells TTS to stop and 44 // ends callbacks being called. 45 if (this.utterances_[len - 1].text != 46 this.sentinelText_) { 47 opt_properties['endCallback'](); 48 } 49 } 50}; 51 52 53/** 54 * Creates a sentinel element that indicates when TTS should stop and callbacks 55 * should stop being called. 56 * @return {Element} The sentinel element. 57 */ 58cvox.TestTts.prototype.createSentinel = function() { 59 var sentinel = document.createElement('div'); 60 sentinel.textContent = this.sentinelText_; 61 return sentinel; 62}; 63 64 65/** 66 * All calls to tts.speak are saved in an array of utterances. 67 * Clear any utterances that were saved up to this point. 68 */ 69cvox.TestTts.prototype.clearUtterances = function() { 70 this.utterances_.length = 0; 71}; 72 73/** 74 * Return a string of what was spoken by tts.speak(). 75 * @return {string} A single string containing all utterances spoken 76 * since initialization or the last call to clearUtterances, 77 * concatenated together with all whitespace collapsed to single 78 * spaces. 79 */ 80cvox.TestTts.prototype.getUtterancesAsString = function() { 81 return cvox.DomUtil.collapseWhitespace(this.getUtteranceList().join(' ')); 82}; 83 84/** 85 * Return a list of strings of what was spoken by tts.speak(). 86 * @return {Array.<string>} A list of all utterances spoken since 87 * initialization or the last call to clearUtterances. 88 */ 89cvox.TestTts.prototype.getUtteranceList = function() { 90 var result = []; 91 for (var i = 0; i < this.utterances_.length; i++) { 92 result.push(this.utterances_[i].text); 93 } 94 return result; 95}; 96 97/** 98 * Return a list of strings of what was spoken by tts.speak(). 99 * @return {Array.<{text: string, queueMode: number}>} 100 * A list of info about all utterances spoken since 101 * initialization or the last call to clearUtterances. 102 */ 103cvox.TestTts.prototype.getUtteranceInfoList = function() { 104 return this.utterances_; 105}; 106 107/** @override */ 108cvox.HostFactory.ttsConstructor = cvox.TestTts; 109