1 // Copyright 2014 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 "chrome/test/base/in_process_browser_test.h"
8 #include "content/public/browser/web_contents.h"
9 #include "ui/base/ime/dummy_text_input_client.h"
10 #include "ui/base/ime/input_method.h"
11 #include "ui/base/ime/input_method_factory.h"
12 #include "ui/keyboard/keyboard_constants.h"
13 #include "ui/keyboard/keyboard_controller.h"
14 #include "ui/keyboard/keyboard_controller_proxy.h"
15 #include "ui/keyboard/keyboard_switches.h"
16 #include "ui/keyboard/keyboard_util.h"
19 const int kKeyboardHeightForTest
= 100;
22 class VirtualKeyboardWebContentTest
: public InProcessBrowserTest
{
24 VirtualKeyboardWebContentTest() {}
25 ~VirtualKeyboardWebContentTest() override
{}
27 void SetUp() override
{
28 ui::SetUpInputMethodFactoryForTesting();
29 InProcessBrowserTest::SetUp();
32 // Ensure that the virtual keyboard is enabled.
33 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
34 command_line
->AppendSwitch(
35 keyboard::switches::kEnableVirtualKeyboard
);
38 keyboard::KeyboardControllerProxy
* proxy() {
39 return keyboard::KeyboardController::GetInstance()->proxy();
43 void FocusEditableNodeAndShowKeyboard(const gfx::Rect
& init_bounds
) {
44 client
.reset(new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT
));
45 ui::InputMethod
* input_method
= proxy()->GetInputMethod();
46 input_method
->SetFocusedTextInputClient(client
.get());
47 input_method
->ShowImeIfNeeded();
48 // Mock window.resizeTo that is expected to be called after navigate to a
49 // new virtual keyboard.
50 proxy()->GetKeyboardWindow()->SetBounds(init_bounds
);
53 void FocusNonEditableNode() {
54 client
.reset(new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_NONE
));
55 ui::InputMethod
* input_method
= proxy()->GetInputMethod();
56 input_method
->SetFocusedTextInputClient(client
.get());
59 void MockEnableIMEInDifferentExtension(const std::string
& url
,
60 const gfx::Rect
& init_bounds
) {
61 keyboard::SetOverrideContentUrl(GURL(url
));
62 keyboard::KeyboardController::GetInstance()->Reload();
63 // Mock window.resizeTo that is expected to be called after navigate to a
64 // new virtual keyboard.
65 proxy()->GetKeyboardWindow()->SetBounds(init_bounds
);
68 bool IsKeyboardVisible() const {
69 return keyboard::KeyboardController::GetInstance()->keyboard_visible();
73 scoped_ptr
<ui::DummyTextInputClient
> client
;
75 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardWebContentTest
);
78 // Test for crbug.com/404340. After enabling an IME in a different extension,
79 // its virtual keyboard should not become visible if previous one is not.
80 IN_PROC_BROWSER_TEST_F(VirtualKeyboardWebContentTest
,
81 EnableIMEInDifferentExtension
) {
82 gfx::Rect
test_bounds(0, 0, 0, kKeyboardHeightForTest
);
83 FocusEditableNodeAndShowKeyboard(test_bounds
);
84 EXPECT_TRUE(IsKeyboardVisible());
85 FocusNonEditableNode();
86 EXPECT_FALSE(IsKeyboardVisible());
88 MockEnableIMEInDifferentExtension("chrome-extension://domain-1", test_bounds
);
89 // Keyboard should not become visible if previous keyboard is not.
90 EXPECT_FALSE(IsKeyboardVisible());
92 FocusEditableNodeAndShowKeyboard(test_bounds
);
93 // Keyboard should become visible after focus on an editable node.
94 EXPECT_TRUE(IsKeyboardVisible());
96 // Simulate hide keyboard by pressing hide key on the virtual keyboard.
97 keyboard::KeyboardController::GetInstance()->HideKeyboard(
98 keyboard::KeyboardController::HIDE_REASON_MANUAL
);
99 EXPECT_FALSE(IsKeyboardVisible());
101 MockEnableIMEInDifferentExtension("chrome-extension://domain-2", test_bounds
);
102 // Keyboard should not become visible if previous keyboard is not, even if it
103 // is currently focused on an editable node.
104 EXPECT_FALSE(IsKeyboardVisible());
107 // Test for crbug.com/489366. In FLOATING mode, switch to a new IME in a
108 // different extension should exist FLOATIN mode and position the new IME in
110 IN_PROC_BROWSER_TEST_F(VirtualKeyboardWebContentTest
,
111 IMEInDifferentExtensionNotCentered
) {
112 gfx::Rect
test_bounds(0, 0, 0, kKeyboardHeightForTest
);
113 FocusEditableNodeAndShowKeyboard(test_bounds
);
114 keyboard::KeyboardController
* controller
=
115 keyboard::KeyboardController::GetInstance();
116 const gfx::Rect
& screen_bounds
= ash::Shell::GetPrimaryRootWindow()->bounds();
117 gfx::Rect keyboard_bounds
= controller
->GetContainerWindow()->bounds();
118 EXPECT_EQ(kKeyboardHeightForTest
, keyboard_bounds
.height());
119 EXPECT_EQ(screen_bounds
.height(),
120 keyboard_bounds
.height() + keyboard_bounds
.y());
121 controller
->SetKeyboardMode(keyboard::FLOATING
);
122 // Move keyboard to a random place.
123 proxy()->GetKeyboardWindow()->SetBounds(gfx::Rect(50, 50, 50, 50));
124 EXPECT_EQ(gfx::Rect(50, 50, 50, 50),
125 controller
->GetContainerWindow()->bounds());
127 MockEnableIMEInDifferentExtension("chrome-extension://domain-1", test_bounds
);
128 keyboard_bounds
= controller
->GetContainerWindow()->bounds();
129 EXPECT_EQ(kKeyboardHeightForTest
, keyboard_bounds
.height());
130 EXPECT_EQ(screen_bounds
.height(),
131 keyboard_bounds
.height() + keyboard_bounds
.y());