virtual_keyboard_browsertest.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
1/*
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.
5 */
6
7#include <vector>
8
9#include "ash/shell.h"
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"
24
25namespace {
26
27const base::FilePath kWebuiTestDir =
28    base::FilePath(FILE_PATH_LITERAL("webui"));
29
30const base::FilePath kVirtualKeyboardTestDir =
31    base::FilePath(FILE_PATH_LITERAL("chromeos/virtual_keyboard"));
32
33const base::FilePath kMockController =
34    base::FilePath(FILE_PATH_LITERAL("mock_controller.js"));
35
36const base::FilePath kMockTimer =
37    base::FilePath(FILE_PATH_LITERAL("mock_timer.js"));
38
39const base::FilePath kBaseKeyboardTestFramework =
40    base::FilePath(FILE_PATH_LITERAL("virtual_keyboard_test_base.js"));
41
42}  // namespace
43
44class VirtualKeyboardBrowserTest : public InProcessBrowserTest {
45 public:
46
47  // Ensure that the virtual keyboard is enabled.
48  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
49    command_line->AppendSwitch(
50        keyboard::switches::kEnableVirtualKeyboard);
51  }
52
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"));
56
57    content::WebContents* web_contents =
58        browser()->tab_strip_model()->GetActiveWebContents();
59    ASSERT_TRUE(web_contents);
60
61    // Inject testing scripts.
62    InjectJavascript(kWebuiTestDir, kMockController);
63    InjectJavascript(kWebuiTestDir, kMockTimer);
64    InjectJavascript(kVirtualKeyboardTestDir, kBaseKeyboardTestFramework);
65    InjectJavascript(kVirtualKeyboardTestDir, file);
66
67    ASSERT_TRUE(content::ExecuteScript(web_contents, utf8_content_));
68
69    // Inject DOM-automation test harness and run tests.
70    std::vector<int> resource_ids;
71    EXPECT_TRUE(ExecuteWebUIResourceTest(web_contents, resource_ids));
72  }
73
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();
80  }
81
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);
97          return view;
98        }
99      }
100    }
101    return NULL;
102  }
103
104 private:
105
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))
113        << path.value();
114    utf8_content_.append(library_content);
115    utf8_content_.append(";\n");
116  }
117
118  std::string utf8_content_;
119};
120
121// Broken due to Polymer version mismatch http://crbug.com/357946.
122IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, DISABLED_AttributesTest) {
123  RunTest(base::FilePath(FILE_PATH_LITERAL("attributes_test.js")));
124}
125
126// Broken due to Polymer version mismatch http://crbug.com/357946.
127IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, DISABLED_TypingTest) {
128  RunTest(base::FilePath(FILE_PATH_LITERAL("typing_test.js")));
129}
130
131// Broken due to Polymer version mismatch http://crbug.com/357946.
132IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, DISABLED_ControlKeysTest) {
133  RunTest(base::FilePath(FILE_PATH_LITERAL("control_keys_test.js")));
134}
135
136// Broken due to Polymer version mismatch http://crbug.com/357946.
137IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest,
138                       DISABLED_HideKeyboardKeyTest) {
139  RunTest(base::FilePath(FILE_PATH_LITERAL("hide_keyboard_key_test.js")));
140}
141
142// Broken due to Polymer version mismatch http://crbug.com/357946.
143IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest,
144                       DISABLED_KeysetTransitionTest) {
145  RunTest(base::FilePath(FILE_PATH_LITERAL("keyset_transition_test.js")));
146}
147
148// Broken due to Polymer version mismatch http://crbug.com/357946.
149IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, DISABLED_IsKeyboardLoaded) {
150  content::RenderViewHost* keyboard_rvh = GetKeyboardRenderViewHost();
151  ASSERT_TRUE(keyboard_rvh);
152  bool loaded = false;
153  std::string script = "!!chrome.virtualKeyboardPrivate";
154  EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
155      keyboard_rvh,
156      "window.domAutomationController.send(" + script + ");",
157      &loaded));
158  // Catches the regression in crbug.com/308653.
159  ASSERT_TRUE(loaded);
160}
161
162// Broken due to Polymer version mismatch http://crbug.com/357946.
163IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, DISABLED_EndToEndTest) {
164  // Get the virtual keyboard's render view host.
165  content::RenderViewHost* keyboard_rvh = GetKeyboardRenderViewHost();
166  ASSERT_TRUE(keyboard_rvh);
167
168  // Get the test page's render view host.
169  content::RenderViewHost* browser_rvh = browser()->tab_strip_model()->
170      GetActiveWebContents()->GetRenderViewHost();
171  ASSERT_TRUE(browser_rvh);
172
173  // Set up the test page.
174  GURL url = ui_test_utils::GetTestUrl(
175      base::FilePath(),
176      base::FilePath(FILE_PATH_LITERAL(
177          "chromeos/virtual_keyboard/end_to_end_test.html")));
178  ui_test_utils::NavigateToURL(browser(), url);
179
180  // Press 'a' on keyboard.
181  base::FilePath path = ui_test_utils::GetTestFilePath(
182      kVirtualKeyboardTestDir,
183      base::FilePath(FILE_PATH_LITERAL("end_to_end_test.js")));
184  std::string script;
185  ASSERT_TRUE(base::ReadFileToString(path, &script));
186  EXPECT_TRUE(content::ExecuteScript(keyboard_rvh, script));
187  // Verify 'a' appeared on test page.
188  bool success = false;
189  EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
190      browser_rvh,
191      "success ? verifyInput('a') : waitForInput('a');",
192      &success));
193  ASSERT_TRUE(success);
194}
195
196// TODO(kevers|rsadam|bshe):  Add UI tests for remaining virtual keyboard
197// functionality.
198