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 ui::DummyTextInputClient
test_text_input_client(ui::TEXT_INPUT_TYPE_TEXT
);
192 SetFocus(&test_text_input_client
);
196 void SetFocus(ui::TextInputClient
* client
) {
197 ui::InputMethod
* input_method
= proxy()->GetInputMethod();
198 input_method
->SetFocusedTextInputClient(client
);
199 if (client
&& client
->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE
) {
200 input_method
->ShowImeIfNeeded();
201 if (proxy_
->GetKeyboardWindow()->bounds().height() == 0) {
202 // Set initial bounds for test keyboard window.
203 proxy_
->GetKeyboardWindow()->SetBounds(
204 KeyboardBoundsFromWindowBounds(
205 controller()->GetContainerWindow()->bounds(), 100));
210 bool WillHideKeyboard() {
211 return controller_
->WillHideKeyboard();
214 base::MessageLoopForUI message_loop_
;
215 scoped_ptr
<aura::test::AuraTestHelper
> aura_test_helper_
;
216 scoped_ptr
<TestFocusController
> focus_controller_
;
219 KeyboardControllerProxy
* proxy_
;
220 scoped_ptr
<KeyboardController
> controller_
;
222 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerTest
);
225 TEST_F(KeyboardControllerTest
, KeyboardSize
) {
226 aura::Window
* container(controller()->GetContainerWindow());
227 aura::Window
* keyboard(proxy()->GetKeyboardWindow());
228 container
->SetBounds(gfx::Rect(0, 0, 200, 100));
230 container
->AddChild(keyboard
);
231 const gfx::Rect
& before_bounds
= keyboard
->bounds();
232 // The initial keyboard should be positioned at the bottom of container and
234 ASSERT_EQ(gfx::Rect(0, 100, 200, 0), before_bounds
);
236 gfx::Rect
new_bounds(
237 before_bounds
.x(), before_bounds
.y() - 50,
238 before_bounds
.width(), 50);
240 keyboard
->SetBounds(new_bounds
);
241 ASSERT_EQ(new_bounds
, keyboard
->bounds());
243 // Mock a screen rotation.
244 container
->SetBounds(gfx::Rect(0, 0, 100, 200));
245 // The above call should resize keyboard to new width while keeping the old
247 ASSERT_EQ(gfx::Rect(0, 150, 100, 50), keyboard
->bounds());
250 // Tests that tapping/clicking inside the keyboard does not give it focus.
251 TEST_F(KeyboardControllerTest
, ClickDoesNotFocusKeyboard
) {
252 const gfx::Rect
& root_bounds
= root_window()->bounds();
253 aura::test::EventCountDelegate delegate
;
254 scoped_ptr
<aura::Window
> window(new aura::Window(&delegate
));
255 window
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
256 window
->SetBounds(root_bounds
);
257 root_window()->AddChild(window
.get());
261 aura::Window
* keyboard_container(controller()->GetContainerWindow());
262 keyboard_container
->SetBounds(root_bounds
);
264 root_window()->AddChild(keyboard_container
);
265 keyboard_container
->Show();
269 EXPECT_TRUE(window
->IsVisible());
270 EXPECT_TRUE(keyboard_container
->IsVisible());
271 EXPECT_TRUE(window
->HasFocus());
272 EXPECT_FALSE(keyboard_container
->HasFocus());
274 // Click on the keyboard. Make sure the keyboard receives the event, but does
276 EventObserver observer
;
277 keyboard_container
->AddPreTargetHandler(&observer
);
279 aura::test::EventGenerator
generator(root_window());
280 generator
.MoveMouseTo(proxy()->GetKeyboardWindow()->bounds().CenterPoint());
281 generator
.ClickLeftButton();
282 EXPECT_TRUE(window
->HasFocus());
283 EXPECT_FALSE(keyboard_container
->HasFocus());
284 EXPECT_EQ("0 0", delegate
.GetMouseButtonCountsAndReset());
285 EXPECT_EQ(1, observer
.GetEventCount(ui::ET_MOUSE_PRESSED
));
286 EXPECT_EQ(1, observer
.GetEventCount(ui::ET_MOUSE_RELEASED
));
288 // Click outside of the keyboard. It should reach the window behind.
289 generator
.MoveMouseTo(gfx::Point());
290 generator
.ClickLeftButton();
291 EXPECT_EQ("1 1", delegate
.GetMouseButtonCountsAndReset());
292 keyboard_container
->RemovePreTargetHandler(&observer
);
295 TEST_F(KeyboardControllerTest
, EventHitTestingInContainer
) {
296 const gfx::Rect
& root_bounds
= root_window()->bounds();
297 aura::test::EventCountDelegate delegate
;
298 scoped_ptr
<aura::Window
> window(new aura::Window(&delegate
));
299 window
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
300 window
->SetBounds(root_bounds
);
301 root_window()->AddChild(window
.get());
305 aura::Window
* keyboard_container(controller()->GetContainerWindow());
306 keyboard_container
->SetBounds(root_bounds
);
308 root_window()->AddChild(keyboard_container
);
309 keyboard_container
->Show();
313 EXPECT_TRUE(window
->IsVisible());
314 EXPECT_TRUE(keyboard_container
->IsVisible());
315 EXPECT_TRUE(window
->HasFocus());
316 EXPECT_FALSE(keyboard_container
->HasFocus());
318 // Make sure hit testing works correctly while the keyboard is visible.
319 aura::Window
* keyboard_window
= proxy()->GetKeyboardWindow();
320 ui::EventTarget
* root
= root_window();
321 ui::EventTargeter
* targeter
= root
->GetEventTargeter();
322 gfx::Point location
= keyboard_window
->bounds().CenterPoint();
323 ui::MouseEvent
mouse1(ui::ET_MOUSE_MOVED
, location
, location
, ui::EF_NONE
,
325 EXPECT_EQ(keyboard_window
, targeter
->FindTargetForEvent(root
, &mouse1
));
328 location
.set_y(keyboard_window
->bounds().y() - 5);
329 ui::MouseEvent
mouse2(ui::ET_MOUSE_MOVED
, location
, location
, ui::EF_NONE
,
331 EXPECT_EQ(window
.get(), targeter
->FindTargetForEvent(root
, &mouse2
));
334 TEST_F(KeyboardControllerTest
, VisibilityChangeWithTextInputTypeChange
) {
335 const gfx::Rect
& root_bounds
= root_window()->bounds();
337 ui::DummyTextInputClient
input_client_0(ui::TEXT_INPUT_TYPE_TEXT
);
338 ui::DummyTextInputClient
input_client_1(ui::TEXT_INPUT_TYPE_TEXT
);
339 ui::DummyTextInputClient
input_client_2(ui::TEXT_INPUT_TYPE_TEXT
);
340 ui::DummyTextInputClient
no_input_client_0(ui::TEXT_INPUT_TYPE_NONE
);
341 ui::DummyTextInputClient
no_input_client_1(ui::TEXT_INPUT_TYPE_NONE
);
343 aura::Window
* keyboard_container(controller()->GetContainerWindow());
344 scoped_ptr
<KeyboardContainerObserver
> keyboard_container_observer(
345 new KeyboardContainerObserver(keyboard_container
));
346 keyboard_container
->SetBounds(root_bounds
);
347 root_window()->AddChild(keyboard_container
);
349 SetFocus(&input_client_0
);
351 EXPECT_TRUE(keyboard_container
->IsVisible());
353 SetFocus(&no_input_client_0
);
354 // Keyboard should not immediately hide itself. It is delayed to avoid layout
355 // flicker when the focus of input field quickly change.
356 EXPECT_TRUE(keyboard_container
->IsVisible());
357 EXPECT_TRUE(WillHideKeyboard());
358 // Wait for hide keyboard to finish.
359 base::MessageLoop::current()->Run();
360 EXPECT_FALSE(keyboard_container
->IsVisible());
362 SetFocus(&input_client_1
);
363 EXPECT_TRUE(keyboard_container
->IsVisible());
365 // Schedule to hide keyboard.
366 SetFocus(&no_input_client_1
);
367 EXPECT_TRUE(WillHideKeyboard());
368 // Cancel keyboard hide.
369 SetFocus(&input_client_2
);
371 EXPECT_FALSE(WillHideKeyboard());
372 EXPECT_TRUE(keyboard_container
->IsVisible());
375 TEST_F(KeyboardControllerTest
, AlwaysVisibleWhenLocked
) {
376 const gfx::Rect
& root_bounds
= root_window()->bounds();
378 ui::DummyTextInputClient
input_client_0(ui::TEXT_INPUT_TYPE_TEXT
);
379 ui::DummyTextInputClient
input_client_1(ui::TEXT_INPUT_TYPE_TEXT
);
380 ui::DummyTextInputClient
no_input_client_0(ui::TEXT_INPUT_TYPE_NONE
);
381 ui::DummyTextInputClient
no_input_client_1(ui::TEXT_INPUT_TYPE_NONE
);
383 aura::Window
* keyboard_container(controller()->GetContainerWindow());
384 scoped_ptr
<KeyboardContainerObserver
> keyboard_container_observer(
385 new KeyboardContainerObserver(keyboard_container
));
386 keyboard_container
->SetBounds(root_bounds
);
387 root_window()->AddChild(keyboard_container
);
389 SetFocus(&input_client_0
);
391 EXPECT_TRUE(keyboard_container
->IsVisible());
394 controller()->set_lock_keyboard(true);
396 SetFocus(&no_input_client_0
);
397 // Keyboard should not try to hide itself as it is locked.
398 EXPECT_TRUE(keyboard_container
->IsVisible());
399 EXPECT_FALSE(WillHideKeyboard());
401 SetFocus(&input_client_1
);
402 EXPECT_TRUE(keyboard_container
->IsVisible());
405 controller()->set_lock_keyboard(false);
407 // Keyboard should hide when focus on no input client.
408 SetFocus(&no_input_client_1
);
409 EXPECT_TRUE(WillHideKeyboard());
411 // Wait for hide keyboard to finish.
412 base::MessageLoop::current()->Run();
413 EXPECT_FALSE(keyboard_container
->IsVisible());
416 class KeyboardControllerAnimationTest
: public KeyboardControllerTest
,
417 public KeyboardControllerObserver
{
419 KeyboardControllerAnimationTest() {}
420 virtual ~KeyboardControllerAnimationTest() {}
422 virtual void SetUp() OVERRIDE
{
423 // We cannot short-circuit animations for this test.
424 ui::ScopedAnimationDurationScaleMode
normal_duration_mode(
425 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION
);
427 KeyboardControllerTest::SetUp();
429 const gfx::Rect
& root_bounds
= root_window()->bounds();
430 keyboard_container()->SetBounds(root_bounds
);
431 root_window()->AddChild(keyboard_container());
432 controller()->AddObserver(this);
435 virtual void TearDown() OVERRIDE
{
436 controller()->RemoveObserver(this);
437 KeyboardControllerTest::TearDown();
441 // KeyboardControllerObserver overrides
442 virtual void OnKeyboardBoundsChanging(const gfx::Rect
& new_bounds
) OVERRIDE
{
443 notified_bounds_
= new_bounds
;
446 const gfx::Rect
& notified_bounds() { return notified_bounds_
; }
448 aura::Window
* keyboard_container() {
449 return controller()->GetContainerWindow();
452 aura::Window
* keyboard_window() {
453 return proxy()->GetKeyboardWindow();
457 gfx::Rect notified_bounds_
;
459 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerAnimationTest
);
462 // Tests virtual keyboard has correct show and hide animation.
463 TEST_F(KeyboardControllerAnimationTest
, ContainerAnimation
) {
464 ui::Layer
* layer
= keyboard_container()->layer();
467 // Keyboard container and window should immediately become visible before
469 EXPECT_TRUE(keyboard_container()->IsVisible());
470 EXPECT_TRUE(keyboard_window()->IsVisible());
471 float show_start_opacity
= layer
->opacity();
472 gfx::Transform transform
;
473 transform
.Translate(0, keyboard_window()->bounds().height());
474 EXPECT_EQ(transform
, layer
->transform());
475 EXPECT_EQ(gfx::Rect(), notified_bounds());
477 RunAnimationForLayer(layer
);
478 EXPECT_TRUE(keyboard_container()->IsVisible());
479 EXPECT_TRUE(keyboard_window()->IsVisible());
480 float show_end_opacity
= layer
->opacity();
481 EXPECT_LT(show_start_opacity
, show_end_opacity
);
482 EXPECT_EQ(gfx::Transform(), layer
->transform());
483 // KeyboardController should notify the bounds of keyboard window to its
484 // observers after show animation finished.
485 EXPECT_EQ(keyboard_window()->bounds(), notified_bounds());
487 // Directly hide keyboard without delay.
488 controller()->HideKeyboard(KeyboardController::HIDE_REASON_AUTOMATIC
);
489 EXPECT_TRUE(keyboard_container()->IsVisible());
490 EXPECT_TRUE(keyboard_container()->layer()->visible());
491 EXPECT_TRUE(keyboard_window()->IsVisible());
492 float hide_start_opacity
= layer
->opacity();
493 // KeyboardController should notify the bounds of keyboard window to its
494 // observers before hide animation starts.
495 EXPECT_EQ(gfx::Rect(), notified_bounds());
497 RunAnimationForLayer(layer
);
498 EXPECT_FALSE(keyboard_container()->IsVisible());
499 EXPECT_FALSE(keyboard_container()->layer()->visible());
500 EXPECT_FALSE(keyboard_window()->IsVisible());
501 float hide_end_opacity
= layer
->opacity();
502 EXPECT_GT(hide_start_opacity
, hide_end_opacity
);
503 EXPECT_EQ(transform
, layer
->transform());
504 EXPECT_EQ(gfx::Rect(), notified_bounds());
507 // Show keyboard during keyboard hide animation should abort the hide animation
508 // and the keyboard should animate in.
509 // Test for crbug.com/333284.
510 TEST_F(KeyboardControllerAnimationTest
, ContainerShowWhileHide
) {
511 ui::Layer
* layer
= keyboard_container()->layer();
513 RunAnimationForLayer(layer
);
515 controller()->HideKeyboard(KeyboardController::HIDE_REASON_AUTOMATIC
);
516 // Before hide animation finishes, show keyboard again.
518 RunAnimationForLayer(layer
);
519 EXPECT_TRUE(keyboard_container()->IsVisible());
520 EXPECT_TRUE(keyboard_window()->IsVisible());
521 EXPECT_EQ(1.0, layer
->opacity());
522 EXPECT_EQ(gfx::Transform(), layer
->transform());
525 class KeyboardControllerUsabilityTest
: public KeyboardControllerTest
{
527 KeyboardControllerUsabilityTest() {}
528 virtual ~KeyboardControllerUsabilityTest() {}
530 virtual void SetUp() OVERRIDE
{
531 CommandLine::ForCurrentProcess()->AppendSwitch(
532 switches::kKeyboardUsabilityExperiment
);
533 KeyboardControllerTest::SetUp();
537 DISALLOW_COPY_AND_ASSIGN(KeyboardControllerUsabilityTest
);
540 TEST_F(KeyboardControllerUsabilityTest
, KeyboardAlwaysVisibleInUsabilityTest
) {
541 const gfx::Rect
& root_bounds
= root_window()->bounds();
543 ui::DummyTextInputClient
input_client(ui::TEXT_INPUT_TYPE_TEXT
);
544 ui::DummyTextInputClient
no_input_client(ui::TEXT_INPUT_TYPE_NONE
);
546 aura::Window
* keyboard_container(controller()->GetContainerWindow());
547 keyboard_container
->SetBounds(root_bounds
);
548 root_window()->AddChild(keyboard_container
);
550 SetFocus(&input_client
);
551 EXPECT_TRUE(keyboard_container
->IsVisible());
553 SetFocus(&no_input_client
);
554 // Keyboard should not hide itself after lost focus.
555 EXPECT_TRUE(keyboard_container
->IsVisible());
556 EXPECT_FALSE(WillHideKeyboard());
559 } // namespace keyboard