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

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

Issue 9972008: Refactoring of chrome speech recognition architecture (CL1.6) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/utf_string_conversions.h" 6 #include "base/utf_string_conversions.h"
7 #include "chrome/browser/speech/speech_recognition_bubble_controller.h" 7 #include "chrome/browser/speech/speech_recognition_bubble_controller.h"
8 #include "chrome/browser/ui/browser.h" 8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 9 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
10 #include "chrome/test/base/browser_with_test_window_test.h" 10 #include "chrome/test/base/browser_with_test_window_test.h"
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 new SpeechRecognitionBubbleController(this))) { 88 new SpeechRecognitionBubbleController(this))) {
89 EXPECT_EQ(NULL, test_fixture_); 89 EXPECT_EQ(NULL, test_fixture_);
90 test_fixture_ = this; 90 test_fixture_ = this;
91 } 91 }
92 92
93 ~SpeechRecognitionBubbleControllerTest() { 93 ~SpeechRecognitionBubbleControllerTest() {
94 test_fixture_ = NULL; 94 test_fixture_ = NULL;
95 } 95 }
96 96
97 // SpeechRecognitionBubbleControllerDelegate methods. 97 // SpeechRecognitionBubbleControllerDelegate methods.
98 virtual void InfoBubbleButtonClicked(int caller_id, 98 virtual void InfoBubbleButtonClicked(int session_id,
99 SpeechRecognitionBubble::Button button) { 99 SpeechRecognitionBubble::Button button) {
100 VLOG(1) << "Received InfoBubbleButtonClicked for button " << button; 100 VLOG(1) << "Received InfoBubbleButtonClicked for button " << button;
101 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 101 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
102 if (button == SpeechRecognitionBubble::BUTTON_CANCEL) { 102 if (button == SpeechRecognitionBubble::BUTTON_CANCEL) {
103 cancel_clicked_ = true; 103 cancel_clicked_ = true;
104 } else if (button == SpeechRecognitionBubble::BUTTON_TRY_AGAIN) { 104 } else if (button == SpeechRecognitionBubble::BUTTON_TRY_AGAIN) {
105 try_again_clicked_ = true; 105 try_again_clicked_ = true;
106 } 106 }
107 message_loop()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 107 message_loop()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
108 } 108 }
109 109
110 virtual void InfoBubbleFocusChanged(int caller_id) { 110 virtual void InfoBubbleFocusChanged(int session_id) {
111 VLOG(1) << "Received InfoBubbleFocusChanged"; 111 VLOG(1) << "Received InfoBubbleFocusChanged";
112 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 112 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
113 focus_changed_ = true; 113 focus_changed_ = true;
114 message_loop()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 114 message_loop()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
115 } 115 }
116 116
117 // testing::Test methods. 117 // testing::Test methods.
118 virtual void SetUp() { 118 virtual void SetUp() {
119 BrowserWithTestWindowTest::SetUp(); 119 BrowserWithTestWindowTest::SetUp();
120 SpeechRecognitionBubble::set_factory( 120 SpeechRecognitionBubble::set_factory(
121 &SpeechRecognitionBubbleControllerTest::CreateBubble); 121 &SpeechRecognitionBubbleControllerTest::CreateBubble);
122 io_thread_.Start(); 122 io_thread_.Start();
123 } 123 }
124 124
125 virtual void TearDown() { 125 virtual void TearDown() {
126 SpeechRecognitionBubble::set_factory(NULL); 126 SpeechRecognitionBubble::set_factory(NULL);
127 io_thread_.Stop(); 127 io_thread_.Stop();
128 BrowserWithTestWindowTest::TearDown(); 128 BrowserWithTestWindowTest::TearDown();
129 } 129 }
130 130
131 static void ActivateBubble() { 131 static void ActivateBubble() {
132 if (MockSpeechRecognitionBubble::type() == 132 if (MockSpeechRecognitionBubble::type() ==
133 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED) { 133 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED) {
134 test_fixture_->controller_->SetBubbleWarmUpMode(kBubbleCallerId); 134 test_fixture_->controller_->SetBubbleWarmUpMode(kBubbleSessionId);
135 } else { 135 } else {
136 test_fixture_->controller_->SetBubbleMessage(kBubbleCallerId, 136 test_fixture_->controller_->SetBubbleMessage(kBubbleSessionId,
137 ASCIIToUTF16("Test")); 137 ASCIIToUTF16("Test"));
138 } 138 }
139 } 139 }
140 140
141 static SpeechRecognitionBubble* CreateBubble( 141 static SpeechRecognitionBubble* CreateBubble(
142 WebContents* web_contents, 142 WebContents* web_contents,
143 SpeechRecognitionBubble::Delegate* delegate, 143 SpeechRecognitionBubble::Delegate* delegate,
144 const gfx::Rect& element_rect) { 144 const gfx::Rect& element_rect) {
145 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); 145 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI));
146 // Set up to activate the bubble soon after it gets created, since we test 146 // Set up to activate the bubble soon after it gets created, since we test
147 // events sent by the bubble and those are handled only when the bubble is 147 // events sent by the bubble and those are handled only when the bubble is
148 // active. 148 // active.
149 MessageLoop::current()->PostTask(FROM_HERE, 149 MessageLoop::current()->PostTask(FROM_HERE,
150 base::Bind(&ActivateBubble)); 150 base::Bind(&ActivateBubble));
151 151
152 // The |tab_contents| parameter would be NULL since the dummy caller id 152 // The |tab_contents| parameter would be NULL since the dummy session id
153 // passed to CreateBubble would not have matched any active tab. So get a 153 // passed to CreateBubble would not have matched any active tab. So get a
154 // real WebContents pointer from the test fixture and pass that, because 154 // real WebContents pointer from the test fixture and pass that, because
155 // the bubble controller registers for tab close notifications which need 155 // the bubble controller registers for tab close notifications which need
156 // a valid WebContents. 156 // a valid WebContents.
157 TabContentsWrapper* wrapper = 157 TabContentsWrapper* wrapper =
158 test_fixture_->browser()->GetSelectedTabContentsWrapper(); 158 test_fixture_->browser()->GetSelectedTabContentsWrapper();
159 if (wrapper) 159 if (wrapper)
160 web_contents = wrapper->web_contents(); 160 web_contents = wrapper->web_contents();
161 return new MockSpeechRecognitionBubble(web_contents, delegate, 161 return new MockSpeechRecognitionBubble(web_contents, delegate,
162 element_rect); 162 element_rect);
163 } 163 }
164 164
165 protected: 165 protected:
166 // The main thread of the test is marked as the IO thread and we create a new 166 // The main thread of the test is marked as the IO thread and we create a new
167 // one for the UI thread. 167 // one for the UI thread.
168 content::TestBrowserThread io_thread_; 168 content::TestBrowserThread io_thread_;
169 bool cancel_clicked_; 169 bool cancel_clicked_;
170 bool try_again_clicked_; 170 bool try_again_clicked_;
171 bool focus_changed_; 171 bool focus_changed_;
172 scoped_refptr<SpeechRecognitionBubbleController> controller_; 172 scoped_refptr<SpeechRecognitionBubbleController> controller_;
173 173
174 static const int kBubbleCallerId; 174 static const int kBubbleSessionId;
175 static SpeechRecognitionBubbleControllerTest* test_fixture_; 175 static SpeechRecognitionBubbleControllerTest* test_fixture_;
176 }; 176 };
177 177
178 SpeechRecognitionBubbleControllerTest* 178 SpeechRecognitionBubbleControllerTest*
179 SpeechRecognitionBubbleControllerTest::test_fixture_ = NULL; 179 SpeechRecognitionBubbleControllerTest::test_fixture_ = NULL;
180 180
181 const int SpeechRecognitionBubbleControllerTest::kBubbleCallerId = 1; 181 const int SpeechRecognitionBubbleControllerTest::kBubbleSessionId = 1;
182 182
183 MockSpeechRecognitionBubble::BubbleType MockSpeechRecognitionBubble::type_ = 183 MockSpeechRecognitionBubble::BubbleType MockSpeechRecognitionBubble::type_ =
184 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED; 184 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED;
185 185
186 // Test that the speech bubble UI gets created in the UI thread and that the 186 // Test that the speech bubble UI gets created in the UI thread and that the
187 // focus changed callback comes back in the IO thread. 187 // focus changed callback comes back in the IO thread.
188 TEST_F(SpeechRecognitionBubbleControllerTest, TestFocusChanged) { 188 TEST_F(SpeechRecognitionBubbleControllerTest, TestFocusChanged) {
189 MockSpeechRecognitionBubble::set_type( 189 MockSpeechRecognitionBubble::set_type(
190 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED); 190 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED);
191 191
192 controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1)); 192 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1));
193 MessageLoop::current()->Run(); 193 MessageLoop::current()->Run();
194 EXPECT_TRUE(focus_changed_); 194 EXPECT_TRUE(focus_changed_);
195 EXPECT_FALSE(cancel_clicked_); 195 EXPECT_FALSE(cancel_clicked_);
196 EXPECT_FALSE(try_again_clicked_); 196 EXPECT_FALSE(try_again_clicked_);
197 controller_->CloseBubble(kBubbleCallerId); 197 controller_->CloseBubble(kBubbleSessionId);
198 } 198 }
199 199
200 // Test that the speech bubble UI gets created in the UI thread and that the 200 // Test that the speech bubble UI gets created in the UI thread and that the
201 // recognition cancelled callback comes back in the IO thread. 201 // recognition cancelled callback comes back in the IO thread.
202 TEST_F(SpeechRecognitionBubbleControllerTest, TestRecognitionCancelled) { 202 TEST_F(SpeechRecognitionBubbleControllerTest, TestRecognitionCancelled) {
203 MockSpeechRecognitionBubble::set_type( 203 MockSpeechRecognitionBubble::set_type(
204 MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_CANCEL); 204 MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_CANCEL);
205 205
206 controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1)); 206 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1));
207 MessageLoop::current()->Run(); 207 MessageLoop::current()->Run();
208 EXPECT_TRUE(cancel_clicked_); 208 EXPECT_TRUE(cancel_clicked_);
209 EXPECT_FALSE(try_again_clicked_); 209 EXPECT_FALSE(try_again_clicked_);
210 EXPECT_FALSE(focus_changed_); 210 EXPECT_FALSE(focus_changed_);
211 controller_->CloseBubble(kBubbleCallerId); 211 controller_->CloseBubble(kBubbleSessionId);
212 } 212 }
213 213
214 // Test that the speech bubble UI gets created in the UI thread and that the 214 // Test that the speech bubble UI gets created in the UI thread and that the
215 // try-again button click event comes back in the IO thread. 215 // try-again button click event comes back in the IO thread.
216 TEST_F(SpeechRecognitionBubbleControllerTest, TestTryAgainClicked) { 216 TEST_F(SpeechRecognitionBubbleControllerTest, TestTryAgainClicked) {
217 MockSpeechRecognitionBubble::set_type( 217 MockSpeechRecognitionBubble::set_type(
218 MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_TRY_AGAIN); 218 MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_TRY_AGAIN);
219 219
220 controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1)); 220 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1));
221 MessageLoop::current()->Run(); 221 MessageLoop::current()->Run();
222 EXPECT_FALSE(cancel_clicked_); 222 EXPECT_FALSE(cancel_clicked_);
223 EXPECT_TRUE(try_again_clicked_); 223 EXPECT_TRUE(try_again_clicked_);
224 EXPECT_FALSE(focus_changed_); 224 EXPECT_FALSE(focus_changed_);
225 controller_->CloseBubble(kBubbleCallerId); 225 controller_->CloseBubble(kBubbleSessionId);
226 } 226 }
227 227
228 } // namespace speech 228 } // namespace speech
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698