ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / virtual_keyboard_browsertest.cc
bloba59c5120b1d73a7b953062ca9ba8cb921fefbd85
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 #include "chrome/browser/chromeos/extensions/virtual_keyboard_browsertest.h"
8 #include <vector>
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/client/aura_constants.h"
23 #include "ui/base/ime/input_method.h"
24 #include "ui/keyboard/keyboard_controller.h"
25 #include "ui/keyboard/keyboard_switches.h"
27 namespace {
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";
47 } // namespace
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 =
90 window->GetProperty(aura::client::kRootWindowInputMethodKey);
91 ASSERT_TRUE(input_method);
92 input_method->ShowImeIfNeeded();
95 content::RenderViewHost* VirtualKeyboardBrowserTest::GetKeyboardRenderViewHost(
96 const std::string& id) {
97 ShowVirtualKeyboard();
98 GURL url = extensions::Extension::GetBaseURLFromExtensionId(id);
99 scoped_ptr<content::RenderWidgetHostIterator> widgets(
100 content::RenderWidgetHost::GetRenderWidgetHosts());
101 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
102 if (widget->IsRenderView()) {
103 content::RenderViewHost* view = content::RenderViewHost::From(widget);
104 if (url == view->GetSiteInstance()->GetSiteURL()) {
105 content::WebContents* wc =
106 content::WebContents::FromRenderViewHost(view);
107 // Waits for virtual keyboard to load.
108 EXPECT_TRUE(content::WaitForLoadStop(wc));
109 return view;
113 LOG(ERROR) << "Extension not found:" << url;
114 return NULL;
117 void VirtualKeyboardBrowserTest::InjectJavascript(const base::FilePath& dir,
118 const base::FilePath& file) {
119 base::FilePath path = ui_test_utils::GetTestFilePath(dir, file);
120 std::string library_content;
121 ASSERT_TRUE(base::ReadFileToString(path, &library_content)) << path.value();
122 utf8_content_.append(library_content);
123 utf8_content_.append(";\n");
126 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, TypingTest) {
127 RunTest(base::FilePath(FILE_PATH_LITERAL("typing_test.js")),
128 VirtualKeyboardBrowserTestConfig());
131 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, LayoutTest) {
132 RunTest(base::FilePath(FILE_PATH_LITERAL("layout_test.js")),
133 VirtualKeyboardBrowserTestConfig());
136 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, ModifierTest) {
137 RunTest(base::FilePath(FILE_PATH_LITERAL("modifier_test.js")),
138 VirtualKeyboardBrowserTestConfig());
141 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, HideKeyboardKeyTest) {
142 RunTest(base::FilePath(FILE_PATH_LITERAL("hide_keyboard_key_test.js")),
143 VirtualKeyboardBrowserTestConfig());
146 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, IsKeyboardLoaded) {
147 content::RenderViewHost* keyboard_rvh =
148 GetKeyboardRenderViewHost(kExtensionId);
149 ASSERT_TRUE(keyboard_rvh);
150 bool loaded = false;
151 std::string script = "!!chrome.virtualKeyboardPrivate";
152 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
153 keyboard_rvh,
154 "window.domAutomationController.send(" + script + ");",
155 &loaded));
156 // Catches the regression in crbug.com/308653.
157 ASSERT_TRUE(loaded);
160 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, 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(
173 base::FilePath(),
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")));
182 std::string script;
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(
188 browser_rvh,
189 "success ? verifyInput('a') : waitForInput('a');",
190 &success));
191 ASSERT_TRUE(success);
194 // TODO(kevers|rsadam|bshe): Add UI tests for remaining virtual keyboard
195 // functionality.