Upstream version 10.39.225.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 "ui/keyboard/keyboard_controller.h"
6
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/test/event_generator.h"
30 #include "ui/gfx/geometry/rect.h"
31 #include "ui/keyboard/keyboard_controller_observer.h"
32 #include "ui/keyboard/keyboard_controller_proxy.h"
33 #include "ui/keyboard/keyboard_util.h"
34 #include "ui/wm/core/default_activation_client.h"
35
36 namespace keyboard {
37 namespace {
38
39 // Steps a layer animation until it is completed. Animations must be enabled.
40 void RunAnimationForLayer(ui::Layer* layer) {
41   // Animations must be enabled for stepping to work.
42   ASSERT_NE(ui::ScopedAnimationDurationScaleMode::duration_scale_mode(),
43             ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
44
45   ui::LayerAnimatorTestController controller(layer->GetAnimator());
46   // Multiple steps are required to complete complex animations.
47   // TODO(vollick): This should not be necessary. crbug.com/154017
48   while (controller.animator()->is_animating()) {
49     controller.StartThreadedAnimationsIfNeeded();
50     base::TimeTicks step_time = controller.animator()->last_step_time();
51     controller.animator()->Step(step_time +
52                                 base::TimeDelta::FromMilliseconds(1000));
53   }
54 }
55
56 // An event handler that focuses a window when it is clicked/touched on. This is
57 // used to match the focus manger behaviour in ash and views.
58 class TestFocusController : public ui::EventHandler {
59  public:
60   explicit TestFocusController(aura::Window* root)
61       : root_(root) {
62     root_->AddPreTargetHandler(this);
63   }
64
65   virtual ~TestFocusController() {
66     root_->RemovePreTargetHandler(this);
67   }
68
69  private:
70   // Overridden from ui::EventHandler:
71   virtual void OnEvent(ui::Event* event) OVERRIDE {
72     aura::Window* target = static_cast<aura::Window*>(event->target());
73     if (event->type() == ui::ET_MOUSE_PRESSED ||
74         event->type() == ui::ET_TOUCH_PRESSED) {
75       aura::client::GetFocusClient(target)->FocusWindow(target);
76     }
77   }
78
79   aura::Window* root_;
80   DISALLOW_COPY_AND_ASSIGN(TestFocusController);
81 };
82
83 class TestKeyboardControllerProxy : public KeyboardControllerProxy {
84  public:
85   TestKeyboardControllerProxy()
86       : input_method_(
87             ui::CreateInputMethod(NULL, gfx::kNullAcceleratedWidget)) {}
88
89   virtual ~TestKeyboardControllerProxy() {
90     // Destroy the window before the delegate.
91     window_.reset();
92   }
93
94   // Overridden from KeyboardControllerProxy:
95   virtual bool HasKeyboardWindow() const OVERRIDE { return window_; }
96   virtual aura::Window* GetKeyboardWindow() OVERRIDE {
97     if (!window_) {
98       window_.reset(new aura::Window(&delegate_));
99       window_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
100       window_->set_owned_by_parent(false);
101     }
102     return window_.get();
103   }
104   virtual content::BrowserContext* GetBrowserContext() OVERRIDE { return NULL; }
105   virtual ui::InputMethod* GetInputMethod() OVERRIDE {
106     return input_method_.get();
107   }
108   virtual void RequestAudioInput(content::WebContents* web_contents,
109       const content::MediaStreamRequest& request,
110       const content::MediaResponseCallback& callback) OVERRIDE { return; }
111   virtual void LoadSystemKeyboard() OVERRIDE {};
112   virtual void ReloadKeyboardIfNeeded() OVERRIDE {};
113
114  private:
115   scoped_ptr<aura::Window> window_;
116   aura::test::TestWindowDelegate delegate_;
117   scoped_ptr<ui::InputMethod> input_method_;
118
119   DISALLOW_COPY_AND_ASSIGN(TestKeyboardControllerProxy);
120 };
121
122 // Keeps a count of all the events a window receives.
123 class EventObserver : public ui::EventHandler {
124  public:
125   EventObserver() {}
126   virtual ~EventObserver() {}
127
128   int GetEventCount(ui::EventType type) {
129     return event_counts_[type];
130   }
131
132  private:
133   // Overridden from ui::EventHandler:
134   virtual void OnEvent(ui::Event* event) OVERRIDE {
135     ui::EventHandler::OnEvent(event);
136     event_counts_[event->type()]++;
137   }
138
139   std::map<ui::EventType, int> event_counts_;
140   DISALLOW_COPY_AND_ASSIGN(EventObserver);
141 };
142
143 class KeyboardContainerObserver : public aura::WindowObserver {
144  public:
145   explicit KeyboardContainerObserver(aura::Window* window) : window_(window) {
146     window_->AddObserver(this);
147   }
148   virtual ~KeyboardContainerObserver() {
149     window_->RemoveObserver(this);
150   }
151
152  private:
153   virtual void OnWindowVisibilityChanged(aura::Window* window,
154                                          bool visible) OVERRIDE {
155     if (!visible)
156       base::MessageLoop::current()->Quit();
157   }
158
159   aura::Window* window_;
160
161   DISALLOW_COPY_AND_ASSIGN(KeyboardContainerObserver);
162 };
163
164 }  // namespace
165
166 class KeyboardControllerTest : public testing::Test {
167  public:
168   KeyboardControllerTest() {}
169   virtual ~KeyboardControllerTest() {}
170
171   virtual void SetUp() OVERRIDE {
172     // The ContextFactory must exist before any Compositors are created.
173     bool enable_pixel_output = false;
174     ui::ContextFactory* context_factory =
175         ui::InitializeContextFactoryForTests(enable_pixel_output);
176
177     aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_));
178     aura_test_helper_->SetUp(context_factory);
179     new wm::DefaultActivationClient(aura_test_helper_->root_window());
180     ui::SetUpInputMethodFactoryForTesting();
181     if (::switches::IsTextInputFocusManagerEnabled())
182       ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(NULL);
183     focus_controller_.reset(new TestFocusController(root_window()));
184     proxy_ = new TestKeyboardControllerProxy();
185     controller_.reset(new KeyboardController(proxy_));
186   }
187
188   virtual void TearDown() OVERRIDE {
189     controller_.reset();
190     focus_controller_.reset();
191     if (::switches::IsTextInputFocusManagerEnabled())
192       ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(NULL);
193     aura_test_helper_->TearDown();
194     ui::TerminateContextFactoryForTests();
195   }
196
197   aura::Window* root_window() { return aura_test_helper_->root_window(); }
198   KeyboardControllerProxy* proxy() { return proxy_; }
199   KeyboardController* controller() { return controller_.get(); }
200
201   void ShowKeyboard() {
202     test_text_input_client_.reset(
203         new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT));
204     SetFocus(test_text_input_client_.get());
205   }
206
207  protected:
208   void SetFocus(ui::TextInputClient* client) {
209     ui::InputMethod* input_method = proxy()->GetInputMethod();
210     if (::switches::IsTextInputFocusManagerEnabled()) {
211       ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(client);
212       input_method->OnTextInputTypeChanged(client);
213     } else {
214       input_method->SetFocusedTextInputClient(client);
215     }
216     if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) {
217       input_method->ShowImeIfNeeded();
218       if (proxy_->GetKeyboardWindow()->bounds().height() == 0) {
219         // Set initial bounds for test keyboard window.
220         proxy_->GetKeyboardWindow()->SetBounds(
221             KeyboardBoundsFromWindowBounds(
222                 controller()->GetContainerWindow()->bounds(), 100));
223       }
224     }
225   }
226
227   bool WillHideKeyboard() {
228     return controller_->WillHideKeyboard();
229   }
230
231   bool ShouldEnableInsets(aura::Window* window) {
232     return controller_->ShouldEnableInsets(window);
233   }
234
235   base::MessageLoopForUI message_loop_;
236   scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
237   scoped_ptr<TestFocusController> focus_controller_;
238
239  private:
240   KeyboardControllerProxy* proxy_;
241   scoped_ptr<KeyboardController> controller_;
242   scoped_ptr<ui::TextInputClient> test_text_input_client_;
243   DISALLOW_COPY_AND_ASSIGN(KeyboardControllerTest);
244 };
245
246 TEST_F(KeyboardControllerTest, KeyboardSize) {
247   aura::Window* container(controller()->GetContainerWindow());
248   aura::Window* keyboard(proxy()->GetKeyboardWindow());
249   container->SetBounds(gfx::Rect(0, 0, 200, 100));
250
251   container->AddChild(keyboard);
252   const gfx::Rect& before_bounds = keyboard->bounds();
253   // The initial keyboard should be positioned at the bottom of container and
254   // has 0 height.
255   ASSERT_EQ(gfx::Rect(0, 100, 200, 0), before_bounds);
256
257   gfx::Rect new_bounds(
258       before_bounds.x(), before_bounds.y() - 50,
259       before_bounds.width(), 50);
260
261   keyboard->SetBounds(new_bounds);
262   ASSERT_EQ(new_bounds, keyboard->bounds());
263
264   // Mock a screen rotation.
265   container->SetBounds(gfx::Rect(0, 0, 100, 200));
266   // The above call should resize keyboard to new width while keeping the old
267   // height.
268   ASSERT_EQ(gfx::Rect(0, 150, 100, 50), keyboard->bounds());
269 }
270
271 // Tests that tapping/clicking inside the keyboard does not give it focus.
272 TEST_F(KeyboardControllerTest, ClickDoesNotFocusKeyboard) {
273   const gfx::Rect& root_bounds = root_window()->bounds();
274   aura::test::EventCountDelegate delegate;
275   scoped_ptr<aura::Window> window(new aura::Window(&delegate));
276   window->Init(aura::WINDOW_LAYER_NOT_DRAWN);
277   window->SetBounds(root_bounds);
278   root_window()->AddChild(window.get());
279   window->Show();
280   window->Focus();
281
282   aura::Window* keyboard_container(controller()->GetContainerWindow());
283   keyboard_container->SetBounds(root_bounds);
284
285   root_window()->AddChild(keyboard_container);
286   keyboard_container->Show();
287
288   ShowKeyboard();
289
290   EXPECT_TRUE(window->IsVisible());
291   EXPECT_TRUE(keyboard_container->IsVisible());
292   EXPECT_TRUE(window->HasFocus());
293   EXPECT_FALSE(keyboard_container->HasFocus());
294
295   // Click on the keyboard. Make sure the keyboard receives the event, but does
296   // not get focus.
297   EventObserver observer;
298   keyboard_container->AddPreTargetHandler(&observer);
299
300   ui::test::EventGenerator generator(root_window());
301   generator.MoveMouseTo(proxy()->GetKeyboardWindow()->bounds().CenterPoint());
302   generator.ClickLeftButton();
303   EXPECT_TRUE(window->HasFocus());
304   EXPECT_FALSE(keyboard_container->HasFocus());
305   EXPECT_EQ("0 0", delegate.GetMouseButtonCountsAndReset());
306   EXPECT_EQ(1, observer.GetEventCount(ui::ET_MOUSE_PRESSED));
307   EXPECT_EQ(1, observer.GetEventCount(ui::ET_MOUSE_RELEASED));
308
309   // Click outside of the keyboard. It should reach the window behind.
310   generator.MoveMouseTo(gfx::Point());
311   generator.ClickLeftButton();
312   EXPECT_EQ("1 1", delegate.GetMouseButtonCountsAndReset());
313   keyboard_container->RemovePreTargetHandler(&observer);
314 }
315
316 TEST_F(KeyboardControllerTest, EventHitTestingInContainer) {
317   const gfx::Rect& root_bounds = root_window()->bounds();
318   aura::test::EventCountDelegate delegate;
319   scoped_ptr<aura::Window> window(new aura::Window(&delegate));
320   window->Init(aura::WINDOW_LAYER_NOT_DRAWN);
321   window->SetBounds(root_bounds);
322   root_window()->AddChild(window.get());
323   window->Show();
324   window->Focus();
325
326   aura::Window* keyboard_container(controller()->GetContainerWindow());
327   keyboard_container->SetBounds(root_bounds);
328
329   root_window()->AddChild(keyboard_container);
330   keyboard_container->Show();
331
332   ShowKeyboard();
333
334   EXPECT_TRUE(window->IsVisible());
335   EXPECT_TRUE(keyboard_container->IsVisible());
336   EXPECT_TRUE(window->HasFocus());
337   EXPECT_FALSE(keyboard_container->HasFocus());
338
339   // Make sure hit testing works correctly while the keyboard is visible.
340   aura::Window* keyboard_window = proxy()->GetKeyboardWindow();
341   ui::EventTarget* root = root_window();
342   ui::EventTargeter* targeter = root->GetEventTargeter();
343   gfx::Point location = keyboard_window->bounds().CenterPoint();
344   ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, location, location, ui::EF_NONE,
345                         ui::EF_NONE);
346   EXPECT_EQ(keyboard_window, targeter->FindTargetForEvent(root, &mouse1));
347
348   location.set_y(keyboard_window->bounds().y() - 5);
349   ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, location, location, ui::EF_NONE,
350                         ui::EF_NONE);
351   EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root, &mouse2));
352 }
353
354 TEST_F(KeyboardControllerTest, KeyboardWindowCreation) {
355   const gfx::Rect& root_bounds = root_window()->bounds();
356   aura::test::EventCountDelegate delegate;
357   scoped_ptr<aura::Window> window(new aura::Window(&delegate));
358   window->Init(aura::WINDOW_LAYER_NOT_DRAWN);
359   window->SetBounds(root_bounds);
360   root_window()->AddChild(window.get());
361   window->Show();
362   window->Focus();
363
364   aura::Window* keyboard_container(controller()->GetContainerWindow());
365   keyboard_container->SetBounds(root_bounds);
366
367   root_window()->AddChild(keyboard_container);
368   keyboard_container->Show();
369
370   EXPECT_FALSE(proxy()->HasKeyboardWindow());
371
372   ui::EventTarget* root = root_window();
373   ui::EventTargeter* targeter = root->GetEventTargeter();
374   gfx::Point location(root_window()->bounds().width() / 2,
375                       root_window()->bounds().height() - 10);
376   ui::MouseEvent mouse(
377       ui::ET_MOUSE_MOVED, location, location, ui::EF_NONE, ui::EF_NONE);
378   EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root, &mouse));
379   EXPECT_FALSE(proxy()->HasKeyboardWindow());
380
381   EXPECT_EQ(
382       controller()->GetContainerWindow(),
383       controller()->GetContainerWindow()->GetEventHandlerForPoint(location));
384   EXPECT_FALSE(proxy()->HasKeyboardWindow());
385 }
386
387 TEST_F(KeyboardControllerTest, VisibilityChangeWithTextInputTypeChange) {
388   const gfx::Rect& root_bounds = root_window()->bounds();
389
390   ui::DummyTextInputClient input_client_0(ui::TEXT_INPUT_TYPE_TEXT);
391   ui::DummyTextInputClient input_client_1(ui::TEXT_INPUT_TYPE_TEXT);
392   ui::DummyTextInputClient input_client_2(ui::TEXT_INPUT_TYPE_TEXT);
393   ui::DummyTextInputClient no_input_client_0(ui::TEXT_INPUT_TYPE_NONE);
394   ui::DummyTextInputClient no_input_client_1(ui::TEXT_INPUT_TYPE_NONE);
395
396   aura::Window* keyboard_container(controller()->GetContainerWindow());
397   scoped_ptr<KeyboardContainerObserver> keyboard_container_observer(
398       new KeyboardContainerObserver(keyboard_container));
399   keyboard_container->SetBounds(root_bounds);
400   root_window()->AddChild(keyboard_container);
401
402   SetFocus(&input_client_0);
403
404   EXPECT_TRUE(keyboard_container->IsVisible());
405
406   SetFocus(&no_input_client_0);
407   // Keyboard should not immediately hide itself. It is delayed to avoid layout
408   // flicker when the focus of input field quickly change.
409   EXPECT_TRUE(keyboard_container->IsVisible());
410   EXPECT_TRUE(WillHideKeyboard());
411   // Wait for hide keyboard to finish.
412   base::MessageLoop::current()->Run();
413   EXPECT_FALSE(keyboard_container->IsVisible());
414
415   SetFocus(&input_client_1);
416   EXPECT_TRUE(keyboard_container->IsVisible());
417
418   // Schedule to hide keyboard.
419   SetFocus(&no_input_client_1);
420   EXPECT_TRUE(WillHideKeyboard());
421   // Cancel keyboard hide.
422   SetFocus(&input_client_2);
423
424   EXPECT_FALSE(WillHideKeyboard());
425   EXPECT_TRUE(keyboard_container->IsVisible());
426 }
427
428 // Test to prevent spurious overscroll boxes when changing tabs during keyboard
429 // hide. Refer to crbug.com/401670 for more context.
430 TEST_F(KeyboardControllerTest, CheckOverscrollInsetDuringVisibilityChange) {
431   const gfx::Rect& root_bounds = root_window()->bounds();
432
433   ui::DummyTextInputClient input_client(ui::TEXT_INPUT_TYPE_TEXT);
434   ui::DummyTextInputClient no_input_client(ui::TEXT_INPUT_TYPE_NONE);
435
436   aura::Window* keyboard_container(controller()->GetContainerWindow());
437   keyboard_container->SetBounds(root_bounds);
438   root_window()->AddChild(keyboard_container);
439
440   // Enable touch keyboard / overscroll mode to test insets.
441   keyboard::SetTouchKeyboardEnabled(true);
442   EXPECT_TRUE(keyboard::IsKeyboardOverscrollEnabled());
443
444   SetFocus(&input_client);
445   SetFocus(&no_input_client);
446   // Insets should not be enabled for new windows while keyboard is in the
447   // process of hiding when overscroll is enabled.
448   EXPECT_FALSE(ShouldEnableInsets(proxy()->GetKeyboardWindow()));
449   // Cancel keyboard hide.
450   SetFocus(&input_client);
451   // Insets should be enabled for new windows as hide was cancelled.
452   EXPECT_TRUE(ShouldEnableInsets(proxy()->GetKeyboardWindow()));
453 }
454
455 TEST_F(KeyboardControllerTest, AlwaysVisibleWhenLocked) {
456   const gfx::Rect& root_bounds = root_window()->bounds();
457
458   ui::DummyTextInputClient input_client_0(ui::TEXT_INPUT_TYPE_TEXT);
459   ui::DummyTextInputClient input_client_1(ui::TEXT_INPUT_TYPE_TEXT);
460   ui::DummyTextInputClient no_input_client_0(ui::TEXT_INPUT_TYPE_NONE);
461   ui::DummyTextInputClient no_input_client_1(ui::TEXT_INPUT_TYPE_NONE);
462
463   aura::Window* keyboard_container(controller()->GetContainerWindow());
464   scoped_ptr<KeyboardContainerObserver> keyboard_container_observer(
465       new KeyboardContainerObserver(keyboard_container));
466   keyboard_container->SetBounds(root_bounds);
467   root_window()->AddChild(keyboard_container);
468
469   SetFocus(&input_client_0);
470
471   EXPECT_TRUE(keyboard_container->IsVisible());
472
473   // Lock keyboard.
474   controller()->set_lock_keyboard(true);
475
476   SetFocus(&no_input_client_0);
477   // Keyboard should not try to hide itself as it is locked.
478   EXPECT_TRUE(keyboard_container->IsVisible());
479   EXPECT_FALSE(WillHideKeyboard());
480
481   SetFocus(&input_client_1);
482   EXPECT_TRUE(keyboard_container->IsVisible());
483
484   // Unlock keyboard.
485   controller()->set_lock_keyboard(false);
486
487   // Keyboard should hide when focus on no input client.
488   SetFocus(&no_input_client_1);
489   EXPECT_TRUE(WillHideKeyboard());
490
491   // Wait for hide keyboard to finish.
492   base::MessageLoop::current()->Run();
493   EXPECT_FALSE(keyboard_container->IsVisible());
494 }
495
496 class KeyboardControllerAnimationTest : public KeyboardControllerTest,
497                                         public KeyboardControllerObserver {
498  public:
499   KeyboardControllerAnimationTest() {}
500   virtual ~KeyboardControllerAnimationTest() {}
501
502   virtual void SetUp() OVERRIDE {
503     // We cannot short-circuit animations for this test.
504     ui::ScopedAnimationDurationScaleMode test_duration_mode(
505         ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
506
507     KeyboardControllerTest::SetUp();
508
509     const gfx::Rect& root_bounds = root_window()->bounds();
510     keyboard_container()->SetBounds(root_bounds);
511     root_window()->AddChild(keyboard_container());
512     controller()->AddObserver(this);
513   }
514
515   virtual void TearDown() OVERRIDE {
516     controller()->RemoveObserver(this);
517     KeyboardControllerTest::TearDown();
518   }
519
520  protected:
521   // KeyboardControllerObserver overrides
522   virtual void OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) OVERRIDE {
523     notified_bounds_ = new_bounds;
524   }
525
526   const gfx::Rect& notified_bounds() { return notified_bounds_; }
527
528   aura::Window* keyboard_container() {
529     return controller()->GetContainerWindow();
530   }
531
532   aura::Window* keyboard_window() {
533     return proxy()->GetKeyboardWindow();
534   }
535
536  private:
537   gfx::Rect notified_bounds_;
538
539   DISALLOW_COPY_AND_ASSIGN(KeyboardControllerAnimationTest);
540 };
541
542 // Tests virtual keyboard has correct show and hide animation.
543 TEST_F(KeyboardControllerAnimationTest, ContainerAnimation) {
544   ui::Layer* layer = keyboard_container()->layer();
545   ShowKeyboard();
546
547   // Keyboard container and window should immediately become visible before
548   // animation starts.
549   EXPECT_TRUE(keyboard_container()->IsVisible());
550   EXPECT_TRUE(keyboard_window()->IsVisible());
551   float show_start_opacity = layer->opacity();
552   gfx::Transform transform;
553   transform.Translate(0, kAnimationDistance);
554   EXPECT_EQ(transform, layer->transform());
555   EXPECT_EQ(gfx::Rect(), notified_bounds());
556
557   RunAnimationForLayer(layer);
558   EXPECT_TRUE(keyboard_container()->IsVisible());
559   EXPECT_TRUE(keyboard_window()->IsVisible());
560   float show_end_opacity = layer->opacity();
561   EXPECT_LT(show_start_opacity, show_end_opacity);
562   EXPECT_EQ(gfx::Transform(), layer->transform());
563   // KeyboardController should notify the bounds of keyboard window to its
564   // observers after show animation finished.
565   EXPECT_EQ(keyboard_window()->bounds(), notified_bounds());
566
567   // Directly hide keyboard without delay.
568   controller()->HideKeyboard(KeyboardController::HIDE_REASON_AUTOMATIC);
569   EXPECT_TRUE(keyboard_container()->IsVisible());
570   EXPECT_TRUE(keyboard_container()->layer()->visible());
571   EXPECT_TRUE(keyboard_window()->IsVisible());
572   float hide_start_opacity = layer->opacity();
573   // KeyboardController should notify the bounds of keyboard window to its
574   // observers before hide animation starts.
575   EXPECT_EQ(gfx::Rect(), notified_bounds());
576
577   RunAnimationForLayer(layer);
578   EXPECT_FALSE(keyboard_container()->IsVisible());
579   EXPECT_FALSE(keyboard_container()->layer()->visible());
580   EXPECT_FALSE(keyboard_window()->IsVisible());
581   float hide_end_opacity = layer->opacity();
582   EXPECT_GT(hide_start_opacity, hide_end_opacity);
583   EXPECT_EQ(transform, layer->transform());
584   EXPECT_EQ(gfx::Rect(), notified_bounds());
585 }
586
587 // Show keyboard during keyboard hide animation should abort the hide animation
588 // and the keyboard should animate in.
589 // Test for crbug.com/333284.
590 TEST_F(KeyboardControllerAnimationTest, ContainerShowWhileHide) {
591   ui::Layer* layer = keyboard_container()->layer();
592   ShowKeyboard();
593   RunAnimationForLayer(layer);
594
595   controller()->HideKeyboard(KeyboardController::HIDE_REASON_AUTOMATIC);
596   // Before hide animation finishes, show keyboard again.
597   ShowKeyboard();
598   RunAnimationForLayer(layer);
599   EXPECT_TRUE(keyboard_container()->IsVisible());
600   EXPECT_TRUE(keyboard_window()->IsVisible());
601   EXPECT_EQ(1.0, layer->opacity());
602   EXPECT_EQ(gfx::Transform(), layer->transform());
603 }
604
605 }  // namespace keyboard