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.
10 #include "base/command_line.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/render_widget_host_iterator.h"
17 #include "content/public/browser/site_instance.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/test/browser_test_utils.h"
20 #include "ui/aura/client/aura_constants.h"
21 #include "ui/base/ime/input_method.h"
22 #include "ui/keyboard/keyboard_controller.h"
23 #include "ui/keyboard/keyboard_switches.h"
27 const base::FilePath kWebuiTestDir =
28 base::FilePath(FILE_PATH_LITERAL("webui"));
30 const base::FilePath kVirtualKeyboardTestDir =
31 base::FilePath(FILE_PATH_LITERAL("chromeos/virtual_keyboard"));
33 const base::FilePath kMockController =
34 base::FilePath(FILE_PATH_LITERAL("mock_controller.js"));
36 const base::FilePath kMockTimer =
37 base::FilePath(FILE_PATH_LITERAL("mock_timer.js"));
39 const base::FilePath kBaseKeyboardTestFramework =
40 base::FilePath(FILE_PATH_LITERAL("virtual_keyboard_test_base.js"));
44 class VirtualKeyboardBrowserTest : public InProcessBrowserTest {
47 // Ensure that the virtual keyboard is enabled.
48 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
49 command_line->AppendSwitch(
50 keyboard::switches::kEnableVirtualKeyboard);
53 // Injects javascript in |file| into the keyboard page and runs test methods.
54 void RunTest(const base::FilePath& file) {
55 ui_test_utils::NavigateToURL(browser(), GURL("chrome://keyboard"));
57 content::WebContents* web_contents =
58 browser()->tab_strip_model()->GetActiveWebContents();
59 ASSERT_TRUE(web_contents);
61 // Inject testing scripts.
62 InjectJavascript(kWebuiTestDir, kMockController);
63 InjectJavascript(kWebuiTestDir, kMockTimer);
64 InjectJavascript(kVirtualKeyboardTestDir, kBaseKeyboardTestFramework);
65 InjectJavascript(kVirtualKeyboardTestDir, file);
67 ASSERT_TRUE(content::ExecuteScript(web_contents, utf8_content_));
69 // Inject DOM-automation test harness and run tests.
70 std::vector<int> resource_ids;
71 EXPECT_TRUE(ExecuteWebUIResourceTest(web_contents, resource_ids));
74 void showVirtualKeyboard() {
75 aura::Window *window = ash::Shell::GetPrimaryRootWindow();
76 ui::InputMethod* input_method = window->GetProperty(
77 aura::client::kRootWindowInputMethodKey);
78 ASSERT_TRUE(input_method);
79 input_method->ShowImeIfNeeded();
82 content::RenderViewHost* GetKeyboardRenderViewHost() {
83 showVirtualKeyboard();
84 std::string kVirtualKeyboardURL =
85 "chrome-extension://mppnpdlheglhdfmldimlhpnegondlapf/";
86 scoped_ptr<content::RenderWidgetHostIterator> widgets(
87 content::RenderWidgetHost::GetRenderWidgetHosts());
88 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
89 if (widget->IsRenderView()) {
90 content::RenderViewHost* view = content::RenderViewHost::From(widget);
91 std::string url = view->GetSiteInstance()->GetSiteURL().spec();
92 if (url == kVirtualKeyboardURL) {
93 content::WebContents* wc =
94 content::WebContents::FromRenderViewHost(view);
95 // Waits for Polymer to load.
96 content::WaitForLoadStop(wc);
106 // Injects javascript into the keyboard page. The test |file| is in
107 // directory |dir| relative to the root testing directory.
108 void InjectJavascript(const base::FilePath& dir,
109 const base::FilePath& file) {
110 base::FilePath path = ui_test_utils::GetTestFilePath(dir, file);
111 std::string library_content;
112 ASSERT_TRUE(base::ReadFileToString(path, &library_content))
114 utf8_content_.append(library_content);
115 utf8_content_.append(";\n");
118 std::string utf8_content_;
121 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, AttributesTest) {
122 RunTest(base::FilePath(FILE_PATH_LITERAL("attributes_test.js")));
125 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, TypingTest) {
126 RunTest(base::FilePath(FILE_PATH_LITERAL("typing_test.js")));
129 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, ControlKeysTest) {
130 RunTest(base::FilePath(FILE_PATH_LITERAL("control_keys_test.js")));
133 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, HideKeyboardKeyTest) {
134 RunTest(base::FilePath(FILE_PATH_LITERAL("hide_keyboard_key_test.js")));
137 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, KeysetTransitionTest) {
138 RunTest(base::FilePath(FILE_PATH_LITERAL("keyset_transition_test.js")));
141 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, IsKeyboardLoaded) {
142 content::RenderViewHost* keyboard_rvh = GetKeyboardRenderViewHost();
143 ASSERT_TRUE(keyboard_rvh);
145 std::string script = "!!chrome.virtualKeyboardPrivate";
146 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
148 "window.domAutomationController.send(" + script + ");",
150 // Catches the regression in crbug.com/308653.
154 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, EndToEndTest) {
155 // Get the virtual keyboard's render view host.
156 content::RenderViewHost* keyboard_rvh = GetKeyboardRenderViewHost();
157 ASSERT_TRUE(keyboard_rvh);
159 // Get the test page's render view host.
160 content::RenderViewHost* browser_rvh = browser()->tab_strip_model()->
161 GetActiveWebContents()->GetRenderViewHost();
162 ASSERT_TRUE(browser_rvh);
164 // Set up the test page.
165 GURL url = ui_test_utils::GetTestUrl(
167 base::FilePath(FILE_PATH_LITERAL(
168 "chromeos/virtual_keyboard/end_to_end_test.html")));
169 ui_test_utils::NavigateToURL(browser(), url);
171 // Press 'a' on keyboard.
172 base::FilePath path = ui_test_utils::GetTestFilePath(
173 kVirtualKeyboardTestDir,
174 base::FilePath(FILE_PATH_LITERAL("end_to_end_test.js")));
176 ASSERT_TRUE(base::ReadFileToString(path, &script));
177 EXPECT_TRUE(content::ExecuteScript(keyboard_rvh, script));
178 // Verify 'a' appeared on test page.
179 bool success = false;
180 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
182 "success ? verifyInput('a') : waitForInput('a');",
184 ASSERT_TRUE(success);
187 // TODO(kevers|rsadam|bshe): Add UI tests for remaining virtual keyboard