Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / tts_engine / register_engine / test.js
blobcd40303b915bc94705b21e4a73b5bc4b7864e995
1 // Copyright (c) 2011 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.
5 // TTS api test for Chrome on ChromeOS.
6 // browser_tests.exe --gtest_filter="TtsApiTest.*"
8 chrome.test.runTests([
9 function testNoListeners() {
10 // This call should go to native speech because we haven't registered
11 // any listeners.
12 chrome.tts.speak(
13 'native speech',
15 'onEvent': function(event) {
16 if (event.type == 'end') {
17 chrome.test.succeed();
20 }, function() {
21 chrome.test.assertNoLastError();
22 });
24 function testTtsEngine() {
25 var calledOurEngine = false;
27 // Register listeners for speech functions, enabling this extension
28 // to be a TTS engine.
29 var speakListener = function(utterance, options, sendTtsEvent) {
30 chrome.test.assertNoLastError();
31 chrome.test.assertEq('extension speech', utterance);
32 calledOurEngine = true;
33 sendTtsEvent({'type': 'end', 'charIndex': utterance.length});
35 var stopListener = function() {};
36 chrome.ttsEngine.onSpeak.addListener(speakListener);
37 chrome.ttsEngine.onStop.addListener(stopListener);
39 // This call should go to our own speech engine.
40 chrome.tts.speak(
41 'extension speech',
43 'onEvent': function(event) {
44 if (event.type == 'end') {
45 chrome.test.assertEq(true, calledOurEngine);
46 chrome.ttsEngine.onSpeak.removeListener(speakListener);
47 chrome.ttsEngine.onStop.removeListener(stopListener);
48 chrome.test.succeed();
52 function() {
53 chrome.test.assertNoLastError();
54 });
56 function testVoiceMatching() {
57 // Count the number of times our callback functions have been called.
58 var callbacks = 0;
59 // Count the number of times our TTS engine has been called.
60 var speakListenerCalls = 0;
62 // Register listeners for speech functions.
63 var speakListener = function(utterance, options, sendTtsEvent) {
64 speakListenerCalls++;
65 sendTtsEvent({'type': 'end', 'charIndex': utterance.length});
67 var stopListener = function() {};
68 chrome.ttsEngine.onSpeak.addListener(speakListener);
69 chrome.ttsEngine.onStop.addListener(stopListener);
71 // These don't match the voices in the manifest, so they should
72 // go to native speech. The gmock assertions in TtsApiTest::RegisterEngine
73 // enforce that the native TTS handlers are called.
74 chrome.tts.speak(
75 'native speech 2',
77 'voiceName': 'George',
78 'enqueue': true,
79 'onEvent': function(event) {
80 if (event.type == 'end') {
81 callbacks++;
84 }, function() {
85 chrome.test.assertNoLastError();
86 });
87 chrome.tts.speak(
88 'native speech 3',
90 'lang': 'fr-FR',
91 'enqueue': true,
92 'onEvent': function(event) {
93 if (event.type == 'end') {
94 callbacks++;
97 }, function() {
98 chrome.test.assertNoLastError();
99 });
101 // These do match the voices in the manifest, so they should go to our
102 // own TTS engine.
103 chrome.tts.speak(
104 'extension speech 2',
106 'voiceName': 'Alice',
107 'enqueue': true,
108 'onEvent': function(event) {
109 if (event.type == 'end') {
110 callbacks++;
113 }, function() {
114 chrome.test.assertNoLastError();
116 chrome.tts.speak(
117 'extension speech 3',
119 'voiceName': 'Pat',
120 'gender': 'male',
121 'enqueue': true,
122 'onEvent': function(event) {
123 if (event.type == 'end') {
124 callbacks++;
125 chrome.ttsEngine.onSpeak.removeListener(speakListener);
126 chrome.ttsEngine.onStop.removeListener(stopListener);
127 if (callbacks == 4 && speakListenerCalls == 2) {
128 chrome.test.succeed();
132 }, function() {
133 chrome.test.assertNoLastError();
136 function testGetVoices() {
137 // We have to register listeners, or the voices provided
138 // by this extension won't be returned.
139 var speakListener = function(utterance, options, sendTtsEvent) {
140 chrome.test.assertNoLastError();
141 chrome.test.assertEq('extension speech', utterance);
142 sendTtsEvent({'type': 'end', 'charIndex': utterance.length});
144 var stopListener = function() {};
145 chrome.ttsEngine.onSpeak.addListener(speakListener);
146 chrome.ttsEngine.onStop.addListener(stopListener);
148 chrome.tts.getVoices(function(voices) {
149 chrome.test.assertEq(3, voices.length);
151 chrome.test.assertEq('Alice', voices[0].voiceName);
152 chrome.test.assertEq('en-US', voices[0].lang);
153 chrome.test.assertEq('female', voices[0].gender);
155 chrome.test.assertEq('Pat', voices[1].voiceName);
156 chrome.test.assertEq('en-US', voices[1].lang);
158 chrome.test.assertEq('TestNativeVoice', voices[2].voiceName);
159 chrome.test.assertEq('en-GB', voices[2].lang);
160 chrome.test.succeed();