Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(754)

Side by Side Diff: chrome/browser/speech/speech_input_extension_apitest.cc

Issue 8386074: Add a tray notification UI for speech input recording in the extension API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: creating the notification object dynamically on first use. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/extensions/extension_apitest.h" 8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/speech_input/extension_speech_input_api.h" 9 #include "chrome/browser/speech/speech_input_extension_api.h"
10 #include "chrome/browser/extensions/speech_input/extension_speech_input_manager. h" 10 #include "chrome/browser/speech/speech_input_extension_manager.h"
11 #include "chrome/browser/ui/browser.h" 11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/common/chrome_notification_types.h" 12 #include "chrome/common/chrome_notification_types.h"
13 #include "chrome/common/chrome_switches.h" 13 #include "chrome/common/chrome_switches.h"
14 #include "content/browser/speech/speech_recognizer.h" 14 #include "content/browser/speech/speech_recognizer.h"
15 #include "content/common/speech_input_result.h" 15 #include "content/common/speech_input_result.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 17
18 using content::BrowserThread; 18 using content::BrowserThread;
19 19
20 namespace net { 20 namespace net {
21 class URLRequestContextGetter; 21 class URLRequestContextGetter;
22 } 22 }
23 23
24 // Mock class used to test the extension speech input API. 24 // Mock class used to test the extension speech input API.
25 class ExtensionSpeechInputApiTest : public ExtensionApiTest, 25 class SpeechInputExtensionApiTest : public ExtensionApiTest,
26 public ExtensionSpeechInterface { 26 public SpeechInputExtensionInterface {
27 public: 27 public:
28 ExtensionSpeechInputApiTest(); 28 SpeechInputExtensionApiTest();
29 virtual ~ExtensionSpeechInputApiTest(); 29 virtual ~SpeechInputExtensionApiTest();
30 30
31 void SetRecordingDevicesAvailable(bool available) { 31 void SetRecordingDevicesAvailable(bool available) {
32 recording_devices_available_ = available; 32 recording_devices_available_ = available;
33 } 33 }
34 34
35 void SetRecognitionError(speech_input::SpeechInputError error) { 35 void SetRecognitionError(speech_input::SpeechInputError error) {
36 next_error_ = error; 36 next_error_ = error;
37 } 37 }
38 38
39 void SetRecognitionResult(const speech_input::SpeechInputResult& result) { 39 void SetRecognitionResult(const speech_input::SpeechInputResult& result) {
40 next_result_ = result; 40 next_result_ = result;
41 } 41 }
42 42
43 void SetRecognitionDelay(int result_delay_ms) { 43 void SetRecognitionDelay(int result_delay_ms) {
44 result_delay_ms_ = result_delay_ms; 44 result_delay_ms_ = result_delay_ms;
45 } 45 }
46 46
47 // Used as delay when the corresponding call should not be dispatched. 47 // Used as delay when the corresponding call should not be dispatched.
48 static const int kDontDispatchCall = -1; 48 static const int kDontDispatchCall = -1;
49 49
50 // ExtensionApiTest methods. 50 // ExtensionApiTest methods.
51 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 51 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
52 ExtensionApiTest::SetUpCommandLine(command_line); 52 ExtensionApiTest::SetUpCommandLine(command_line);
53 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); 53 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
54 } 54 }
55 55
56 // ExtensionSpeechInterface methods. 56 // SpeechInputExtensionInterface methods.
57 virtual bool HasAudioInputDevices() OVERRIDE { 57 virtual bool HasAudioInputDevices() OVERRIDE {
58 return recording_devices_available_; 58 return recording_devices_available_;
59 } 59 }
60 60
61 virtual bool IsRecordingInProcess() OVERRIDE { 61 virtual bool IsRecordingInProcess() OVERRIDE {
62 // Only the mock recognizer is supposed to be recording during testing. 62 // Only the mock recognizer is supposed to be recording during testing.
63 return HasValidRecognizer(); 63 return HasValidRecognizer();
64 } 64 }
65 65
66 virtual bool HasValidRecognizer() OVERRIDE { 66 virtual bool HasValidRecognizer() OVERRIDE {
67 return recognizer_is_valid_; 67 return recognizer_is_valid_;
68 } 68 }
69 69
70 virtual void StartRecording( 70 virtual void StartRecording(
71 speech_input::SpeechRecognizerDelegate* delegate, 71 speech_input::SpeechRecognizerDelegate* delegate,
72 net::URLRequestContextGetter* context_getter, 72 net::URLRequestContextGetter* context_getter,
73 int caller_id, 73 int caller_id,
74 const std::string& language, 74 const std::string& language,
75 const std::string& grammar, 75 const std::string& grammar,
76 bool filter_profanities) OVERRIDE; 76 bool filter_profanities) OVERRIDE;
77 77
78 virtual void StopRecording(bool recognition_failed) OVERRIDE; 78 virtual void StopRecording(bool recognition_failed) OVERRIDE;
79 79
80 ExtensionSpeechInputManager* GetManager() { 80 SpeechInputExtensionManager* GetManager() {
81 return ExtensionSpeechInputManager::GetForProfile(browser()->profile()); 81 return SpeechInputExtensionManager::GetForProfile(browser()->profile());
82 } 82 }
83 83
84 // Auxiliary class used to hook the API manager into the test during its 84 // Auxiliary class used to hook the API manager into the test during its
85 // lifetime. Required since browser() is not available during the set up 85 // lifetime. Required since browser() is not available during the set up
86 // or tear down callbacks, or during the test class construction. 86 // or tear down callbacks, or during the test class construction.
87 class AutoManagerHook { 87 class AutoManagerHook {
88 public: 88 public:
89 explicit AutoManagerHook(ExtensionSpeechInputApiTest* test) 89 explicit AutoManagerHook(SpeechInputExtensionApiTest* test)
90 : test_(test) { 90 : test_(test) {
91 test_->GetManager()->SetExtensionSpeechInterface(test_); 91 test_->GetManager()->SetSpeechInputExtensionInterface(test_);
92 } 92 }
93 93
94 ~AutoManagerHook() { 94 ~AutoManagerHook() {
95 test_->GetManager()->SetExtensionSpeechInterface(NULL); 95 test_->GetManager()->SetSpeechInputExtensionInterface(NULL);
96 } 96 }
97 97
98 private: 98 private:
99 ExtensionSpeechInputApiTest* test_; 99 SpeechInputExtensionApiTest* test_;
100 }; 100 };
101 101
102 private: 102 private:
103 void ProvideResults(int caller_id); 103 void ProvideResults(int caller_id);
104 104
105 bool recording_devices_available_; 105 bool recording_devices_available_;
106 bool recognizer_is_valid_; 106 bool recognizer_is_valid_;
107 speech_input::SpeechInputError next_error_; 107 speech_input::SpeechInputError next_error_;
108 speech_input::SpeechInputResult next_result_; 108 speech_input::SpeechInputResult next_result_;
109 int result_delay_ms_; 109 int result_delay_ms_;
110 }; 110 };
111 111
112 ExtensionSpeechInputApiTest::ExtensionSpeechInputApiTest() 112 SpeechInputExtensionApiTest::SpeechInputExtensionApiTest()
113 : recording_devices_available_(true), 113 : recording_devices_available_(true),
114 recognizer_is_valid_(false), 114 recognizer_is_valid_(false),
115 next_error_(speech_input::kErrorNone), 115 next_error_(speech_input::kErrorNone),
116 result_delay_ms_(0) { 116 result_delay_ms_(0) {
117 } 117 }
118 118
119 ExtensionSpeechInputApiTest::~ExtensionSpeechInputApiTest() { 119 SpeechInputExtensionApiTest::~SpeechInputExtensionApiTest() {
120 } 120 }
121 121
122 void ExtensionSpeechInputApiTest::StartRecording( 122 void SpeechInputExtensionApiTest::StartRecording(
123 speech_input::SpeechRecognizerDelegate* delegate, 123 speech_input::SpeechRecognizerDelegate* delegate,
124 net::URLRequestContextGetter* context_getter, 124 net::URLRequestContextGetter* context_getter,
125 int caller_id, 125 int caller_id,
126 const std::string& language, 126 const std::string& language,
127 const std::string& grammar, 127 const std::string& grammar,
128 bool filter_profanities) { 128 bool filter_profanities) {
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
130 recognizer_is_valid_ = true; 130 recognizer_is_valid_ = true;
131 131
132 // Notify that recording started. 132 // Notify that recording started.
133 MessageLoop::current()->PostDelayedTask(FROM_HERE, 133 MessageLoop::current()->PostDelayedTask(FROM_HERE,
134 base::Bind(&ExtensionSpeechInputManager::DidStartReceivingAudio, 134 base::Bind(&SpeechInputExtensionManager::DidStartReceivingAudio,
135 GetManager(), caller_id), 0); 135 GetManager(), caller_id), 0);
136 136
137 // Notify sound start in the input device. 137 // Notify sound start in the input device.
138 MessageLoop::current()->PostDelayedTask(FROM_HERE, 138 MessageLoop::current()->PostDelayedTask(FROM_HERE,
139 base::Bind(&ExtensionSpeechInputManager::DidStartReceivingSpeech, 139 base::Bind(&SpeechInputExtensionManager::DidStartReceivingSpeech,
140 GetManager(), caller_id), 0); 140 GetManager(), caller_id), 0);
141 141
142 if (result_delay_ms_ != kDontDispatchCall) { 142 if (result_delay_ms_ != kDontDispatchCall) {
143 // Dispatch the recognition results. 143 // Dispatch the recognition results.
144 MessageLoop::current()->PostDelayedTask(FROM_HERE, 144 MessageLoop::current()->PostDelayedTask(FROM_HERE,
145 base::Bind(&ExtensionSpeechInputApiTest::ProvideResults, this, 145 base::Bind(&SpeechInputExtensionApiTest::ProvideResults, this,
146 caller_id), result_delay_ms_); 146 caller_id), result_delay_ms_);
147 } 147 }
148 } 148 }
149 149
150 void ExtensionSpeechInputApiTest::StopRecording(bool recognition_failed) { 150 void SpeechInputExtensionApiTest::StopRecording(bool recognition_failed) {
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
152 recognizer_is_valid_ = false; 152 recognizer_is_valid_ = false;
153 } 153 }
154 154
155 void ExtensionSpeechInputApiTest::ProvideResults(int caller_id) { 155 void SpeechInputExtensionApiTest::ProvideResults(int caller_id) {
156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
157 157
158 if (next_error_ != speech_input::kErrorNone) { 158 if (next_error_ != speech_input::kErrorNone) {
159 GetManager()->OnRecognizerError(caller_id, next_error_); 159 GetManager()->OnRecognizerError(caller_id, next_error_);
160 return; 160 return;
161 } 161 }
162 162
163 GetManager()->DidStopReceivingSpeech(caller_id); 163 GetManager()->DidStopReceivingSpeech(caller_id);
164 GetManager()->SetRecognitionResult(caller_id, next_result_); 164 GetManager()->SetRecognitionResult(caller_id, next_result_);
165 } 165 }
166 166
167 // Every test should leave the manager in the idle state when finished. 167 // Every test should leave the manager in the idle state when finished.
168 IN_PROC_BROWSER_TEST_F(ExtensionSpeechInputApiTest, StartStopTest) { 168 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, StartStopTest) {
169 AutoManagerHook hook(this); 169 AutoManagerHook hook(this);
170 170
171 SetRecognitionDelay(kDontDispatchCall); 171 SetRecognitionDelay(kDontDispatchCall);
172 ASSERT_TRUE(RunExtensionTest("speech_input/start_stop")) << message_; 172 ASSERT_TRUE(RunExtensionTest("speech_input/start_stop")) << message_;
173 } 173 }
174 174
175 IN_PROC_BROWSER_TEST_F(ExtensionSpeechInputApiTest, NoDevicesAvailable) { 175 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, NoDevicesAvailable) {
176 AutoManagerHook hook(this); 176 AutoManagerHook hook(this);
177 177
178 SetRecordingDevicesAvailable(false); 178 SetRecordingDevicesAvailable(false);
179 ASSERT_TRUE(RunExtensionTest("speech_input/start_error")) << message_; 179 ASSERT_TRUE(RunExtensionTest("speech_input/start_error")) << message_;
180 } 180 }
181 181
182 IN_PROC_BROWSER_TEST_F(ExtensionSpeechInputApiTest, RecognitionSuccessful) { 182 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionSuccessful) {
183 AutoManagerHook hook(this); 183 AutoManagerHook hook(this);
184 184
185 speech_input::SpeechInputResult result; 185 speech_input::SpeechInputResult result;
186 result.hypotheses.push_back( 186 result.hypotheses.push_back(
187 speech_input::SpeechInputHypothesis(UTF8ToUTF16("this is a test"), 0.99)); 187 speech_input::SpeechInputHypothesis(UTF8ToUTF16("this is a test"), 0.99));
188 SetRecognitionResult(result); 188 SetRecognitionResult(result);
189 ASSERT_TRUE(RunExtensionTest("speech_input/recognition")) << message_; 189 ASSERT_TRUE(RunExtensionTest("speech_input/recognition")) << message_;
190 } 190 }
191 191
192 IN_PROC_BROWSER_TEST_F(ExtensionSpeechInputApiTest, RecognitionError) { 192 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionError) {
193 AutoManagerHook hook(this); 193 AutoManagerHook hook(this);
194 194
195 SetRecognitionError(speech_input::kErrorNetwork); 195 SetRecognitionError(speech_input::kErrorNetwork);
196 ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_; 196 ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_;
197 } 197 }
OLDNEW
« no previous file with comments | « chrome/browser/speech/speech_input_extension_api.cc ('k') | chrome/browser/speech/speech_input_extension_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698