Fix build break
[chromium-blink-merge.git] / chrome / browser / speech / extension_api / tts_extension_apitest.cc
blob5cfaf4784e5bcafce788ed36859a0f78d08f6296
1 // Copyright (c) 2012 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 #include "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/memory/weak_ptr.h"
8 #include "base/message_loop.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/speech/extension_api/tts_extension_api.h"
11 #include "chrome/browser/speech/tts_controller.h"
12 #include "chrome/browser/speech/tts_platform.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 // Needed for CreateFunctor.
18 #define GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING
19 #include "testing/gmock_mutant.h"
21 using ::testing::AnyNumber;
22 using ::testing::CreateFunctor;
23 using ::testing::DoAll;
24 using ::testing::InSequence;
25 using ::testing::InvokeWithoutArgs;
26 using ::testing::Return;
27 using ::testing::StrictMock;
28 using ::testing::_;
30 class MockTtsPlatformImpl : public TtsPlatformImpl {
31 public:
32 MockTtsPlatformImpl()
33 : ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) {}
35 virtual bool PlatformImplAvailable() {
36 return true;
39 virtual bool SendsEvent(TtsEventType event_type) {
40 return (event_type == TTS_EVENT_END ||
41 event_type == TTS_EVENT_WORD);
45 MOCK_METHOD4(Speak,
46 bool(int utterance_id,
47 const std::string& utterance,
48 const std::string& lang,
49 const UtteranceContinuousParameters& params));
50 MOCK_METHOD0(StopSpeaking, bool(void));
52 MOCK_METHOD0(IsSpeaking, bool(void));
54 void SetErrorToEpicFail() {
55 set_error("epic fail");
58 void SendEndEvent(int utterance_id,
59 const std::string& utterance,
60 const std::string& lang,
61 const UtteranceContinuousParameters& params) {
62 MessageLoop::current()->PostDelayedTask(
63 FROM_HERE, base::Bind(
64 &MockTtsPlatformImpl::SendEvent,
65 ptr_factory_.GetWeakPtr(),
66 false, utterance_id, TTS_EVENT_END, utterance.size(),
67 std::string()),
68 base::TimeDelta());
71 void SendEndEventWhenQueueNotEmpty(
72 int utterance_id,
73 const std::string& utterance,
74 const std::string& lang,
75 const UtteranceContinuousParameters& params) {
76 MessageLoop::current()->PostDelayedTask(
77 FROM_HERE, base::Bind(
78 &MockTtsPlatformImpl::SendEvent,
79 ptr_factory_.GetWeakPtr(),
80 true, utterance_id, TTS_EVENT_END, utterance.size(), std::string()),
81 base::TimeDelta());
84 void SendWordEvents(int utterance_id,
85 const std::string& utterance,
86 const std::string& lang,
87 const UtteranceContinuousParameters& params) {
88 for (int i = 0; i < static_cast<int>(utterance.size()); i++) {
89 if (i == 0 || utterance[i - 1] == ' ') {
90 MessageLoop::current()->PostDelayedTask(
91 FROM_HERE, base::Bind(
92 &MockTtsPlatformImpl::SendEvent,
93 ptr_factory_.GetWeakPtr(),
94 false, utterance_id, TTS_EVENT_WORD, i,
95 std::string()),
96 base::TimeDelta());
101 void SendEvent(bool wait_for_non_empty_queue,
102 int utterance_id,
103 TtsEventType event_type,
104 int char_index,
105 const std::string& message) {
106 TtsController* controller = TtsController::GetInstance();
107 if (wait_for_non_empty_queue && controller->QueueSize() == 0) {
108 MessageLoop::current()->PostDelayedTask(
109 FROM_HERE, base::Bind(
110 &MockTtsPlatformImpl::SendEvent,
111 ptr_factory_.GetWeakPtr(),
112 true, utterance_id, event_type, char_index, message),
113 base::TimeDelta::FromMilliseconds(100));
114 return;
117 controller->OnTtsEvent(utterance_id, event_type, char_index, message);
120 private:
121 base::WeakPtrFactory<MockTtsPlatformImpl> ptr_factory_;
124 class TtsApiTest : public ExtensionApiTest {
125 public:
126 virtual void SetUpInProcessBrowserTestFixture() {
127 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
128 TtsController::GetInstance()->SetPlatformImpl(&mock_platform_impl_);
131 protected:
132 StrictMock<MockTtsPlatformImpl> mock_platform_impl_;
135 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakOptionalArgs) {
136 EXPECT_CALL(mock_platform_impl_, IsSpeaking());
138 InSequence s;
139 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
140 .WillOnce(Return(true));
141 EXPECT_CALL(mock_platform_impl_, Speak(_, "", _, _))
142 .WillOnce(Return(true));
143 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
144 .WillOnce(Return(true));
145 EXPECT_CALL(mock_platform_impl_, Speak(_, "Alpha", _, _))
146 .WillOnce(Return(true));
147 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
148 .WillOnce(Return(true));
149 EXPECT_CALL(mock_platform_impl_, Speak(_, "Bravo", _, _))
150 .WillOnce(Return(true));
151 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
152 .WillOnce(Return(true));
153 EXPECT_CALL(mock_platform_impl_, Speak(_, "Charlie", _, _))
154 .WillOnce(Return(true));
155 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
156 .WillOnce(Return(true));
157 EXPECT_CALL(mock_platform_impl_, Speak(_, "Echo", _, _))
158 .WillOnce(Return(true));
159 ASSERT_TRUE(RunExtensionTest("tts/optional_args")) << message_;
162 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakFinishesImmediately) {
163 InSequence s;
164 EXPECT_CALL(mock_platform_impl_, IsSpeaking());
165 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
166 .WillOnce(Return(true));
167 EXPECT_CALL(mock_platform_impl_, Speak(_, _, _, _))
168 .WillOnce(DoAll(
169 Invoke(&mock_platform_impl_,
170 &MockTtsPlatformImpl::SendEndEvent),
171 Return(true)));
172 ASSERT_TRUE(RunExtensionTest("tts/speak_once")) << message_;
175 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakInterrupt) {
176 EXPECT_CALL(mock_platform_impl_, IsSpeaking());
178 // One utterance starts speaking, and then a second interrupts.
179 InSequence s;
180 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
181 .WillOnce(Return(true));
182 EXPECT_CALL(mock_platform_impl_, Speak(_, "text 1", _, _))
183 .WillOnce(Return(true));
184 // Expect the second utterance and allow it to finish.
185 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
186 .WillOnce(Return(true));
187 EXPECT_CALL(mock_platform_impl_, Speak(_, "text 2", _, _))
188 .WillOnce(DoAll(
189 Invoke(&mock_platform_impl_,
190 &MockTtsPlatformImpl::SendEndEvent),
191 Return(true)));
192 ASSERT_TRUE(RunExtensionTest("tts/interrupt")) << message_;
195 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakQueueInterrupt) {
196 EXPECT_CALL(mock_platform_impl_, IsSpeaking());
198 // In this test, two utterances are queued, and then a third
199 // interrupts. Speak() never gets called on the second utterance.
200 InSequence s;
201 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
202 .WillOnce(Return(true));
203 EXPECT_CALL(mock_platform_impl_, Speak(_, "text 1", _, _))
204 .WillOnce(Return(true));
205 // Don't expect the second utterance, because it's queued up and the
206 // first never finishes.
207 // Expect the third utterance and allow it to finish successfully.
208 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
209 .WillOnce(Return(true));
210 EXPECT_CALL(mock_platform_impl_, Speak(_, "text 3", _, _))
211 .WillOnce(DoAll(
212 Invoke(&mock_platform_impl_,
213 &MockTtsPlatformImpl::SendEndEvent),
214 Return(true)));
215 ASSERT_TRUE(RunExtensionTest("tts/queue_interrupt")) << message_;
218 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakEnqueue) {
219 EXPECT_CALL(mock_platform_impl_, IsSpeaking());
221 InSequence s;
222 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
223 .WillOnce(Return(true));
224 EXPECT_CALL(mock_platform_impl_, Speak(_, "text 1", _, _))
225 .WillOnce(DoAll(
226 Invoke(&mock_platform_impl_,
227 &MockTtsPlatformImpl::SendEndEventWhenQueueNotEmpty),
228 Return(true)));
229 EXPECT_CALL(mock_platform_impl_, Speak(_, "text 2", _, _))
230 .WillOnce(DoAll(
231 Invoke(&mock_platform_impl_,
232 &MockTtsPlatformImpl::SendEndEvent),
233 Return(true)));
234 ASSERT_TRUE(RunExtensionTest("tts/enqueue")) << message_;
237 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakError) {
238 EXPECT_CALL(mock_platform_impl_, IsSpeaking())
239 .Times(AnyNumber());
241 InSequence s;
242 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
243 .WillOnce(Return(true));
244 EXPECT_CALL(mock_platform_impl_, Speak(_, "first try", _, _))
245 .WillOnce(DoAll(
246 InvokeWithoutArgs(
247 CreateFunctor(&mock_platform_impl_,
248 &MockTtsPlatformImpl::SetErrorToEpicFail)),
249 Return(false)));
250 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
251 .WillOnce(Return(true));
252 EXPECT_CALL(mock_platform_impl_, Speak(_, "second try", _, _))
253 .WillOnce(DoAll(
254 Invoke(&mock_platform_impl_,
255 &MockTtsPlatformImpl::SendEndEvent),
256 Return(true)));
257 ASSERT_TRUE(RunExtensionTest("tts/speak_error")) << message_;
260 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformWordCallbacks) {
261 EXPECT_CALL(mock_platform_impl_, IsSpeaking());
263 InSequence s;
264 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
265 .WillOnce(Return(true));
266 EXPECT_CALL(mock_platform_impl_, Speak(_, "one two three", _, _))
267 .WillOnce(DoAll(
268 Invoke(&mock_platform_impl_,
269 &MockTtsPlatformImpl::SendWordEvents),
270 Invoke(&mock_platform_impl_,
271 &MockTtsPlatformImpl::SendEndEvent),
272 Return(true)));
273 ASSERT_TRUE(RunExtensionTest("tts/word_callbacks")) << message_;
276 IN_PROC_BROWSER_TEST_F(TtsApiTest, RegisterEngine) {
277 EXPECT_CALL(mock_platform_impl_, IsSpeaking())
278 .Times(AnyNumber());
279 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
280 .WillRepeatedly(Return(true));
283 InSequence s;
284 EXPECT_CALL(mock_platform_impl_, Speak(_, "native speech", _, _))
285 .WillOnce(DoAll(
286 Invoke(&mock_platform_impl_,
287 &MockTtsPlatformImpl::SendEndEvent),
288 Return(true)));
289 EXPECT_CALL(mock_platform_impl_, Speak(_, "native speech 2", _, _))
290 .WillOnce(DoAll(
291 Invoke(&mock_platform_impl_,
292 &MockTtsPlatformImpl::SendEndEvent),
293 Return(true)));
294 EXPECT_CALL(mock_platform_impl_, Speak(_, "native speech 3", _, _))
295 .WillOnce(DoAll(
296 Invoke(&mock_platform_impl_,
297 &MockTtsPlatformImpl::SendEndEvent),
298 Return(true)));
301 ASSERT_TRUE(RunExtensionTest("tts_engine/register_engine")) << message_;
304 IN_PROC_BROWSER_TEST_F(TtsApiTest, EngineError) {
305 EXPECT_CALL(mock_platform_impl_, IsSpeaking());
306 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
307 .WillRepeatedly(Return(true));
309 ASSERT_TRUE(RunExtensionTest("tts_engine/engine_error")) << message_;
312 IN_PROC_BROWSER_TEST_F(TtsApiTest, EngineWordCallbacks) {
313 EXPECT_CALL(mock_platform_impl_, IsSpeaking());
314 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
315 .WillRepeatedly(Return(true));
317 ASSERT_TRUE(RunExtensionTest("tts_engine/engine_word_callbacks")) << message_;
320 IN_PROC_BROWSER_TEST_F(TtsApiTest, LangMatching) {
321 EXPECT_CALL(mock_platform_impl_, IsSpeaking());
322 EXPECT_CALL(mock_platform_impl_, StopSpeaking())
323 .WillRepeatedly(Return(true));
325 ASSERT_TRUE(RunExtensionTest("tts_engine/lang_matching")) << message_;
328 // http://crbug.com/122474
329 IN_PROC_BROWSER_TEST_F(TtsApiTest, EngineApi) {
330 ASSERT_TRUE(RunExtensionTest("tts_engine/engine_api")) << message_;