| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 97   virtual void InfoBubbleButtonClicked( | 97   virtual void InfoBubbleButtonClicked( | 
| 98       int session_id, | 98       int session_id, | 
| 99       SpeechRecognitionBubble::Button button) OVERRIDE { | 99       SpeechRecognitionBubble::Button button) OVERRIDE { | 
| 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, base::MessageLoop::QuitClosure()); | 
| 108   } | 108   } | 
| 109 | 109 | 
| 110   virtual void InfoBubbleFocusChanged(int session_id) OVERRIDE { | 110   virtual void InfoBubbleFocusChanged(int session_id) OVERRIDE { | 
| 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, base::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 | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
| 136   } | 136   } | 
| 137 | 137 | 
| 138   static SpeechRecognitionBubble* CreateBubble( | 138   static SpeechRecognitionBubble* CreateBubble( | 
| 139       WebContents* web_contents, | 139       WebContents* web_contents, | 
| 140       SpeechRecognitionBubble::Delegate* delegate, | 140       SpeechRecognitionBubble::Delegate* delegate, | 
| 141       const gfx::Rect& element_rect) { | 141       const gfx::Rect& element_rect) { | 
| 142     EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 142     EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 143     // Set up to activate the bubble soon after it gets created, since we test | 143     // Set up to activate the bubble soon after it gets created, since we test | 
| 144     // events sent by the bubble and those are handled only when the bubble is | 144     // events sent by the bubble and those are handled only when the bubble is | 
| 145     // active. | 145     // active. | 
| 146     MessageLoop::current()->PostTask(FROM_HERE, | 146     base::MessageLoop::current()->PostTask(FROM_HERE, | 
| 147                                      base::Bind(&ActivateBubble)); | 147                                            base::Bind(&ActivateBubble)); | 
| 148 | 148 | 
| 149     // The |web_contents| parameter would be NULL since the dummy session id | 149     // The |web_contents| parameter would be NULL since the dummy session id | 
| 150     // passed to CreateBubble would not have matched any active tab. So get a | 150     // passed to CreateBubble would not have matched any active tab. So get a | 
| 151     // real WebContents pointer from the test fixture and pass that, because | 151     // real WebContents pointer from the test fixture and pass that, because | 
| 152     // the bubble controller registers for tab close notifications which need | 152     // the bubble controller registers for tab close notifications which need | 
| 153     // a valid WebContents. | 153     // a valid WebContents. | 
| 154     web_contents = | 154     web_contents = | 
| 155         test_fixture_->browser()->tab_strip_model()->GetActiveWebContents(); | 155         test_fixture_->browser()->tab_strip_model()->GetActiveWebContents(); | 
| 156     return new MockSpeechRecognitionBubble(web_contents, delegate, | 156     return new MockSpeechRecognitionBubble(web_contents, delegate, | 
| 157                                            element_rect); | 157                                            element_rect); | 
| (...skipping 20 matching lines...) Expand all  Loading... | 
| 178 MockSpeechRecognitionBubble::BubbleType MockSpeechRecognitionBubble::type_ = | 178 MockSpeechRecognitionBubble::BubbleType MockSpeechRecognitionBubble::type_ = | 
| 179     MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED; | 179     MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED; | 
| 180 | 180 | 
| 181 // Test that the speech bubble UI gets created in the UI thread and that the | 181 // Test that the speech bubble UI gets created in the UI thread and that the | 
| 182 // focus changed callback comes back in the IO thread. | 182 // focus changed callback comes back in the IO thread. | 
| 183 TEST_F(SpeechRecognitionBubbleControllerTest, TestFocusChanged) { | 183 TEST_F(SpeechRecognitionBubbleControllerTest, TestFocusChanged) { | 
| 184   MockSpeechRecognitionBubble::set_type( | 184   MockSpeechRecognitionBubble::set_type( | 
| 185       MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED); | 185       MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED); | 
| 186 | 186 | 
| 187   controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); | 187   controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); | 
| 188   MessageLoop::current()->Run(); | 188   base::MessageLoop::current()->Run(); | 
| 189   EXPECT_TRUE(focus_changed_); | 189   EXPECT_TRUE(focus_changed_); | 
| 190   EXPECT_FALSE(cancel_clicked_); | 190   EXPECT_FALSE(cancel_clicked_); | 
| 191   EXPECT_FALSE(try_again_clicked_); | 191   EXPECT_FALSE(try_again_clicked_); | 
| 192   controller_->CloseBubble(); | 192   controller_->CloseBubble(); | 
| 193 } | 193 } | 
| 194 | 194 | 
| 195 // Test that the speech bubble UI gets created in the UI thread and that the | 195 // Test that the speech bubble UI gets created in the UI thread and that the | 
| 196 // recognition cancelled callback comes back in the IO thread. | 196 // recognition cancelled callback comes back in the IO thread. | 
| 197 TEST_F(SpeechRecognitionBubbleControllerTest, TestRecognitionCancelled) { | 197 TEST_F(SpeechRecognitionBubbleControllerTest, TestRecognitionCancelled) { | 
| 198   MockSpeechRecognitionBubble::set_type( | 198   MockSpeechRecognitionBubble::set_type( | 
| 199       MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_CANCEL); | 199       MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_CANCEL); | 
| 200 | 200 | 
| 201   controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); | 201   controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); | 
| 202   MessageLoop::current()->Run(); | 202   base::MessageLoop::current()->Run(); | 
| 203   EXPECT_TRUE(cancel_clicked_); | 203   EXPECT_TRUE(cancel_clicked_); | 
| 204   EXPECT_FALSE(try_again_clicked_); | 204   EXPECT_FALSE(try_again_clicked_); | 
| 205   EXPECT_FALSE(focus_changed_); | 205   EXPECT_FALSE(focus_changed_); | 
| 206   controller_->CloseBubble(); | 206   controller_->CloseBubble(); | 
| 207 } | 207 } | 
| 208 | 208 | 
| 209 // Test that the speech bubble UI gets created in the UI thread and that the | 209 // Test that the speech bubble UI gets created in the UI thread and that the | 
| 210 // try-again button click event comes back in the IO thread. | 210 // try-again button click event comes back in the IO thread. | 
| 211 TEST_F(SpeechRecognitionBubbleControllerTest, TestTryAgainClicked) { | 211 TEST_F(SpeechRecognitionBubbleControllerTest, TestTryAgainClicked) { | 
| 212   MockSpeechRecognitionBubble::set_type( | 212   MockSpeechRecognitionBubble::set_type( | 
| 213       MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_TRY_AGAIN); | 213       MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_TRY_AGAIN); | 
| 214 | 214 | 
| 215   controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); | 215   controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); | 
| 216   MessageLoop::current()->Run(); | 216   base::MessageLoop::current()->Run(); | 
| 217   EXPECT_FALSE(cancel_clicked_); | 217   EXPECT_FALSE(cancel_clicked_); | 
| 218   EXPECT_TRUE(try_again_clicked_); | 218   EXPECT_TRUE(try_again_clicked_); | 
| 219   EXPECT_FALSE(focus_changed_); | 219   EXPECT_FALSE(focus_changed_); | 
| 220   controller_->CloseBubble(); | 220   controller_->CloseBubble(); | 
| 221 } | 221 } | 
| 222 | 222 | 
| 223 }  // namespace speech | 223 }  // namespace speech | 
| OLD | NEW | 
|---|