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

Side by Side Diff: ui/keyboard/keyboard_controller_unittest.cc

Issue 1392713002: Extract content dependency from keyboard code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 5 years, 2 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
« no previous file with comments | « ui/keyboard/keyboard_controller_proxy.cc ('k') | ui/keyboard/keyboard_layout_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "ui/keyboard/keyboard_controller.h" 5 #include "ui/keyboard/keyboard_controller.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 10 matching lines...) Expand all
21 #include "ui/base/ime/text_input_client.h" 21 #include "ui/base/ime/text_input_client.h"
22 #include "ui/compositor/compositor.h" 22 #include "ui/compositor/compositor.h"
23 #include "ui/compositor/layer_type.h" 23 #include "ui/compositor/layer_type.h"
24 #include "ui/compositor/scoped_animation_duration_scale_mode.h" 24 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
25 #include "ui/compositor/test/context_factories_for_test.h" 25 #include "ui/compositor/test/context_factories_for_test.h"
26 #include "ui/compositor/test/layer_animator_test_controller.h" 26 #include "ui/compositor/test/layer_animator_test_controller.h"
27 #include "ui/events/event_utils.h" 27 #include "ui/events/event_utils.h"
28 #include "ui/events/test/event_generator.h" 28 #include "ui/events/test/event_generator.h"
29 #include "ui/gfx/geometry/rect.h" 29 #include "ui/gfx/geometry/rect.h"
30 #include "ui/keyboard/keyboard_controller_observer.h" 30 #include "ui/keyboard/keyboard_controller_observer.h"
31 #include "ui/keyboard/keyboard_controller_proxy.h" 31 #include "ui/keyboard/keyboard_ui.h"
32 #include "ui/keyboard/keyboard_util.h" 32 #include "ui/keyboard/keyboard_util.h"
33 #include "ui/wm/core/default_activation_client.h" 33 #include "ui/wm/core/default_activation_client.h"
34 34
35 namespace keyboard { 35 namespace keyboard {
36 namespace { 36 namespace {
37 37
38 const int kDefaultVirtualKeyboardHeight = 100; 38 const int kDefaultVirtualKeyboardHeight = 100;
39 39
40 // Verify if the |keyboard| window covers the |container| window completely. 40 // Verify if the |keyboard| window covers the |container| window completely.
41 void VerifyKeyboardWindowSize(aura::Window* container, aura::Window* keyboard) { 41 void VerifyKeyboardWindowSize(aura::Window* container, aura::Window* keyboard) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 if (event->type() == ui::ET_MOUSE_PRESSED || 79 if (event->type() == ui::ET_MOUSE_PRESSED ||
80 event->type() == ui::ET_TOUCH_PRESSED) { 80 event->type() == ui::ET_TOUCH_PRESSED) {
81 aura::client::GetFocusClient(target)->FocusWindow(target); 81 aura::client::GetFocusClient(target)->FocusWindow(target);
82 } 82 }
83 } 83 }
84 84
85 aura::Window* root_; 85 aura::Window* root_;
86 DISALLOW_COPY_AND_ASSIGN(TestFocusController); 86 DISALLOW_COPY_AND_ASSIGN(TestFocusController);
87 }; 87 };
88 88
89 class TestKeyboardControllerProxy : public KeyboardControllerProxy { 89 class TestKeyboardUI : public KeyboardUI {
90 public: 90 public:
91 TestKeyboardControllerProxy(ui::InputMethod* input_method) 91 TestKeyboardUI(ui::InputMethod* input_method) : input_method_(input_method) {}
92 : KeyboardControllerProxy(nullptr), 92 ~TestKeyboardUI() override {
93 input_method_(input_method) {}
94
95 ~TestKeyboardControllerProxy() override {
96 // Destroy the window before the delegate. 93 // Destroy the window before the delegate.
97 window_.reset(); 94 window_.reset();
98 } 95 }
99 96
100 // Overridden from KeyboardControllerProxy: 97 // Overridden from KeyboardUI:
101 bool HasKeyboardWindow() const override { return window_; } 98 bool HasKeyboardWindow() const override { return window_; }
102 aura::Window* GetKeyboardWindow() override { 99 aura::Window* GetKeyboardWindow() override {
103 if (!window_) { 100 if (!window_) {
104 window_.reset(new aura::Window(&delegate_)); 101 window_.reset(new aura::Window(&delegate_));
105 window_->Init(ui::LAYER_NOT_DRAWN); 102 window_->Init(ui::LAYER_NOT_DRAWN);
106 window_->set_owned_by_parent(false); 103 window_->set_owned_by_parent(false);
107 } 104 }
108 return window_.get(); 105 return window_.get();
109 } 106 }
110 ui::InputMethod* GetInputMethod() override { return input_method_; } 107 ui::InputMethod* GetInputMethod() override { return input_method_; }
111 void RequestAudioInput( 108 void SetUpdateInputType(ui::TextInputType type) override {}
112 content::WebContents* web_contents, 109 void ReloadKeyboardIfNeeded() override {};
113 const content::MediaStreamRequest& request, 110 void InitInsets(const gfx::Rect& keyboard_bounds) override {}
114 const content::MediaResponseCallback& callback) override { 111 void ResetInsets() override {}
115 return;
116 }
117 void LoadSystemKeyboard() override{};
118 void ReloadKeyboardIfNeeded() override{};
119 112
120 private: 113 private:
121 scoped_ptr<aura::Window> window_; 114 scoped_ptr<aura::Window> window_;
122 aura::test::TestWindowDelegate delegate_; 115 aura::test::TestWindowDelegate delegate_;
123 ui::InputMethod* input_method_; 116 ui::InputMethod* input_method_;
124 117
125 DISALLOW_COPY_AND_ASSIGN(TestKeyboardControllerProxy); 118 DISALLOW_COPY_AND_ASSIGN(TestKeyboardUI);
126 }; 119 };
127 120
128 // Keeps a count of all the events a window receives. 121 // Keeps a count of all the events a window receives.
129 class EventObserver : public ui::EventHandler { 122 class EventObserver : public ui::EventHandler {
130 public: 123 public:
131 EventObserver() {} 124 EventObserver() {}
132 ~EventObserver() override {} 125 ~EventObserver() override {}
133 126
134 int GetEventCount(ui::EventType type) { 127 int GetEventCount(ui::EventType type) {
135 return event_counts_[type]; 128 return event_counts_[type];
(...skipping 26 matching lines...) Expand all
162 aura::Window* window_; 155 aura::Window* window_;
163 156
164 DISALLOW_COPY_AND_ASSIGN(KeyboardContainerObserver); 157 DISALLOW_COPY_AND_ASSIGN(KeyboardContainerObserver);
165 }; 158 };
166 159
167 } // namespace 160 } // namespace
168 161
169 class KeyboardControllerTest : public testing::Test, 162 class KeyboardControllerTest : public testing::Test,
170 public KeyboardControllerObserver { 163 public KeyboardControllerObserver {
171 public: 164 public:
172 KeyboardControllerTest() : number_of_calls_(0), proxy_(nullptr) {} 165 KeyboardControllerTest() : number_of_calls_(0), ui_(nullptr) {}
173 ~KeyboardControllerTest() override {} 166 ~KeyboardControllerTest() override {}
174 167
175 void SetUp() override { 168 void SetUp() override {
176 // The ContextFactory must exist before any Compositors are created. 169 // The ContextFactory must exist before any Compositors are created.
177 bool enable_pixel_output = false; 170 bool enable_pixel_output = false;
178 ui::ContextFactory* context_factory = 171 ui::ContextFactory* context_factory =
179 ui::InitializeContextFactoryForTests(enable_pixel_output); 172 ui::InitializeContextFactoryForTests(enable_pixel_output);
180 173
181 ui::SetUpInputMethodFactoryForTesting(); 174 ui::SetUpInputMethodFactoryForTesting();
182 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_)); 175 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_));
183 aura_test_helper_->SetUp(context_factory); 176 aura_test_helper_->SetUp(context_factory);
184 new wm::DefaultActivationClient(aura_test_helper_->root_window()); 177 new wm::DefaultActivationClient(aura_test_helper_->root_window());
185 focus_controller_.reset(new TestFocusController(root_window())); 178 focus_controller_.reset(new TestFocusController(root_window()));
186 proxy_ = new TestKeyboardControllerProxy( 179 ui_ = new TestKeyboardUI(aura_test_helper_->host()->GetInputMethod());
187 aura_test_helper_->host()->GetInputMethod()); 180 controller_.reset(new KeyboardController(ui_));
188 controller_.reset(new KeyboardController(proxy_));
189 controller()->AddObserver(this); 181 controller()->AddObserver(this);
190 } 182 }
191 183
192 void TearDown() override { 184 void TearDown() override {
193 controller()->RemoveObserver(this); 185 controller()->RemoveObserver(this);
194 controller_.reset(); 186 controller_.reset();
195 focus_controller_.reset(); 187 focus_controller_.reset();
196 aura_test_helper_->TearDown(); 188 aura_test_helper_->TearDown();
197 ui::TerminateContextFactoryForTests(); 189 ui::TerminateContextFactoryForTests();
198 } 190 }
199 191
200 aura::Window* root_window() { return aura_test_helper_->root_window(); } 192 aura::Window* root_window() { return aura_test_helper_->root_window(); }
201 KeyboardControllerProxy* proxy() { return proxy_; } 193 KeyboardUI* ui() { return ui_; }
202 KeyboardController* controller() { return controller_.get(); } 194 KeyboardController* controller() { return controller_.get(); }
203 195
204 void ShowKeyboard() { 196 void ShowKeyboard() {
205 test_text_input_client_.reset( 197 test_text_input_client_.reset(
206 new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT)); 198 new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT));
207 SetFocus(test_text_input_client_.get()); 199 SetFocus(test_text_input_client_.get());
208 } 200 }
209 201
210 void MockRotateScreen() { 202 void MockRotateScreen() {
211 const gfx::Rect root_bounds = root_window()->bounds(); 203 const gfx::Rect root_bounds = root_window()->bounds();
212 controller_->OnWindowBoundsChanged(root_window(), gfx::Rect(), 204 controller_->OnWindowBoundsChanged(root_window(), gfx::Rect(),
213 gfx::Rect(0, 205 gfx::Rect(0,
214 0, 206 0,
215 root_bounds.height(), 207 root_bounds.height(),
216 root_bounds.width())); 208 root_bounds.width()));
217 } 209 }
218 210
219 protected: 211 protected:
220 // KeyboardControllerObserver overrides 212 // KeyboardControllerObserver overrides
221 void OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) override { 213 void OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) override {
222 notified_bounds_ = new_bounds; 214 notified_bounds_ = new_bounds;
223 number_of_calls_++; 215 number_of_calls_++;
224 } 216 }
225 217
226 int number_of_calls() const { return number_of_calls_; } 218 int number_of_calls() const { return number_of_calls_; }
227 219
228 const gfx::Rect& notified_bounds() { return notified_bounds_; } 220 const gfx::Rect& notified_bounds() { return notified_bounds_; }
229 221
230 void SetFocus(ui::TextInputClient* client) { 222 void SetFocus(ui::TextInputClient* client) {
231 ui::InputMethod* input_method = proxy()->GetInputMethod(); 223 ui::InputMethod* input_method = ui()->GetInputMethod();
232 input_method->SetFocusedTextInputClient(client); 224 input_method->SetFocusedTextInputClient(client);
233 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) { 225 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) {
234 input_method->ShowImeIfNeeded(); 226 input_method->ShowImeIfNeeded();
235 if (proxy_->GetKeyboardWindow()->bounds().height() == 0) { 227 if (ui_->GetKeyboardWindow()->bounds().height() == 0) {
236 // Set initial bounds for test keyboard window. 228 // Set initial bounds for test keyboard window.
237 proxy_->GetKeyboardWindow()->SetBounds( 229 ui_->GetKeyboardWindow()->SetBounds(
238 FullWidthKeyboardBoundsFromRootBounds( 230 FullWidthKeyboardBoundsFromRootBounds(
239 root_window()->bounds(), kDefaultVirtualKeyboardHeight)); 231 root_window()->bounds(), kDefaultVirtualKeyboardHeight));
240 } 232 }
241 } 233 }
242 } 234 }
243 235
244 bool WillHideKeyboard() { 236 bool WillHideKeyboard() {
245 return controller_->WillHideKeyboard(); 237 return controller_->WillHideKeyboard();
246 } 238 }
247 239
248 bool ShouldEnableInsets(aura::Window* window) { 240 bool ShouldEnableInsets(aura::Window* window) {
249 return controller_->ShouldEnableInsets(window); 241 aura::Window* keyboard_window = ui_->GetKeyboardWindow();
242 return (keyboard_window->GetRootWindow() == window->GetRootWindow() &&
243 keyboard::IsKeyboardOverscrollEnabled() &&
244 keyboard_window->IsVisible() &&
245 controller_->keyboard_visible());
250 } 246 }
251 247
252 base::MessageLoopForUI message_loop_; 248 base::MessageLoopForUI message_loop_;
253 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_; 249 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
254 scoped_ptr<TestFocusController> focus_controller_; 250 scoped_ptr<TestFocusController> focus_controller_;
255 251
256 private: 252 private:
257 int number_of_calls_; 253 int number_of_calls_;
258 gfx::Rect notified_bounds_; 254 gfx::Rect notified_bounds_;
259 KeyboardControllerProxy* proxy_; 255 KeyboardUI* ui_;
260 scoped_ptr<KeyboardController> controller_; 256 scoped_ptr<KeyboardController> controller_;
261 scoped_ptr<ui::TextInputClient> test_text_input_client_; 257 scoped_ptr<ui::TextInputClient> test_text_input_client_;
262 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerTest); 258 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerTest);
263 }; 259 };
264 260
265 TEST_F(KeyboardControllerTest, KeyboardSize) { 261 TEST_F(KeyboardControllerTest, KeyboardSize) {
266 aura::Window* container(controller()->GetContainerWindow()); 262 aura::Window* container(controller()->GetContainerWindow());
267 aura::Window* keyboard(proxy()->GetKeyboardWindow()); 263 aura::Window* keyboard(ui()->GetKeyboardWindow());
268 gfx::Rect screen_bounds = root_window()->bounds(); 264 gfx::Rect screen_bounds = root_window()->bounds();
269 root_window()->AddChild(container); 265 root_window()->AddChild(container);
270 container->AddChild(keyboard); 266 container->AddChild(keyboard);
271 const gfx::Rect& initial_bounds = container->bounds(); 267 const gfx::Rect& initial_bounds = container->bounds();
272 // The container should be positioned at the bottom of screen and has 0 268 // The container should be positioned at the bottom of screen and has 0
273 // height. 269 // height.
274 ASSERT_EQ(gfx::Rect(), initial_bounds); 270 ASSERT_EQ(gfx::Rect(), initial_bounds);
275 VerifyKeyboardWindowSize(container, keyboard); 271 VerifyKeyboardWindowSize(container, keyboard);
276 272
277 273
(...skipping 18 matching lines...) Expand all
296 ASSERT_EQ(gfx::Rect(0, 292 ASSERT_EQ(gfx::Rect(0,
297 screen_bounds.width() - 50, 293 screen_bounds.width() - 50,
298 screen_bounds.height(), 294 screen_bounds.height(),
299 50), 295 50),
300 container->bounds()); 296 container->bounds());
301 VerifyKeyboardWindowSize(container, keyboard); 297 VerifyKeyboardWindowSize(container, keyboard);
302 } 298 }
303 299
304 TEST_F(KeyboardControllerTest, FloatingKeyboardSize) { 300 TEST_F(KeyboardControllerTest, FloatingKeyboardSize) {
305 aura::Window* container(controller()->GetContainerWindow()); 301 aura::Window* container(controller()->GetContainerWindow());
306 aura::Window* keyboard(proxy()->GetKeyboardWindow()); 302 aura::Window* keyboard(ui()->GetKeyboardWindow());
307 gfx::Rect screen_bounds = root_window()->bounds(); 303 gfx::Rect screen_bounds = root_window()->bounds();
308 root_window()->AddChild(container); 304 root_window()->AddChild(container);
309 controller()->SetKeyboardMode(FLOATING); 305 controller()->SetKeyboardMode(FLOATING);
310 container->AddChild(keyboard); 306 container->AddChild(keyboard);
311 gfx::Rect new_bounds(0, 50, 50, 50); 307 gfx::Rect new_bounds(0, 50, 50, 50);
312 keyboard->SetBounds(new_bounds); 308 keyboard->SetBounds(new_bounds);
313 ASSERT_EQ(new_bounds, container->bounds()); 309 ASSERT_EQ(new_bounds, container->bounds());
314 VerifyKeyboardWindowSize(container, keyboard); 310 VerifyKeyboardWindowSize(container, keyboard);
315 } 311 }
316 312
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 root_window()->AddChild(keyboard_container); 402 root_window()->AddChild(keyboard_container);
407 403
408 // Enable touch keyboard / overscroll mode to test insets. 404 // Enable touch keyboard / overscroll mode to test insets.
409 keyboard::SetTouchKeyboardEnabled(true); 405 keyboard::SetTouchKeyboardEnabled(true);
410 EXPECT_TRUE(keyboard::IsKeyboardOverscrollEnabled()); 406 EXPECT_TRUE(keyboard::IsKeyboardOverscrollEnabled());
411 407
412 SetFocus(&input_client); 408 SetFocus(&input_client);
413 SetFocus(&no_input_client); 409 SetFocus(&no_input_client);
414 // Insets should not be enabled for new windows while keyboard is in the 410 // Insets should not be enabled for new windows while keyboard is in the
415 // process of hiding when overscroll is enabled. 411 // process of hiding when overscroll is enabled.
416 EXPECT_FALSE(ShouldEnableInsets(proxy()->GetKeyboardWindow())); 412 EXPECT_FALSE(ShouldEnableInsets(ui()->GetKeyboardWindow()));
417 // Cancel keyboard hide. 413 // Cancel keyboard hide.
418 SetFocus(&input_client); 414 SetFocus(&input_client);
419 // Insets should be enabled for new windows as hide was cancelled. 415 // Insets should be enabled for new windows as hide was cancelled.
420 EXPECT_TRUE(ShouldEnableInsets(proxy()->GetKeyboardWindow())); 416 EXPECT_TRUE(ShouldEnableInsets(ui()->GetKeyboardWindow()));
421 } 417 }
422 418
423 // Verify switch to FLOATING mode will reset the overscroll or resize and when 419 // Verify switch to FLOATING mode will reset the overscroll or resize and when
424 // in FLOATING mode, overscroll or resize wont be triggered. 420 // in FLOATING mode, overscroll or resize wont be triggered.
425 TEST_F(KeyboardControllerTest, FloatingKeyboardDontOverscrollOrResize) { 421 TEST_F(KeyboardControllerTest, FloatingKeyboardDontOverscrollOrResize) {
426 ui::DummyTextInputClient input_client(ui::TEXT_INPUT_TYPE_TEXT); 422 ui::DummyTextInputClient input_client(ui::TEXT_INPUT_TYPE_TEXT);
427 ui::DummyTextInputClient no_input_client(ui::TEXT_INPUT_TYPE_NONE); 423 ui::DummyTextInputClient no_input_client(ui::TEXT_INPUT_TYPE_NONE);
428 424
429 aura::Window* container(controller()->GetContainerWindow()); 425 aura::Window* container(controller()->GetContainerWindow());
430 root_window()->AddChild(container); 426 root_window()->AddChild(container);
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 void TearDown() override { 532 void TearDown() override {
537 KeyboardControllerTest::TearDown(); 533 KeyboardControllerTest::TearDown();
538 } 534 }
539 535
540 protected: 536 protected:
541 aura::Window* keyboard_container() { 537 aura::Window* keyboard_container() {
542 return controller()->GetContainerWindow(); 538 return controller()->GetContainerWindow();
543 } 539 }
544 540
545 aura::Window* keyboard_window() { 541 aura::Window* keyboard_window() {
546 return proxy()->GetKeyboardWindow(); 542 return ui()->GetKeyboardWindow();
547 } 543 }
548 544
549 private: 545 private:
550 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerAnimationTest); 546 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerAnimationTest);
551 }; 547 };
552 548
553 // Tests virtual keyboard has correct show and hide animation. 549 // Tests virtual keyboard has correct show and hide animation.
554 TEST_F(KeyboardControllerAnimationTest, ContainerAnimation) { 550 TEST_F(KeyboardControllerAnimationTest, ContainerAnimation) {
555 ui::Layer* layer = keyboard_container()->layer(); 551 ui::Layer* layer = keyboard_container()->layer();
556 ShowKeyboard(); 552 ShowKeyboard();
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
607 // Before hide animation finishes, show keyboard again. 603 // Before hide animation finishes, show keyboard again.
608 ShowKeyboard(); 604 ShowKeyboard();
609 RunAnimationForLayer(layer); 605 RunAnimationForLayer(layer);
610 EXPECT_TRUE(keyboard_container()->IsVisible()); 606 EXPECT_TRUE(keyboard_container()->IsVisible());
611 EXPECT_TRUE(keyboard_window()->IsVisible()); 607 EXPECT_TRUE(keyboard_window()->IsVisible());
612 EXPECT_EQ(1.0, layer->opacity()); 608 EXPECT_EQ(1.0, layer->opacity());
613 EXPECT_EQ(gfx::Transform(), layer->transform()); 609 EXPECT_EQ(gfx::Transform(), layer->transform());
614 } 610 }
615 611
616 } // namespace keyboard 612 } // namespace keyboard
OLDNEW
« no previous file with comments | « ui/keyboard/keyboard_controller_proxy.cc ('k') | ui/keyboard/keyboard_layout_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698