| OLD | NEW |
| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "ui/aura/client/focus_client.h" | 10 #include "ui/aura/client/focus_client.h" |
| 11 #include "ui/aura/layout_manager.h" | 11 #include "ui/aura/layout_manager.h" |
| 12 #include "ui/aura/test/aura_test_helper.h" | 12 #include "ui/aura/test/aura_test_helper.h" |
| 13 #include "ui/aura/test/event_generator.h" | 13 #include "ui/aura/test/event_generator.h" |
| 14 #include "ui/aura/test/test_window_delegate.h" | 14 #include "ui/aura/test/test_window_delegate.h" |
| 15 #include "ui/aura/window.h" | 15 #include "ui/aura/window.h" |
| 16 #include "ui/aura/window_event_dispatcher.h" | 16 #include "ui/aura/window_event_dispatcher.h" |
| 17 #include "ui/base/ime/dummy_text_input_client.h" | 17 #include "ui/base/ime/dummy_text_input_client.h" |
| 18 #include "ui/base/ime/input_method.h" | 18 #include "ui/base/ime/input_method.h" |
| 19 #include "ui/base/ime/input_method_factory.h" | 19 #include "ui/base/ime/input_method_factory.h" |
| 20 #include "ui/base/ime/text_input_client.h" | 20 #include "ui/base/ime/text_input_client.h" |
| 21 #include "ui/compositor/layer_type.h" | 21 #include "ui/compositor/layer_type.h" |
| 22 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | 22 #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
| 23 #include "ui/compositor/test/context_factories_for_test.h" | 23 #include "ui/compositor/test/context_factories_for_test.h" |
| 24 #include "ui/compositor/test/layer_animator_test_controller.h" | 24 #include "ui/compositor/test/layer_animator_test_controller.h" |
| 25 #include "ui/gfx/rect.h" | 25 #include "ui/gfx/rect.h" |
| 26 #include "ui/keyboard/keyboard_controller.h" | 26 #include "ui/keyboard/keyboard_controller.h" |
| 27 #include "ui/keyboard/keyboard_controller_observer.h" | 27 #include "ui/keyboard/keyboard_controller_observer.h" |
| 28 #include "ui/keyboard/keyboard_controller_proxy.h" | 28 #include "ui/keyboard/keyboard_controller_proxy.h" |
| 29 #include "ui/keyboard/keyboard_switches.h" | 29 #include "ui/keyboard/keyboard_switches.h" |
| 30 #include "ui/keyboard/keyboard_util.h" |
| 30 | 31 |
| 31 namespace keyboard { | 32 namespace keyboard { |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 // Steps a layer animation until it is completed. Animations must be enabled. | 35 // Steps a layer animation until it is completed. Animations must be enabled. |
| 35 void RunAnimationForLayer(ui::Layer* layer) { | 36 void RunAnimationForLayer(ui::Layer* layer) { |
| 36 // Animations must be enabled for stepping to work. | 37 // Animations must be enabled for stepping to work. |
| 37 ASSERT_NE(ui::ScopedAnimationDurationScaleMode::duration_scale_mode(), | 38 ASSERT_NE(ui::ScopedAnimationDurationScaleMode::duration_scale_mode(), |
| 38 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION); | 39 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION); |
| 39 | 40 |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 187 |
| 187 void ShowKeyboard() { | 188 void ShowKeyboard() { |
| 188 ui::DummyTextInputClient test_text_input_client(ui::TEXT_INPUT_TYPE_TEXT); | 189 ui::DummyTextInputClient test_text_input_client(ui::TEXT_INPUT_TYPE_TEXT); |
| 189 SetFocus(&test_text_input_client); | 190 SetFocus(&test_text_input_client); |
| 190 } | 191 } |
| 191 | 192 |
| 192 protected: | 193 protected: |
| 193 void SetFocus(ui::TextInputClient* client) { | 194 void SetFocus(ui::TextInputClient* client) { |
| 194 ui::InputMethod* input_method = proxy()->GetInputMethod(); | 195 ui::InputMethod* input_method = proxy()->GetInputMethod(); |
| 195 input_method->SetFocusedTextInputClient(client); | 196 input_method->SetFocusedTextInputClient(client); |
| 196 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) | 197 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) { |
| 197 input_method->ShowImeIfNeeded(); | 198 input_method->ShowImeIfNeeded(); |
| 199 if (proxy_->GetKeyboardWindow()->bounds().height() == 0) { |
| 200 // Set initial bounds for test keyboard window. |
| 201 proxy_->GetKeyboardWindow()->SetBounds( |
| 202 KeyboardBoundsFromWindowBounds( |
| 203 controller()->GetContainerWindow()->bounds(), 100)); |
| 204 } |
| 205 } |
| 198 } | 206 } |
| 199 | 207 |
| 200 bool WillHideKeyboard() { | 208 bool WillHideKeyboard() { |
| 201 return controller_->WillHideKeyboard(); | 209 return controller_->WillHideKeyboard(); |
| 202 } | 210 } |
| 203 | 211 |
| 204 base::MessageLoopForUI message_loop_; | 212 base::MessageLoopForUI message_loop_; |
| 205 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_; | 213 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_; |
| 206 scoped_ptr<TestFocusController> focus_controller_; | 214 scoped_ptr<TestFocusController> focus_controller_; |
| 207 | 215 |
| 208 private: | 216 private: |
| 209 KeyboardControllerProxy* proxy_; | 217 KeyboardControllerProxy* proxy_; |
| 210 scoped_ptr<KeyboardController> controller_; | 218 scoped_ptr<KeyboardController> controller_; |
| 211 | 219 |
| 212 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerTest); | 220 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerTest); |
| 213 }; | 221 }; |
| 214 | 222 |
| 215 TEST_F(KeyboardControllerTest, KeyboardSize) { | 223 TEST_F(KeyboardControllerTest, KeyboardSize) { |
| 216 aura::Window* container(controller()->GetContainerWindow()); | 224 aura::Window* container(controller()->GetContainerWindow()); |
| 217 gfx::Rect bounds(0, 0, 100, 100); | 225 aura::Window* keyboard(proxy()->GetKeyboardWindow()); |
| 218 container->SetBounds(bounds); | 226 container->SetBounds(gfx::Rect(0, 0, 200, 100)); |
| 219 | 227 |
| 220 const gfx::Rect& before_bounds = proxy()->GetKeyboardWindow()->bounds(); | 228 container->AddChild(keyboard); |
| 229 const gfx::Rect& before_bounds = keyboard->bounds(); |
| 230 // The initial keyboard should be positioned at the bottom of container and |
| 231 // has 0 height. |
| 232 ASSERT_EQ(gfx::Rect(0, 100, 200, 0), before_bounds); |
| 233 |
| 221 gfx::Rect new_bounds( | 234 gfx::Rect new_bounds( |
| 222 before_bounds.x(), before_bounds.y(), | 235 before_bounds.x(), before_bounds.y() - 50, |
| 223 before_bounds.width() / 2, before_bounds.height() / 2); | 236 before_bounds.width(), 50); |
| 224 | 237 |
| 225 // The KeyboardController's LayoutManager shouldn't let this happen | 238 keyboard->SetBounds(new_bounds); |
| 226 proxy()->GetKeyboardWindow()->SetBounds(new_bounds); | 239 ASSERT_EQ(new_bounds, keyboard->bounds()); |
| 227 ASSERT_EQ(before_bounds, proxy()->GetKeyboardWindow()->bounds()); | 240 |
| 241 // Mock a screen rotation. |
| 242 container->SetBounds(gfx::Rect(0, 0, 100, 200)); |
| 243 // The above call should resize keyboard to new width while keeping the old |
| 244 // height. |
| 245 ASSERT_EQ(gfx::Rect(0, 150, 100, 50), keyboard->bounds()); |
| 228 } | 246 } |
| 229 | 247 |
| 230 // Tests that tapping/clicking inside the keyboard does not give it focus. | 248 // Tests that tapping/clicking inside the keyboard does not give it focus. |
| 231 TEST_F(KeyboardControllerTest, ClickDoesNotFocusKeyboard) { | 249 TEST_F(KeyboardControllerTest, ClickDoesNotFocusKeyboard) { |
| 232 const gfx::Rect& root_bounds = root_window()->bounds(); | 250 const gfx::Rect& root_bounds = root_window()->bounds(); |
| 233 aura::test::EventCountDelegate delegate; | 251 aura::test::EventCountDelegate delegate; |
| 234 scoped_ptr<aura::Window> window(new aura::Window(&delegate)); | 252 scoped_ptr<aura::Window> window(new aura::Window(&delegate)); |
| 235 window->Init(aura::WINDOW_LAYER_NOT_DRAWN); | 253 window->Init(aura::WINDOW_LAYER_NOT_DRAWN); |
| 236 window->SetBounds(root_bounds); | 254 window->SetBounds(root_bounds); |
| 237 root_window()->AddChild(window.get()); | 255 root_window()->AddChild(window.get()); |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 | 404 |
| 387 // Keyboard should hide when focus on no input client. | 405 // Keyboard should hide when focus on no input client. |
| 388 SetFocus(&no_input_client_1); | 406 SetFocus(&no_input_client_1); |
| 389 EXPECT_TRUE(WillHideKeyboard()); | 407 EXPECT_TRUE(WillHideKeyboard()); |
| 390 | 408 |
| 391 // Wait for hide keyboard to finish. | 409 // Wait for hide keyboard to finish. |
| 392 base::MessageLoop::current()->Run(); | 410 base::MessageLoop::current()->Run(); |
| 393 EXPECT_FALSE(keyboard_container->IsVisible()); | 411 EXPECT_FALSE(keyboard_container->IsVisible()); |
| 394 } | 412 } |
| 395 | 413 |
| 396 TEST_F(KeyboardControllerTest, KeyboardResizingFromContents) { | |
| 397 aura::Window* keyboard_container = controller()->GetContainerWindow(); | |
| 398 aura::Window* keyboard_window = proxy()->GetKeyboardWindow(); | |
| 399 keyboard_container->SetBounds(gfx::Rect(800, 600)); | |
| 400 keyboard_container->AddChild(keyboard_window); | |
| 401 | |
| 402 int original_height = keyboard_window->bounds().height(); | |
| 403 | |
| 404 // Resizes from contents when flag is unset. | |
| 405 keyboard_window->SetBounds(gfx::Rect(100, 80)); | |
| 406 EXPECT_EQ(original_height, keyboard_window->bounds().height()); | |
| 407 | |
| 408 // Resizes from contents when flag is set. | |
| 409 proxy()->set_resizing_from_contents(true); | |
| 410 keyboard_window->SetBounds(gfx::Rect(100, 80)); | |
| 411 EXPECT_EQ(80, keyboard_window->bounds().height()); | |
| 412 | |
| 413 // Resizes from container when flag is set. | |
| 414 keyboard_container->SetBounds(gfx::Rect(400, 300)); | |
| 415 EXPECT_EQ(80, keyboard_window->bounds().height()); | |
| 416 | |
| 417 // Resizes from container when flag is unset. | |
| 418 proxy()->set_resizing_from_contents(false); | |
| 419 keyboard_container->SetBounds(gfx::Rect(800, 600)); | |
| 420 EXPECT_EQ(original_height, keyboard_window->bounds().height()); | |
| 421 } | |
| 422 | |
| 423 class KeyboardControllerAnimationTest : public KeyboardControllerTest, | 414 class KeyboardControllerAnimationTest : public KeyboardControllerTest, |
| 424 public KeyboardControllerObserver { | 415 public KeyboardControllerObserver { |
| 425 public: | 416 public: |
| 426 KeyboardControllerAnimationTest() {} | 417 KeyboardControllerAnimationTest() {} |
| 427 virtual ~KeyboardControllerAnimationTest() {} | 418 virtual ~KeyboardControllerAnimationTest() {} |
| 428 | 419 |
| 429 virtual void SetUp() OVERRIDE { | 420 virtual void SetUp() OVERRIDE { |
| 430 // We cannot short-circuit animations for this test. | 421 // We cannot short-circuit animations for this test. |
| 431 ui::ScopedAnimationDurationScaleMode normal_duration_mode( | 422 ui::ScopedAnimationDurationScaleMode normal_duration_mode( |
| 432 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION); | 423 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 SetFocus(&input_client); | 548 SetFocus(&input_client); |
| 558 EXPECT_TRUE(keyboard_container->IsVisible()); | 549 EXPECT_TRUE(keyboard_container->IsVisible()); |
| 559 | 550 |
| 560 SetFocus(&no_input_client); | 551 SetFocus(&no_input_client); |
| 561 // Keyboard should not hide itself after lost focus. | 552 // Keyboard should not hide itself after lost focus. |
| 562 EXPECT_TRUE(keyboard_container->IsVisible()); | 553 EXPECT_TRUE(keyboard_container->IsVisible()); |
| 563 EXPECT_FALSE(WillHideKeyboard()); | 554 EXPECT_FALSE(WillHideKeyboard()); |
| 564 } | 555 } |
| 565 | 556 |
| 566 } // namespace keyboard | 557 } // namespace keyboard |
| OLD | NEW |