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 "base/command_line.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/render_widget_host_iterator.h"
16 #include "content/public/browser/site_instance.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "ui/keyboard/keyboard_switches.h"
20
21 namespace {
22
23 const base::FilePath kWebuiTestDir =
24 base::FilePath(FILE_PATH_LITERAL("webui"));
25
26 const base::FilePath kVirtualKeyboardTestDir =
27 base::FilePath(FILE_PATH_LITERAL("chromeos/virtual_keyboard"));
28
29 const base::FilePath kMockController =
30 base::FilePath(FILE_PATH_LITERAL("mock_controller.js"));
31
32 const base::FilePath kMockTimer =
33 base::FilePath(FILE_PATH_LITERAL("mock_timer.js"));
34
35 const base::FilePath kBaseKeyboardTestFramework =
36 base::FilePath(FILE_PATH_LITERAL("virtual_keyboard_test_base.js"));
37
38 } // namespace
39
40 class VirtualKeyboardBrowserTest : public InProcessBrowserTest {
41 public:
42
43 // Ensure that the virtual keyboard is enabled.
SetUpCommandLine(CommandLine * command_line)44 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
45 command_line->AppendSwitch(
46 keyboard::switches::kEnableVirtualKeyboard);
47 }
48
49 //Injects javascript in |file| into the keyboard page and runs test methods.
RunTest(const base::FilePath & file)50 void RunTest(const base::FilePath& file) {
51 ui_test_utils::NavigateToURL(browser(), GURL("chrome://keyboard"));
52
53 content::RenderViewHost* rvh = browser()->tab_strip_model()
54 ->GetActiveWebContents()->GetRenderViewHost();
55 ASSERT_TRUE(rvh);
56
57 // Inject testing scripts.
58 InjectJavascript(kWebuiTestDir, kMockController);
59 InjectJavascript(kWebuiTestDir, kMockTimer);
60 InjectJavascript(kVirtualKeyboardTestDir, kBaseKeyboardTestFramework);
61 InjectJavascript(kVirtualKeyboardTestDir, file);
62
63 ASSERT_TRUE(content::ExecuteScript(rvh, utf8_content_));
64
65 // Inject DOM-automation test harness and run tests.
66 std::vector<int> resource_ids;
67 EXPECT_TRUE(ExecuteWebUIResourceTest(rvh, resource_ids));
68 }
69
GetKeyboardRenderViewHost()70 content::RenderViewHost* GetKeyboardRenderViewHost() {
71 std::string kVirtualKeyboardURL =
72 "chrome-extension://mppnpdlheglhdfmldimlhpnegondlapf/";
73 scoped_ptr<content::RenderWidgetHostIterator> widgets(
74 content::RenderWidgetHost::GetRenderWidgetHosts());
75 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
76 if (widget->IsRenderView()) {
77 content::RenderViewHost* view = content::RenderViewHost::From(widget);
78 std::string url = view->GetSiteInstance()->GetSiteURL().spec();
79 if (url == kVirtualKeyboardURL) {
80 content::WebContents* wc =
81 content::WebContents::FromRenderViewHost(view);
82 // Waits for Polymer to load.
83 content::WaitForLoadStop(wc);
84 return view;
85 }
86 }
87 }
88 return NULL;
89 }
90
91 private:
92
93 // Injects javascript into the keyboard page. The test |file| is in
94 // directory |dir| relative to the root testing directory.
InjectJavascript(const base::FilePath & dir,const base::FilePath & file)95 void InjectJavascript(const base::FilePath& dir,
96 const base::FilePath& file) {
97 base::FilePath path = ui_test_utils::GetTestFilePath(dir, file);
98 std::string library_content;
99 ASSERT_TRUE(base::ReadFileToString(path, &library_content))
100 << path.value();
101 utf8_content_.append(library_content);
102 utf8_content_.append(";\n");
103 }
104
105 std::string utf8_content_;
106 };
107
IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest,AttributesTest)108 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, AttributesTest) {
109 RunTest(base::FilePath(FILE_PATH_LITERAL("attributes_test.js")));
110 }
111
IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest,TypingTest)112 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, TypingTest) {
113 RunTest(base::FilePath(FILE_PATH_LITERAL("typing_test.js")));
114 }
115
IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest,ControlKeysTest)116 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, ControlKeysTest) {
117 RunTest(base::FilePath(FILE_PATH_LITERAL("control_keys_test.js")));
118 }
119
IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest,KeysetTransitionTest)120 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, KeysetTransitionTest) {
121 RunTest(base::FilePath(FILE_PATH_LITERAL("keyset_transition_test.js")));
122 }
123
IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest,IsKeyboardLoaded)124 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, IsKeyboardLoaded) {
125 content::RenderViewHost* keyboard_rvh = GetKeyboardRenderViewHost();
126 ASSERT_TRUE(keyboard_rvh);
127 bool loaded = false;
128 std::string script = "!!chrome.virtualKeyboardPrivate";
129 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
130 keyboard_rvh,
131 "window.domAutomationController.send(" + script + ");",
132 &loaded));
133 // Catches the regression in crbug.com/308653.
134 ASSERT_TRUE(loaded);
135 }
136
IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest,EndToEndTest)137 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, EndToEndTest) {
138 // Get the virtual keyboard's render view host.
139 content::RenderViewHost* keyboard_rvh = GetKeyboardRenderViewHost();
140 ASSERT_TRUE(keyboard_rvh);
141
142 // Get the test page's render view host.
143 content::RenderViewHost* browser_rvh = browser()->tab_strip_model()->
144 GetActiveWebContents()->GetRenderViewHost();
145 ASSERT_TRUE(browser_rvh);
146
147 // Set up the test page.
148 GURL url = ui_test_utils::GetTestUrl(
149 base::FilePath(),
150 base::FilePath(FILE_PATH_LITERAL(
151 "chromeos/virtual_keyboard/end_to_end_test.html")));
152 ui_test_utils::NavigateToURL(browser(), url);
153
154 // Press 'a' on keyboard.
155 base::FilePath path = ui_test_utils::GetTestFilePath(
156 kVirtualKeyboardTestDir,
157 base::FilePath(FILE_PATH_LITERAL("end_to_end_test.js")));
158 std::string script;
159 ASSERT_TRUE(base::ReadFileToString(path, &script));
160 EXPECT_TRUE(content::ExecuteScript(keyboard_rvh, script));
161 // Verify 'a' appeared on test page.
162 bool success = false;
163 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
164 browser_rvh,
165 "success ? verifyInput('a') : waitForInput('a');",
166 &success));
167 ASSERT_TRUE(success);
168 }
169
170 // TODO(kevers|rsadam|bshe): Add UI tests for remaining virtual keyboard
171 // functionality.
172