| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/app_list/speech_recognizer.h" | 5 #include "chrome/browser/ui/app_list/speech_recognizer.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
| 12 #include "chrome/browser/ui/app_list/speech_recognizer_delegate.h" | 12 #include "chrome/browser/ui/app_list/speech_recognizer_delegate.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/test/base/in_process_browser_test.h" | 15 #include "chrome/test/base/in_process_browser_test.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/test/fake_speech_recognition_manager.h" | 18 #include "content/public/test/fake_speech_recognition_manager.h" |
| 19 #include "content/public/test/web_contents_tester.h" | |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 21 |
| 23 using ::testing::InvokeWithoutArgs; | 22 using ::testing::InvokeWithoutArgs; |
| 24 using ::testing::Return; | 23 using ::testing::Return; |
| 25 | 24 |
| 26 namespace app_list { | 25 namespace app_list { |
| 27 | 26 |
| 28 class MockSpeechRecognizerDelegate : public SpeechRecognizerDelegate { | 27 class MockSpeechRecognizerDelegate : public SpeechRecognizerDelegate { |
| 29 public: | 28 public: |
| 30 MockSpeechRecognizerDelegate() : weak_factory_(this) {} | 29 MockSpeechRecognizerDelegate() : weak_factory_(this) {} |
| 31 | 30 |
| 32 base::WeakPtr<MockSpeechRecognizerDelegate> GetWeakPtr() { | 31 base::WeakPtr<MockSpeechRecognizerDelegate> GetWeakPtr() { |
| 33 return weak_factory_.GetWeakPtr(); | 32 return weak_factory_.GetWeakPtr(); |
| 34 } | 33 } |
| 35 | 34 |
| 36 MOCK_METHOD2(OnSpeechResult, void(const base::string16&, bool)); | 35 MOCK_METHOD2(OnSpeechResult, void(const base::string16&, bool)); |
| 37 MOCK_METHOD1(OnSpeechSoundLevelChanged, void(int16_t)); | 36 MOCK_METHOD1(OnSpeechSoundLevelChanged, void(int16_t)); |
| 38 MOCK_METHOD1(OnSpeechRecognitionStateChanged, void(SpeechRecognitionState)); | 37 MOCK_METHOD1(OnSpeechRecognitionStateChanged, void(SpeechRecognitionState)); |
| 39 MOCK_METHOD0(GetSpeechContents, content::WebContents*()); | |
| 40 MOCK_METHOD2(GetSpeechAuthParameters, void(std::string*, std::string*)); | 38 MOCK_METHOD2(GetSpeechAuthParameters, void(std::string*, std::string*)); |
| 41 | 39 |
| 42 private: | 40 private: |
| 43 base::WeakPtrFactory<MockSpeechRecognizerDelegate> weak_factory_; | 41 base::WeakPtrFactory<MockSpeechRecognizerDelegate> weak_factory_; |
| 44 }; | 42 }; |
| 45 | 43 |
| 46 class AppListSpeechRecognizerBrowserTest : public InProcessBrowserTest { | 44 class AppListSpeechRecognizerBrowserTest : public InProcessBrowserTest { |
| 47 public: | 45 public: |
| 48 AppListSpeechRecognizerBrowserTest() {} | 46 AppListSpeechRecognizerBrowserTest() {} |
| 49 | 47 |
| 50 void SetUpOnMainThread() override { | 48 void SetUpOnMainThread() override { |
| 51 InProcessBrowserTest::SetUpOnMainThread(); | 49 InProcessBrowserTest::SetUpOnMainThread(); |
| 52 | 50 |
| 53 fake_speech_recognition_manager_.reset( | 51 fake_speech_recognition_manager_.reset( |
| 54 new content::FakeSpeechRecognitionManager()); | 52 new content::FakeSpeechRecognitionManager()); |
| 55 fake_speech_recognition_manager_->set_should_send_fake_response(true); | 53 fake_speech_recognition_manager_->set_should_send_fake_response(true); |
| 56 content::SpeechRecognitionManager::SetManagerForTesting( | 54 content::SpeechRecognitionManager::SetManagerForTesting( |
| 57 fake_speech_recognition_manager_.get()); | 55 fake_speech_recognition_manager_.get()); |
| 58 mock_speech_delegate_.reset(new MockSpeechRecognizerDelegate()); | 56 mock_speech_delegate_.reset(new MockSpeechRecognizerDelegate()); |
| 59 test_contents_.reset(content::WebContentsTester::CreateTestWebContents( | |
| 60 browser()->profile(), NULL)); | |
| 61 } | 57 } |
| 62 | 58 |
| 63 void TearDownOnMainThread() override { | 59 void TearDownOnMainThread() override { |
| 64 // This puts stuff on the IO loop which needs to be executed. | |
| 65 test_contents_.reset(); | |
| 66 | |
| 67 // Poor-person's way of ensuring IO loop is idle. | 60 // Poor-person's way of ensuring IO loop is idle. |
| 68 auto io_loop = content::BrowserThread::UnsafeGetMessageLoopForThread( | 61 auto io_loop = content::BrowserThread::UnsafeGetMessageLoopForThread( |
| 69 content::BrowserThread::IO); | 62 content::BrowserThread::IO); |
| 70 ASSERT_TRUE(io_loop); | 63 ASSERT_TRUE(io_loop); |
| 71 while (!io_loop->IsIdleForTesting()) { | 64 while (!io_loop->IsIdleForTesting()) { |
| 72 // Sleep for a little bit, allowing the IO thread to obtain any locks | 65 // Sleep for a little bit, allowing the IO thread to obtain any locks |
| 73 // taken by IsIdleForTesting(). Without this sleep, this loop may livelock | 66 // taken by IsIdleForTesting(). Without this sleep, this loop may livelock |
| 74 // the message loop causing the test to fail. | 67 // the message loop causing the test to fail. |
| 75 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); | 68 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
| 76 } | 69 } |
| 77 } | 70 } |
| 78 | 71 |
| 79 protected: | 72 protected: |
| 80 scoped_ptr<content::FakeSpeechRecognitionManager> | 73 scoped_ptr<content::FakeSpeechRecognitionManager> |
| 81 fake_speech_recognition_manager_; | 74 fake_speech_recognition_manager_; |
| 82 scoped_ptr<MockSpeechRecognizerDelegate> mock_speech_delegate_; | 75 scoped_ptr<MockSpeechRecognizerDelegate> mock_speech_delegate_; |
| 83 scoped_ptr<content::WebContents> test_contents_; | |
| 84 | 76 |
| 85 private: | 77 private: |
| 86 DISALLOW_COPY_AND_ASSIGN(AppListSpeechRecognizerBrowserTest); | 78 DISALLOW_COPY_AND_ASSIGN(AppListSpeechRecognizerBrowserTest); |
| 87 }; | 79 }; |
| 88 | 80 |
| 89 IN_PROC_BROWSER_TEST_F(AppListSpeechRecognizerBrowserTest, RecognizeSpeech) { | 81 IN_PROC_BROWSER_TEST_F(AppListSpeechRecognizerBrowserTest, RecognizeSpeech) { |
| 90 SpeechRecognizer recognizer(mock_speech_delegate_->GetWeakPtr(), | 82 SpeechRecognizer recognizer(mock_speech_delegate_->GetWeakPtr(), |
| 91 browser()->profile()->GetRequestContext(), | 83 browser()->profile()->GetRequestContext(), |
| 92 "en"); | 84 "en"); |
| 93 | 85 |
| 94 base::RunLoop run_loop; | 86 base::RunLoop run_loop; |
| 95 EXPECT_CALL(*mock_speech_delegate_, GetSpeechContents()) | |
| 96 .WillOnce(Return(test_contents_.get())); | |
| 97 EXPECT_CALL(*mock_speech_delegate_, | 87 EXPECT_CALL(*mock_speech_delegate_, |
| 98 OnSpeechResult(base::ASCIIToUTF16("Pictures of the moon"), true)); | 88 OnSpeechResult(base::ASCIIToUTF16("Pictures of the moon"), true)); |
| 99 EXPECT_CALL(*mock_speech_delegate_, | 89 EXPECT_CALL(*mock_speech_delegate_, |
| 100 OnSpeechRecognitionStateChanged(SPEECH_RECOGNITION_READY)) | 90 OnSpeechRecognitionStateChanged(SPEECH_RECOGNITION_READY)) |
| 101 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 91 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
| 102 recognizer.Start(); | 92 recognizer.Start(); |
| 103 run_loop.Run(); | 93 run_loop.Run(); |
| 104 } | 94 } |
| 105 | 95 |
| 106 } // namespace app_list | 96 } // namespace app_list |
| 107 | 97 |
| 108 | 98 |
| OLD | NEW |