Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / keyboard / keyboard_controller_unittest.cc
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.
4
5 #include "base/bind.h"
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"
32
33 namespace keyboard {
34 namespace {
35
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);
41
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));
50   }
51 }
52
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 {
56  public:
57   explicit TestFocusController(aura::Window* root)
58       : root_(root) {
59     root_->AddPreTargetHandler(this);
60   }
61
62   virtual ~TestFocusController() {
63     root_->RemovePreTargetHandler(this);
64   }
65
66  private:
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);
73     }
74   }
75
76   aura::Window* root_;
77   DISALLOW_COPY_AND_ASSIGN(TestFocusController);
78 };
79
80 class TestKeyboardControllerProxy : public KeyboardControllerProxy {
81  public:
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);
88   }
89
90   virtual ~TestKeyboardControllerProxy() {
91     // Destroy the window before the delegate.
92     window_.reset();
93   }
94
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();
101   }
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 {};
107
108  private:
109   scoped_ptr<aura::Window> window_;
110   aura::test::TestWindowDelegate delegate_;
111   scoped_ptr<ui::InputMethod> input_method_;
112
113   DISALLOW_COPY_AND_ASSIGN(TestKeyboardControllerProxy);
114 };
115
116 // Keeps a count of all the events a window receives.
117 class EventObserver : public ui::EventHandler {
118  public:
119   EventObserver() {}
120   virtual ~EventObserver() {}
121
122   int GetEventCount(ui::EventType type) {
123     return event_counts_[type];
124   }
125
126  private:
127   // Overridden from ui::EventHandler:
128   virtual void OnEvent(ui::Event* event) OVERRIDE {
129     ui::EventHandler::OnEvent(event);
130     event_counts_[event->type()]++;
131   }
132
133   std::map<ui::EventType, int> event_counts_;
134   DISALLOW_COPY_AND_ASSIGN(EventObserver);
135 };
136
137 class KeyboardContainerObserver : public aura::WindowObserver {
138  public:
139   explicit KeyboardContainerObserver(aura::Window* window) : window_(window) {
140     window_->AddObserver(this);
141   }
142   virtual ~KeyboardContainerObserver() {
143     window_->RemoveObserver(this);
144   }
145
146  private:
147   virtual void OnWindowVisibilityChanged(aura::Window* window,
148                                          bool visible) OVERRIDE {
149     if (!visible)
150       base::MessageLoop::current()->Quit();
151   }
152
153   aura::Window* window_;
154
155   DISALLOW_COPY_AND_ASSIGN(KeyboardContainerObserver);
156 };
157
158 }  // namespace
159
160 class KeyboardControllerTest : public testing::Test {
161  public:
162   KeyboardControllerTest() {}
163   virtual ~KeyboardControllerTest() {}
164
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);
169
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_));
177   }
178
179   virtual void TearDown() OVERRIDE {
180     controller_.reset();
181     focus_controller_.reset();
182     aura_test_helper_->TearDown();
183     ui::TerminateContextFactoryForTests();
184   }
185
186   aura::Window* root_window() { return aura_test_helper_->root_window(); }
187   KeyboardControllerProxy* proxy() { return proxy_; }
188   KeyboardController* controller() { return controller_.get(); }
189
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());
194   }
195
196  protected:
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));
207       }
208     }
209   }
210
211   bool WillHideKeyboard() {
212     return controller_->WillHideKeyboard();
213   }
214
215   base::MessageLoopForUI message_loop_;
216   scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
217   scoped_ptr<TestFocusController> focus_controller_;
218
219  private:
220   KeyboardControllerProxy* proxy_;
221   scoped_ptr<KeyboardController> controller_;
222   scoped_ptr<ui::TextInputClient> test_text_input_client_;
223   DISALLOW_COPY_AND_ASSIGN(KeyboardControllerTest);
224 };
225
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));
230
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
234   // has 0 height.
235   ASSERT_EQ(gfx::Rect(0, 100, 200, 0), before_bounds);
236
237   gfx::Rect new_bounds(
238       before_bounds.x(), before_bounds.y() - 50,
239       before_bounds.width(), 50);
240
241   keyboard->SetBounds(new_bounds);
242   ASSERT_EQ(new_bounds, keyboard->bounds());
243
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
247   // height.
248   ASSERT_EQ(gfx::Rect(0, 150, 100, 50), keyboard->bounds());
249 }
250
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());
259   window->Show();
260   window->Focus();
261
262   aura::Window* keyboard_container(controller()->GetContainerWindow());
263   keyboard_container->SetBounds(root_bounds);
264
265   root_window()->AddChild(keyboard_container);
266   keyboard_container->Show();
267
268   ShowKeyboard();
269
270   EXPECT_TRUE(window->IsVisible());
271   EXPECT_TRUE(keyboard_container->IsVisible());
272   EXPECT_TRUE(window->HasFocus());
273   EXPECT_FALSE(keyboard_container->HasFocus());
274
275   // Click on the keyboard. Make sure the keyboard receives the event, but does
276   // not get focus.
277   EventObserver observer;
278   keyboard_container->AddPreTargetHandler(&observer);
279
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));
288
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);
294 }
295
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());
303   window->Show();
304   window->Focus();
305
306   aura::Window* keyboard_container(controller()->GetContainerWindow());
307   keyboard_container->SetBounds(root_bounds);
308
309   root_window()->AddChild(keyboard_container);
310   keyboard_container->Show();
311
312   ShowKeyboard();
313
314   EXPECT_TRUE(window->IsVisible());
315   EXPECT_TRUE(keyboard_container->IsVisible());
316   EXPECT_TRUE(window->HasFocus());
317   EXPECT_FALSE(keyboard_container->HasFocus());
318
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,
325                         ui::EF_NONE);
326   EXPECT_EQ(keyboard_window, targeter->FindTargetForEvent(root, &mouse1));
327
328
329   location.set_y(keyboard_window->bounds().y() - 5);
330   ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, location, location, ui::EF_NONE,
331                         ui::EF_NONE);
332   EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root, &mouse2));
333 }
334
335 TEST_F(KeyboardControllerTest, VisibilityChangeWithTextInputTypeChange) {
336   const gfx::Rect& root_bounds = root_window()->bounds();
337
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);
343
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);
349
350   SetFocus(&input_client_0);
351
352   EXPECT_TRUE(keyboard_container->IsVisible());
353
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());
362
363   SetFocus(&input_client_1);
364   EXPECT_TRUE(keyboard_container->IsVisible());
365
366   // Schedule to hide keyboard.
367   SetFocus(&no_input_client_1);
368   EXPECT_TRUE(WillHideKeyboard());
369   // Cancel keyboard hide.
370   SetFocus(&input_client_2);
371
372   EXPECT_FALSE(WillHideKeyboard());
373   EXPECT_TRUE(keyboard_container->IsVisible());
374 }
375
376 TEST_F(KeyboardControllerTest, AlwaysVisibleWhenLocked) {
377   const gfx::Rect& root_bounds = root_window()->bounds();
378
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);
383
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);
389
390   SetFocus(&input_client_0);
391
392   EXPECT_TRUE(keyboard_container->IsVisible());
393
394   // Lock keyboard.
395   controller()->set_lock_keyboard(true);
396
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());
401
402   SetFocus(&input_client_1);
403   EXPECT_TRUE(keyboard_container->IsVisible());
404
405   // Unlock keyboard.
406   controller()->set_lock_keyboard(false);
407
408   // Keyboard should hide when focus on no input client.
409   SetFocus(&no_input_client_1);
410   EXPECT_TRUE(WillHideKeyboard());
411
412   // Wait for hide keyboard to finish.
413   base::MessageLoop::current()->Run();
414   EXPECT_FALSE(keyboard_container->IsVisible());
415 }
416
417 class KeyboardControllerAnimationTest : public KeyboardControllerTest,
418                                         public KeyboardControllerObserver {
419  public:
420   KeyboardControllerAnimationTest() {}
421   virtual ~KeyboardControllerAnimationTest() {}
422
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);
427
428     KeyboardControllerTest::SetUp();
429
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);
434   }
435
436   virtual void TearDown() OVERRIDE {
437     controller()->RemoveObserver(this);
438     KeyboardControllerTest::TearDown();
439   }
440
441  protected:
442   // KeyboardControllerObserver overrides
443   virtual void OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) OVERRIDE {
444     notified_bounds_ = new_bounds;
445   }
446
447   const gfx::Rect& notified_bounds() { return notified_bounds_; }
448
449   aura::Window* keyboard_container() {
450     return controller()->GetContainerWindow();
451   }
452
453   aura::Window* keyboard_window() {
454     return proxy()->GetKeyboardWindow();
455   }
456
457  private:
458   gfx::Rect notified_bounds_;
459
460   DISALLOW_COPY_AND_ASSIGN(KeyboardControllerAnimationTest);
461 };
462
463 // Tests virtual keyboard has correct show and hide animation.
464 TEST_F(KeyboardControllerAnimationTest, ContainerAnimation) {
465   ui::Layer* layer = keyboard_container()->layer();
466   ShowKeyboard();
467
468   // Keyboard container and window should immediately become visible before
469   // animation starts.
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());
477
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());
487
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());
497
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());
506 }
507
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();
513   ShowKeyboard();
514   RunAnimationForLayer(layer);
515
516   controller()->HideKeyboard(KeyboardController::HIDE_REASON_AUTOMATIC);
517   // Before hide animation finishes, show keyboard again.
518   ShowKeyboard();
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());
524 }
525
526 class KeyboardControllerUsabilityTest : public KeyboardControllerTest {
527  public:
528   KeyboardControllerUsabilityTest() {}
529   virtual ~KeyboardControllerUsabilityTest() {}
530
531   virtual void SetUp() OVERRIDE {
532     CommandLine::ForCurrentProcess()->AppendSwitch(
533         switches::kKeyboardUsabilityExperiment);
534     KeyboardControllerTest::SetUp();
535   }
536
537  private:
538   DISALLOW_COPY_AND_ASSIGN(KeyboardControllerUsabilityTest);
539 };
540
541 TEST_F(KeyboardControllerUsabilityTest, KeyboardAlwaysVisibleInUsabilityTest) {
542   const gfx::Rect& root_bounds = root_window()->bounds();
543
544   ui::DummyTextInputClient input_client(ui::TEXT_INPUT_TYPE_TEXT);
545   ui::DummyTextInputClient no_input_client(ui::TEXT_INPUT_TYPE_NONE);
546
547   aura::Window* keyboard_container(controller()->GetContainerWindow());
548   keyboard_container->SetBounds(root_bounds);
549   root_window()->AddChild(keyboard_container);
550
551   SetFocus(&input_client);
552   EXPECT_TRUE(keyboard_container->IsVisible());
553
554   SetFocus(&no_input_client);
555   // Keyboard should not hide itself after lost focus.
556   EXPECT_TRUE(keyboard_container->IsVisible());
557   EXPECT_FALSE(WillHideKeyboard());
558 }
559
560 }  // namespace keyboard