OLD | NEW |
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/browser_tabstrip.h" | 9 #include "chrome/browser/ui/browser_tabstrip.h" |
10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 BUBBLE_TEST_FOCUS_CHANGED, | 27 BUBBLE_TEST_FOCUS_CHANGED, |
28 BUBBLE_TEST_CLICK_CANCEL, | 28 BUBBLE_TEST_CLICK_CANCEL, |
29 BUBBLE_TEST_CLICK_TRY_AGAIN, | 29 BUBBLE_TEST_CLICK_TRY_AGAIN, |
30 }; | 30 }; |
31 | 31 |
32 MockSpeechRecognitionBubble(WebContents* web_contents, | 32 MockSpeechRecognitionBubble(WebContents* web_contents, |
33 Delegate* delegate, | 33 Delegate* delegate, |
34 const gfx::Rect&) | 34 const gfx::Rect&) |
35 : SpeechRecognitionBubbleBase(web_contents) { | 35 : SpeechRecognitionBubbleBase(web_contents) { |
36 VLOG(1) << "MockSpeechRecognitionBubble created"; | 36 VLOG(1) << "MockSpeechRecognitionBubble created"; |
37 MessageLoop::current()->PostTask( | 37 base::MessageLoop::current()->PostTask( |
38 FROM_HERE, base::Bind(&InvokeDelegate, delegate)); | 38 FROM_HERE, base::Bind(&InvokeDelegate, delegate)); |
39 } | 39 } |
40 | 40 |
41 static void InvokeDelegate(Delegate* delegate) { | 41 static void InvokeDelegate(Delegate* delegate) { |
42 VLOG(1) << "MockSpeechRecognitionBubble invoking delegate for type " | 42 VLOG(1) << "MockSpeechRecognitionBubble invoking delegate for type " |
43 << type_; | 43 << type_; |
44 switch (type_) { | 44 switch (type_) { |
45 case BUBBLE_TEST_FOCUS_CHANGED: | 45 case BUBBLE_TEST_FOCUS_CHANGED: |
46 delegate->InfoBubbleFocusChanged(); | 46 delegate->InfoBubbleFocusChanged(); |
47 break; | 47 break; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 virtual void InfoBubbleButtonClicked( | 96 virtual void InfoBubbleButtonClicked( |
97 int session_id, | 97 int session_id, |
98 SpeechRecognitionBubble::Button button) OVERRIDE { | 98 SpeechRecognitionBubble::Button button) OVERRIDE { |
99 VLOG(1) << "Received InfoBubbleButtonClicked for button " << button; | 99 VLOG(1) << "Received InfoBubbleButtonClicked for button " << button; |
100 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 100 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
101 if (button == SpeechRecognitionBubble::BUTTON_CANCEL) { | 101 if (button == SpeechRecognitionBubble::BUTTON_CANCEL) { |
102 cancel_clicked_ = true; | 102 cancel_clicked_ = true; |
103 } else if (button == SpeechRecognitionBubble::BUTTON_TRY_AGAIN) { | 103 } else if (button == SpeechRecognitionBubble::BUTTON_TRY_AGAIN) { |
104 try_again_clicked_ = true; | 104 try_again_clicked_ = true; |
105 } | 105 } |
106 message_loop()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 106 message_loop()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
107 } | 107 } |
108 | 108 |
109 virtual void InfoBubbleFocusChanged(int session_id) OVERRIDE { | 109 virtual void InfoBubbleFocusChanged(int session_id) OVERRIDE { |
110 VLOG(1) << "Received InfoBubbleFocusChanged"; | 110 VLOG(1) << "Received InfoBubbleFocusChanged"; |
111 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 111 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
112 focus_changed_ = true; | 112 focus_changed_ = true; |
113 message_loop()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 113 message_loop()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
114 } | 114 } |
115 | 115 |
116 // testing::Test methods. | 116 // testing::Test methods. |
117 virtual void SetUp() { | 117 virtual void SetUp() { |
118 BrowserWithTestWindowTest::SetUp(); | 118 BrowserWithTestWindowTest::SetUp(); |
119 SpeechRecognitionBubble::set_factory( | 119 SpeechRecognitionBubble::set_factory( |
120 &SpeechRecognitionBubbleControllerTest::CreateBubble); | 120 &SpeechRecognitionBubbleControllerTest::CreateBubble); |
121 io_thread_.Start(); | 121 io_thread_.Start(); |
122 } | 122 } |
123 | 123 |
(...skipping 11 matching lines...) Expand all Loading... |
135 } | 135 } |
136 | 136 |
137 static SpeechRecognitionBubble* CreateBubble( | 137 static SpeechRecognitionBubble* CreateBubble( |
138 WebContents* web_contents, | 138 WebContents* web_contents, |
139 SpeechRecognitionBubble::Delegate* delegate, | 139 SpeechRecognitionBubble::Delegate* delegate, |
140 const gfx::Rect& element_rect) { | 140 const gfx::Rect& element_rect) { |
141 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 141 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
142 // Set up to activate the bubble soon after it gets created, since we test | 142 // Set up to activate the bubble soon after it gets created, since we test |
143 // events sent by the bubble and those are handled only when the bubble is | 143 // events sent by the bubble and those are handled only when the bubble is |
144 // active. | 144 // active. |
145 MessageLoop::current()->PostTask(FROM_HERE, | 145 base::MessageLoop::current()->PostTask(FROM_HERE, |
146 base::Bind(&ActivateBubble)); | 146 base::Bind(&ActivateBubble)); |
147 | 147 |
148 // The |web_contents| parameter would be NULL since the dummy session id | 148 // The |web_contents| parameter would be NULL since the dummy session id |
149 // passed to CreateBubble would not have matched any active tab. So get a | 149 // passed to CreateBubble would not have matched any active tab. So get a |
150 // real WebContents pointer from the test fixture and pass that, because | 150 // real WebContents pointer from the test fixture and pass that, because |
151 // the bubble controller registers for tab close notifications which need | 151 // the bubble controller registers for tab close notifications which need |
152 // a valid WebContents. | 152 // a valid WebContents. |
153 web_contents = | 153 web_contents = |
154 test_fixture_->browser()->tab_strip_model()->GetActiveWebContents(); | 154 test_fixture_->browser()->tab_strip_model()->GetActiveWebContents(); |
155 return new MockSpeechRecognitionBubble(web_contents, delegate, | 155 return new MockSpeechRecognitionBubble(web_contents, delegate, |
156 element_rect); | 156 element_rect); |
(...skipping 20 matching lines...) Expand all Loading... |
177 MockSpeechRecognitionBubble::BubbleType MockSpeechRecognitionBubble::type_ = | 177 MockSpeechRecognitionBubble::BubbleType MockSpeechRecognitionBubble::type_ = |
178 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED; | 178 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED; |
179 | 179 |
180 // Test that the speech bubble UI gets created in the UI thread and that the | 180 // Test that the speech bubble UI gets created in the UI thread and that the |
181 // focus changed callback comes back in the IO thread. | 181 // focus changed callback comes back in the IO thread. |
182 TEST_F(SpeechRecognitionBubbleControllerTest, TestFocusChanged) { | 182 TEST_F(SpeechRecognitionBubbleControllerTest, TestFocusChanged) { |
183 MockSpeechRecognitionBubble::set_type( | 183 MockSpeechRecognitionBubble::set_type( |
184 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED); | 184 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED); |
185 | 185 |
186 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); | 186 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); |
187 MessageLoop::current()->Run(); | 187 base::MessageLoop::current()->Run(); |
188 EXPECT_TRUE(focus_changed_); | 188 EXPECT_TRUE(focus_changed_); |
189 EXPECT_FALSE(cancel_clicked_); | 189 EXPECT_FALSE(cancel_clicked_); |
190 EXPECT_FALSE(try_again_clicked_); | 190 EXPECT_FALSE(try_again_clicked_); |
191 controller_->CloseBubble(); | 191 controller_->CloseBubble(); |
192 } | 192 } |
193 | 193 |
194 // Test that the speech bubble UI gets created in the UI thread and that the | 194 // Test that the speech bubble UI gets created in the UI thread and that the |
195 // recognition cancelled callback comes back in the IO thread. | 195 // recognition cancelled callback comes back in the IO thread. |
196 TEST_F(SpeechRecognitionBubbleControllerTest, TestRecognitionCancelled) { | 196 TEST_F(SpeechRecognitionBubbleControllerTest, TestRecognitionCancelled) { |
197 MockSpeechRecognitionBubble::set_type( | 197 MockSpeechRecognitionBubble::set_type( |
198 MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_CANCEL); | 198 MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_CANCEL); |
199 | 199 |
200 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); | 200 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); |
201 MessageLoop::current()->Run(); | 201 base::MessageLoop::current()->Run(); |
202 EXPECT_TRUE(cancel_clicked_); | 202 EXPECT_TRUE(cancel_clicked_); |
203 EXPECT_FALSE(try_again_clicked_); | 203 EXPECT_FALSE(try_again_clicked_); |
204 EXPECT_FALSE(focus_changed_); | 204 EXPECT_FALSE(focus_changed_); |
205 controller_->CloseBubble(); | 205 controller_->CloseBubble(); |
206 } | 206 } |
207 | 207 |
208 // Test that the speech bubble UI gets created in the UI thread and that the | 208 // Test that the speech bubble UI gets created in the UI thread and that the |
209 // try-again button click event comes back in the IO thread. | 209 // try-again button click event comes back in the IO thread. |
210 TEST_F(SpeechRecognitionBubbleControllerTest, TestTryAgainClicked) { | 210 TEST_F(SpeechRecognitionBubbleControllerTest, TestTryAgainClicked) { |
211 MockSpeechRecognitionBubble::set_type( | 211 MockSpeechRecognitionBubble::set_type( |
212 MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_TRY_AGAIN); | 212 MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_TRY_AGAIN); |
213 | 213 |
214 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); | 214 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); |
215 MessageLoop::current()->Run(); | 215 base::MessageLoop::current()->Run(); |
216 EXPECT_FALSE(cancel_clicked_); | 216 EXPECT_FALSE(cancel_clicked_); |
217 EXPECT_TRUE(try_again_clicked_); | 217 EXPECT_TRUE(try_again_clicked_); |
218 EXPECT_FALSE(focus_changed_); | 218 EXPECT_FALSE(focus_changed_); |
219 controller_->CloseBubble(); | 219 controller_->CloseBubble(); |
220 } | 220 } |
221 | 221 |
222 } // namespace speech | 222 } // namespace speech |
OLD | NEW |