OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/utf_string_conversions.h" | |
7 #include "app/keyboard_code_conversion.h" | 8 #include "app/keyboard_code_conversion.h" |
8 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
9 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
10 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
11 #include "base/string16.h" | 12 #include "base/string16.h" |
12 #include "chrome/browser/autofill/autofill_common_test.h" | 13 #include "chrome/browser/autofill/autofill_common_test.h" |
13 #include "chrome/browser/autofill/autofill_profile.h" | 14 #include "chrome/browser/autofill/autofill_profile.h" |
14 #include "chrome/browser/autofill/personal_data_manager.h" | 15 #include "chrome/browser/autofill/personal_data_manager.h" |
15 #include "chrome/browser/net/predictor_api.h" | 16 #include "chrome/browser/net/predictor_api.h" |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/renderer_host/mock_render_process_host.h" | |
17 #include "chrome/browser/tab_contents/tab_contents.h" | 19 #include "chrome/browser/tab_contents/tab_contents.h" |
20 #include "chrome/browser/translate/translate_infobar_delegate.h" | |
21 #include "chrome/browser/translate/translate_manager.h" | |
18 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
19 #include "chrome/browser/ui/browser_window.h" | 23 #include "chrome/browser/ui/browser_window.h" |
24 #include "chrome/browser/renderer_host/render_view_host.h" | |
dhollowa
2010/12/10 18:56:50
It looks like these previous fixes got reverted so
honten.org
2010/12/10 19:40:02
Yes,
Accidentally this file is roll backed. I sta
| |
25 #include "chrome/common/net/test_url_fetcher_factory.h" | |
20 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
27 #include "chrome/common/render_messages.h" | |
28 #include "chrome/renderer/translate_helper.h" | |
21 #include "chrome/test/in_process_browser_test.h" | 29 #include "chrome/test/in_process_browser_test.h" |
22 #include "chrome/test/ui_test_utils.h" | 30 #include "chrome/test/ui_test_utils.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
24 | 32 |
33 static const std::string kTestFormString = | |
34 "<form action=\"http://www.google.com/\" method=\"POST\">" | |
35 "<label for=\"firstname\">First name:</label>" | |
36 " <input type=\"text\" id=\"firstname\"" | |
37 " onFocus=\"domAutomationController.send(true)\"" | |
38 " /><br />" | |
39 "<label for=\"lastname\">Last name:</label>" | |
40 " <input type=\"text\" id=\"lastname\" /><br />" | |
41 "<label for=\"address1\">Address line 1:</label>" | |
42 " <input type=\"text\" id=\"address1\" /><br />" | |
43 "<label for=\"address2\">Address line 2:</label>" | |
44 " <input type=\"text\" id=\"address2\" /><br />" | |
45 "<label for=\"city\">City:</label>" | |
46 " <input type=\"text\" id=\"city\" /><br />" | |
47 "<label for=\"state\">State:</label>" | |
48 " <select id=\"state\">" | |
49 " <option value=\"\" selected=\"yes\">--</option>" | |
50 " <option value=\"CA\">California</option>" | |
51 " <option value=\"TX\">Texas</option>" | |
52 " </select><br />" | |
53 "<label for=\"zip\">ZIP code:</label>" | |
54 " <input type=\"text\" id=\"zip\" /><br />" | |
55 "<label for=\"country\">Country:</label>" | |
56 " <select id=\"country\">" | |
57 " <option value=\"\" selected=\"yes\">--</option>" | |
58 " <option value=\"CA\">Canada</option>" | |
59 " <option value=\"US\">United States</option>" | |
60 " </select><br />" | |
61 "<label for=\"phone\">Phone number:</label>" | |
62 " <input type=\"text\" id=\"phone\" /><br />" | |
63 "</form>"; | |
64 | |
25 class AutoFillTest : public InProcessBrowserTest { | 65 class AutoFillTest : public InProcessBrowserTest { |
66 TestURLFetcherFactory url_fetcher_factory_; | |
26 protected: | 67 protected: |
27 AutoFillTest() { | 68 AutoFillTest() { |
28 set_show_window(true); | 69 set_show_window(true); |
29 EnableDOMAutomation(); | 70 EnableDOMAutomation(); |
30 } | 71 } |
31 | 72 |
32 void SetUpProfile() { | 73 void SetUpProfile() { |
33 autofill_test::DisableSystemServices(browser()->profile()); | 74 autofill_test::DisableSystemServices(browser()->profile()); |
34 | 75 |
35 AutoFillProfile profile; | 76 AutoFillProfile profile; |
(...skipping 13 matching lines...) Expand all Loading... | |
49 | 90 |
50 void ExpectFieldValue(const std::wstring& field_name, | 91 void ExpectFieldValue(const std::wstring& field_name, |
51 const std::string& expected_value) { | 92 const std::string& expected_value) { |
52 std::string value; | 93 std::string value; |
53 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 94 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
54 browser()->GetSelectedTabContents()->render_view_host(), L"", | 95 browser()->GetSelectedTabContents()->render_view_host(), L"", |
55 L"window.domAutomationController.send(" | 96 L"window.domAutomationController.send(" |
56 L"document.getElementById('" + field_name + L"').value);", &value)); | 97 L"document.getElementById('" + field_name + L"').value);", &value)); |
57 EXPECT_EQ(expected_value, value); | 98 EXPECT_EQ(expected_value, value); |
58 } | 99 } |
100 | |
101 RenderViewHost* rvh() { | |
102 return browser()->GetSelectedTabContents()->render_view_host(); | |
103 } | |
104 | |
105 virtual void SetUp() { | |
106 URLFetcher::set_factory(&url_fetcher_factory_); | |
107 InProcessBrowserTest::SetUp(); | |
108 } | |
109 | |
110 void SimulateURLFetch(bool success) { | |
111 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | |
112 ASSERT_TRUE(fetcher); | |
113 URLRequestStatus status; | |
114 status.set_status(success ? URLRequestStatus::SUCCESS : | |
115 URLRequestStatus::FAILED); | |
116 | |
117 std::string script = " var google = {};" | |
118 "google.translate = (function() {" | |
119 " return {" | |
120 " TranslateService: function() {" | |
121 " return {" | |
122 " isAvailable : function() {" | |
123 " return true;" | |
124 " }," | |
125 " restore : function() {" | |
126 " return;" | |
127 " }," | |
128 " getDetectedLanguage : function() {" | |
129 " return \"ja\";" | |
130 " }," | |
131 " translatePage : function(originalLang, targetLang," | |
132 " onTranslateProgress) {" | |
133 " document.getElementsByTagName(\"body\")[0].innerHTML = '" + | |
134 kTestFormString + | |
135 " ';" | |
136 " onTranslateProgress(100, true, false);" | |
137 " }" | |
138 " };" | |
139 " }" | |
140 " };" | |
141 "})();"; | |
142 | |
143 fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(), | |
144 status, success ? 200 : 500, | |
145 ResponseCookies(), | |
146 script); | |
147 } | |
148 | |
149 void TryBasicFormFillWithMKey() { | |
150 ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), | |
151 VIEW_ID_TAB_CONTAINER)); | |
152 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), | |
153 VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | |
154 | |
155 bool result = false; | |
156 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
157 rvh(), L"", L"document.getElementById('firstname').focus();", &result)); | |
158 ASSERT_TRUE(result); | |
159 // Start filling the first name field with "M" and wait for the popup to be | |
160 // shown. | |
161 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | |
162 browser(), app::VKEY_M, false, true, false, false, | |
163 NotificationType::AUTOFILL_DID_SHOW_SUGGESTIONS, | |
164 Source<RenderViewHost>(rvh()))); | |
165 | |
166 // Press the down arrow to select the suggestion and preview the autofilled | |
167 // form. | |
168 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | |
169 browser(), app::VKEY_DOWN, false, false, false, false, | |
170 NotificationType::AUTOFILL_DID_FILL_FORM_DATA, | |
171 Source<RenderViewHost>(rvh()))); | |
172 | |
173 // The previewed values should not be accessible to JavaScript. | |
174 ExpectFieldValue(L"firstname", "M"); | |
175 ExpectFieldValue(L"lastname", ""); | |
176 ExpectFieldValue(L"address1", ""); | |
177 ExpectFieldValue(L"address2", ""); | |
178 ExpectFieldValue(L"city", ""); | |
179 ExpectFieldValue(L"state", ""); | |
180 ExpectFieldValue(L"zip", ""); | |
181 ExpectFieldValue(L"country", ""); | |
182 ExpectFieldValue(L"phone", ""); | |
183 // TODO(isherman): It would be nice to test that the previewed values are | |
184 // displayed: http://crbug.com/57220 | |
185 | |
186 // Press Enter to accept the autofill suggestions. | |
187 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | |
188 browser(), app::VKEY_RETURN, false, false, false, false, | |
189 NotificationType::AUTOFILL_DID_FILL_FORM_DATA, | |
190 Source<RenderViewHost>(rvh()))); | |
191 | |
192 // The form should be filled. | |
193 ExpectFieldValue(L"firstname", "Milton"); | |
194 ExpectFieldValue(L"lastname", "Waddams"); | |
195 ExpectFieldValue(L"address1", "4120 Freidrich Lane"); | |
196 ExpectFieldValue(L"address2", "Basement"); | |
197 ExpectFieldValue(L"city", "Austin"); | |
198 ExpectFieldValue(L"state", "TX"); | |
199 ExpectFieldValue(L"zip", "78744"); | |
200 ExpectFieldValue(L"country", "US"); | |
201 ExpectFieldValue(L"phone", "5125551234"); | |
202 | |
203 } | |
59 }; | 204 }; |
60 | 205 |
61 // Test that basic form fill is working. | 206 // Test that basic form fill is working. |
62 IN_PROC_BROWSER_TEST_F(AutoFillTest, BasicFormFill) { | 207 IN_PROC_BROWSER_TEST_F(AutoFillTest, BasicFormFill) { |
63 SetUpProfile(); | 208 SetUpProfile(); |
64 | 209 |
65 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 210 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
66 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( | 211 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
67 browser(), GURL("data:text/html;charset=utf-8," | 212 browser(), GURL("data:text/html;charset=utf-8," + kTestFormString))); |
68 "<form action=\"http://www.google.com/\" method=\"POST\">" | |
69 "<label for=\"firstname\">First name:</label>" | |
70 " <input type=\"text\" id=\"firstname\"" | |
71 " onFocus=\"domAutomationController.send(true)\"" | |
72 " /><br />" | |
73 "<label for=\"lastname\">Last name:</label>" | |
74 " <input type=\"text\" id=\"lastname\" /><br />" | |
75 "<label for=\"address1\">Address line 1:</label>" | |
76 " <input type=\"text\" id=\"address1\" /><br />" | |
77 "<label for=\"address2\">Address line 2:</label>" | |
78 " <input type=\"text\" id=\"address2\" /><br />" | |
79 "<label for=\"city\">City:</label>" | |
80 " <input type=\"text\" id=\"city\" /><br />" | |
81 "<label for=\"state\">State:</label>" | |
82 " <select id=\"state\">" | |
83 " <option value=\"\" selected=\"yes\">--</option>" | |
84 " <option value=\"CA\">California</option>" | |
85 " <option value=\"TX\">Texas</option>" | |
86 " </select><br />" | |
87 "<label for=\"zip\">ZIP code:</label>" | |
88 " <input type=\"text\" id=\"zip\" /><br />" | |
89 "<label for=\"country\">Country:</label>" | |
90 " <select id=\"country\">" | |
91 " <option value=\"\" selected=\"yes\">--</option>" | |
92 " <option value=\"CA\">Canada</option>" | |
93 " <option value=\"US\">United States</option>" | |
94 " </select><br />" | |
95 "<label for=\"phone\">Phone number:</label>" | |
96 " <input type=\"text\" id=\"phone\" /><br />" | |
97 "</form>"))); | |
98 | 213 |
99 ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), | 214 TryBasicFormFillWithMKey(); |
100 VIEW_ID_TAB_CONTAINER)); | 215 } |
101 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), | |
102 VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | |
103 | 216 |
104 RenderViewHost* render_view_host = | 217 // Test that basic form fill is working. |
105 browser()->GetSelectedTabContents()->render_view_host(); | 218 IN_PROC_BROWSER_TEST_F(AutoFillTest, TranslateAndFormFill) { |
106 bool result; | 219 SetUpProfile(); |
107 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
108 render_view_host, L"", L"document.getElementById('firstname').focus();", | |
109 &result)); | |
110 ASSERT_TRUE(result); | |
111 | 220 |
112 // Start filling the first name field with "M" and wait for the popup to be | 221 GURL url("data:text/html;charset=utf-8," |
113 // shown. | 222 "<form action=\"http://www.google.com/\" method=\"POST\">" |
114 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | 223 "<label for=\"firstname\">なまえ</label>" |
115 browser(), app::VKEY_M, false, true, false, false, | 224 " <input type=\"text\" id=\"firstname\"" |
116 NotificationType::AUTOFILL_DID_SHOW_SUGGESTIONS, | 225 " onFocus=\"domAutomationController.send(true)\"" |
117 Source<RenderViewHost>(render_view_host))); | 226 " /><br />" |
227 "<label for=\"lastname\">みょうじ</label>" | |
228 " <input type=\"text\" id=\"lastname\" /><br />" | |
229 "<label for=\"address1\">Address line 1:</label>" | |
230 " <input type=\"text\" id=\"address1\" /><br />" | |
231 "<label for=\"address2\">Address line 2:</label>" | |
232 " <input type=\"text\" id=\"address2\" /><br />" | |
233 "<label for=\"city\">City:</label>" | |
234 " <input type=\"text\" id=\"city\" /><br />" | |
235 "<label for=\"state\">State:</label>" | |
236 " <select id=\"state\">" | |
237 " <option value=\"\" selected=\"yes\">--</option>" | |
238 " <option value=\"CA\">California</option>" | |
239 " <option value=\"TX\">Texas</option>" | |
240 " </select><br />" | |
241 "<label for=\"zip\">ZIP code:</label>" | |
242 " <input type=\"text\" id=\"zip\" /><br />" | |
243 "<label for=\"country\">Country:</label>" | |
244 " <select id=\"country\">" | |
245 " <option value=\"\" selected=\"yes\">--</option>" | |
246 " <option value=\"CA\">Canada</option>" | |
247 " <option value=\"US\">United States</option>" | |
248 " </select><br />" | |
249 "<label for=\"phone\">Phone number:</label>" | |
250 " <input type=\"text\" id=\"phone\" /><br />" | |
251 "</form>"); | |
252 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | |
253 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( | |
254 browser(), url)); | |
118 | 255 |
119 // Press the down arrow to select the suggestion and preview the autofilled | 256 // Get translation bar. |
120 // form. | 257 int page_id = browser()->GetSelectedTabContents()->controller(). |
121 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | 258 GetLastCommittedEntry()->page_id(); |
122 browser(), app::VKEY_DOWN, false, false, false, false, | 259 rvh()->OnMessageReceived(ViewHostMsg_PageContents(0, url, page_id, |
123 NotificationType::AUTOFILL_DID_FILL_FORM_DATA, | 260 UTF8ToUTF16("test"), "ja", true)); |
124 Source<RenderViewHost>(render_view_host))); | 261 TranslateInfoBarDelegate* infobar = browser()->GetSelectedTabContents()-> |
262 GetInfoBarDelegateAt(0)->AsTranslateInfoBarDelegate(); | |
125 | 263 |
126 // The previewed values should not be accessible to JavaScript. | 264 ASSERT_TRUE(infobar != NULL); |
127 ExpectFieldValue(L"firstname", "M"); | 265 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type()); |
128 ExpectFieldValue(L"lastname", ""); | |
129 ExpectFieldValue(L"address1", ""); | |
130 ExpectFieldValue(L"address2", ""); | |
131 ExpectFieldValue(L"city", ""); | |
132 ExpectFieldValue(L"state", ""); | |
133 ExpectFieldValue(L"zip", ""); | |
134 ExpectFieldValue(L"country", ""); | |
135 ExpectFieldValue(L"phone", ""); | |
136 // TODO(isherman): It would be nice to test that the previewed values are | |
137 // displayed: http://crbug.com/57220 | |
138 | 266 |
139 // Press Enter to accept the autofill suggestions. | 267 // Simulate press translation button. |
140 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | 268 infobar->Translate(); |
141 browser(), app::VKEY_RETURN, false, false, false, false, | |
142 NotificationType::AUTOFILL_DID_FILL_FORM_DATA, | |
143 Source<RenderViewHost>(render_view_host))); | |
144 | 269 |
145 // The form should be filled. | 270 // Simulate the translate script being retrieved. |
146 ExpectFieldValue(L"firstname", "Milton"); | 271 // Pass fake google.translate lib as the translate script. |
147 ExpectFieldValue(L"lastname", "Waddams"); | 272 SimulateURLFetch(true); |
148 ExpectFieldValue(L"address1", "4120 Freidrich Lane"); | 273 |
149 ExpectFieldValue(L"address2", "Basement"); | 274 // Simulate translation to kick onTranslateElementLoad. |
150 ExpectFieldValue(L"city", "Austin"); | 275 // But right now, the call stucks here. |
151 ExpectFieldValue(L"state", "TX"); | 276 // Once click the text field, it starts again. |
152 ExpectFieldValue(L"zip", "78744"); | 277 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript( |
153 ExpectFieldValue(L"country", "US"); | 278 rvh(), L"", L"cr.googleTranslate.onTranslateElementLoad();")); |
154 ExpectFieldValue(L"phone", "5125551234"); | 279 |
280 // Simulate the render notifying the translation has been done. | |
281 ui_test_utils::WaitForNotification(NotificationType::PAGE_TRANSLATED); | |
282 | |
283 TryBasicFormFillWithMKey(); | |
284 | |
155 } | 285 } |
OLD | NEW |