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" |
(...skipping 18 matching lines...) Expand all Loading... |
34 base::FilePath(FILE_PATH_LITERAL("mock_controller.js")); | 35 base::FilePath(FILE_PATH_LITERAL("mock_controller.js")); |
35 | 36 |
36 const base::FilePath kMockTimer = | 37 const base::FilePath kMockTimer = |
37 base::FilePath(FILE_PATH_LITERAL("mock_timer.js")); | 38 base::FilePath(FILE_PATH_LITERAL("mock_timer.js")); |
38 | 39 |
39 const base::FilePath kBaseKeyboardTestFramework = | 40 const base::FilePath kBaseKeyboardTestFramework = |
40 base::FilePath(FILE_PATH_LITERAL("virtual_keyboard_test_base.js")); | 41 base::FilePath(FILE_PATH_LITERAL("virtual_keyboard_test_base.js")); |
41 | 42 |
42 } // namespace | 43 } // namespace |
43 | 44 |
44 class VirtualKeyboardBrowserTest : public InProcessBrowserTest { | 45 void VirtualKeyboardBrowserTest::SetUpCommandLine(CommandLine* command_line) { |
45 public: | 46 command_line->AppendSwitch(keyboard::switches::kEnableVirtualKeyboard); |
| 47 } |
46 | 48 |
47 // Ensure that the virtual keyboard is enabled. | 49 GURL VirtualKeyboardBrowserTest::GetURL() { |
48 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 50 return GURL("chrome://keyboard"); |
49 command_line->AppendSwitch( | 51 } |
50 keyboard::switches::kEnableVirtualKeyboard); | |
51 } | |
52 | 52 |
53 // Injects javascript in |file| into the keyboard page and runs test methods. | 53 content::WebContents* VirtualKeyboardBrowserTest::NavigateToWebContents() { |
54 void RunTest(const base::FilePath& file) { | 54 ui_test_utils::NavigateToURL(browser(), GetURL()); |
55 ui_test_utils::NavigateToURL(browser(), GURL("chrome://keyboard")); | 55 content::WebContents* wc = |
| 56 browser()->tab_strip_model()->GetActiveWebContents(); |
| 57 content::WaitForLoadStop(wc); |
| 58 return wc; |
| 59 } |
56 | 60 |
57 content::WebContents* web_contents = | 61 base::FilePath VirtualKeyboardBrowserTest::GetTestDir() { |
58 browser()->tab_strip_model()->GetActiveWebContents(); | 62 return kVirtualKeyboardTestDir; |
59 ASSERT_TRUE(web_contents); | 63 } |
60 | 64 |
61 // Inject testing scripts. | 65 void VirtualKeyboardBrowserTest::RunTest(const base::FilePath& file) { |
62 InjectJavascript(kWebuiTestDir, kMockController); | 66 content::WebContents* web_contents = NavigateToWebContents(); |
63 InjectJavascript(kWebuiTestDir, kMockTimer); | 67 ASSERT_TRUE(web_contents); |
64 InjectJavascript(kVirtualKeyboardTestDir, kBaseKeyboardTestFramework); | |
65 InjectJavascript(kVirtualKeyboardTestDir, file); | |
66 | 68 |
67 ASSERT_TRUE(content::ExecuteScript(web_contents, utf8_content_)); | 69 // Inject testing scripts. |
| 70 InjectJavascript(kWebuiTestDir, kMockController); |
| 71 InjectJavascript(kWebuiTestDir, kMockTimer); |
| 72 InjectJavascript(GetTestDir(), GetBaseFrameWork()); |
| 73 InjectJavascript(GetTestDir(), file); |
68 | 74 |
69 // Inject DOM-automation test harness and run tests. | 75 ASSERT_TRUE(content::ExecuteScript(web_contents, utf8_content_)); |
70 std::vector<int> resource_ids; | |
71 EXPECT_TRUE(ExecuteWebUIResourceTest(web_contents, resource_ids)); | |
72 } | |
73 | 76 |
74 void showVirtualKeyboard() { | 77 // Inject DOM-automation test harness and run tests. |
75 aura::Window *window = ash::Shell::GetPrimaryRootWindow(); | 78 std::vector<int> resource_ids; |
76 ui::InputMethod* input_method = window->GetProperty( | 79 EXPECT_TRUE(ExecuteWebUIResourceTest(web_contents, resource_ids)); |
77 aura::client::kRootWindowInputMethodKey); | 80 } |
78 ASSERT_TRUE(input_method); | |
79 input_method->ShowImeIfNeeded(); | |
80 } | |
81 | 81 |
82 content::RenderViewHost* GetKeyboardRenderViewHost() { | 82 void VirtualKeyboardBrowserTest::ShowVirtualKeyboard() { |
83 showVirtualKeyboard(); | 83 aura::Window* window = ash::Shell::GetPrimaryRootWindow(); |
84 std::string kVirtualKeyboardURL = | 84 ui::InputMethod* input_method = |
85 "chrome-extension://mppnpdlheglhdfmldimlhpnegondlapf/"; | 85 window->GetProperty(aura::client::kRootWindowInputMethodKey); |
86 scoped_ptr<content::RenderWidgetHostIterator> widgets( | 86 ASSERT_TRUE(input_method); |
87 content::RenderWidgetHost::GetRenderWidgetHosts()); | 87 input_method->ShowImeIfNeeded(); |
88 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) { | 88 } |
89 if (widget->IsRenderView()) { | 89 |
90 content::RenderViewHost* view = content::RenderViewHost::From(widget); | 90 std::string VirtualKeyboardBrowserTest::GetKeyboardExtensionId() { |
91 std::string url = view->GetSiteInstance()->GetSiteURL().spec(); | 91 return "mppnpdlheglhdfmldimlhpnegondlapf"; |
92 if (url == kVirtualKeyboardURL) { | 92 } |
93 content::WebContents* wc = | 93 |
94 content::WebContents::FromRenderViewHost(view); | 94 base::FilePath VirtualKeyboardBrowserTest::GetBaseFrameWork() { |
95 // Waits for Polymer to load. | 95 return kBaseKeyboardTestFramework; |
96 content::WaitForLoadStop(wc); | 96 }; |
97 return view; | 97 |
98 } | 98 content::RenderViewHost* |
| 99 VirtualKeyboardBrowserTest::GetKeyboardRenderViewHost() { |
| 100 ShowVirtualKeyboard(); |
| 101 std::string kVirtualKeyboardURL = |
| 102 "chrome-extension://" + GetKeyboardExtensionId() + "/"; |
| 103 |
| 104 scoped_ptr<content::RenderWidgetHostIterator> widgets( |
| 105 content::RenderWidgetHost::GetRenderWidgetHosts()); |
| 106 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) { |
| 107 if (widget->IsRenderView()) { |
| 108 content::RenderViewHost* view = content::RenderViewHost::From(widget); |
| 109 std::string url = view->GetSiteInstance()->GetSiteURL().spec(); |
| 110 if (url == kVirtualKeyboardURL) { |
| 111 content::WebContents* wc = |
| 112 content::WebContents::FromRenderViewHost(view); |
| 113 // Waits for Polymer to load. |
| 114 content::WaitForLoadStop(wc); |
| 115 return view; |
99 } | 116 } |
100 } | 117 } |
101 return NULL; | |
102 } | 118 } |
| 119 LOG(ERROR) << "Extension not found:" << kVirtualKeyboardURL; |
| 120 return NULL; |
| 121 } |
103 | 122 |
104 private: | 123 void VirtualKeyboardBrowserTest::InjectJavascript(const base::FilePath& dir, |
105 | 124 const base::FilePath& file) { |
106 // Injects javascript into the keyboard page. The test |file| is in | 125 base::FilePath path = ui_test_utils::GetTestFilePath(dir, file); |
107 // directory |dir| relative to the root testing directory. | 126 std::string library_content; |
108 void InjectJavascript(const base::FilePath& dir, | 127 ASSERT_TRUE(base::ReadFileToString(path, &library_content)) << path.value(); |
109 const base::FilePath& file) { | 128 utf8_content_.append(library_content); |
110 base::FilePath path = ui_test_utils::GetTestFilePath(dir, file); | 129 utf8_content_.append(";\n"); |
111 std::string library_content; | 130 } |
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 | 131 |
121 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, AttributesTest) { | 132 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, AttributesTest) { |
122 RunTest(base::FilePath(FILE_PATH_LITERAL("attributes_test.js"))); | 133 RunTest(base::FilePath(FILE_PATH_LITERAL("attributes_test.js"))); |
123 } | 134 } |
124 | 135 |
125 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, TypingTest) { | 136 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, TypingTest) { |
126 RunTest(base::FilePath(FILE_PATH_LITERAL("typing_test.js"))); | 137 RunTest(base::FilePath(FILE_PATH_LITERAL("typing_test.js"))); |
127 } | 138 } |
128 | 139 |
129 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, ControlKeysTest) { | 140 IN_PROC_BROWSER_TEST_F(VirtualKeyboardBrowserTest, ControlKeysTest) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 bool success = false; | 190 bool success = false; |
180 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | 191 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
181 browser_rvh, | 192 browser_rvh, |
182 "success ? verifyInput('a') : waitForInput('a');", | 193 "success ? verifyInput('a') : waitForInput('a');", |
183 &success)); | 194 &success)); |
184 ASSERT_TRUE(success); | 195 ASSERT_TRUE(success); |
185 } | 196 } |
186 | 197 |
187 // TODO(kevers|rsadam|bshe): Add UI tests for remaining virtual keyboard | 198 // TODO(kevers|rsadam|bshe): Add UI tests for remaining virtual keyboard |
188 // functionality. | 199 // functionality. |
OLD | NEW |