virtual_keyboard_browsertest.cc revision e5d81f57cb97b3b6b7fccc9c5610d21eb81db09d
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 121IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, AttributesTest) { 122 RunTest(base::FilePath(FILE_PATH_LITERAL("attributes_test.js"))); 123} 124 125IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, TypingTest) { 126 RunTest(base::FilePath(FILE_PATH_LITERAL("typing_test.js"))); 127} 128 129IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, ControlKeysTest) { 130 RunTest(base::FilePath(FILE_PATH_LITERAL("control_keys_test.js"))); 131} 132 133IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, HideKeyboardKeyTest) { 134 RunTest(base::FilePath(FILE_PATH_LITERAL("hide_keyboard_key_test.js"))); 135} 136 137IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, KeysetTransitionTest) { 138 RunTest(base::FilePath(FILE_PATH_LITERAL("keyset_transition_test.js"))); 139} 140 141IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, IsKeyboardLoaded) { 142 content::RenderViewHost* keyboard_rvh = GetKeyboardRenderViewHost(); 143 ASSERT_TRUE(keyboard_rvh); 144 bool loaded = false; 145 std::string script = "!!chrome.virtualKeyboardPrivate"; 146 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( 147 keyboard_rvh, 148 "window.domAutomationController.send(" + script + ");", 149 &loaded)); 150 // Catches the regression in crbug.com/308653. 151 ASSERT_TRUE(loaded); 152} 153 154IN_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); 158 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); 163 164 // Set up the test page. 165 GURL url = ui_test_utils::GetTestUrl( 166 base::FilePath(), 167 base::FilePath(FILE_PATH_LITERAL( 168 "chromeos/virtual_keyboard/end_to_end_test.html"))); 169 ui_test_utils::NavigateToURL(browser(), url); 170 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"))); 175 std::string script; 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( 181 browser_rvh, 182 "success ? verifyInput('a') : waitForInput('a');", 183 &success)); 184 ASSERT_TRUE(success); 185} 186 187// TODO(kevers|rsadam|bshe): Add UI tests for remaining virtual keyboard 188// functionality. 189