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.
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"
23 const base::FilePath kWebuiTestDir
=
24 base::FilePath(FILE_PATH_LITERAL("webui"));
26 const base::FilePath kVirtualKeyboardTestDir
=
27 base::FilePath(FILE_PATH_LITERAL("chromeos/virtual_keyboard"));
29 const base::FilePath kMockController
=
30 base::FilePath(FILE_PATH_LITERAL("mock_controller.js"));
32 const base::FilePath kMockTimer
=
33 base::FilePath(FILE_PATH_LITERAL("mock_timer.js"));
35 const base::FilePath kBaseKeyboardTestFramework
=
36 base::FilePath(FILE_PATH_LITERAL("virtual_keyboard_test_base.js"));
40 class VirtualKeyboardBrowserTest
: public InProcessBrowserTest
{
43 // Ensure that the virtual keyboard is enabled.
44 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
45 command_line
->AppendSwitch(
46 keyboard::switches::kEnableVirtualKeyboard
);
49 //Injects javascript in |file| into the keyboard page and runs test methods.
50 void RunTest(const base::FilePath
& file
) {
51 ui_test_utils::NavigateToURL(browser(), GURL("chrome://keyboard"));
53 content::RenderViewHost
* rvh
= browser()->tab_strip_model()
54 ->GetActiveWebContents()->GetRenderViewHost();
57 // Inject testing scripts.
58 InjectJavascript(kWebuiTestDir
, kMockController
);
59 InjectJavascript(kWebuiTestDir
, kMockTimer
);
60 InjectJavascript(kVirtualKeyboardTestDir
, kBaseKeyboardTestFramework
);
61 InjectJavascript(kVirtualKeyboardTestDir
, file
);
63 ASSERT_TRUE(content::ExecuteScript(rvh
, utf8_content_
));
65 // Inject DOM-automation test harness and run tests.
66 std::vector
<int> resource_ids
;
67 EXPECT_TRUE(ExecuteWebUIResourceTest(rvh
, resource_ids
));
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
);
93 // Injects javascript into the keyboard page. The test |file| is in
94 // directory |dir| relative to the root testing directory.
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
))
101 utf8_content_
.append(library_content
);
102 utf8_content_
.append(";\n");
105 std::string utf8_content_
;
108 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest
, AttributesTest
) {
109 RunTest(base::FilePath(FILE_PATH_LITERAL("attributes_test.js")));
112 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest
, TypingTest
) {
113 RunTest(base::FilePath(FILE_PATH_LITERAL("typing_test.js")));
116 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest
, ControlKeysTest
) {
117 RunTest(base::FilePath(FILE_PATH_LITERAL("control_keys_test.js")));
120 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest
, HideKeyboardKeyTest
) {
121 RunTest(base::FilePath(FILE_PATH_LITERAL("hide_keyboard_key_test.js")));
124 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest
, KeysetTransitionTest
) {
125 RunTest(base::FilePath(FILE_PATH_LITERAL("keyset_transition_test.js")));
128 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest
, IsKeyboardLoaded
) {
129 content::RenderViewHost
* keyboard_rvh
= GetKeyboardRenderViewHost();
130 ASSERT_TRUE(keyboard_rvh
);
132 std::string script
= "!!chrome.virtualKeyboardPrivate";
133 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
135 "window.domAutomationController.send(" + script
+ ");",
137 // Catches the regression in crbug.com/308653.
141 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest
, EndToEndTest
) {
142 // Get the virtual keyboard's render view host.
143 content::RenderViewHost
* keyboard_rvh
= GetKeyboardRenderViewHost();
144 ASSERT_TRUE(keyboard_rvh
);
146 // Get the test page's render view host.
147 content::RenderViewHost
* browser_rvh
= browser()->tab_strip_model()->
148 GetActiveWebContents()->GetRenderViewHost();
149 ASSERT_TRUE(browser_rvh
);
151 // Set up the test page.
152 GURL url
= ui_test_utils::GetTestUrl(
154 base::FilePath(FILE_PATH_LITERAL(
155 "chromeos/virtual_keyboard/end_to_end_test.html")));
156 ui_test_utils::NavigateToURL(browser(), url
);
158 // Press 'a' on keyboard.
159 base::FilePath path
= ui_test_utils::GetTestFilePath(
160 kVirtualKeyboardTestDir
,
161 base::FilePath(FILE_PATH_LITERAL("end_to_end_test.js")));
163 ASSERT_TRUE(base::ReadFileToString(path
, &script
));
164 EXPECT_TRUE(content::ExecuteScript(keyboard_rvh
, script
));
165 // Verify 'a' appeared on test page.
166 bool success
= false;
167 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
169 "success ? verifyInput('a') : waitForInput('a');",
171 ASSERT_TRUE(success
);
174 // TODO(kevers|rsadam|bshe): Add UI tests for remaining virtual keyboard