| 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 "ui/keyboard/keyboard_controller.h" | 5 #include "ui/keyboard/keyboard_controller.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 base::RunLoop* const run_loop_; | 164 base::RunLoop* const run_loop_; |
| 165 | 165 |
| 166 DISALLOW_COPY_AND_ASSIGN(KeyboardContainerObserver); | 166 DISALLOW_COPY_AND_ASSIGN(KeyboardContainerObserver); |
| 167 }; | 167 }; |
| 168 | 168 |
| 169 } // namespace | 169 } // namespace |
| 170 | 170 |
| 171 class KeyboardControllerTest : public testing::Test, | 171 class KeyboardControllerTest : public testing::Test, |
| 172 public KeyboardControllerObserver { | 172 public KeyboardControllerObserver { |
| 173 public: | 173 public: |
| 174 KeyboardControllerTest() : number_of_calls_(0), ui_(nullptr) {} | 174 KeyboardControllerTest() |
| 175 : number_of_calls_(0), ui_(nullptr), keyboard_closed_(false) {} |
| 175 ~KeyboardControllerTest() override {} | 176 ~KeyboardControllerTest() override {} |
| 176 | 177 |
| 177 void SetUp() override { | 178 void SetUp() override { |
| 178 // The ContextFactory must exist before any Compositors are created. | 179 // The ContextFactory must exist before any Compositors are created. |
| 179 bool enable_pixel_output = false; | 180 bool enable_pixel_output = false; |
| 180 ui::ContextFactory* context_factory = | 181 ui::ContextFactory* context_factory = |
| 181 ui::InitializeContextFactoryForTests(enable_pixel_output); | 182 ui::InitializeContextFactoryForTests(enable_pixel_output); |
| 182 | 183 |
| 183 ui::SetUpInputMethodFactoryForTesting(); | 184 ui::SetUpInputMethodFactoryForTesting(); |
| 184 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_)); | 185 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_)); |
| 185 aura_test_helper_->SetUp(context_factory); | 186 aura_test_helper_->SetUp(context_factory); |
| 186 new wm::DefaultActivationClient(aura_test_helper_->root_window()); | 187 new wm::DefaultActivationClient(aura_test_helper_->root_window()); |
| 187 focus_controller_.reset(new TestFocusController(root_window())); | 188 focus_controller_.reset(new TestFocusController(root_window())); |
| 188 ui_ = new TestKeyboardUI(aura_test_helper_->host()->GetInputMethod()); | 189 ui_ = new TestKeyboardUI(aura_test_helper_->host()->GetInputMethod()); |
| 189 controller_.reset(new KeyboardController(ui_)); | 190 controller_.reset(new KeyboardController(ui_)); |
| 190 controller()->AddObserver(this); | 191 controller()->AddObserver(this); |
| 191 } | 192 } |
| 192 | 193 |
| 193 void TearDown() override { | 194 void TearDown() override { |
| 194 controller()->RemoveObserver(this); | 195 if (controller()) |
| 196 controller()->RemoveObserver(this); |
| 195 controller_.reset(); | 197 controller_.reset(); |
| 196 focus_controller_.reset(); | 198 focus_controller_.reset(); |
| 197 aura_test_helper_->TearDown(); | 199 aura_test_helper_->TearDown(); |
| 198 ui::TerminateContextFactoryForTests(); | 200 ui::TerminateContextFactoryForTests(); |
| 199 } | 201 } |
| 200 | 202 |
| 201 aura::Window* root_window() { return aura_test_helper_->root_window(); } | 203 aura::Window* root_window() { return aura_test_helper_->root_window(); } |
| 202 KeyboardUI* ui() { return ui_; } | 204 KeyboardUI* ui() { return ui_; } |
| 203 KeyboardController* controller() { return controller_.get(); } | 205 KeyboardController* controller() { return controller_.get(); } |
| 204 | 206 |
| 205 void ShowKeyboard() { | 207 void ShowKeyboard() { |
| 206 test_text_input_client_.reset( | 208 test_text_input_client_.reset( |
| 207 new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT)); | 209 new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT)); |
| 208 SetFocus(test_text_input_client_.get()); | 210 SetFocus(test_text_input_client_.get()); |
| 209 } | 211 } |
| 210 | 212 |
| 211 void MockRotateScreen() { | 213 void MockRotateScreen() { |
| 212 const gfx::Rect root_bounds = root_window()->bounds(); | 214 const gfx::Rect root_bounds = root_window()->bounds(); |
| 213 root_window()->SetBounds( | 215 root_window()->SetBounds( |
| 214 gfx::Rect(0, 0, root_bounds.height(), root_bounds.width())); | 216 gfx::Rect(0, 0, root_bounds.height(), root_bounds.width())); |
| 215 } | 217 } |
| 216 | 218 |
| 217 protected: | 219 protected: |
| 218 // KeyboardControllerObserver overrides | 220 // KeyboardControllerObserver overrides |
| 219 void OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) override { | 221 void OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) override { |
| 220 notified_bounds_ = new_bounds; | 222 notified_bounds_ = new_bounds; |
| 221 number_of_calls_++; | 223 number_of_calls_++; |
| 222 } | 224 } |
| 225 void OnKeyboardClosed() override { keyboard_closed_ = true; } |
| 223 | 226 |
| 224 int number_of_calls() const { return number_of_calls_; } | 227 int number_of_calls() const { return number_of_calls_; } |
| 225 | 228 |
| 226 const gfx::Rect& notified_bounds() { return notified_bounds_; } | 229 const gfx::Rect& notified_bounds() { return notified_bounds_; } |
| 227 | 230 |
| 231 bool IsKeyboardClosed() { return keyboard_closed_; } |
| 232 |
| 228 void SetFocus(ui::TextInputClient* client) { | 233 void SetFocus(ui::TextInputClient* client) { |
| 229 ui::InputMethod* input_method = ui()->GetInputMethod(); | 234 ui::InputMethod* input_method = ui()->GetInputMethod(); |
| 230 input_method->SetFocusedTextInputClient(client); | 235 input_method->SetFocusedTextInputClient(client); |
| 231 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) { | 236 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) { |
| 232 input_method->ShowImeIfNeeded(); | 237 input_method->ShowImeIfNeeded(); |
| 233 if (ui_->GetKeyboardWindow()->bounds().height() == 0) { | 238 if (ui_->GetKeyboardWindow()->bounds().height() == 0) { |
| 234 // Set initial bounds for test keyboard window. | 239 // Set initial bounds for test keyboard window. |
| 235 ui_->GetKeyboardWindow()->SetBounds( | 240 ui_->GetKeyboardWindow()->SetBounds( |
| 236 FullWidthKeyboardBoundsFromRootBounds( | 241 FullWidthKeyboardBoundsFromRootBounds( |
| 237 root_window()->bounds(), kDefaultVirtualKeyboardHeight)); | 242 root_window()->bounds(), kDefaultVirtualKeyboardHeight)); |
| 238 } | 243 } |
| 239 } | 244 } |
| 240 } | 245 } |
| 241 | 246 |
| 242 bool WillHideKeyboard() { | 247 bool WillHideKeyboard() { |
| 243 return controller_->WillHideKeyboard(); | 248 return controller_->WillHideKeyboard(); |
| 244 } | 249 } |
| 245 | 250 |
| 246 bool ShouldEnableInsets(aura::Window* window) { | 251 bool ShouldEnableInsets(aura::Window* window) { |
| 247 aura::Window* keyboard_window = ui_->GetKeyboardWindow(); | 252 aura::Window* keyboard_window = ui_->GetKeyboardWindow(); |
| 248 return (keyboard_window->GetRootWindow() == window->GetRootWindow() && | 253 return (keyboard_window->GetRootWindow() == window->GetRootWindow() && |
| 249 keyboard::IsKeyboardOverscrollEnabled() && | 254 keyboard::IsKeyboardOverscrollEnabled() && |
| 250 keyboard_window->IsVisible() && | 255 keyboard_window->IsVisible() && |
| 251 controller_->keyboard_visible()); | 256 controller_->keyboard_visible()); |
| 252 } | 257 } |
| 253 | 258 |
| 259 void ResetController() { controller_.reset(); } |
| 260 |
| 254 base::MessageLoopForUI message_loop_; | 261 base::MessageLoopForUI message_loop_; |
| 255 std::unique_ptr<aura::test::AuraTestHelper> aura_test_helper_; | 262 std::unique_ptr<aura::test::AuraTestHelper> aura_test_helper_; |
| 256 std::unique_ptr<TestFocusController> focus_controller_; | 263 std::unique_ptr<TestFocusController> focus_controller_; |
| 257 | 264 |
| 258 private: | 265 private: |
| 259 int number_of_calls_; | 266 int number_of_calls_; |
| 260 gfx::Rect notified_bounds_; | 267 gfx::Rect notified_bounds_; |
| 261 KeyboardUI* ui_; | 268 KeyboardUI* ui_; |
| 262 std::unique_ptr<KeyboardController> controller_; | 269 std::unique_ptr<KeyboardController> controller_; |
| 263 std::unique_ptr<ui::TextInputClient> test_text_input_client_; | 270 std::unique_ptr<ui::TextInputClient> test_text_input_client_; |
| 271 bool keyboard_closed_; |
| 264 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerTest); | 272 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerTest); |
| 265 }; | 273 }; |
| 266 | 274 |
| 267 TEST_F(KeyboardControllerTest, KeyboardSize) { | 275 TEST_F(KeyboardControllerTest, KeyboardSize) { |
| 268 aura::Window* container(controller()->GetContainerWindow()); | 276 aura::Window* container(controller()->GetContainerWindow()); |
| 269 aura::Window* keyboard(ui()->GetKeyboardWindow()); | 277 aura::Window* keyboard(ui()->GetKeyboardWindow()); |
| 270 gfx::Rect screen_bounds = root_window()->bounds(); | 278 gfx::Rect screen_bounds = root_window()->bounds(); |
| 271 root_window()->AddChild(container); | 279 root_window()->AddChild(container); |
| 272 container->AddChild(keyboard); | 280 container->AddChild(keyboard); |
| 273 const gfx::Rect& initial_bounds = container->bounds(); | 281 const gfx::Rect& initial_bounds = container->bounds(); |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 // Keyboard should hide when focus on no input client. | 528 // Keyboard should hide when focus on no input client. |
| 521 SetFocus(&no_input_client_1); | 529 SetFocus(&no_input_client_1); |
| 522 EXPECT_TRUE(WillHideKeyboard()); | 530 EXPECT_TRUE(WillHideKeyboard()); |
| 523 | 531 |
| 524 // Wait for hide keyboard to finish. | 532 // Wait for hide keyboard to finish. |
| 525 run_loop.Run(); | 533 run_loop.Run(); |
| 526 EXPECT_FALSE(keyboard_container->IsVisible()); | 534 EXPECT_FALSE(keyboard_container->IsVisible()); |
| 527 keyboard::SetAccessibilityKeyboardEnabled(false); | 535 keyboard::SetAccessibilityKeyboardEnabled(false); |
| 528 } | 536 } |
| 529 | 537 |
| 538 // Tests that deactivates keyboard will get closed event. |
| 539 TEST_F(KeyboardControllerTest, CloseKeyboard) { |
| 540 keyboard::SetAccessibilityKeyboardEnabled(true); |
| 541 aura::Window* keyboard_container(controller()->GetContainerWindow()); |
| 542 root_window()->AddChild(keyboard_container); |
| 543 keyboard_container->Show(); |
| 544 |
| 545 ShowKeyboard(); |
| 546 EXPECT_TRUE(keyboard_container->IsVisible()); |
| 547 EXPECT_FALSE(IsKeyboardClosed()); |
| 548 |
| 549 root_window()->RemoveChild(keyboard_container); |
| 550 ResetController(); |
| 551 EXPECT_TRUE(IsKeyboardClosed()); |
| 552 } |
| 553 |
| 530 class KeyboardControllerAnimationTest : public KeyboardControllerTest { | 554 class KeyboardControllerAnimationTest : public KeyboardControllerTest { |
| 531 public: | 555 public: |
| 532 KeyboardControllerAnimationTest() {} | 556 KeyboardControllerAnimationTest() {} |
| 533 ~KeyboardControllerAnimationTest() override {} | 557 ~KeyboardControllerAnimationTest() override {} |
| 534 | 558 |
| 535 void SetUp() override { | 559 void SetUp() override { |
| 536 // We cannot short-circuit animations for this test. | 560 // We cannot short-circuit animations for this test. |
| 537 ui::ScopedAnimationDurationScaleMode test_duration_mode( | 561 ui::ScopedAnimationDurationScaleMode test_duration_mode( |
| 538 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION); | 562 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION); |
| 539 | 563 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 gfx::Rect new_bounds(0, 0, 1280, 800); | 682 gfx::Rect new_bounds(0, 0, 1280, 800); |
| 659 ASSERT_NE(new_bounds, root_window()->bounds()); | 683 ASSERT_NE(new_bounds, root_window()->bounds()); |
| 660 EXPECT_EQ(1, number_of_calls()); | 684 EXPECT_EQ(1, number_of_calls()); |
| 661 root_window()->SetBounds(new_bounds); | 685 root_window()->SetBounds(new_bounds); |
| 662 EXPECT_EQ(2, number_of_calls()); | 686 EXPECT_EQ(2, number_of_calls()); |
| 663 MockRotateScreen(); | 687 MockRotateScreen(); |
| 664 EXPECT_EQ(3, number_of_calls()); | 688 EXPECT_EQ(3, number_of_calls()); |
| 665 } | 689 } |
| 666 | 690 |
| 667 } // namespace keyboard | 691 } // namespace keyboard |
| OLD | NEW |