2 * Copyright 2013 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
6 #include "chrome/browser/chromeos/extensions/virtual_keyboard_browsertest.h"
10 #include "ash/shell.h"
11 #include "base/command_line.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/render_widget_host_iterator.h"
18 #include "content/public/browser/site_instance.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/browser_test_utils.h"
21 #include "extensions/common/extension.h"
22 #include "ui/aura/window_tree_host.h"
23 #include "ui/base/ime/input_method.h"
24 #include "ui/keyboard/keyboard_controller.h"
25 #include "ui/keyboard/keyboard_switches.h"
28 const base::FilePath::CharType kWebuiTestDir
[] = FILE_PATH_LITERAL("webui");
30 const base::FilePath::CharType kMockController
[] =
31 FILE_PATH_LITERAL("mock_controller.js");
33 const base::FilePath::CharType kMockTimer
[] =
34 FILE_PATH_LITERAL("mock_timer.js");
36 const char kVirtualKeyboardTestDir
[] = "chromeos/virtual_keyboard";
38 const char kBaseKeyboardTestFramework
[] = "virtual_keyboard_test_base.js";
40 const char kExtensionId
[] = "mppnpdlheglhdfmldimlhpnegondlapf";
42 // Loading the virtual keyboard with id=none suppresses asynchronous loading of
43 // layout and configuration assets. This allows the javascript test code to be
44 // injected ahead of the keyboard initialization.
45 const char kVirtualKeyboardURL
[] = "chrome://keyboard?id=none";
49 VirtualKeyboardBrowserTestConfig::VirtualKeyboardBrowserTestConfig()
50 : base_framework_(kBaseKeyboardTestFramework
),
51 extension_id_(kExtensionId
),
52 test_dir_(kVirtualKeyboardTestDir
),
53 url_(kVirtualKeyboardURL
) {
56 VirtualKeyboardBrowserTestConfig::~VirtualKeyboardBrowserTestConfig() {};
58 void VirtualKeyboardBrowserTest::SetUpCommandLine(
59 base::CommandLine
* command_line
) {
60 command_line
->AppendSwitch(keyboard::switches::kEnableVirtualKeyboard
);
63 void VirtualKeyboardBrowserTest::RunTest(
64 const base::FilePath
& file
,
65 const VirtualKeyboardBrowserTestConfig
& config
) {
66 ui_test_utils::NavigateToURL(browser(), GURL(config
.url_
));
67 content::WebContents
* web_contents
=
68 browser()->tab_strip_model()->GetActiveWebContents();
69 EXPECT_TRUE(content::WaitForLoadStop(web_contents
));
70 ASSERT_TRUE(web_contents
);
72 // Inject testing scripts.
73 InjectJavascript(base::FilePath(kWebuiTestDir
),
74 base::FilePath(kMockController
));
75 InjectJavascript(base::FilePath(kWebuiTestDir
), base::FilePath(kMockTimer
));
76 InjectJavascript(base::FilePath(FILE_PATH_LITERAL(config
.test_dir_
)),
77 base::FilePath(FILE_PATH_LITERAL(config
.base_framework_
)));
78 InjectJavascript(base::FilePath(FILE_PATH_LITERAL(config
.test_dir_
)), file
);
80 ASSERT_TRUE(content::ExecuteScript(web_contents
, utf8_content_
));
82 // Inject DOM-automation test harness and run tests.
83 std::vector
<int> resource_ids
;
84 EXPECT_TRUE(ExecuteWebUIResourceTest(web_contents
, resource_ids
));
87 void VirtualKeyboardBrowserTest::ShowVirtualKeyboard() {
88 aura::Window
* window
= ash::Shell::GetPrimaryRootWindow();
89 ui::InputMethod
* input_method
= window
->GetHost()->GetInputMethod();
90 ASSERT_TRUE(input_method
);
91 input_method
->ShowImeIfNeeded();
94 content::RenderViewHost
* VirtualKeyboardBrowserTest::GetKeyboardRenderViewHost(
95 const std::string
& id
) {
96 ShowVirtualKeyboard();
97 GURL url
= extensions::Extension::GetBaseURLFromExtensionId(id
);
98 scoped_ptr
<content::RenderWidgetHostIterator
> widgets(
99 content::RenderWidgetHost::GetRenderWidgetHosts());
100 while (content::RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
101 if (widget
->IsRenderView()) {
102 content::RenderViewHost
* view
= content::RenderViewHost::From(widget
);
103 if (url
== view
->GetSiteInstance()->GetSiteURL()) {
104 content::WebContents
* wc
=
105 content::WebContents::FromRenderViewHost(view
);
106 // Waits for virtual keyboard to load.
107 EXPECT_TRUE(content::WaitForLoadStop(wc
));
112 LOG(ERROR
) << "Extension not found:" << url
;
116 void VirtualKeyboardBrowserTest::InjectJavascript(const base::FilePath
& dir
,
117 const base::FilePath
& file
) {
118 base::FilePath path
= ui_test_utils::GetTestFilePath(dir
, file
);
119 std::string library_content
;
120 ASSERT_TRUE(base::ReadFileToString(path
, &library_content
)) << path
.value();
121 utf8_content_
.append(library_content
);
122 utf8_content_
.append(";\n");
125 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest
, TypingTest
) {
126 RunTest(base::FilePath(FILE_PATH_LITERAL("typing_test.js")),
127 VirtualKeyboardBrowserTestConfig());
130 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest
, LayoutTest
) {
131 RunTest(base::FilePath(FILE_PATH_LITERAL("layout_test.js")),
132 VirtualKeyboardBrowserTestConfig());
135 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest
, ModifierTest
) {
136 RunTest(base::FilePath(FILE_PATH_LITERAL("modifier_test.js")),
137 VirtualKeyboardBrowserTestConfig());
140 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest
, HideKeyboardKeyTest
) {
141 RunTest(base::FilePath(FILE_PATH_LITERAL("hide_keyboard_key_test.js")),
142 VirtualKeyboardBrowserTestConfig());
145 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest
, IsKeyboardLoaded
) {
146 content::RenderViewHost
* keyboard_rvh
=
147 GetKeyboardRenderViewHost(kExtensionId
);
148 ASSERT_TRUE(keyboard_rvh
);
150 std::string script
= "!!chrome.virtualKeyboardPrivate";
151 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
153 "window.domAutomationController.send(" + script
+ ");",
155 // Catches the regression in crbug.com/308653.
159 // Disabled; http://crbug.com/515596
160 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest
, DISABLED_EndToEndTest
) {
161 // Get the virtual keyboard's render view host.
162 content::RenderViewHost
* keyboard_rvh
=
163 GetKeyboardRenderViewHost(kExtensionId
);
164 ASSERT_TRUE(keyboard_rvh
);
166 // Get the test page's render view host.
167 content::RenderViewHost
* browser_rvh
= browser()->tab_strip_model()->
168 GetActiveWebContents()->GetRenderViewHost();
169 ASSERT_TRUE(browser_rvh
);
171 // Set up the test page.
172 GURL url
= ui_test_utils::GetTestUrl(
174 base::FilePath(FILE_PATH_LITERAL(
175 "chromeos/virtual_keyboard/end_to_end_test.html")));
176 ui_test_utils::NavigateToURL(browser(), url
);
178 // Press 'a' on keyboard.
179 base::FilePath path
= ui_test_utils::GetTestFilePath(
180 base::FilePath(FILE_PATH_LITERAL(kVirtualKeyboardTestDir
)),
181 base::FilePath(FILE_PATH_LITERAL("end_to_end_test.js")));
183 ASSERT_TRUE(base::ReadFileToString(path
, &script
));
184 EXPECT_TRUE(content::ExecuteScript(keyboard_rvh
, script
));
185 // Verify 'a' appeared on test page.
186 bool success
= false;
187 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
189 "success ? verifyInput('a') : waitForInput('a');",
191 ASSERT_TRUE(success
);
194 // TODO(kevers|rsadam|bshe): Add UI tests for remaining virtual keyboard