a16e300854e64bd3e6641bf6c1b617a4b0e713f6
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / chromeos / chromevox / host / testing / tts.js
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
10 goog.provide('cvox.TestTts');
11
12 goog.require('cvox.AbstractTts');
13 goog.require('cvox.DomUtil');
14 goog.require('cvox.HostFactory');
15
16
17
18 /**
19  * @constructor
20  * @extends {cvox.AbstractTts}
21  */
22 cvox.TestTts = function() {
23   cvox.AbstractTts.call(this);
24   this.utterances_ = [];
25 };
26 goog.inherits(cvox.TestTts, cvox.AbstractTts);
27
28
29 /**
30  * @type {string}
31  * @private
32  */
33 cvox.TestTts.prototype.sentinelText_ = '@@@STOP@@@';
34
35
36 /**
37  * @override
38  */
39 cvox.TestTts.prototype.speak = function(text, queueMode, opt_properties) {
40   this.utterances_.push({text: text,
41                          queueMode: queueMode,
42                          properties: opt_properties});
43   if (opt_properties && opt_properties['endCallback'] != undefined) {
44     var len = this.utterances_.length;
45     // 'After' is a sentinel value in the tests that tells TTS to stop and
46     // ends callbacks being called.
47     if (this.utterances_[len - 1].text !=
48         this.sentinelText_) {
49       opt_properties['endCallback']();
50     }
51   }
52 };
53
54
55 /**
56  * Creates a sentinel element that indicates when TTS should stop and callbacks
57  * should stop being called.
58  * @return {Element} The sentinel element.
59  */
60 cvox.TestTts.prototype.createSentinel = function() {
61   var sentinel = document.createElement('div');
62   sentinel.textContent = this.sentinelText_;
63   return sentinel;
64 };
65
66
67 /**
68  * All calls to tts.speak are saved in an array of utterances.
69  * Clear any utterances that were saved up to this point.
70  */
71 cvox.TestTts.prototype.clearUtterances = function() {
72   this.utterances_.length = 0;
73 };
74
75 /**
76  * Return a string of what was spoken by tts.speak().
77  * @return {string} A single string containing all utterances spoken
78  *     since initialization or the last call to clearUtterances,
79  *     concatenated together with all whitespace collapsed to single
80  *     spaces.
81  */
82 cvox.TestTts.prototype.getUtterancesAsString = function() {
83   return cvox.DomUtil.collapseWhitespace(this.getUtteranceList().join(' '));
84 };
85
86 /**
87  * Processes the utterances spoken the same way the speech queue does,
88  * as if they were all generated one after another, with no delay between
89  * them, and returns a list of strings that would be output.
90  *
91  * For example, if two strings were spoken with a queue mode of FLUSH,
92  * only the second string will be returned.
93  * @return {Array.<string>} A list of strings representing the speech output.
94  */
95 cvox.TestTts.prototype.getSpeechQueueOutput = function() {
96   var queue = [];
97   for (var i = 0; i < this.utterances_.length; i++) {
98     var utterance = this.utterances_[i];
99     switch (utterance.queueMode) {
100       case cvox.AbstractTts.QUEUE_MODE_FLUSH:
101         queue = [];
102         break;
103       case cvox.AbstractTts.QUEUE_MODE_QUEUE:
104         break;
105       case cvox.AbstractTts.QUEUE_MODE_CATEGORY_FLUSH:
106         queue = queue.filter(function(u) {
107           return (utterance.properties && utterance.properties.category) &&
108                  (!u.properties ||
109                       u.properties.category != utterance.properties.category);
110         });
111         break;
112     }
113     queue.push(utterance);
114   }
115
116   return queue.map(function(u) { return u.text; });
117 };
118
119 /**
120  * Return a list of strings of what was spoken by tts.speak().
121  * @return {Array.<string>} A list of all utterances spoken since
122  *     initialization or the last call to clearUtterances.
123  */
124 cvox.TestTts.prototype.getUtteranceList = function() {
125   var result = [];
126   for (var i = 0; i < this.utterances_.length; i++) {
127     result.push(this.utterances_[i].text);
128   }
129   return result;
130 };
131
132 /**
133  * Return a list of strings of what was spoken by tts.speak().
134  * @return {Array.<{text: string, queueMode: cvox.QueueMode}>}
135  *     A list of info about all utterances spoken since
136  *     initialization or the last call to clearUtterances.
137  */
138 cvox.TestTts.prototype.getUtteranceInfoList = function() {
139   return this.utterances_;
140 };
141
142 /** @override */
143 cvox.HostFactory.ttsConstructor = cvox.TestTts;