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/run_loop.h" |
6 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
7 #include "chrome/browser/speech/speech_recognition_bubble_controller.h" | 8 #include "chrome/browser/speech/speech_recognition_bubble_controller.h" |
8 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/browser/ui/browser_tabstrip.h" | 10 #include "chrome/browser/ui/browser_tabstrip.h" |
10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
11 #include "chrome/test/base/browser_with_test_window_test.h" | 12 #include "chrome/test/base/browser_with_test_window_test.h" |
12 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
13 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "ui/gfx/rect.h" | 16 #include "ui/gfx/rect.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 static BubbleType type_; | 73 static BubbleType type_; |
73 }; | 74 }; |
74 | 75 |
75 // The test fixture. | 76 // The test fixture. |
76 class SpeechRecognitionBubbleControllerTest | 77 class SpeechRecognitionBubbleControllerTest |
77 : public SpeechRecognitionBubbleControllerDelegate, | 78 : public SpeechRecognitionBubbleControllerDelegate, |
78 public BrowserWithTestWindowTest { | 79 public BrowserWithTestWindowTest { |
79 public: | 80 public: |
80 SpeechRecognitionBubbleControllerTest() | 81 SpeechRecognitionBubbleControllerTest() |
81 : BrowserWithTestWindowTest(), | 82 : BrowserWithTestWindowTest(), |
82 io_thread_(BrowserThread::IO), // constructs a new thread and loop | |
83 cancel_clicked_(false), | 83 cancel_clicked_(false), |
84 try_again_clicked_(false), | 84 try_again_clicked_(false), |
85 focus_changed_(false), | 85 focus_changed_(false), |
86 controller_(new SpeechRecognitionBubbleController(this)) { | 86 controller_(new SpeechRecognitionBubbleController(this)) { |
87 EXPECT_EQ(NULL, test_fixture_); | 87 EXPECT_EQ(NULL, test_fixture_); |
88 test_fixture_ = this; | 88 test_fixture_ = this; |
89 } | 89 } |
90 | 90 |
91 virtual ~SpeechRecognitionBubbleControllerTest() { | 91 virtual ~SpeechRecognitionBubbleControllerTest() { |
92 test_fixture_ = NULL; | 92 test_fixture_ = NULL; |
93 } | 93 } |
94 | 94 |
95 // SpeechRecognitionBubbleControllerDelegate methods. | 95 // SpeechRecognitionBubbleControllerDelegate methods. |
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, base::MessageLoop::QuitClosure()); | |
107 } | 106 } |
108 | 107 |
109 virtual void InfoBubbleFocusChanged(int session_id) OVERRIDE { | 108 virtual void InfoBubbleFocusChanged(int session_id) OVERRIDE { |
110 VLOG(1) << "Received InfoBubbleFocusChanged"; | 109 VLOG(1) << "Received InfoBubbleFocusChanged"; |
111 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 110 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
112 focus_changed_ = true; | 111 focus_changed_ = true; |
113 message_loop()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | |
114 } | 112 } |
115 | 113 |
116 // testing::Test methods. | 114 // testing::Test methods. |
117 virtual void SetUp() { | 115 virtual void SetUp() { |
118 BrowserWithTestWindowTest::SetUp(); | 116 BrowserWithTestWindowTest::SetUp(); |
119 SpeechRecognitionBubble::set_factory( | 117 SpeechRecognitionBubble::set_factory( |
120 &SpeechRecognitionBubbleControllerTest::CreateBubble); | 118 &SpeechRecognitionBubbleControllerTest::CreateBubble); |
121 io_thread_.Start(); | |
122 } | 119 } |
123 | 120 |
124 virtual void TearDown() { | 121 virtual void TearDown() { |
125 SpeechRecognitionBubble::set_factory(NULL); | 122 SpeechRecognitionBubble::set_factory(NULL); |
126 io_thread_.Stop(); | |
127 BrowserWithTestWindowTest::TearDown(); | 123 BrowserWithTestWindowTest::TearDown(); |
128 } | 124 } |
129 | 125 |
130 static void ActivateBubble() { | 126 static void ActivateBubble() { |
131 if (MockSpeechRecognitionBubble::type() != | 127 if (MockSpeechRecognitionBubble::type() != |
132 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED) { | 128 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED) { |
133 test_fixture_->controller_->SetBubbleMessage(ASCIIToUTF16("Test")); | 129 test_fixture_->controller_->SetBubbleMessage(ASCIIToUTF16("Test")); |
134 } | 130 } |
135 } | 131 } |
136 | 132 |
(...skipping 13 matching lines...) Expand all Loading... |
150 // real WebContents pointer from the test fixture and pass that, because | 146 // real WebContents pointer from the test fixture and pass that, because |
151 // the bubble controller registers for tab close notifications which need | 147 // the bubble controller registers for tab close notifications which need |
152 // a valid WebContents. | 148 // a valid WebContents. |
153 web_contents = | 149 web_contents = |
154 test_fixture_->browser()->tab_strip_model()->GetActiveWebContents(); | 150 test_fixture_->browser()->tab_strip_model()->GetActiveWebContents(); |
155 return new MockSpeechRecognitionBubble(web_contents, delegate, | 151 return new MockSpeechRecognitionBubble(web_contents, delegate, |
156 element_rect); | 152 element_rect); |
157 } | 153 } |
158 | 154 |
159 protected: | 155 protected: |
160 // The main thread of the test is marked as the IO thread and we create a new | |
161 // one for the UI thread. | |
162 content::TestBrowserThread io_thread_; | |
163 bool cancel_clicked_; | 156 bool cancel_clicked_; |
164 bool try_again_clicked_; | 157 bool try_again_clicked_; |
165 bool focus_changed_; | 158 bool focus_changed_; |
166 scoped_refptr<SpeechRecognitionBubbleController> controller_; | 159 scoped_refptr<SpeechRecognitionBubbleController> controller_; |
167 | 160 |
168 static const int kBubbleSessionId; | 161 static const int kBubbleSessionId; |
169 static SpeechRecognitionBubbleControllerTest* test_fixture_; | 162 static SpeechRecognitionBubbleControllerTest* test_fixture_; |
170 }; | 163 }; |
171 | 164 |
172 SpeechRecognitionBubbleControllerTest* | 165 SpeechRecognitionBubbleControllerTest* |
173 SpeechRecognitionBubbleControllerTest::test_fixture_ = NULL; | 166 SpeechRecognitionBubbleControllerTest::test_fixture_ = NULL; |
174 | 167 |
175 const int SpeechRecognitionBubbleControllerTest::kBubbleSessionId = 1; | 168 const int SpeechRecognitionBubbleControllerTest::kBubbleSessionId = 1; |
176 | 169 |
177 MockSpeechRecognitionBubble::BubbleType MockSpeechRecognitionBubble::type_ = | 170 MockSpeechRecognitionBubble::BubbleType MockSpeechRecognitionBubble::type_ = |
178 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED; | 171 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED; |
179 | 172 |
180 // Test that the speech bubble UI gets created in the UI thread and that the | 173 // 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. | 174 // focus changed callback comes back in the IO thread. |
182 TEST_F(SpeechRecognitionBubbleControllerTest, TestFocusChanged) { | 175 TEST_F(SpeechRecognitionBubbleControllerTest, TestFocusChanged) { |
183 MockSpeechRecognitionBubble::set_type( | 176 MockSpeechRecognitionBubble::set_type( |
184 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED); | 177 MockSpeechRecognitionBubble::BUBBLE_TEST_FOCUS_CHANGED); |
185 | 178 |
186 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); | 179 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); |
187 base::MessageLoop::current()->Run(); | 180 base::RunLoop().RunUntilIdle(); |
188 EXPECT_TRUE(focus_changed_); | 181 EXPECT_TRUE(focus_changed_); |
189 EXPECT_FALSE(cancel_clicked_); | 182 EXPECT_FALSE(cancel_clicked_); |
190 EXPECT_FALSE(try_again_clicked_); | 183 EXPECT_FALSE(try_again_clicked_); |
191 controller_->CloseBubble(); | 184 controller_->CloseBubble(); |
192 } | 185 } |
193 | 186 |
194 // Test that the speech bubble UI gets created in the UI thread and that the | 187 // 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. | 188 // recognition cancelled callback comes back in the IO thread. |
196 TEST_F(SpeechRecognitionBubbleControllerTest, TestRecognitionCancelled) { | 189 TEST_F(SpeechRecognitionBubbleControllerTest, TestRecognitionCancelled) { |
197 MockSpeechRecognitionBubble::set_type( | 190 MockSpeechRecognitionBubble::set_type( |
198 MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_CANCEL); | 191 MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_CANCEL); |
199 | 192 |
200 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); | 193 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); |
201 base::MessageLoop::current()->Run(); | 194 base::RunLoop().RunUntilIdle(); |
202 EXPECT_TRUE(cancel_clicked_); | 195 EXPECT_TRUE(cancel_clicked_); |
203 EXPECT_FALSE(try_again_clicked_); | 196 EXPECT_FALSE(try_again_clicked_); |
204 EXPECT_FALSE(focus_changed_); | 197 EXPECT_FALSE(focus_changed_); |
205 controller_->CloseBubble(); | 198 controller_->CloseBubble(); |
206 } | 199 } |
207 | 200 |
208 // Test that the speech bubble UI gets created in the UI thread and that the | 201 // 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. | 202 // try-again button click event comes back in the IO thread. |
210 TEST_F(SpeechRecognitionBubbleControllerTest, TestTryAgainClicked) { | 203 TEST_F(SpeechRecognitionBubbleControllerTest, TestTryAgainClicked) { |
211 MockSpeechRecognitionBubble::set_type( | 204 MockSpeechRecognitionBubble::set_type( |
212 MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_TRY_AGAIN); | 205 MockSpeechRecognitionBubble::BUBBLE_TEST_CLICK_TRY_AGAIN); |
213 | 206 |
214 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); | 207 controller_->CreateBubble(kBubbleSessionId, 1, 1, gfx::Rect(1, 1)); |
215 base::MessageLoop::current()->Run(); | 208 base::RunLoop().RunUntilIdle(); |
216 EXPECT_FALSE(cancel_clicked_); | 209 EXPECT_FALSE(cancel_clicked_); |
217 EXPECT_TRUE(try_again_clicked_); | 210 EXPECT_TRUE(try_again_clicked_); |
218 EXPECT_FALSE(focus_changed_); | 211 EXPECT_FALSE(focus_changed_); |
219 controller_->CloseBubble(); | 212 controller_->CloseBubble(); |
220 } | 213 } |
221 | 214 |
222 } // namespace speech | 215 } // namespace speech |
OLD | NEW |