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.
5 #include "base/command_line.h"
6 #include "chrome/test/base/in_process_browser_test.h"
7 #include "content/public/browser/web_contents.h"
8 #include "ui/base/ime/dummy_text_input_client.h"
9 #include "ui/base/ime/input_method.h"
10 #include "ui/base/ime/input_method_factory.h"
11 #include "ui/keyboard/keyboard_constants.h"
12 #include "ui/keyboard/keyboard_controller.h"
13 #include "ui/keyboard/keyboard_controller_proxy.h"
14 #include "ui/keyboard/keyboard_switches.h"
15 #include "ui/keyboard/keyboard_util.h"
18 const int kKeyboardHeightForTest
= 100;
21 class VirtualKeyboardWebContentTest
: public InProcessBrowserTest
{
23 VirtualKeyboardWebContentTest() {};
24 virtual ~VirtualKeyboardWebContentTest() {};
26 virtual void SetUp() OVERRIDE
{
27 ui::SetUpInputMethodFactoryForTesting();
28 InProcessBrowserTest::SetUp();
31 // Ensure that the virtual keyboard is enabled.
32 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
33 command_line
->AppendSwitch(
34 keyboard::switches::kEnableVirtualKeyboard
);
37 keyboard::KeyboardControllerProxy
* proxy() {
38 return keyboard::KeyboardController::GetInstance()->proxy();
42 void FocusEditableNodeAndShowKeyboard() {
43 client
.reset(new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT
));
44 ui::InputMethod
* input_method
= proxy()->GetInputMethod();
45 input_method
->SetFocusedTextInputClient(client
.get());
46 input_method
->ShowImeIfNeeded();
47 ResizeKeyboardWindow();
50 void FocusNonEditableNode() {
51 client
.reset(new ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_NONE
));
52 ui::InputMethod
* input_method
= proxy()->GetInputMethod();
53 input_method
->SetFocusedTextInputClient(client
.get());
56 void MockEnableIMEInDifferentExtension(const std::string
& url
) {
57 keyboard::SetOverrideContentUrl(GURL(url
));
58 keyboard::KeyboardController::GetInstance()->Reload();
59 ResizeKeyboardWindow();
62 bool IsKeyboardVisible() const {
63 return keyboard::KeyboardController::GetInstance()->keyboard_visible();
67 // Mock window.resizeTo that is expected to be called after navigate to a new
69 void ResizeKeyboardWindow() {
70 gfx::Rect bounds
= proxy()->GetKeyboardWindow()->bounds();
71 proxy()->GetKeyboardWindow()->SetBounds(gfx::Rect(
73 bounds
.bottom() - kKeyboardHeightForTest
,
75 kKeyboardHeightForTest
));
78 scoped_ptr
<ui::DummyTextInputClient
> client
;
80 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardWebContentTest
);
83 // Test for crbug.com/404340. After enabling an IME in a different extension,
84 // its virtual keyboard should not become visible if previous one is not.
85 IN_PROC_BROWSER_TEST_F(VirtualKeyboardWebContentTest
,
86 EnableIMEInDifferentExtension
) {
87 FocusEditableNodeAndShowKeyboard();
88 EXPECT_TRUE(IsKeyboardVisible());
89 FocusNonEditableNode();
90 EXPECT_FALSE(IsKeyboardVisible());
92 MockEnableIMEInDifferentExtension("chrome-extension://domain-1");
93 // Keyboard should not become visible if previous keyboard is not.
94 EXPECT_FALSE(IsKeyboardVisible());
96 FocusEditableNodeAndShowKeyboard();
97 // Keyboard should become visible after focus on an editable node.
98 EXPECT_TRUE(IsKeyboardVisible());
100 // Simulate hide keyboard by pressing hide key on the virtual keyboard.
101 keyboard::KeyboardController::GetInstance()->HideKeyboard(
102 keyboard::KeyboardController::HIDE_REASON_MANUAL
);
103 EXPECT_FALSE(IsKeyboardVisible());
105 MockEnableIMEInDifferentExtension("chrome-extension://domain-2");
106 // Keyboard should not become visible if previous keyboard is not, even if it
107 // is currently focused on an editable node.
108 EXPECT_FALSE(IsKeyboardVisible());