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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "base/threading/platform_thread.h" | |
15 #include "chrome/browser/ui/app_list/speech_recognizer_delegate.h" | 14 #include "chrome/browser/ui/app_list/speech_recognizer_delegate.h" |
16 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/test/base/in_process_browser_test.h" | 17 #include "chrome/test/base/in_process_browser_test.h" |
19 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
20 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/test/fake_speech_recognition_manager.h" | 20 #include "content/public/test/fake_speech_recognition_manager.h" |
| 21 #include "content/public/test/test_utils.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 | 24 |
25 using ::testing::InvokeWithoutArgs; | 25 using ::testing::InvokeWithoutArgs; |
26 using ::testing::Return; | 26 using ::testing::Return; |
27 | 27 |
28 namespace app_list { | 28 namespace app_list { |
29 | 29 |
30 class MockSpeechRecognizerDelegate : public SpeechRecognizerDelegate { | 30 class MockSpeechRecognizerDelegate : public SpeechRecognizerDelegate { |
31 public: | 31 public: |
(...skipping 21 matching lines...) Expand all Loading... |
53 | 53 |
54 fake_speech_recognition_manager_.reset( | 54 fake_speech_recognition_manager_.reset( |
55 new content::FakeSpeechRecognitionManager()); | 55 new content::FakeSpeechRecognitionManager()); |
56 fake_speech_recognition_manager_->set_should_send_fake_response(true); | 56 fake_speech_recognition_manager_->set_should_send_fake_response(true); |
57 content::SpeechRecognitionManager::SetManagerForTesting( | 57 content::SpeechRecognitionManager::SetManagerForTesting( |
58 fake_speech_recognition_manager_.get()); | 58 fake_speech_recognition_manager_.get()); |
59 mock_speech_delegate_.reset(new MockSpeechRecognizerDelegate()); | 59 mock_speech_delegate_.reset(new MockSpeechRecognizerDelegate()); |
60 } | 60 } |
61 | 61 |
62 void TearDownOnMainThread() override { | 62 void TearDownOnMainThread() override { |
63 // Poor-person's way of ensuring IO loop is idle. | 63 content::RunAllPendingInMessageLoop(content::BrowserThread::IO); |
64 auto* io_loop = content::BrowserThread::UnsafeGetMessageLoopForThread( | |
65 content::BrowserThread::IO); | |
66 ASSERT_TRUE(io_loop); | |
67 while (!io_loop->IsIdleForTesting()) { | |
68 // Sleep for a little bit, allowing the IO thread to obtain any locks | |
69 // taken by IsIdleForTesting(). Without this sleep, this loop may livelock | |
70 // the message loop causing the test to fail. | |
71 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); | |
72 } | |
73 } | 64 } |
74 | 65 |
75 protected: | 66 protected: |
76 std::unique_ptr<content::FakeSpeechRecognitionManager> | 67 std::unique_ptr<content::FakeSpeechRecognitionManager> |
77 fake_speech_recognition_manager_; | 68 fake_speech_recognition_manager_; |
78 std::unique_ptr<MockSpeechRecognizerDelegate> mock_speech_delegate_; | 69 std::unique_ptr<MockSpeechRecognizerDelegate> mock_speech_delegate_; |
79 | 70 |
80 private: | 71 private: |
81 DISALLOW_COPY_AND_ASSIGN(AppListSpeechRecognizerBrowserTest); | 72 DISALLOW_COPY_AND_ASSIGN(AppListSpeechRecognizerBrowserTest); |
82 }; | 73 }; |
83 | 74 |
84 IN_PROC_BROWSER_TEST_F(AppListSpeechRecognizerBrowserTest, RecognizeSpeech) { | 75 IN_PROC_BROWSER_TEST_F(AppListSpeechRecognizerBrowserTest, RecognizeSpeech) { |
85 SpeechRecognizer recognizer(mock_speech_delegate_->GetWeakPtr(), | 76 SpeechRecognizer recognizer(mock_speech_delegate_->GetWeakPtr(), |
86 browser()->profile()->GetRequestContext(), | 77 browser()->profile()->GetRequestContext(), |
87 "en"); | 78 "en"); |
88 | 79 |
89 base::RunLoop run_loop; | 80 base::RunLoop run_loop; |
90 EXPECT_CALL(*mock_speech_delegate_, | 81 EXPECT_CALL(*mock_speech_delegate_, |
91 OnSpeechResult(base::ASCIIToUTF16("Pictures of the moon"), true)); | 82 OnSpeechResult(base::ASCIIToUTF16("Pictures of the moon"), true)); |
92 EXPECT_CALL(*mock_speech_delegate_, | 83 EXPECT_CALL(*mock_speech_delegate_, |
93 OnSpeechRecognitionStateChanged(SPEECH_RECOGNITION_READY)) | 84 OnSpeechRecognitionStateChanged(SPEECH_RECOGNITION_READY)) |
94 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 85 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
95 recognizer.Start(nullptr); | 86 recognizer.Start(nullptr); |
96 run_loop.Run(); | 87 run_loop.Run(); |
97 } | 88 } |
98 | 89 |
99 } // namespace app_list | 90 } // namespace app_list |
100 | 91 |
101 | 92 |
OLD | NEW |