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.
5 #include "chrome/browser/ui/app_list/speech_recognizer.h"
7 #include "base/command_line.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/run_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/threading/platform_thread.h"
12 #include "chrome/browser/ui/app_list/speech_recognizer_delegate.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/fake_speech_recognition_manager.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using ::testing::InvokeWithoutArgs
;
23 using ::testing::Return
;
27 class MockSpeechRecognizerDelegate
: public SpeechRecognizerDelegate
{
29 MockSpeechRecognizerDelegate() : weak_factory_(this) {}
31 base::WeakPtr
<MockSpeechRecognizerDelegate
> GetWeakPtr() {
32 return weak_factory_
.GetWeakPtr();
35 MOCK_METHOD2(OnSpeechResult
, void(const base::string16
&, bool));
36 MOCK_METHOD1(OnSpeechSoundLevelChanged
, void(int16_t));
37 MOCK_METHOD1(OnSpeechRecognitionStateChanged
, void(SpeechRecognitionState
));
38 MOCK_METHOD2(GetSpeechAuthParameters
, void(std::string
*, std::string
*));
41 base::WeakPtrFactory
<MockSpeechRecognizerDelegate
> weak_factory_
;
44 class AppListSpeechRecognizerBrowserTest
: public InProcessBrowserTest
{
46 AppListSpeechRecognizerBrowserTest() {}
48 void SetUpOnMainThread() override
{
49 InProcessBrowserTest::SetUpOnMainThread();
51 fake_speech_recognition_manager_
.reset(
52 new content::FakeSpeechRecognitionManager());
53 fake_speech_recognition_manager_
->set_should_send_fake_response(true);
54 content::SpeechRecognitionManager::SetManagerForTesting(
55 fake_speech_recognition_manager_
.get());
56 mock_speech_delegate_
.reset(new MockSpeechRecognizerDelegate());
59 void TearDownOnMainThread() override
{
60 // Poor-person's way of ensuring IO loop is idle.
61 auto io_loop
= content::BrowserThread::UnsafeGetMessageLoopForThread(
62 content::BrowserThread::IO
);
64 while (!io_loop
->IsIdleForTesting()) {
65 // Sleep for a little bit, allowing the IO thread to obtain any locks
66 // taken by IsIdleForTesting(). Without this sleep, this loop may livelock
67 // the message loop causing the test to fail.
68 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
73 scoped_ptr
<content::FakeSpeechRecognitionManager
>
74 fake_speech_recognition_manager_
;
75 scoped_ptr
<MockSpeechRecognizerDelegate
> mock_speech_delegate_
;
78 DISALLOW_COPY_AND_ASSIGN(AppListSpeechRecognizerBrowserTest
);
81 IN_PROC_BROWSER_TEST_F(AppListSpeechRecognizerBrowserTest
, RecognizeSpeech
) {
82 SpeechRecognizer
recognizer(mock_speech_delegate_
->GetWeakPtr(),
83 browser()->profile()->GetRequestContext(),
86 base::RunLoop run_loop
;
87 EXPECT_CALL(*mock_speech_delegate_
,
88 OnSpeechResult(base::ASCIIToUTF16("Pictures of the moon"), true));
89 EXPECT_CALL(*mock_speech_delegate_
,
90 OnSpeechRecognitionStateChanged(SPEECH_RECOGNITION_READY
))
91 .WillOnce(InvokeWithoutArgs(&run_loop
, &base::RunLoop::Quit
));
92 recognizer
.Start(nullptr);
96 } // namespace app_list