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