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

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

Issue 6115001: Listen for tab close notifications and cancel active speech sessions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed tests Created 9 years, 11 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/synchronization/waitable_event.h"
5 #include "base/utf_string_conversions.h" 6 #include "base/utf_string_conversions.h"
6 #include "chrome/browser/browser_thread.h" 7 #include "chrome/browser/browser_thread.h"
7 #include "chrome/browser/speech/speech_input_bubble_controller.h" 8 #include "chrome/browser/speech/speech_input_bubble_controller.h"
9 #include "chrome/test/browser_with_test_window_test.h"
10 #include "chrome/test/testing_profile.h"
8 #include "gfx/rect.h" 11 #include "gfx/rect.h"
9 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
10 13
11 class SkBitmap; 14 class SkBitmap;
12 15
13 namespace speech_input { 16 namespace speech_input {
14 17
15 // A mock bubble class which fakes a focus change or recognition cancel by the 18 // A mock bubble class which fakes a focus change or recognition cancel by the
16 // user and closing of the info bubble. 19 // user and closing of the info bubble.
17 class MockSpeechInputBubble : public SpeechInputBubbleBase { 20 class MockSpeechInputBubble : public SpeechInputBubbleBase {
18 public: 21 public:
19 enum BubbleType { 22 enum BubbleType {
20 BUBBLE_TEST_FOCUS_CHANGED, 23 BUBBLE_TEST_FOCUS_CHANGED,
21 BUBBLE_TEST_CLICK_CANCEL, 24 BUBBLE_TEST_CLICK_CANCEL,
22 BUBBLE_TEST_CLICK_TRY_AGAIN, 25 BUBBLE_TEST_CLICK_TRY_AGAIN,
23 }; 26 };
24 27
25 MockSpeechInputBubble(TabContents*, Delegate* delegate, const gfx::Rect&) { 28 MockSpeechInputBubble(TabContents* tab_contents,
29 Delegate* delegate,
30 const gfx::Rect&)
31 : SpeechInputBubbleBase(tab_contents) {
26 VLOG(1) << "MockSpeechInputBubble created"; 32 VLOG(1) << "MockSpeechInputBubble created";
27 MessageLoop::current()->PostTask( 33 MessageLoop::current()->PostTask(
28 FROM_HERE, NewRunnableFunction(&InvokeDelegate, delegate)); 34 FROM_HERE, NewRunnableFunction(&InvokeDelegate, delegate));
29 } 35 }
30 36
31 static void InvokeDelegate(Delegate* delegate) { 37 static void InvokeDelegate(Delegate* delegate) {
32 VLOG(1) << "MockSpeechInputBubble invoking delegate for type " << type_; 38 VLOG(1) << "MockSpeechInputBubble invoking delegate for type " << type_;
33 switch (type_) { 39 switch (type_) {
34 case BUBBLE_TEST_FOCUS_CHANGED: 40 case BUBBLE_TEST_FOCUS_CHANGED:
35 delegate->InfoBubbleFocusChanged(); 41 delegate->InfoBubbleFocusChanged();
(...skipping 19 matching lines...) Expand all
55 virtual void UpdateLayout() {} 61 virtual void UpdateLayout() {}
56 virtual void SetImage(const SkBitmap&) {} 62 virtual void SetImage(const SkBitmap&) {}
57 63
58 private: 64 private:
59 static BubbleType type_; 65 static BubbleType type_;
60 }; 66 };
61 67
62 // The test fixture. 68 // The test fixture.
63 class SpeechInputBubbleControllerTest 69 class SpeechInputBubbleControllerTest
64 : public SpeechInputBubbleControllerDelegate, 70 : public SpeechInputBubbleControllerDelegate,
65 public testing::Test { 71 public BrowserWithTestWindowTest {
66 public: 72 public:
67 SpeechInputBubbleControllerTest() 73 SpeechInputBubbleControllerTest()
68 : io_loop_(MessageLoop::TYPE_IO), 74 : BrowserWithTestWindowTest(),
69 ui_thread_(BrowserThread::UI), // constructs a new thread and loop 75 io_thread_(BrowserThread::IO), // constructs a new thread and loop
70 io_thread_(BrowserThread::IO, &io_loop_), // resuses main thread loop
71 cancel_clicked_(false), 76 cancel_clicked_(false),
72 try_again_clicked_(false), 77 try_again_clicked_(false),
73 focus_changed_(false), 78 focus_changed_(false),
79 bubble_callback_event_(false, false),
bulach 2011/01/07 11:11:29 I'm not very familiar, but seems that BrowserWithT
Satish 2011/01/07 11:19:15 Done.
74 controller_(ALLOW_THIS_IN_INITIALIZER_LIST( 80 controller_(ALLOW_THIS_IN_INITIALIZER_LIST(
75 new SpeechInputBubbleController(this))) { 81 new SpeechInputBubbleController(this))) {
76 EXPECT_EQ(NULL, test_fixture_); 82 EXPECT_EQ(NULL, test_fixture_);
77 test_fixture_ = this; 83 test_fixture_ = this;
78 } 84 }
79 85
80 ~SpeechInputBubbleControllerTest() { 86 ~SpeechInputBubbleControllerTest() {
81 test_fixture_ = NULL; 87 test_fixture_ = NULL;
82 } 88 }
83 89
84 // SpeechInputBubbleControllerDelegate methods. 90 // SpeechInputBubbleControllerDelegate methods.
85 virtual void InfoBubbleButtonClicked(int caller_id, 91 virtual void InfoBubbleButtonClicked(int caller_id,
86 SpeechInputBubble::Button button) { 92 SpeechInputBubble::Button button) {
87 VLOG(1) << "Received InfoBubbleButtonClicked for button " << button; 93 VLOG(1) << "Received InfoBubbleButtonClicked for button " << button;
88 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 94 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
89 if (button == SpeechInputBubble::BUTTON_CANCEL) { 95 if (button == SpeechInputBubble::BUTTON_CANCEL) {
90 cancel_clicked_ = true; 96 cancel_clicked_ = true;
91 } else if (button == SpeechInputBubble::BUTTON_TRY_AGAIN) { 97 } else if (button == SpeechInputBubble::BUTTON_TRY_AGAIN) {
92 try_again_clicked_ = true; 98 try_again_clicked_ = true;
93 } 99 }
94 MessageLoop::current()->Quit(); 100 bubble_callback_event_.Signal();
95 } 101 }
96 102
97 virtual void InfoBubbleFocusChanged(int caller_id) { 103 virtual void InfoBubbleFocusChanged(int caller_id) {
98 VLOG(1) << "Received InfoBubbleFocusChanged"; 104 VLOG(1) << "Received InfoBubbleFocusChanged";
99 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 105 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
100 focus_changed_ = true; 106 focus_changed_ = true;
101 MessageLoop::current()->Quit(); 107 bubble_callback_event_.Signal();
102 } 108 }
103 109
104 // testing::Test methods. 110 // testing::Test methods.
105 virtual void SetUp() { 111 virtual void SetUp() {
112 BrowserWithTestWindowTest::SetUp();
106 SpeechInputBubble::set_factory( 113 SpeechInputBubble::set_factory(
107 &SpeechInputBubbleControllerTest::CreateBubble); 114 &SpeechInputBubbleControllerTest::CreateBubble);
108 ui_thread_.Start(); 115 io_thread_.Start();
109 } 116 }
110 117
111 virtual void TearDown() { 118 virtual void TearDown() {
112 SpeechInputBubble::set_factory(NULL); 119 SpeechInputBubble::set_factory(NULL);
113 ui_thread_.Stop(); 120 io_thread_.Stop();
121 BrowserWithTestWindowTest::TearDown();
114 } 122 }
115 123
116 static void ActivateBubble() { 124 static void ActivateBubble() {
117 if (MockSpeechInputBubble::type() == 125 if (MockSpeechInputBubble::type() ==
118 MockSpeechInputBubble::BUBBLE_TEST_FOCUS_CHANGED) { 126 MockSpeechInputBubble::BUBBLE_TEST_FOCUS_CHANGED) {
119 test_fixture_->controller_->SetBubbleRecordingMode(kBubbleCallerId); 127 test_fixture_->controller_->SetBubbleRecordingMode(kBubbleCallerId);
120 } else { 128 } else {
121 test_fixture_->controller_->SetBubbleMessage(kBubbleCallerId, 129 test_fixture_->controller_->SetBubbleMessage(kBubbleCallerId,
122 ASCIIToUTF16("Test")); 130 ASCIIToUTF16("Test"));
123 } 131 }
124 } 132 }
125 133
126 static SpeechInputBubble* CreateBubble(TabContents* tab_contents, 134 static SpeechInputBubble* CreateBubble(TabContents* tab_contents,
127 SpeechInputBubble::Delegate* delegate, 135 SpeechInputBubble::Delegate* delegate,
128 const gfx::Rect& element_rect) { 136 const gfx::Rect& element_rect) {
129 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); 137 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI));
130 // Set up to activate the bubble soon after it gets created, since we test 138 // Set up to activate the bubble soon after it gets created, since we test
131 // events sent by the bubble and those are handled only when the bubble is 139 // events sent by the bubble and those are handled only when the bubble is
132 // active. 140 // active.
133 MessageLoop::current()->PostTask(FROM_HERE, 141 MessageLoop::current()->PostTask(FROM_HERE,
134 NewRunnableFunction(&ActivateBubble)); 142 NewRunnableFunction(&ActivateBubble));
143
144 // The |tab_contents| parameter would be NULL since the dummy caller id
145 // passed to CreateBubble would not have matched any active tab. So get a
146 // real TabContents pointer from the test fixture and pass that, because
147 // the bubble controller registers for tab close notifications which need
148 // a valid TabContents.
149 tab_contents = test_fixture_->browser()->GetSelectedTabContents();
135 return new MockSpeechInputBubble(tab_contents, delegate, element_rect); 150 return new MockSpeechInputBubble(tab_contents, delegate, element_rect);
136 } 151 }
137 152
138 protected: 153 protected:
139 // The main thread of the test is marked as the IO thread and we create a new 154 // The main thread of the test is marked as the IO thread and we create a new
140 // one for the UI thread. 155 // one for the UI thread.
141 MessageLoop io_loop_;
142 BrowserThread ui_thread_;
143 BrowserThread io_thread_; 156 BrowserThread io_thread_;
144 bool cancel_clicked_; 157 bool cancel_clicked_;
145 bool try_again_clicked_; 158 bool try_again_clicked_;
146 bool focus_changed_; 159 bool focus_changed_;
160 base::WaitableEvent bubble_callback_event_;
147 scoped_refptr<SpeechInputBubbleController> controller_; 161 scoped_refptr<SpeechInputBubbleController> controller_;
148 162
149 static const int kBubbleCallerId; 163 static const int kBubbleCallerId;
150 static SpeechInputBubbleControllerTest* test_fixture_; 164 static SpeechInputBubbleControllerTest* test_fixture_;
151 }; 165 };
152 166
153 SpeechInputBubbleControllerTest* 167 SpeechInputBubbleControllerTest*
154 SpeechInputBubbleControllerTest::test_fixture_ = NULL; 168 SpeechInputBubbleControllerTest::test_fixture_ = NULL;
155 169
156 const int SpeechInputBubbleControllerTest::kBubbleCallerId = 1; 170 const int SpeechInputBubbleControllerTest::kBubbleCallerId = 1;
157 171
158 MockSpeechInputBubble::BubbleType MockSpeechInputBubble::type_ = 172 MockSpeechInputBubble::BubbleType MockSpeechInputBubble::type_ =
159 MockSpeechInputBubble::BUBBLE_TEST_FOCUS_CHANGED; 173 MockSpeechInputBubble::BUBBLE_TEST_FOCUS_CHANGED;
160 174
161 // Test that the speech bubble UI gets created in the UI thread and that the 175 // Test that the speech bubble UI gets created in the UI thread and that the
162 // focus changed callback comes back in the IO thread. 176 // focus changed callback comes back in the IO thread.
163 TEST_F(SpeechInputBubbleControllerTest, TestFocusChanged) { 177 TEST_F(SpeechInputBubbleControllerTest, TestFocusChanged) {
164 MockSpeechInputBubble::set_type( 178 MockSpeechInputBubble::set_type(
165 MockSpeechInputBubble::BUBBLE_TEST_FOCUS_CHANGED); 179 MockSpeechInputBubble::BUBBLE_TEST_FOCUS_CHANGED);
166 180
167 controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1)); 181 controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1));
168 MessageLoop::current()->Run(); 182 MessageLoop::current()->RunAllPending();
183 bubble_callback_event_.Wait();
169 EXPECT_TRUE(focus_changed_); 184 EXPECT_TRUE(focus_changed_);
170 EXPECT_FALSE(cancel_clicked_); 185 EXPECT_FALSE(cancel_clicked_);
171 EXPECT_FALSE(try_again_clicked_); 186 EXPECT_FALSE(try_again_clicked_);
172 controller_->CloseBubble(kBubbleCallerId); 187 controller_->CloseBubble(kBubbleCallerId);
173 } 188 }
174 189
175 // Test that the speech bubble UI gets created in the UI thread and that the 190 // Test that the speech bubble UI gets created in the UI thread and that the
176 // recognition cancelled callback comes back in the IO thread. 191 // recognition cancelled callback comes back in the IO thread.
177 TEST_F(SpeechInputBubbleControllerTest, TestRecognitionCancelled) { 192 TEST_F(SpeechInputBubbleControllerTest, TestRecognitionCancelled) {
178 MockSpeechInputBubble::set_type( 193 MockSpeechInputBubble::set_type(
179 MockSpeechInputBubble::BUBBLE_TEST_CLICK_CANCEL); 194 MockSpeechInputBubble::BUBBLE_TEST_CLICK_CANCEL);
180 195
181 controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1)); 196 controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1));
182 MessageLoop::current()->Run(); 197 MessageLoop::current()->RunAllPending();
198 bubble_callback_event_.Wait();
183 EXPECT_TRUE(cancel_clicked_); 199 EXPECT_TRUE(cancel_clicked_);
184 EXPECT_FALSE(try_again_clicked_); 200 EXPECT_FALSE(try_again_clicked_);
185 EXPECT_FALSE(focus_changed_); 201 EXPECT_FALSE(focus_changed_);
186 controller_->CloseBubble(kBubbleCallerId); 202 controller_->CloseBubble(kBubbleCallerId);
187 } 203 }
188 204
189 // Test that the speech bubble UI gets created in the UI thread and that the 205 // Test that the speech bubble UI gets created in the UI thread and that the
190 // try-again button click event comes back in the IO thread. 206 // try-again button click event comes back in the IO thread.
191 TEST_F(SpeechInputBubbleControllerTest, TestTryAgainClicked) { 207 TEST_F(SpeechInputBubbleControllerTest, TestTryAgainClicked) {
192 MockSpeechInputBubble::set_type( 208 MockSpeechInputBubble::set_type(
193 MockSpeechInputBubble::BUBBLE_TEST_CLICK_TRY_AGAIN); 209 MockSpeechInputBubble::BUBBLE_TEST_CLICK_TRY_AGAIN);
194 210
195 controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1)); 211 controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1));
196 MessageLoop::current()->Run(); 212 MessageLoop::current()->RunAllPending();
213 bubble_callback_event_.Wait();
197 EXPECT_FALSE(cancel_clicked_); 214 EXPECT_FALSE(cancel_clicked_);
198 EXPECT_TRUE(try_again_clicked_); 215 EXPECT_TRUE(try_again_clicked_);
199 EXPECT_FALSE(focus_changed_); 216 EXPECT_FALSE(focus_changed_);
200 controller_->CloseBubble(kBubbleCallerId); 217 controller_->CloseBubble(kBubbleCallerId);
201 } 218 }
202 219
203 } // namespace speech_input 220 } // namespace speech_input
OLDNEW
« no previous file with comments | « chrome/browser/speech/speech_input_bubble_controller.cc ('k') | chrome/browser/speech/speech_input_bubble_gtk.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698