OLD | NEW |
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/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
6 #include "chrome/browser/browser_thread.h" | 6 #include "chrome/browser/browser_thread.h" |
7 #include "chrome/browser/speech/speech_input_bubble_controller.h" | 7 #include "chrome/browser/speech/speech_input_bubble_controller.h" |
| 8 #include "chrome/test/browser_with_test_window_test.h" |
| 9 #include "chrome/test/testing_profile.h" |
8 #include "gfx/rect.h" | 10 #include "gfx/rect.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
10 | 12 |
11 class SkBitmap; | 13 class SkBitmap; |
12 | 14 |
13 namespace speech_input { | 15 namespace speech_input { |
14 | 16 |
15 // A mock bubble class which fakes a focus change or recognition cancel by the | 17 // A mock bubble class which fakes a focus change or recognition cancel by the |
16 // user and closing of the info bubble. | 18 // user and closing of the info bubble. |
17 class MockSpeechInputBubble : public SpeechInputBubbleBase { | 19 class MockSpeechInputBubble : public SpeechInputBubbleBase { |
18 public: | 20 public: |
19 enum BubbleType { | 21 enum BubbleType { |
20 BUBBLE_TEST_FOCUS_CHANGED, | 22 BUBBLE_TEST_FOCUS_CHANGED, |
21 BUBBLE_TEST_CLICK_CANCEL, | 23 BUBBLE_TEST_CLICK_CANCEL, |
22 BUBBLE_TEST_CLICK_TRY_AGAIN, | 24 BUBBLE_TEST_CLICK_TRY_AGAIN, |
23 }; | 25 }; |
24 | 26 |
25 MockSpeechInputBubble(TabContents*, Delegate* delegate, const gfx::Rect&) { | 27 MockSpeechInputBubble(TabContents* tab_contents, |
| 28 Delegate* delegate, |
| 29 const gfx::Rect&) |
| 30 : SpeechInputBubbleBase(tab_contents) { |
26 VLOG(1) << "MockSpeechInputBubble created"; | 31 VLOG(1) << "MockSpeechInputBubble created"; |
27 MessageLoop::current()->PostTask( | 32 MessageLoop::current()->PostTask( |
28 FROM_HERE, NewRunnableFunction(&InvokeDelegate, delegate)); | 33 FROM_HERE, NewRunnableFunction(&InvokeDelegate, delegate)); |
29 } | 34 } |
30 | 35 |
31 static void InvokeDelegate(Delegate* delegate) { | 36 static void InvokeDelegate(Delegate* delegate) { |
32 VLOG(1) << "MockSpeechInputBubble invoking delegate for type " << type_; | 37 VLOG(1) << "MockSpeechInputBubble invoking delegate for type " << type_; |
33 switch (type_) { | 38 switch (type_) { |
34 case BUBBLE_TEST_FOCUS_CHANGED: | 39 case BUBBLE_TEST_FOCUS_CHANGED: |
35 delegate->InfoBubbleFocusChanged(); | 40 delegate->InfoBubbleFocusChanged(); |
(...skipping 19 matching lines...) Expand all Loading... |
55 virtual void UpdateLayout() {} | 60 virtual void UpdateLayout() {} |
56 virtual void SetImage(const SkBitmap&) {} | 61 virtual void SetImage(const SkBitmap&) {} |
57 | 62 |
58 private: | 63 private: |
59 static BubbleType type_; | 64 static BubbleType type_; |
60 }; | 65 }; |
61 | 66 |
62 // The test fixture. | 67 // The test fixture. |
63 class SpeechInputBubbleControllerTest | 68 class SpeechInputBubbleControllerTest |
64 : public SpeechInputBubbleControllerDelegate, | 69 : public SpeechInputBubbleControllerDelegate, |
65 public testing::Test { | 70 public BrowserWithTestWindowTest { |
66 public: | 71 public: |
67 SpeechInputBubbleControllerTest() | 72 SpeechInputBubbleControllerTest() |
68 : io_loop_(MessageLoop::TYPE_IO), | 73 : BrowserWithTestWindowTest(), |
69 ui_thread_(BrowserThread::UI), // constructs a new thread and loop | 74 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), | 75 cancel_clicked_(false), |
72 try_again_clicked_(false), | 76 try_again_clicked_(false), |
73 focus_changed_(false), | 77 focus_changed_(false), |
74 controller_(ALLOW_THIS_IN_INITIALIZER_LIST( | 78 controller_(ALLOW_THIS_IN_INITIALIZER_LIST( |
75 new SpeechInputBubbleController(this))) { | 79 new SpeechInputBubbleController(this))) { |
76 EXPECT_EQ(NULL, test_fixture_); | 80 EXPECT_EQ(NULL, test_fixture_); |
77 test_fixture_ = this; | 81 test_fixture_ = this; |
78 } | 82 } |
79 | 83 |
80 ~SpeechInputBubbleControllerTest() { | 84 ~SpeechInputBubbleControllerTest() { |
81 test_fixture_ = NULL; | 85 test_fixture_ = NULL; |
82 } | 86 } |
83 | 87 |
84 // SpeechInputBubbleControllerDelegate methods. | 88 // SpeechInputBubbleControllerDelegate methods. |
85 virtual void InfoBubbleButtonClicked(int caller_id, | 89 virtual void InfoBubbleButtonClicked(int caller_id, |
86 SpeechInputBubble::Button button) { | 90 SpeechInputBubble::Button button) { |
87 VLOG(1) << "Received InfoBubbleButtonClicked for button " << button; | 91 VLOG(1) << "Received InfoBubbleButtonClicked for button " << button; |
88 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 92 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
89 if (button == SpeechInputBubble::BUTTON_CANCEL) { | 93 if (button == SpeechInputBubble::BUTTON_CANCEL) { |
90 cancel_clicked_ = true; | 94 cancel_clicked_ = true; |
91 } else if (button == SpeechInputBubble::BUTTON_TRY_AGAIN) { | 95 } else if (button == SpeechInputBubble::BUTTON_TRY_AGAIN) { |
92 try_again_clicked_ = true; | 96 try_again_clicked_ = true; |
93 } | 97 } |
94 MessageLoop::current()->Quit(); | 98 message_loop()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
95 } | 99 } |
96 | 100 |
97 virtual void InfoBubbleFocusChanged(int caller_id) { | 101 virtual void InfoBubbleFocusChanged(int caller_id) { |
98 VLOG(1) << "Received InfoBubbleFocusChanged"; | 102 VLOG(1) << "Received InfoBubbleFocusChanged"; |
99 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 103 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
100 focus_changed_ = true; | 104 focus_changed_ = true; |
101 MessageLoop::current()->Quit(); | 105 message_loop()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
102 } | 106 } |
103 | 107 |
104 // testing::Test methods. | 108 // testing::Test methods. |
105 virtual void SetUp() { | 109 virtual void SetUp() { |
| 110 BrowserWithTestWindowTest::SetUp(); |
106 SpeechInputBubble::set_factory( | 111 SpeechInputBubble::set_factory( |
107 &SpeechInputBubbleControllerTest::CreateBubble); | 112 &SpeechInputBubbleControllerTest::CreateBubble); |
108 ui_thread_.Start(); | 113 io_thread_.Start(); |
109 } | 114 } |
110 | 115 |
111 virtual void TearDown() { | 116 virtual void TearDown() { |
112 SpeechInputBubble::set_factory(NULL); | 117 SpeechInputBubble::set_factory(NULL); |
113 ui_thread_.Stop(); | 118 io_thread_.Stop(); |
| 119 BrowserWithTestWindowTest::TearDown(); |
114 } | 120 } |
115 | 121 |
116 static void ActivateBubble() { | 122 static void ActivateBubble() { |
117 if (MockSpeechInputBubble::type() == | 123 if (MockSpeechInputBubble::type() == |
118 MockSpeechInputBubble::BUBBLE_TEST_FOCUS_CHANGED) { | 124 MockSpeechInputBubble::BUBBLE_TEST_FOCUS_CHANGED) { |
119 test_fixture_->controller_->SetBubbleRecordingMode(kBubbleCallerId); | 125 test_fixture_->controller_->SetBubbleRecordingMode(kBubbleCallerId); |
120 } else { | 126 } else { |
121 test_fixture_->controller_->SetBubbleMessage(kBubbleCallerId, | 127 test_fixture_->controller_->SetBubbleMessage(kBubbleCallerId, |
122 ASCIIToUTF16("Test")); | 128 ASCIIToUTF16("Test")); |
123 } | 129 } |
124 } | 130 } |
125 | 131 |
126 static SpeechInputBubble* CreateBubble(TabContents* tab_contents, | 132 static SpeechInputBubble* CreateBubble(TabContents* tab_contents, |
127 SpeechInputBubble::Delegate* delegate, | 133 SpeechInputBubble::Delegate* delegate, |
128 const gfx::Rect& element_rect) { | 134 const gfx::Rect& element_rect) { |
129 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 135 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
130 // Set up to activate the bubble soon after it gets created, since we test | 136 // 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 | 137 // events sent by the bubble and those are handled only when the bubble is |
132 // active. | 138 // active. |
133 MessageLoop::current()->PostTask(FROM_HERE, | 139 MessageLoop::current()->PostTask(FROM_HERE, |
134 NewRunnableFunction(&ActivateBubble)); | 140 NewRunnableFunction(&ActivateBubble)); |
| 141 |
| 142 // The |tab_contents| parameter would be NULL since the dummy caller id |
| 143 // passed to CreateBubble would not have matched any active tab. So get a |
| 144 // real TabContents pointer from the test fixture and pass that, because |
| 145 // the bubble controller registers for tab close notifications which need |
| 146 // a valid TabContents. |
| 147 tab_contents = test_fixture_->browser()->GetSelectedTabContents(); |
135 return new MockSpeechInputBubble(tab_contents, delegate, element_rect); | 148 return new MockSpeechInputBubble(tab_contents, delegate, element_rect); |
136 } | 149 } |
137 | 150 |
138 protected: | 151 protected: |
139 // The main thread of the test is marked as the IO thread and we create a new | 152 // The main thread of the test is marked as the IO thread and we create a new |
140 // one for the UI thread. | 153 // one for the UI thread. |
141 MessageLoop io_loop_; | |
142 BrowserThread ui_thread_; | |
143 BrowserThread io_thread_; | 154 BrowserThread io_thread_; |
144 bool cancel_clicked_; | 155 bool cancel_clicked_; |
145 bool try_again_clicked_; | 156 bool try_again_clicked_; |
146 bool focus_changed_; | 157 bool focus_changed_; |
147 scoped_refptr<SpeechInputBubbleController> controller_; | 158 scoped_refptr<SpeechInputBubbleController> controller_; |
148 | 159 |
149 static const int kBubbleCallerId; | 160 static const int kBubbleCallerId; |
150 static SpeechInputBubbleControllerTest* test_fixture_; | 161 static SpeechInputBubbleControllerTest* test_fixture_; |
151 }; | 162 }; |
152 | 163 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 | 205 |
195 controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1)); | 206 controller_->CreateBubble(kBubbleCallerId, 1, 1, gfx::Rect(1, 1)); |
196 MessageLoop::current()->Run(); | 207 MessageLoop::current()->Run(); |
197 EXPECT_FALSE(cancel_clicked_); | 208 EXPECT_FALSE(cancel_clicked_); |
198 EXPECT_TRUE(try_again_clicked_); | 209 EXPECT_TRUE(try_again_clicked_); |
199 EXPECT_FALSE(focus_changed_); | 210 EXPECT_FALSE(focus_changed_); |
200 controller_->CloseBubble(kBubbleCallerId); | 211 controller_->CloseBubble(kBubbleCallerId); |
201 } | 212 } |
202 | 213 |
203 } // namespace speech_input | 214 } // namespace speech_input |
OLD | NEW |