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