Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / keyboard / keyboard_controller_unittest.cc
blob52d7608b831bcbfe036f6db9860d069cfd7e2afd
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
3 // found in the LICENSE file.
5 #include "ui/keyboard/keyboard_controller.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/aura/client/focus_client.h"
13 #include "ui/aura/layout_manager.h"
14 #include "ui/aura/test/aura_test_helper.h"
15 #include "ui/aura/test/test_window_delegate.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/base/ime/dummy_text_input_client.h"
19 #include "ui/base/ime/input_method.h"
20 #include "ui/base/ime/input_method_factory.h"
21 #include "ui/base/ime/text_input_client.h"
22 #include "ui/base/ime/text_input_focus_manager.h"
23 #include "ui/base/ui_base_switches_util.h"
24 #include "ui/compositor/compositor.h"
25 #include "ui/compositor/layer_type.h"
26 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
27 #include "ui/compositor/test/context_factories_for_test.h"
28 #include "ui/compositor/test/layer_animator_test_controller.h"
29 #include "ui/events/event_utils.h"
30 #include "ui/events/test/event_generator.h"
31 #include "ui/gfx/geometry/rect.h"
32 #include "ui/keyboard/keyboard_controller_observer.h"
33 #include "ui/keyboard/keyboard_controller_proxy.h"
34 #include "ui/keyboard/keyboard_util.h"
35 #include "ui/wm/core/default_activation_client.h"
37 namespace keyboard {
38 namespace {
40 // Verify if the |keyboard| window covers the |container| window completely.
41 void VerifyKeyboardWindowSize(aura::Window* container, aura::Window* keyboard) {
42 ASSERT_EQ(gfx::Rect(0, 0, container->bounds().width(),
43 container->bounds().height()),
44 keyboard->bounds());
47 // Steps a layer animation until it is completed. Animations must be enabled.
48 void RunAnimationForLayer(ui::Layer* layer) {
49 // Animations must be enabled for stepping to work.
50 ASSERT_NE(ui::ScopedAnimationDurationScaleMode::duration_scale_mode(),
51 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
53 ui::LayerAnimatorTestController controller(layer->GetAnimator());
54 // Multiple steps are required to complete complex animations.
55 // TODO(vollick): This should not be necessary. crbug.com/154017
56 while (controller.animator()->is_animating()) {
57 controller.StartThreadedAnimationsIfNeeded();
58 base::TimeTicks step_time = controller.animator()->last_step_time();
59 controller.animator()->Step(step_time +
60 base::TimeDelta::FromMilliseconds(1000));
64 // An event handler that focuses a window when it is clicked/touched on. This is
65 // used to match the focus manger behaviour in ash and views.
66 class TestFocusController : public ui::EventHandler {
67 public:
68 explicit TestFocusController(aura::Window* root)
69 : root_(root) {
70 root_->AddPreTargetHandler(this);
73 ~TestFocusController() override { root_->RemovePreTargetHandler(this); }
75 private:
76 // Overridden from ui::EventHandler:
77 void OnEvent(ui::Event* event) override {
78 aura::Window* target = static_cast<aura::Window*>(event->target());
79 if (event->type() == ui::ET_MOUSE_PRESSED ||
80 event->type() == ui::ET_TOUCH_PRESSED) {
81 aura::client::GetFocusClient(target)->FocusWindow(target);
85 aura::Window* root_;
86 DISALLOW_COPY_AND_ASSIGN(TestFocusController);
89 class TestKeyboardControllerProxy : public KeyboardControllerProxy {
90 public:
91 TestKeyboardControllerProxy()
92 : KeyboardControllerProxy(nullptr),
93 input_method_(
94 ui::CreateInputMethod(nullptr, gfx::kNullAcceleratedWidget)) {}
96 ~TestKeyboardControllerProxy() override {
97 // Destroy the window before the delegate.
98 window_.reset();
101 // Overridden from KeyboardControllerProxy:
102 bool HasKeyboardWindow() const override { return window_; }
103 aura::Window* GetKeyboardWindow() override {
104 if (!window_) {
105 window_.reset(new aura::Window(&delegate_));
106 window_->Init(ui::LAYER_NOT_DRAWN);
107 window_->set_owned_by_parent(false);
109 return window_.get();
111 ui::InputMethod* GetInputMethod() override { return input_method_.get(); }
112 void RequestAudioInput(
113 content::WebContents* web_contents,
114 const content::MediaStreamRequest& request,
115 const content::MediaResponseCallback& callback) override {
116 return;
118 void LoadSystemKeyboard() override{};
119 void ReloadKeyboardIfNeeded() override{};
121 private:
122 scoped_ptr<aura::Window> window_;
123 aura::test::TestWindowDelegate delegate_;
124 scoped_ptr<ui::InputMethod> input_method_;
126 DISALLOW_COPY_AND_ASSIGN(TestKeyboardControllerProxy);
129 // Keeps a count of all the events a window receives.
130 class EventObserver : public ui::EventHandler {
131 public:
132 EventObserver() {}
133 ~EventObserver() override {}
135 int GetEventCount(ui::EventType type) {
136 return event_counts_[type];
139 private:
140 // Overridden from ui::EventHandler:
141 void OnEvent(ui::Event* event) override {
142 ui::EventHandler::OnEvent(event);
143 event_counts_[event->type()]++;
146 std::map<ui::EventType, int> event_counts_;
147 DISALLOW_COPY_AND_ASSIGN(EventObserver);
150 class KeyboardContainerObserver : public aura::WindowObserver {
151 public:
152 explicit KeyboardContainerObserver(aura::Window* window) : window_(window) {
153 window_->AddObserver(this);
155 ~KeyboardContainerObserver() override { window_->RemoveObserver(this); }
157 private:
158 void OnWindowVisibilityChanged(aura::Window* window, bool visible) override {
159 if (!visible)
160 base::MessageLoop::current()->Quit();
163 aura::Window* window_;
165 DISALLOW_COPY_AND_ASSIGN(KeyboardContainerObserver);
168 } // namespace
170 class KeyboardControllerTest : public testing::Test {
171 public:
172 KeyboardControllerTest() {}
173 ~KeyboardControllerTest() override {}
175 void SetUp() override {
176 // The ContextFactory must exist before any Compositors are created.
177 bool enable_pixel_output = false;
178 ui::ContextFactory* context_factory =
179 ui::InitializeContextFactoryForTests(enable_pixel_output);
181 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_));
182 aura_test_helper_->SetUp(context_factory);
183 new wm::DefaultActivationClient(aura_test_helper_->root_window());
184 ui::SetUpInputMethodFactoryForTesting();
185 if (::switches::IsTextInputFocusManagerEnabled())
186 ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(NULL);
187 focus_controller_.reset(new TestFocusController(root_window()));
188 proxy_ = new TestKeyboardControllerProxy();
189 controller_.reset(new KeyboardController(proxy_));
192 void TearDown() override {
193 controller_.reset();
194 focus_controller_.reset();
195 if (::switches::IsTextInputFocusManagerEnabled())
196 ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(NULL);
197 aura_test_helper_->TearDown();
198 ui::TerminateContextFactoryForTests();
201 aura::Window* root_window() { return aura_test_helper_->root_window(); }
202 KeyboardControllerProxy* proxy() { return proxy_; }
203 KeyboardController* controller() { return controller_.get(); }
205 void ShowKeyboard() {
206 test_text_input_client_.reset(
207 new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT));
208 SetFocus(test_text_input_client_.get());
211 void MockRotateScreen() {
212 const gfx::Rect root_bounds = root_window()->bounds();
213 controller_->OnWindowBoundsChanged(root_window(), gfx::Rect(),
214 gfx::Rect(0,
216 root_bounds.height(),
217 root_bounds.width()));
220 protected:
221 void SetFocus(ui::TextInputClient* client) {
222 ui::InputMethod* input_method = proxy()->GetInputMethod();
223 if (::switches::IsTextInputFocusManagerEnabled()) {
224 ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(client);
225 input_method->OnTextInputTypeChanged(client);
226 } else {
227 input_method->SetFocusedTextInputClient(client);
229 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) {
230 input_method->ShowImeIfNeeded();
231 if (proxy_->GetKeyboardWindow()->bounds().height() == 0) {
232 // Set initial bounds for test keyboard window.
233 proxy_->GetKeyboardWindow()->SetBounds(
234 FullWidthKeyboardBoundsFromRootBounds(root_window()->bounds(),
235 100));
240 bool WillHideKeyboard() {
241 return controller_->WillHideKeyboard();
244 bool ShouldEnableInsets(aura::Window* window) {
245 return controller_->ShouldEnableInsets(window);
248 base::MessageLoopForUI message_loop_;
249 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
250 scoped_ptr<TestFocusController> focus_controller_;
252 private:
253 KeyboardControllerProxy* proxy_;
254 scoped_ptr<KeyboardController> controller_;
255 scoped_ptr<ui::TextInputClient> test_text_input_client_;
256 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerTest);
259 TEST_F(KeyboardControllerTest, KeyboardSize) {
260 aura::Window* container(controller()->GetContainerWindow());
261 aura::Window* keyboard(proxy()->GetKeyboardWindow());
262 gfx::Rect screen_bounds = root_window()->bounds();
263 root_window()->AddChild(container);
264 container->AddChild(keyboard);
265 const gfx::Rect& initial_bounds = container->bounds();
266 // The container should be positioned at the bottom of screen and has 0
267 // height.
268 ASSERT_EQ(gfx::Rect(), initial_bounds);
269 VerifyKeyboardWindowSize(container, keyboard);
271 gfx::Rect new_bounds(0, 0, 50, 50);
273 // In FULL_WIDTH mode, attempt to change window width or move window up from
274 // the bottom are ignored. Changing window height is supported.
275 gfx::Rect expected_bounds(0,
276 screen_bounds.height() - 50,
277 screen_bounds.width(),
278 50);
280 keyboard->SetBounds(new_bounds);
281 ASSERT_EQ(expected_bounds, container->bounds());
282 VerifyKeyboardWindowSize(container, keyboard);
284 MockRotateScreen();
285 // The above call should resize keyboard to new width while keeping the old
286 // height.
287 ASSERT_EQ(gfx::Rect(0,
288 screen_bounds.width() - 50,
289 screen_bounds.height(),
290 50),
291 container->bounds());
292 VerifyKeyboardWindowSize(container, keyboard);
295 TEST_F(KeyboardControllerTest, FloatingKeyboardSize) {
296 aura::Window* container(controller()->GetContainerWindow());
297 aura::Window* keyboard(proxy()->GetKeyboardWindow());
298 gfx::Rect screen_bounds = root_window()->bounds();
299 root_window()->AddChild(container);
300 controller()->SetKeyboardMode(FLOATING);
301 container->AddChild(keyboard);
302 gfx::Rect new_bounds(0, 50, 50, 50);
303 keyboard->SetBounds(new_bounds);
304 ASSERT_EQ(new_bounds, container->bounds());
305 VerifyKeyboardWindowSize(container, keyboard);
308 // Tests that tapping/clicking inside the keyboard does not give it focus.
309 TEST_F(KeyboardControllerTest, ClickDoesNotFocusKeyboard) {
310 const gfx::Rect& root_bounds = root_window()->bounds();
311 aura::test::EventCountDelegate delegate;
312 scoped_ptr<aura::Window> window(new aura::Window(&delegate));
313 window->Init(ui::LAYER_NOT_DRAWN);
314 window->SetBounds(root_bounds);
315 root_window()->AddChild(window.get());
316 window->Show();
317 window->Focus();
319 aura::Window* keyboard_container(controller()->GetContainerWindow());
321 root_window()->AddChild(keyboard_container);
322 keyboard_container->Show();
324 ShowKeyboard();
326 EXPECT_TRUE(window->IsVisible());
327 EXPECT_TRUE(keyboard_container->IsVisible());
328 EXPECT_TRUE(window->HasFocus());
329 EXPECT_FALSE(keyboard_container->HasFocus());
331 // Click on the keyboard. Make sure the keyboard receives the event, but does
332 // not get focus.
333 EventObserver observer;
334 keyboard_container->AddPreTargetHandler(&observer);
336 ui::test::EventGenerator generator(root_window());
337 generator.MoveMouseTo(keyboard_container->bounds().CenterPoint());
338 generator.ClickLeftButton();
339 EXPECT_TRUE(window->HasFocus());
340 EXPECT_FALSE(keyboard_container->HasFocus());
341 EXPECT_EQ("0 0", delegate.GetMouseButtonCountsAndReset());
342 EXPECT_EQ(1, observer.GetEventCount(ui::ET_MOUSE_PRESSED));
343 EXPECT_EQ(1, observer.GetEventCount(ui::ET_MOUSE_RELEASED));
345 // Click outside of the keyboard. It should reach the window behind.
346 generator.MoveMouseTo(gfx::Point());
347 generator.ClickLeftButton();
348 EXPECT_EQ("1 1", delegate.GetMouseButtonCountsAndReset());
349 keyboard_container->RemovePreTargetHandler(&observer);
352 TEST_F(KeyboardControllerTest, VisibilityChangeWithTextInputTypeChange) {
353 ui::DummyTextInputClient input_client_0(ui::TEXT_INPUT_TYPE_TEXT);
354 ui::DummyTextInputClient input_client_1(ui::TEXT_INPUT_TYPE_TEXT);
355 ui::DummyTextInputClient input_client_2(ui::TEXT_INPUT_TYPE_TEXT);
356 ui::DummyTextInputClient no_input_client_0(ui::TEXT_INPUT_TYPE_NONE);
357 ui::DummyTextInputClient no_input_client_1(ui::TEXT_INPUT_TYPE_NONE);
359 aura::Window* keyboard_container(controller()->GetContainerWindow());
360 scoped_ptr<KeyboardContainerObserver> keyboard_container_observer(
361 new KeyboardContainerObserver(keyboard_container));
362 root_window()->AddChild(keyboard_container);
364 SetFocus(&input_client_0);
366 EXPECT_TRUE(keyboard_container->IsVisible());
368 SetFocus(&no_input_client_0);
369 // Keyboard should not immediately hide itself. It is delayed to avoid layout
370 // flicker when the focus of input field quickly change.
371 EXPECT_TRUE(keyboard_container->IsVisible());
372 EXPECT_TRUE(WillHideKeyboard());
373 // Wait for hide keyboard to finish.
374 base::MessageLoop::current()->Run();
375 EXPECT_FALSE(keyboard_container->IsVisible());
377 SetFocus(&input_client_1);
378 EXPECT_TRUE(keyboard_container->IsVisible());
380 // Schedule to hide keyboard.
381 SetFocus(&no_input_client_1);
382 EXPECT_TRUE(WillHideKeyboard());
383 // Cancel keyboard hide.
384 SetFocus(&input_client_2);
386 EXPECT_FALSE(WillHideKeyboard());
387 EXPECT_TRUE(keyboard_container->IsVisible());
390 // Test to prevent spurious overscroll boxes when changing tabs during keyboard
391 // hide. Refer to crbug.com/401670 for more context.
392 TEST_F(KeyboardControllerTest, CheckOverscrollInsetDuringVisibilityChange) {
393 ui::DummyTextInputClient input_client(ui::TEXT_INPUT_TYPE_TEXT);
394 ui::DummyTextInputClient no_input_client(ui::TEXT_INPUT_TYPE_NONE);
396 aura::Window* keyboard_container(controller()->GetContainerWindow());
397 root_window()->AddChild(keyboard_container);
399 // Enable touch keyboard / overscroll mode to test insets.
400 keyboard::SetTouchKeyboardEnabled(true);
401 EXPECT_TRUE(keyboard::IsKeyboardOverscrollEnabled());
403 SetFocus(&input_client);
404 SetFocus(&no_input_client);
405 // Insets should not be enabled for new windows while keyboard is in the
406 // process of hiding when overscroll is enabled.
407 EXPECT_FALSE(ShouldEnableInsets(proxy()->GetKeyboardWindow()));
408 // Cancel keyboard hide.
409 SetFocus(&input_client);
410 // Insets should be enabled for new windows as hide was cancelled.
411 EXPECT_TRUE(ShouldEnableInsets(proxy()->GetKeyboardWindow()));
414 TEST_F(KeyboardControllerTest, AlwaysVisibleWhenLocked) {
415 ui::DummyTextInputClient input_client_0(ui::TEXT_INPUT_TYPE_TEXT);
416 ui::DummyTextInputClient input_client_1(ui::TEXT_INPUT_TYPE_TEXT);
417 ui::DummyTextInputClient no_input_client_0(ui::TEXT_INPUT_TYPE_NONE);
418 ui::DummyTextInputClient no_input_client_1(ui::TEXT_INPUT_TYPE_NONE);
420 aura::Window* keyboard_container(controller()->GetContainerWindow());
421 scoped_ptr<KeyboardContainerObserver> keyboard_container_observer(
422 new KeyboardContainerObserver(keyboard_container));
423 root_window()->AddChild(keyboard_container);
425 SetFocus(&input_client_0);
427 EXPECT_TRUE(keyboard_container->IsVisible());
429 // Lock keyboard.
430 controller()->set_lock_keyboard(true);
432 SetFocus(&no_input_client_0);
433 // Keyboard should not try to hide itself as it is locked.
434 EXPECT_TRUE(keyboard_container->IsVisible());
435 EXPECT_FALSE(WillHideKeyboard());
437 SetFocus(&input_client_1);
438 EXPECT_TRUE(keyboard_container->IsVisible());
440 // Unlock keyboard.
441 controller()->set_lock_keyboard(false);
443 // Keyboard should hide when focus on no input client.
444 SetFocus(&no_input_client_1);
445 EXPECT_TRUE(WillHideKeyboard());
447 // Wait for hide keyboard to finish.
448 base::MessageLoop::current()->Run();
449 EXPECT_FALSE(keyboard_container->IsVisible());
452 class KeyboardControllerAnimationTest : public KeyboardControllerTest,
453 public KeyboardControllerObserver {
454 public:
455 KeyboardControllerAnimationTest() {}
456 ~KeyboardControllerAnimationTest() override {}
458 void SetUp() override {
459 // We cannot short-circuit animations for this test.
460 ui::ScopedAnimationDurationScaleMode test_duration_mode(
461 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
463 KeyboardControllerTest::SetUp();
465 const gfx::Rect& root_bounds = root_window()->bounds();
466 keyboard_container()->SetBounds(root_bounds);
467 root_window()->AddChild(keyboard_container());
468 controller()->AddObserver(this);
471 void TearDown() override {
472 controller()->RemoveObserver(this);
473 KeyboardControllerTest::TearDown();
476 protected:
477 // KeyboardControllerObserver overrides
478 void OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) override {
479 notified_bounds_ = new_bounds;
482 const gfx::Rect& notified_bounds() { return notified_bounds_; }
484 aura::Window* keyboard_container() {
485 return controller()->GetContainerWindow();
488 aura::Window* keyboard_window() {
489 return proxy()->GetKeyboardWindow();
492 private:
493 gfx::Rect notified_bounds_;
495 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerAnimationTest);
498 // Tests virtual keyboard has correct show and hide animation.
499 TEST_F(KeyboardControllerAnimationTest, ContainerAnimation) {
500 ui::Layer* layer = keyboard_container()->layer();
501 ShowKeyboard();
503 // Keyboard container and window should immediately become visible before
504 // animation starts.
505 EXPECT_TRUE(keyboard_container()->IsVisible());
506 EXPECT_TRUE(keyboard_window()->IsVisible());
507 float show_start_opacity = layer->opacity();
508 gfx::Transform transform;
509 transform.Translate(0, kAnimationDistance);
510 EXPECT_EQ(transform, layer->transform());
511 EXPECT_EQ(gfx::Rect(), notified_bounds());
513 RunAnimationForLayer(layer);
514 EXPECT_TRUE(keyboard_container()->IsVisible());
515 EXPECT_TRUE(keyboard_window()->IsVisible());
516 float show_end_opacity = layer->opacity();
517 EXPECT_LT(show_start_opacity, show_end_opacity);
518 EXPECT_EQ(gfx::Transform(), layer->transform());
519 // KeyboardController should notify the bounds of container window to its
520 // observers after show animation finished.
521 EXPECT_EQ(keyboard_container()->bounds(), notified_bounds());
523 // Directly hide keyboard without delay.
524 controller()->HideKeyboard(KeyboardController::HIDE_REASON_AUTOMATIC);
525 EXPECT_TRUE(keyboard_container()->IsVisible());
526 EXPECT_TRUE(keyboard_container()->layer()->visible());
527 EXPECT_TRUE(keyboard_window()->IsVisible());
528 float hide_start_opacity = layer->opacity();
529 // KeyboardController should notify the bounds of keyboard window to its
530 // observers before hide animation starts.
531 EXPECT_EQ(gfx::Rect(), notified_bounds());
533 RunAnimationForLayer(layer);
534 EXPECT_FALSE(keyboard_container()->IsVisible());
535 EXPECT_FALSE(keyboard_container()->layer()->visible());
536 EXPECT_FALSE(keyboard_window()->IsVisible());
537 float hide_end_opacity = layer->opacity();
538 EXPECT_GT(hide_start_opacity, hide_end_opacity);
539 EXPECT_EQ(transform, layer->transform());
540 EXPECT_EQ(gfx::Rect(), notified_bounds());
543 // Show keyboard during keyboard hide animation should abort the hide animation
544 // and the keyboard should animate in.
545 // Test for crbug.com/333284.
546 TEST_F(KeyboardControllerAnimationTest, ContainerShowWhileHide) {
547 ui::Layer* layer = keyboard_container()->layer();
548 ShowKeyboard();
549 RunAnimationForLayer(layer);
551 controller()->HideKeyboard(KeyboardController::HIDE_REASON_AUTOMATIC);
552 // Before hide animation finishes, show keyboard again.
553 ShowKeyboard();
554 RunAnimationForLayer(layer);
555 EXPECT_TRUE(keyboard_container()->IsVisible());
556 EXPECT_TRUE(keyboard_window()->IsVisible());
557 EXPECT_EQ(1.0, layer->opacity());
558 EXPECT_EQ(gfx::Transform(), layer->transform());
561 } // namespace keyboard