| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 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/extensions/extension_tts_api.h" | |
| 11 #include "chrome/browser/extensions/extension_tts_api_controller.h" | |
| 12 #include "chrome/browser/extensions/extension_tts_api_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" | |
| 16 | |
| 17 // Needed for CreateFunctor. | |
| 18 #define GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING | |
| 19 #include "testing/gmock_mutant.h" | |
| 20 | |
| 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::_; | |
| 29 | |
| 30 class MockExtensionTtsPlatformImpl : public ExtensionTtsPlatformImpl { | |
| 31 public: | |
| 32 MockExtensionTtsPlatformImpl() | |
| 33 : ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) {} | |
| 34 | |
| 35 virtual bool PlatformImplAvailable() { | |
| 36 return true; | |
| 37 } | |
| 38 | |
| 39 virtual bool SendsEvent(TtsEventType event_type) { | |
| 40 return (event_type == TTS_EVENT_END || | |
| 41 event_type == TTS_EVENT_WORD); | |
| 42 } | |
| 43 | |
| 44 | |
| 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)); | |
| 51 | |
| 52 void SetErrorToEpicFail() { | |
| 53 set_error("epic fail"); | |
| 54 } | |
| 55 | |
| 56 void SendEndEvent(int utterance_id, | |
| 57 const std::string& utterance, | |
| 58 const std::string& lang, | |
| 59 const UtteranceContinuousParameters& params) { | |
| 60 MessageLoop::current()->PostDelayedTask( | |
| 61 FROM_HERE, base::Bind( | |
| 62 &MockExtensionTtsPlatformImpl::SendEvent, | |
| 63 ptr_factory_.GetWeakPtr(), | |
| 64 false, utterance_id, TTS_EVENT_END, utterance.size(), | |
| 65 std::string()), | |
| 66 base::TimeDelta()); | |
| 67 } | |
| 68 | |
| 69 void SendEndEventWhenQueueNotEmpty( | |
| 70 int utterance_id, | |
| 71 const std::string& utterance, | |
| 72 const std::string& lang, | |
| 73 const UtteranceContinuousParameters& params) { | |
| 74 MessageLoop::current()->PostDelayedTask( | |
| 75 FROM_HERE, base::Bind( | |
| 76 &MockExtensionTtsPlatformImpl::SendEvent, | |
| 77 ptr_factory_.GetWeakPtr(), | |
| 78 true, utterance_id, TTS_EVENT_END, utterance.size(), std::string()), | |
| 79 base::TimeDelta()); | |
| 80 } | |
| 81 | |
| 82 void SendWordEvents(int utterance_id, | |
| 83 const std::string& utterance, | |
| 84 const std::string& lang, | |
| 85 const UtteranceContinuousParameters& params) { | |
| 86 for (int i = 0; i < static_cast<int>(utterance.size()); i++) { | |
| 87 if (i == 0 || utterance[i - 1] == ' ') { | |
| 88 MessageLoop::current()->PostDelayedTask( | |
| 89 FROM_HERE, base::Bind( | |
| 90 &MockExtensionTtsPlatformImpl::SendEvent, | |
| 91 ptr_factory_.GetWeakPtr(), | |
| 92 false, utterance_id, TTS_EVENT_WORD, i, | |
| 93 std::string()), | |
| 94 base::TimeDelta()); | |
| 95 } | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void SendEvent(bool wait_for_non_empty_queue, | |
| 100 int utterance_id, | |
| 101 TtsEventType event_type, | |
| 102 int char_index, | |
| 103 const std::string& message) { | |
| 104 ExtensionTtsController* controller = ExtensionTtsController::GetInstance(); | |
| 105 if (wait_for_non_empty_queue && controller->QueueSize() == 0) { | |
| 106 MessageLoop::current()->PostDelayedTask( | |
| 107 FROM_HERE, base::Bind( | |
| 108 &MockExtensionTtsPlatformImpl::SendEvent, | |
| 109 ptr_factory_.GetWeakPtr(), | |
| 110 true, utterance_id, event_type, char_index, message), | |
| 111 base::TimeDelta::FromMilliseconds(100)); | |
| 112 return; | |
| 113 } | |
| 114 | |
| 115 controller->OnTtsEvent(utterance_id, event_type, char_index, message); | |
| 116 } | |
| 117 | |
| 118 private: | |
| 119 base::WeakPtrFactory<MockExtensionTtsPlatformImpl> ptr_factory_; | |
| 120 }; | |
| 121 | |
| 122 class TtsApiTest : public ExtensionApiTest { | |
| 123 public: | |
| 124 virtual void SetUpInProcessBrowserTestFixture() { | |
| 125 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); | |
| 126 ExtensionTtsController::GetInstance()->SetPlatformImpl( | |
| 127 &mock_platform_impl_); | |
| 128 } | |
| 129 | |
| 130 protected: | |
| 131 StrictMock<MockExtensionTtsPlatformImpl> mock_platform_impl_; | |
| 132 }; | |
| 133 | |
| 134 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakOptionalArgs) { | |
| 135 InSequence s; | |
| 136 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 137 .WillOnce(Return(true)); | |
| 138 EXPECT_CALL(mock_platform_impl_, Speak(_, "", _, _)) | |
| 139 .WillOnce(Return(true)); | |
| 140 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 141 .WillOnce(Return(true)); | |
| 142 EXPECT_CALL(mock_platform_impl_, Speak(_, "Alpha", _, _)) | |
| 143 .WillOnce(Return(true)); | |
| 144 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 145 .WillOnce(Return(true)); | |
| 146 EXPECT_CALL(mock_platform_impl_, Speak(_, "Bravo", _, _)) | |
| 147 .WillOnce(Return(true)); | |
| 148 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 149 .WillOnce(Return(true)); | |
| 150 EXPECT_CALL(mock_platform_impl_, Speak(_, "Charlie", _, _)) | |
| 151 .WillOnce(Return(true)); | |
| 152 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 153 .WillOnce(Return(true)); | |
| 154 EXPECT_CALL(mock_platform_impl_, Speak(_, "Echo", _, _)) | |
| 155 .WillOnce(Return(true)); | |
| 156 ASSERT_TRUE(RunExtensionTest("tts/optional_args")) << message_; | |
| 157 } | |
| 158 | |
| 159 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakFinishesImmediately) { | |
| 160 InSequence s; | |
| 161 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 162 .WillOnce(Return(true)); | |
| 163 EXPECT_CALL(mock_platform_impl_, Speak(_, _, _, _)) | |
| 164 .WillOnce(DoAll( | |
| 165 Invoke(&mock_platform_impl_, | |
| 166 &MockExtensionTtsPlatformImpl::SendEndEvent), | |
| 167 Return(true))); | |
| 168 ASSERT_TRUE(RunExtensionTest("tts/speak_once")) << message_; | |
| 169 } | |
| 170 | |
| 171 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakInterrupt) { | |
| 172 // One utterance starts speaking, and then a second interrupts. | |
| 173 InSequence s; | |
| 174 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 175 .WillOnce(Return(true)); | |
| 176 EXPECT_CALL(mock_platform_impl_, Speak(_, "text 1", _, _)) | |
| 177 .WillOnce(Return(true)); | |
| 178 // Expect the second utterance and allow it to finish. | |
| 179 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 180 .WillOnce(Return(true)); | |
| 181 EXPECT_CALL(mock_platform_impl_, Speak(_, "text 2", _, _)) | |
| 182 .WillOnce(DoAll( | |
| 183 Invoke(&mock_platform_impl_, | |
| 184 &MockExtensionTtsPlatformImpl::SendEndEvent), | |
| 185 Return(true))); | |
| 186 ASSERT_TRUE(RunExtensionTest("tts/interrupt")) << message_; | |
| 187 } | |
| 188 | |
| 189 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakQueueInterrupt) { | |
| 190 // In this test, two utterances are queued, and then a third | |
| 191 // interrupts. Speak() never gets called on the second utterance. | |
| 192 InSequence s; | |
| 193 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 194 .WillOnce(Return(true)); | |
| 195 EXPECT_CALL(mock_platform_impl_, Speak(_, "text 1", _, _)) | |
| 196 .WillOnce(Return(true)); | |
| 197 // Don't expect the second utterance, because it's queued up and the | |
| 198 // first never finishes. | |
| 199 // Expect the third utterance and allow it to finish successfully. | |
| 200 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 201 .WillOnce(Return(true)); | |
| 202 EXPECT_CALL(mock_platform_impl_, Speak(_, "text 3", _, _)) | |
| 203 .WillOnce(DoAll( | |
| 204 Invoke(&mock_platform_impl_, | |
| 205 &MockExtensionTtsPlatformImpl::SendEndEvent), | |
| 206 Return(true))); | |
| 207 ASSERT_TRUE(RunExtensionTest("tts/queue_interrupt")) << message_; | |
| 208 } | |
| 209 | |
| 210 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakEnqueue) { | |
| 211 InSequence s; | |
| 212 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 213 .WillOnce(Return(true)); | |
| 214 EXPECT_CALL(mock_platform_impl_, Speak(_, "text 1", _, _)) | |
| 215 .WillOnce(DoAll( | |
| 216 Invoke(&mock_platform_impl_, | |
| 217 &MockExtensionTtsPlatformImpl::SendEndEventWhenQueueNotEmpty), | |
| 218 Return(true))); | |
| 219 EXPECT_CALL(mock_platform_impl_, Speak(_, "text 2", _, _)) | |
| 220 .WillOnce(DoAll( | |
| 221 Invoke(&mock_platform_impl_, | |
| 222 &MockExtensionTtsPlatformImpl::SendEndEvent), | |
| 223 Return(true))); | |
| 224 ASSERT_TRUE(RunExtensionTest("tts/enqueue")) << message_; | |
| 225 } | |
| 226 | |
| 227 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakError) { | |
| 228 InSequence s; | |
| 229 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 230 .WillOnce(Return(true)); | |
| 231 EXPECT_CALL(mock_platform_impl_, Speak(_, "first try", _, _)) | |
| 232 .WillOnce(DoAll( | |
| 233 InvokeWithoutArgs( | |
| 234 CreateFunctor(&mock_platform_impl_, | |
| 235 &MockExtensionTtsPlatformImpl::SetErrorToEpicFail)), | |
| 236 Return(false))); | |
| 237 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 238 .WillOnce(Return(true)); | |
| 239 EXPECT_CALL(mock_platform_impl_, Speak(_, "second try", _, _)) | |
| 240 .WillOnce(DoAll( | |
| 241 Invoke(&mock_platform_impl_, | |
| 242 &MockExtensionTtsPlatformImpl::SendEndEvent), | |
| 243 Return(true))); | |
| 244 ASSERT_TRUE(RunExtensionTest("tts/speak_error")) << message_; | |
| 245 } | |
| 246 | |
| 247 IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformWordCallbacks) { | |
| 248 InSequence s; | |
| 249 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 250 .WillOnce(Return(true)); | |
| 251 EXPECT_CALL(mock_platform_impl_, Speak(_, "one two three", _, _)) | |
| 252 .WillOnce(DoAll( | |
| 253 Invoke(&mock_platform_impl_, | |
| 254 &MockExtensionTtsPlatformImpl::SendWordEvents), | |
| 255 Invoke(&mock_platform_impl_, | |
| 256 &MockExtensionTtsPlatformImpl::SendEndEvent), | |
| 257 Return(true))); | |
| 258 ASSERT_TRUE(RunExtensionTest("tts/word_callbacks")) << message_; | |
| 259 } | |
| 260 | |
| 261 IN_PROC_BROWSER_TEST_F(TtsApiTest, RegisterEngine) { | |
| 262 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 263 .WillRepeatedly(Return(true)); | |
| 264 | |
| 265 { | |
| 266 InSequence s; | |
| 267 EXPECT_CALL(mock_platform_impl_, Speak(_, "native speech", _, _)) | |
| 268 .WillOnce(DoAll( | |
| 269 Invoke(&mock_platform_impl_, | |
| 270 &MockExtensionTtsPlatformImpl::SendEndEvent), | |
| 271 Return(true))); | |
| 272 EXPECT_CALL(mock_platform_impl_, Speak(_, "native speech 2", _, _)) | |
| 273 .WillOnce(DoAll( | |
| 274 Invoke(&mock_platform_impl_, | |
| 275 &MockExtensionTtsPlatformImpl::SendEndEvent), | |
| 276 Return(true))); | |
| 277 EXPECT_CALL(mock_platform_impl_, Speak(_, "native speech 3", _, _)) | |
| 278 .WillOnce(DoAll( | |
| 279 Invoke(&mock_platform_impl_, | |
| 280 &MockExtensionTtsPlatformImpl::SendEndEvent), | |
| 281 Return(true))); | |
| 282 } | |
| 283 | |
| 284 ASSERT_TRUE(RunExtensionTest("tts_engine/register_engine")) << message_; | |
| 285 } | |
| 286 | |
| 287 IN_PROC_BROWSER_TEST_F(TtsApiTest, EngineError) { | |
| 288 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 289 .WillRepeatedly(Return(true)); | |
| 290 | |
| 291 ASSERT_TRUE(RunExtensionTest("tts_engine/engine_error")) << message_; | |
| 292 } | |
| 293 | |
| 294 IN_PROC_BROWSER_TEST_F(TtsApiTest, EngineWordCallbacks) { | |
| 295 EXPECT_CALL(mock_platform_impl_, StopSpeaking()) | |
| 296 .WillRepeatedly(Return(true)); | |
| 297 | |
| 298 ASSERT_TRUE(RunExtensionTest("tts_engine/engine_word_callbacks")) << message_; | |
| 299 } | |
| OLD | NEW |