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.
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
;
30 class MockTtsPlatformImpl
: public TtsPlatformImpl
{
33 : ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) {}
35 virtual bool PlatformImplAvailable() {
39 virtual bool SendsEvent(TtsEventType event_type
) {
40 return (event_type
== TTS_EVENT_END
||
41 event_type
== TTS_EVENT_WORD
);
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(),
71 void SendEndEventWhenQueueNotEmpty(
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()),
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
,
101 void SendEvent(bool wait_for_non_empty_queue
,
103 TtsEventType event_type
,
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));
117 controller
->OnTtsEvent(utterance_id
, event_type
, char_index
, message
);
121 base::WeakPtrFactory
<MockTtsPlatformImpl
> ptr_factory_
;
124 class TtsApiTest
: public ExtensionApiTest
{
126 virtual void SetUpInProcessBrowserTestFixture() {
127 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
128 TtsController::GetInstance()->SetPlatformImpl(&mock_platform_impl_
);
132 StrictMock
<MockTtsPlatformImpl
> mock_platform_impl_
;
135 IN_PROC_BROWSER_TEST_F(TtsApiTest
, PlatformSpeakOptionalArgs
) {
136 EXPECT_CALL(mock_platform_impl_
, IsSpeaking());
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
) {
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(_
, _
, _
, _
))
169 Invoke(&mock_platform_impl_
,
170 &MockTtsPlatformImpl::SendEndEvent
),
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.
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", _
, _
))
189 Invoke(&mock_platform_impl_
,
190 &MockTtsPlatformImpl::SendEndEvent
),
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.
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", _
, _
))
212 Invoke(&mock_platform_impl_
,
213 &MockTtsPlatformImpl::SendEndEvent
),
215 ASSERT_TRUE(RunExtensionTest("tts/queue_interrupt")) << message_
;
218 IN_PROC_BROWSER_TEST_F(TtsApiTest
, PlatformSpeakEnqueue
) {
219 EXPECT_CALL(mock_platform_impl_
, IsSpeaking());
222 EXPECT_CALL(mock_platform_impl_
, StopSpeaking())
223 .WillOnce(Return(true));
224 EXPECT_CALL(mock_platform_impl_
, Speak(_
, "text 1", _
, _
))
226 Invoke(&mock_platform_impl_
,
227 &MockTtsPlatformImpl::SendEndEventWhenQueueNotEmpty
),
229 EXPECT_CALL(mock_platform_impl_
, Speak(_
, "text 2", _
, _
))
231 Invoke(&mock_platform_impl_
,
232 &MockTtsPlatformImpl::SendEndEvent
),
234 ASSERT_TRUE(RunExtensionTest("tts/enqueue")) << message_
;
237 IN_PROC_BROWSER_TEST_F(TtsApiTest
, PlatformSpeakError
) {
238 EXPECT_CALL(mock_platform_impl_
, IsSpeaking())
242 EXPECT_CALL(mock_platform_impl_
, StopSpeaking())
243 .WillOnce(Return(true));
244 EXPECT_CALL(mock_platform_impl_
, Speak(_
, "first try", _
, _
))
247 CreateFunctor(&mock_platform_impl_
,
248 &MockTtsPlatformImpl::SetErrorToEpicFail
)),
250 EXPECT_CALL(mock_platform_impl_
, StopSpeaking())
251 .WillOnce(Return(true));
252 EXPECT_CALL(mock_platform_impl_
, Speak(_
, "second try", _
, _
))
254 Invoke(&mock_platform_impl_
,
255 &MockTtsPlatformImpl::SendEndEvent
),
257 ASSERT_TRUE(RunExtensionTest("tts/speak_error")) << message_
;
260 IN_PROC_BROWSER_TEST_F(TtsApiTest
, PlatformWordCallbacks
) {
261 EXPECT_CALL(mock_platform_impl_
, IsSpeaking());
264 EXPECT_CALL(mock_platform_impl_
, StopSpeaking())
265 .WillOnce(Return(true));
266 EXPECT_CALL(mock_platform_impl_
, Speak(_
, "one two three", _
, _
))
268 Invoke(&mock_platform_impl_
,
269 &MockTtsPlatformImpl::SendWordEvents
),
270 Invoke(&mock_platform_impl_
,
271 &MockTtsPlatformImpl::SendEndEvent
),
273 ASSERT_TRUE(RunExtensionTest("tts/word_callbacks")) << message_
;
276 IN_PROC_BROWSER_TEST_F(TtsApiTest
, RegisterEngine
) {
277 EXPECT_CALL(mock_platform_impl_
, IsSpeaking())
279 EXPECT_CALL(mock_platform_impl_
, StopSpeaking())
280 .WillRepeatedly(Return(true));
284 EXPECT_CALL(mock_platform_impl_
, Speak(_
, "native speech", _
, _
))
286 Invoke(&mock_platform_impl_
,
287 &MockTtsPlatformImpl::SendEndEvent
),
289 EXPECT_CALL(mock_platform_impl_
, Speak(_
, "native speech 2", _
, _
))
291 Invoke(&mock_platform_impl_
,
292 &MockTtsPlatformImpl::SendEndEvent
),
294 EXPECT_CALL(mock_platform_impl_
, Speak(_
, "native speech 3", _
, _
))
296 Invoke(&mock_platform_impl_
,
297 &MockTtsPlatformImpl::SendEndEvent
),
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_
;