Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 #include "chrome/browser/chromeos/extensions/virtual_keyboard_browsertest.h" | |
| 6 | 7 |
| 7 #include <vector> | 8 #include <vector> |
| 8 | 9 |
| 9 #include "ash/shell.h" | 10 #include "ash/shell.h" |
| 10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
| 16 #include "content/public/browser/render_widget_host_iterator.h" | 17 #include "content/public/browser/render_widget_host_iterator.h" |
| 17 #include "content/public/browser/site_instance.h" | 18 #include "content/public/browser/site_instance.h" |
| 18 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/test/browser_test_utils.h" | 20 #include "content/public/test/browser_test_utils.h" |
| 21 #include "extensions/common/extension.h" | |
| 20 #include "ui/aura/client/aura_constants.h" | 22 #include "ui/aura/client/aura_constants.h" |
| 21 #include "ui/base/ime/input_method.h" | 23 #include "ui/base/ime/input_method.h" |
| 22 #include "ui/keyboard/keyboard_controller.h" | 24 #include "ui/keyboard/keyboard_controller.h" |
| 23 #include "ui/keyboard/keyboard_switches.h" | 25 #include "ui/keyboard/keyboard_switches.h" |
| 24 | 26 |
| 25 namespace { | 27 namespace { |
| 28 const base::FilePath::CharType kWebuiTestDir[] = FILE_PATH_LITERAL("webui"); | |
| 26 | 29 |
| 27 const base::FilePath kWebuiTestDir = | 30 const base::FilePath::CharType kMockController[] = |
| 28 base::FilePath(FILE_PATH_LITERAL("webui")); | 31 FILE_PATH_LITERAL("mock_controller.js"); |
| 29 | 32 |
| 30 const base::FilePath kVirtualKeyboardTestDir = | 33 const base::FilePath::CharType kMockTimer[] = |
| 31 base::FilePath(FILE_PATH_LITERAL("chromeos/virtual_keyboard")); | 34 FILE_PATH_LITERAL("mock_timer.js"); |
| 32 | 35 |
| 33 const base::FilePath kMockController = | 36 const char kVirtualKeyboardTestDir[] = "chromeos/virtual_keyboard"; |
| 34 base::FilePath(FILE_PATH_LITERAL("mock_controller.js")); | |
| 35 | 37 |
| 36 const base::FilePath kMockTimer = | 38 const char kBaseKeyboardTestFramework[] = "virtual_keyboard_test_base.js"; |
| 37 base::FilePath(FILE_PATH_LITERAL("mock_timer.js")); | |
| 38 | 39 |
| 39 const base::FilePath kBaseKeyboardTestFramework = | 40 const char kExtensionId[] = "mppnpdlheglhdfmldimlhpnegondlapf"; |
| 40 base::FilePath(FILE_PATH_LITERAL("virtual_keyboard_test_base.js")); | 41 |
| 42 const char kVirtualKeyboardURL[] = "chrome://keyboard"; | |
| 41 | 43 |
| 42 } // namespace | 44 } // namespace |
| 43 | 45 |
| 44 class VirtualKeyboardBrowserTest : public InProcessBrowserTest { | 46 VirtualKeyboardBrowserTestConfig::VirtualKeyboardBrowserTestConfig() |
| 45 public: | 47 : base_framework_(kBaseKeyboardTestFramework), |
| 48 extension_id_(kExtensionId), | |
| 49 test_dir_(kVirtualKeyboardTestDir), | |
| 50 url_(kVirtualKeyboardURL) { | |
| 51 } | |
| 46 | 52 |
| 47 // Ensure that the virtual keyboard is enabled. | 53 VirtualKeyboardBrowserTestConfig::~VirtualKeyboardBrowserTestConfig() {}; |
| 48 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
| 49 command_line->AppendSwitch( | |
| 50 keyboard::switches::kEnableVirtualKeyboard); | |
| 51 } | |
| 52 | 54 |
| 53 // Injects javascript in |file| into the keyboard page and runs test methods. | 55 void VirtualKeyboardBrowserTest::SetUpCommandLine(CommandLine* command_line) { |
| 54 void RunTest(const base::FilePath& file) { | 56 command_line->AppendSwitch(keyboard::switches::kEnableVirtualKeyboard); |
| 55 ui_test_utils::NavigateToURL(browser(), GURL("chrome://keyboard")); | 57 } |
| 56 | 58 |
| 57 content::WebContents* web_contents = | 59 void VirtualKeyboardBrowserTest::RunTest( |
| 58 browser()->tab_strip_model()->GetActiveWebContents(); | 60 const base::FilePath& file, |
| 59 ASSERT_TRUE(web_contents); | 61 const VirtualKeyboardBrowserTestConfig& config) { |
| 62 ui_test_utils::NavigateToURL(browser(), GURL(config.url_)); | |
| 63 content::WebContents* web_contents = | |
| 64 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 65 content::WaitForLoadStop(web_contents); | |
| 66 ASSERT_TRUE(web_contents); | |
| 60 | 67 |
| 61 // Inject testing scripts. | 68 // Inject testing scripts. |
| 62 InjectJavascript(kWebuiTestDir, kMockController); | 69 InjectJavascript(base::FilePath(kWebuiTestDir), |
| 63 InjectJavascript(kWebuiTestDir, kMockTimer); | 70 base::FilePath(kMockController)); |
| 64 InjectJavascript(kVirtualKeyboardTestDir, kBaseKeyboardTestFramework); | 71 InjectJavascript(base::FilePath(kWebuiTestDir), base::FilePath(kMockTimer)); |
| 65 InjectJavascript(kVirtualKeyboardTestDir, file); | 72 InjectJavascript(base::FilePath(FILE_PATH_LITERAL(config.test_dir_)), |
| 73 base::FilePath(FILE_PATH_LITERAL(config.base_framework_))); | |
| 74 InjectJavascript(base::FilePath(FILE_PATH_LITERAL(config.test_dir_)), file); | |
| 66 | 75 |
| 67 ASSERT_TRUE(content::ExecuteScript(web_contents, utf8_content_)); | 76 ASSERT_TRUE(content::ExecuteScript(web_contents, utf8_content_)); |
| 68 | 77 |
| 69 // Inject DOM-automation test harness and run tests. | 78 // Inject DOM-automation test harness and run tests. |
| 70 std::vector<int> resource_ids; | 79 std::vector<int> resource_ids; |
| 71 EXPECT_TRUE(ExecuteWebUIResourceTest(web_contents, resource_ids)); | 80 EXPECT_TRUE(ExecuteWebUIResourceTest(web_contents, resource_ids)); |
| 72 } | 81 } |
| 73 | 82 |
| 74 void showVirtualKeyboard() { | 83 void VirtualKeyboardBrowserTest::ShowVirtualKeyboard() { |
| 75 aura::Window *window = ash::Shell::GetPrimaryRootWindow(); | 84 aura::Window* window = ash::Shell::GetPrimaryRootWindow(); |
| 76 ui::InputMethod* input_method = window->GetProperty( | 85 ui::InputMethod* input_method = |
| 77 aura::client::kRootWindowInputMethodKey); | 86 window->GetProperty(aura::client::kRootWindowInputMethodKey); |
| 78 ASSERT_TRUE(input_method); | 87 ASSERT_TRUE(input_method); |
| 79 input_method->ShowImeIfNeeded(); | 88 input_method->ShowImeIfNeeded(); |
| 80 } | 89 } |
| 81 | 90 |
| 82 content::RenderViewHost* GetKeyboardRenderViewHost() { | 91 content::RenderViewHost* VirtualKeyboardBrowserTest::GetKeyboardRenderViewHost( |
| 83 showVirtualKeyboard(); | 92 const std::string& id) { |
| 84 std::string kVirtualKeyboardURL = | 93 ShowVirtualKeyboard(); |
| 85 "chrome-extension://mppnpdlheglhdfmldimlhpnegondlapf/"; | 94 std::string url = extensions::Extension::GetBaseURLFromExtensionId(id).spec(); |
|
Jeffrey Yasskin
2014/05/08 00:04:16
You can remove both .spec()s if you make this a GU
rsadam
2014/05/08 15:06:44
Done.
| |
| 86 scoped_ptr<content::RenderWidgetHostIterator> widgets( | 95 scoped_ptr<content::RenderWidgetHostIterator> widgets( |
| 87 content::RenderWidgetHost::GetRenderWidgetHosts()); | 96 content::RenderWidgetHost::GetRenderWidgetHosts()); |
| 88 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) { | 97 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) { |
| 89 if (widget->IsRenderView()) { | 98 if (widget->IsRenderView()) { |
| 90 content::RenderViewHost* view = content::RenderViewHost::From(widget); | 99 content::RenderViewHost* view = content::RenderViewHost::From(widget); |
| 91 std::string url = view->GetSiteInstance()->GetSiteURL().spec(); | 100 if (url == view->GetSiteInstance()->GetSiteURL().spec()) { |
| 92 if (url == kVirtualKeyboardURL) { | 101 content::WebContents* wc = |
| 93 content::WebContents* wc = | 102 content::WebContents::FromRenderViewHost(view); |
| 94 content::WebContents::FromRenderViewHost(view); | 103 // Waits for Polymer to load. |
| 95 // Waits for Polymer to load. | 104 content::WaitForLoadStop(wc); |
| 96 content::WaitForLoadStop(wc); | 105 return view; |
| 97 return view; | |
| 98 } | |
| 99 } | 106 } |
| 100 } | 107 } |
| 101 return NULL; | |
| 102 } | 108 } |
| 109 LOG(ERROR) << "Extension not found:" << url; | |
| 110 return NULL; | |
| 111 } | |
| 103 | 112 |
| 104 private: | 113 void VirtualKeyboardBrowserTest::InjectJavascript(const base::FilePath& dir, |
| 105 | 114 const base::FilePath& file) { |
| 106 // Injects javascript into the keyboard page. The test |file| is in | 115 base::FilePath path = ui_test_utils::GetTestFilePath(dir, file); |
| 107 // directory |dir| relative to the root testing directory. | 116 std::string library_content; |
| 108 void InjectJavascript(const base::FilePath& dir, | 117 ASSERT_TRUE(base::ReadFileToString(path, &library_content)) << path.value(); |
| 109 const base::FilePath& file) { | 118 utf8_content_.append(library_content); |
| 110 base::FilePath path = ui_test_utils::GetTestFilePath(dir, file); | 119 utf8_content_.append(";\n"); |
| 111 std::string library_content; | 120 } |
| 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 |
| 121 // crbug.com/367817. Either this feature or just the test are depending | 122 // crbug.com/367817. Either this feature or just the test are depending |
| 122 // on the presense of Object.observe which is presently disabled by default. | 123 // on the presense of Object.observe which is presently disabled by default. |
| 123 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, DISABLED_AttributesTest) { | 124 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, DISABLED_AttributesTest) { |
| 124 RunTest(base::FilePath(FILE_PATH_LITERAL("attributes_test.js"))); | 125 RunTest(base::FilePath(FILE_PATH_LITERAL("attributes_test.js")), |
| 126 VirtualKeyboardBrowserTestConfig()); | |
| 125 } | 127 } |
| 126 | 128 |
| 127 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, TypingTest) { | 129 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, TypingTest) { |
| 128 RunTest(base::FilePath(FILE_PATH_LITERAL("typing_test.js"))); | 130 RunTest(base::FilePath(FILE_PATH_LITERAL("typing_test.js")), |
| 131 VirtualKeyboardBrowserTestConfig()); | |
| 129 } | 132 } |
| 130 | 133 |
| 131 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, ControlKeysTest) { | 134 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, ControlKeysTest) { |
| 132 RunTest(base::FilePath(FILE_PATH_LITERAL("control_keys_test.js"))); | 135 RunTest(base::FilePath(FILE_PATH_LITERAL("control_keys_test.js")), |
| 136 VirtualKeyboardBrowserTestConfig()); | |
| 133 } | 137 } |
| 134 | 138 |
| 135 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, HideKeyboardKeyTest) { | 139 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, HideKeyboardKeyTest) { |
| 136 RunTest(base::FilePath(FILE_PATH_LITERAL("hide_keyboard_key_test.js"))); | 140 RunTest(base::FilePath(FILE_PATH_LITERAL("hide_keyboard_key_test.js")), |
| 141 VirtualKeyboardBrowserTestConfig()); | |
| 137 } | 142 } |
| 138 | 143 |
| 139 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, KeysetTransitionTest) { | 144 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, KeysetTransitionTest) { |
| 140 RunTest(base::FilePath(FILE_PATH_LITERAL("keyset_transition_test.js"))); | 145 RunTest(base::FilePath(FILE_PATH_LITERAL("keyset_transition_test.js")), |
| 146 VirtualKeyboardBrowserTestConfig()); | |
| 141 } | 147 } |
| 142 | 148 |
| 143 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, IsKeyboardLoaded) { | 149 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, IsKeyboardLoaded) { |
| 144 content::RenderViewHost* keyboard_rvh = GetKeyboardRenderViewHost(); | 150 content::RenderViewHost* keyboard_rvh = |
| 151 GetKeyboardRenderViewHost(kExtensionId); | |
| 145 ASSERT_TRUE(keyboard_rvh); | 152 ASSERT_TRUE(keyboard_rvh); |
| 146 bool loaded = false; | 153 bool loaded = false; |
| 147 std::string script = "!!chrome.virtualKeyboardPrivate"; | 154 std::string script = "!!chrome.virtualKeyboardPrivate"; |
| 148 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | 155 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 149 keyboard_rvh, | 156 keyboard_rvh, |
| 150 "window.domAutomationController.send(" + script + ");", | 157 "window.domAutomationController.send(" + script + ");", |
| 151 &loaded)); | 158 &loaded)); |
| 152 // Catches the regression in crbug.com/308653. | 159 // Catches the regression in crbug.com/308653. |
| 153 ASSERT_TRUE(loaded); | 160 ASSERT_TRUE(loaded); |
| 154 } | 161 } |
| 155 | 162 |
| 156 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, DISABLED_EndToEndTest) { | 163 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, DISABLED_EndToEndTest) { |
| 157 // Get the virtual keyboard's render view host. | 164 // Get the virtual keyboard's render view host. |
| 158 content::RenderViewHost* keyboard_rvh = GetKeyboardRenderViewHost(); | 165 content::RenderViewHost* keyboard_rvh = |
| 166 GetKeyboardRenderViewHost(kExtensionId); | |
| 159 ASSERT_TRUE(keyboard_rvh); | 167 ASSERT_TRUE(keyboard_rvh); |
| 160 | 168 |
| 161 // Get the test page's render view host. | 169 // Get the test page's render view host. |
| 162 content::RenderViewHost* browser_rvh = browser()->tab_strip_model()-> | 170 content::RenderViewHost* browser_rvh = browser()->tab_strip_model()-> |
| 163 GetActiveWebContents()->GetRenderViewHost(); | 171 GetActiveWebContents()->GetRenderViewHost(); |
| 164 ASSERT_TRUE(browser_rvh); | 172 ASSERT_TRUE(browser_rvh); |
| 165 | 173 |
| 166 // Set up the test page. | 174 // Set up the test page. |
| 167 GURL url = ui_test_utils::GetTestUrl( | 175 GURL url = ui_test_utils::GetTestUrl( |
| 168 base::FilePath(), | 176 base::FilePath(), |
| 169 base::FilePath(FILE_PATH_LITERAL( | 177 base::FilePath(FILE_PATH_LITERAL( |
| 170 "chromeos/virtual_keyboard/end_to_end_test.html"))); | 178 "chromeos/virtual_keyboard/end_to_end_test.html"))); |
| 171 ui_test_utils::NavigateToURL(browser(), url); | 179 ui_test_utils::NavigateToURL(browser(), url); |
| 172 | 180 |
| 173 // Press 'a' on keyboard. | 181 // Press 'a' on keyboard. |
| 174 base::FilePath path = ui_test_utils::GetTestFilePath( | 182 base::FilePath path = ui_test_utils::GetTestFilePath( |
| 175 kVirtualKeyboardTestDir, | 183 base::FilePath(FILE_PATH_LITERAL(kVirtualKeyboardTestDir)), |
| 176 base::FilePath(FILE_PATH_LITERAL("end_to_end_test.js"))); | 184 base::FilePath(FILE_PATH_LITERAL("end_to_end_test.js"))); |
| 177 std::string script; | 185 std::string script; |
| 178 ASSERT_TRUE(base::ReadFileToString(path, &script)); | 186 ASSERT_TRUE(base::ReadFileToString(path, &script)); |
| 179 EXPECT_TRUE(content::ExecuteScript(keyboard_rvh, script)); | 187 EXPECT_TRUE(content::ExecuteScript(keyboard_rvh, script)); |
| 180 // Verify 'a' appeared on test page. | 188 // Verify 'a' appeared on test page. |
| 181 bool success = false; | 189 bool success = false; |
| 182 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | 190 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 183 browser_rvh, | 191 browser_rvh, |
| 184 "success ? verifyInput('a') : waitForInput('a');", | 192 "success ? verifyInput('a') : waitForInput('a');", |
| 185 &success)); | 193 &success)); |
| 186 ASSERT_TRUE(success); | 194 ASSERT_TRUE(success); |
| 187 } | 195 } |
| 188 | 196 |
| 189 // TODO(kevers|rsadam|bshe): Add UI tests for remaining virtual keyboard | 197 // TODO(kevers|rsadam|bshe): Add UI tests for remaining virtual keyboard |
| 190 // functionality. | 198 // functionality. |
| OLD | NEW |