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.
6 #include "base/command_line.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/aura/client/focus_client.h"
11 #include "ui/aura/layout_manager.h"
12 #include "ui/aura/test/aura_test_helper.h"
13 #include "ui/aura/test/event_generator.h"
14 #include "ui/aura/test/test_window_delegate.h"
15 #include "ui/aura/window.h"
16 #include "ui/aura/window_event_dispatcher.h"
17 #include "ui/base/ime/dummy_text_input_client.h"
18 #include "ui/base/ime/input_method.h"
19 #include "ui/base/ime/input_method_factory.h"
20 #include "ui/base/ime/text_input_client.h"
21 #include "ui/compositor/layer_type.h"
22 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
23 #include "ui/compositor/test/context_factories_for_test.h"
24 #include "ui/compositor/test/layer_animator_test_controller.h"
25 #include "ui/gfx/rect.h"
26 #include "ui/keyboard/keyboard_controller.h"
27 #include "ui/keyboard/keyboard_controller_observer.h"
28 #include "ui/keyboard/keyboard_controller_proxy.h"
29 #include "ui/keyboard/keyboard_switches.h"
30 #include "ui/keyboard/keyboard_util.h"
31 #include "ui/wm/core/default_activation_client.h"
36 // Steps a layer animation until it is completed. Animations must be enabled.
37 void RunAnimationForLayer(ui::Layer* layer) {
38 // Animations must be enabled for stepping to work.
39 ASSERT_NE(ui::ScopedAnimationDurationScaleMode::duration_scale_mode(),
40 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
42 ui::LayerAnimatorTestController controller(layer->GetAnimator());
43 gfx::AnimationContainerElement* element = layer->GetAnimator();
44 // Multiple steps are required to complete complex animations.
45 // TODO(vollick): This should not be necessary. crbug.com/154017
46 while (controller.animator()->is_animating()) {
47 controller.StartThreadedAnimationsIfNeeded();
48 base::TimeTicks step_time = controller.animator()->last_step_time();
49 element->Step(step_time + base::TimeDelta::FromMilliseconds(1000));
53 // An event handler that focuses a window when it is clicked/touched on. This is
54 // used to match the focus manger behaviour in ash and views.
55 class TestFocusController : public ui::EventHandler {
57 explicit TestFocusController(aura::Window* root)
59 root_->AddPreTargetHandler(this);
62 virtual ~TestFocusController() {
63 root_->RemovePreTargetHandler(this);
67 // Overridden from ui::EventHandler:
68 virtual void OnEvent(ui::Event* event) OVERRIDE {
69 aura::Window* target = static_cast<aura::Window*>(event->target());
70 if (event->type() == ui::ET_MOUSE_PRESSED ||
71 event->type() == ui::ET_TOUCH_PRESSED) {
72 aura::client::GetFocusClient(target)->FocusWindow(target);
77 DISALLOW_COPY_AND_ASSIGN(TestFocusController);
80 class TestKeyboardControllerProxy : public KeyboardControllerProxy {
82 TestKeyboardControllerProxy()
83 : window_(new aura::Window(&delegate_)),
84 input_method_(ui::CreateInputMethod(NULL,
85 gfx::kNullAcceleratedWidget)) {
86 window_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
87 window_->set_owned_by_parent(false);
90 virtual ~TestKeyboardControllerProxy() {
91 // Destroy the window before the delegate.
95 // Overridden from KeyboardControllerProxy:
96 virtual bool HasKeyboardWindow() const OVERRIDE { return true; }
97 virtual aura::Window* GetKeyboardWindow() OVERRIDE { return window_.get(); }
98 virtual content::BrowserContext* GetBrowserContext() OVERRIDE { return NULL; }
99 virtual ui::InputMethod* GetInputMethod() OVERRIDE {
100 return input_method_.get();
102 virtual void RequestAudioInput(content::WebContents* web_contents,
103 const content::MediaStreamRequest& request,
104 const content::MediaResponseCallback& callback) OVERRIDE { return; }
105 virtual void LoadSystemKeyboard() OVERRIDE {};
106 virtual void ReloadKeyboardIfNeeded() OVERRIDE {};
109 scoped_ptr<aura::Window> window_;
110 aura::test::TestWindowDelegate delegate_;
111 scoped_ptr<ui::InputMethod> input_method_;
113 DISALLOW_COPY_AND_ASSIGN(TestKeyboardControllerProxy);
116 // Keeps a count of all the events a window receives.
117 class EventObserver : public ui::EventHandler {
120 virtual ~EventObserver() {}
122 int GetEventCount(ui::EventType type) {
123 return event_counts_[type];
127 // Overridden from ui::EventHandler:
128 virtual void OnEvent(ui::Event* event) OVERRIDE {
129 ui::EventHandler::OnEvent(event);
130 event_counts_[event->type()]++;
133 std::map<ui::EventType, int> event_counts_;
134 DISALLOW_COPY_AND_ASSIGN(EventObserver);
137 class KeyboardContainerObserver : public aura::WindowObserver {
139 explicit KeyboardContainerObserver(aura::Window* window) : window_(window) {
140 window_->AddObserver(this);
142 virtual ~KeyboardContainerObserver() {
143 window_->RemoveObserver(this);
147 virtual void OnWindowVisibilityChanged(aura::Window* window,
148 bool visible) OVERRIDE {
150 base::MessageLoop::current()->Quit();
153 aura::Window* window_;
155 DISALLOW_COPY_AND_ASSIGN(KeyboardContainerObserver);
160 class KeyboardControllerTest : public testing::Test {
162 KeyboardControllerTest() {}
163 virtual ~KeyboardControllerTest() {}
165 virtual void SetUp() OVERRIDE {
166 // The ContextFactory must exist before any Compositors are created.
167 bool enable_pixel_output = false;
168 ui::InitializeContextFactoryForTests(enable_pixel_output);
170 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_));
171 aura_test_helper_->SetUp();
172 new wm::DefaultActivationClient(aura_test_helper_->root_window());
173 ui::SetUpInputMethodFactoryForTesting();
174 focus_controller_.reset(new TestFocusController(root_window()));
175 proxy_ = new TestKeyboardControllerProxy();
176 controller_.reset(new KeyboardController(proxy_));
179 virtual void TearDown() OVERRIDE {
181 focus_controller_.reset();
182 aura_test_helper_->TearDown();
183 ui::TerminateContextFactoryForTests();
186 aura::Window* root_window() { return aura_test_helper_->root_window(); }
187 KeyboardControllerProxy* proxy() { return proxy_; }
188 KeyboardController* controller() { return controller_.get(); }
190 void ShowKeyboard() {
191 test_text_input_client_.reset(
192 new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT));
193 SetFocus(test_text_input_client_.get());
197 void SetFocus(ui::TextInputClient* client) {
198 ui::InputMethod* input_method = proxy()->GetInputMethod();
199 input_method->SetFocusedTextInputClient(client);
200 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) {
201 input_method->ShowImeIfNeeded();
202 if (proxy_->GetKeyboardWindow()->bounds().height() == 0) {
203 // Set initial bounds for test keyboard window.
204 proxy_->GetKeyboardWindow()->SetBounds(
205 KeyboardBoundsFromWindowBounds(
206 controller()->GetContainerWindow()->bounds(), 100));
211 bool WillHideKeyboard() {
212 return controller_->WillHideKeyboard();
215 base::MessageLoopForUI message_loop_;
216 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
217 scoped_ptr<TestFocusController> focus_controller_;
220 KeyboardControllerProxy* proxy_;
221 scoped_ptr<KeyboardController> controller_;
222 scoped_ptr<ui::TextInputClient> test_text_input_client_;
223 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerTest);
226 TEST_F(KeyboardControllerTest, KeyboardSize) {
227 aura::Window* container(controller()->GetContainerWindow());
228 aura::Window* keyboard(proxy()->GetKeyboardWindow());
229 container->SetBounds(gfx::Rect(0, 0, 200, 100));
231 container->AddChild(keyboard);
232 const gfx::Rect& before_bounds = keyboard->bounds();
233 // The initial keyboard should be positioned at the bottom of container and
235 ASSERT_EQ(gfx::Rect(0, 100, 200, 0), before_bounds);
237 gfx::Rect new_bounds(
238 before_bounds.x(), before_bounds.y() - 50,
239 before_bounds.width(), 50);
241 keyboard->SetBounds(new_bounds);
242 ASSERT_EQ(new_bounds, keyboard->bounds());
244 // Mock a screen rotation.
245 container->SetBounds(gfx::Rect(0, 0, 100, 200));
246 // The above call should resize keyboard to new width while keeping the old
248 ASSERT_EQ(gfx::Rect(0, 150, 100, 50), keyboard->bounds());
251 // Tests that tapping/clicking inside the keyboard does not give it focus.
252 TEST_F(KeyboardControllerTest, ClickDoesNotFocusKeyboard) {
253 const gfx::Rect& root_bounds = root_window()->bounds();
254 aura::test::EventCountDelegate delegate;
255 scoped_ptr<aura::Window> window(new aura::Window(&delegate));
256 window->Init(aura::WINDOW_LAYER_NOT_DRAWN);
257 window->SetBounds(root_bounds);
258 root_window()->AddChild(window.get());
262 aura::Window* keyboard_container(controller()->GetContainerWindow());
263 keyboard_container->SetBounds(root_bounds);
265 root_window()->AddChild(keyboard_container);
266 keyboard_container->Show();
270 EXPECT_TRUE(window->IsVisible());
271 EXPECT_TRUE(keyboard_container->IsVisible());
272 EXPECT_TRUE(window->HasFocus());
273 EXPECT_FALSE(keyboard_container->HasFocus());
275 // Click on the keyboard. Make sure the keyboard receives the event, but does
277 EventObserver observer;
278 keyboard_container->AddPreTargetHandler(&observer);
280 aura::test::EventGenerator generator(root_window());
281 generator.MoveMouseTo(proxy()->GetKeyboardWindow()->bounds().CenterPoint());
282 generator.ClickLeftButton();
283 EXPECT_TRUE(window->HasFocus());
284 EXPECT_FALSE(keyboard_container->HasFocus());
285 EXPECT_EQ("0 0", delegate.GetMouseButtonCountsAndReset());
286 EXPECT_EQ(1, observer.GetEventCount(ui::ET_MOUSE_PRESSED));
287 EXPECT_EQ(1, observer.GetEventCount(ui::ET_MOUSE_RELEASED));
289 // Click outside of the keyboard. It should reach the window behind.
290 generator.MoveMouseTo(gfx::Point());
291 generator.ClickLeftButton();
292 EXPECT_EQ("1 1", delegate.GetMouseButtonCountsAndReset());
293 keyboard_container->RemovePreTargetHandler(&observer);
296 TEST_F(KeyboardControllerTest, EventHitTestingInContainer) {
297 const gfx::Rect& root_bounds = root_window()->bounds();
298 aura::test::EventCountDelegate delegate;
299 scoped_ptr<aura::Window> window(new aura::Window(&delegate));
300 window->Init(aura::WINDOW_LAYER_NOT_DRAWN);
301 window->SetBounds(root_bounds);
302 root_window()->AddChild(window.get());
306 aura::Window* keyboard_container(controller()->GetContainerWindow());
307 keyboard_container->SetBounds(root_bounds);
309 root_window()->AddChild(keyboard_container);
310 keyboard_container->Show();
314 EXPECT_TRUE(window->IsVisible());
315 EXPECT_TRUE(keyboard_container->IsVisible());
316 EXPECT_TRUE(window->HasFocus());
317 EXPECT_FALSE(keyboard_container->HasFocus());
319 // Make sure hit testing works correctly while the keyboard is visible.
320 aura::Window* keyboard_window = proxy()->GetKeyboardWindow();
321 ui::EventTarget* root = root_window();
322 ui::EventTargeter* targeter = root->GetEventTargeter();
323 gfx::Point location = keyboard_window->bounds().CenterPoint();
324 ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, location, location, ui::EF_NONE,
326 EXPECT_EQ(keyboard_window, targeter->FindTargetForEvent(root, &mouse1));
329 location.set_y(keyboard_window->bounds().y() - 5);
330 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, location, location, ui::EF_NONE,
332 EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root, &mouse2));
335 TEST_F(KeyboardControllerTest, VisibilityChangeWithTextInputTypeChange) {
336 const gfx::Rect& root_bounds = root_window()->bounds();
338 ui::DummyTextInputClient input_client_0(ui::TEXT_INPUT_TYPE_TEXT);
339 ui::DummyTextInputClient input_client_1(ui::TEXT_INPUT_TYPE_TEXT);
340 ui::DummyTextInputClient input_client_2(ui::TEXT_INPUT_TYPE_TEXT);
341 ui::DummyTextInputClient no_input_client_0(ui::TEXT_INPUT_TYPE_NONE);
342 ui::DummyTextInputClient no_input_client_1(ui::TEXT_INPUT_TYPE_NONE);
344 aura::Window* keyboard_container(controller()->GetContainerWindow());
345 scoped_ptr<KeyboardContainerObserver> keyboard_container_observer(
346 new KeyboardContainerObserver(keyboard_container));
347 keyboard_container->SetBounds(root_bounds);
348 root_window()->AddChild(keyboard_container);
350 SetFocus(&input_client_0);
352 EXPECT_TRUE(keyboard_container->IsVisible());
354 SetFocus(&no_input_client_0);
355 // Keyboard should not immediately hide itself. It is delayed to avoid layout
356 // flicker when the focus of input field quickly change.
357 EXPECT_TRUE(keyboard_container->IsVisible());
358 EXPECT_TRUE(WillHideKeyboard());
359 // Wait for hide keyboard to finish.
360 base::MessageLoop::current()->Run();
361 EXPECT_FALSE(keyboard_container->IsVisible());
363 SetFocus(&input_client_1);
364 EXPECT_TRUE(keyboard_container->IsVisible());
366 // Schedule to hide keyboard.
367 SetFocus(&no_input_client_1);
368 EXPECT_TRUE(WillHideKeyboard());
369 // Cancel keyboard hide.
370 SetFocus(&input_client_2);
372 EXPECT_FALSE(WillHideKeyboard());
373 EXPECT_TRUE(keyboard_container->IsVisible());
376 TEST_F(KeyboardControllerTest, AlwaysVisibleWhenLocked) {
377 const gfx::Rect& root_bounds = root_window()->bounds();
379 ui::DummyTextInputClient input_client_0(ui::TEXT_INPUT_TYPE_TEXT);
380 ui::DummyTextInputClient input_client_1(ui::TEXT_INPUT_TYPE_TEXT);
381 ui::DummyTextInputClient no_input_client_0(ui::TEXT_INPUT_TYPE_NONE);
382 ui::DummyTextInputClient no_input_client_1(ui::TEXT_INPUT_TYPE_NONE);
384 aura::Window* keyboard_container(controller()->GetContainerWindow());
385 scoped_ptr<KeyboardContainerObserver> keyboard_container_observer(
386 new KeyboardContainerObserver(keyboard_container));
387 keyboard_container->SetBounds(root_bounds);
388 root_window()->AddChild(keyboard_container);
390 SetFocus(&input_client_0);
392 EXPECT_TRUE(keyboard_container->IsVisible());
395 controller()->set_lock_keyboard(true);
397 SetFocus(&no_input_client_0);
398 // Keyboard should not try to hide itself as it is locked.
399 EXPECT_TRUE(keyboard_container->IsVisible());
400 EXPECT_FALSE(WillHideKeyboard());
402 SetFocus(&input_client_1);
403 EXPECT_TRUE(keyboard_container->IsVisible());
406 controller()->set_lock_keyboard(false);
408 // Keyboard should hide when focus on no input client.
409 SetFocus(&no_input_client_1);
410 EXPECT_TRUE(WillHideKeyboard());
412 // Wait for hide keyboard to finish.
413 base::MessageLoop::current()->Run();
414 EXPECT_FALSE(keyboard_container->IsVisible());
417 class KeyboardControllerAnimationTest : public KeyboardControllerTest,
418 public KeyboardControllerObserver {
420 KeyboardControllerAnimationTest() {}
421 virtual ~KeyboardControllerAnimationTest() {}
423 virtual void SetUp() OVERRIDE {
424 // We cannot short-circuit animations for this test.
425 ui::ScopedAnimationDurationScaleMode normal_duration_mode(
426 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
428 KeyboardControllerTest::SetUp();
430 const gfx::Rect& root_bounds = root_window()->bounds();
431 keyboard_container()->SetBounds(root_bounds);
432 root_window()->AddChild(keyboard_container());
433 controller()->AddObserver(this);
436 virtual void TearDown() OVERRIDE {
437 controller()->RemoveObserver(this);
438 KeyboardControllerTest::TearDown();
442 // KeyboardControllerObserver overrides
443 virtual void OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) OVERRIDE {
444 notified_bounds_ = new_bounds;
447 const gfx::Rect& notified_bounds() { return notified_bounds_; }
449 aura::Window* keyboard_container() {
450 return controller()->GetContainerWindow();
453 aura::Window* keyboard_window() {
454 return proxy()->GetKeyboardWindow();
458 gfx::Rect notified_bounds_;
460 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerAnimationTest);
463 // Tests virtual keyboard has correct show and hide animation.
464 TEST_F(KeyboardControllerAnimationTest, ContainerAnimation) {
465 ui::Layer* layer = keyboard_container()->layer();
468 // Keyboard container and window should immediately become visible before
470 EXPECT_TRUE(keyboard_container()->IsVisible());
471 EXPECT_TRUE(keyboard_window()->IsVisible());
472 float show_start_opacity = layer->opacity();
473 gfx::Transform transform;
474 transform.Translate(0, keyboard_window()->bounds().height());
475 EXPECT_EQ(transform, layer->transform());
476 EXPECT_EQ(gfx::Rect(), notified_bounds());
478 RunAnimationForLayer(layer);
479 EXPECT_TRUE(keyboard_container()->IsVisible());
480 EXPECT_TRUE(keyboard_window()->IsVisible());
481 float show_end_opacity = layer->opacity();
482 EXPECT_LT(show_start_opacity, show_end_opacity);
483 EXPECT_EQ(gfx::Transform(), layer->transform());
484 // KeyboardController should notify the bounds of keyboard window to its
485 // observers after show animation finished.
486 EXPECT_EQ(keyboard_window()->bounds(), notified_bounds());
488 // Directly hide keyboard without delay.
489 controller()->HideKeyboard(KeyboardController::HIDE_REASON_AUTOMATIC);
490 EXPECT_TRUE(keyboard_container()->IsVisible());
491 EXPECT_TRUE(keyboard_container()->layer()->visible());
492 EXPECT_TRUE(keyboard_window()->IsVisible());
493 float hide_start_opacity = layer->opacity();
494 // KeyboardController should notify the bounds of keyboard window to its
495 // observers before hide animation starts.
496 EXPECT_EQ(gfx::Rect(), notified_bounds());
498 RunAnimationForLayer(layer);
499 EXPECT_FALSE(keyboard_container()->IsVisible());
500 EXPECT_FALSE(keyboard_container()->layer()->visible());
501 EXPECT_FALSE(keyboard_window()->IsVisible());
502 float hide_end_opacity = layer->opacity();
503 EXPECT_GT(hide_start_opacity, hide_end_opacity);
504 EXPECT_EQ(transform, layer->transform());
505 EXPECT_EQ(gfx::Rect(), notified_bounds());
508 // Show keyboard during keyboard hide animation should abort the hide animation
509 // and the keyboard should animate in.
510 // Test for crbug.com/333284.
511 TEST_F(KeyboardControllerAnimationTest, ContainerShowWhileHide) {
512 ui::Layer* layer = keyboard_container()->layer();
514 RunAnimationForLayer(layer);
516 controller()->HideKeyboard(KeyboardController::HIDE_REASON_AUTOMATIC);
517 // Before hide animation finishes, show keyboard again.
519 RunAnimationForLayer(layer);
520 EXPECT_TRUE(keyboard_container()->IsVisible());
521 EXPECT_TRUE(keyboard_window()->IsVisible());
522 EXPECT_EQ(1.0, layer->opacity());
523 EXPECT_EQ(gfx::Transform(), layer->transform());
526 class KeyboardControllerUsabilityTest : public KeyboardControllerTest {
528 KeyboardControllerUsabilityTest() {}
529 virtual ~KeyboardControllerUsabilityTest() {}
531 virtual void SetUp() OVERRIDE {
532 CommandLine::ForCurrentProcess()->AppendSwitch(
533 switches::kKeyboardUsabilityExperiment);
534 KeyboardControllerTest::SetUp();
538 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerUsabilityTest);
541 TEST_F(KeyboardControllerUsabilityTest, KeyboardAlwaysVisibleInUsabilityTest) {
542 const gfx::Rect& root_bounds = root_window()->bounds();
544 ui::DummyTextInputClient input_client(ui::TEXT_INPUT_TYPE_TEXT);
545 ui::DummyTextInputClient no_input_client(ui::TEXT_INPUT_TYPE_NONE);
547 aura::Window* keyboard_container(controller()->GetContainerWindow());
548 keyboard_container->SetBounds(root_bounds);
549 root_window()->AddChild(keyboard_container);
551 SetFocus(&input_client);
552 EXPECT_TRUE(keyboard_container->IsVisible());
554 SetFocus(&no_input_client);
555 // Keyboard should not hide itself after lost focus.
556 EXPECT_TRUE(keyboard_container->IsVisible());
557 EXPECT_FALSE(WillHideKeyboard());
560 } // namespace keyboard