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" |
17 #include "chrome/browser/renderer_host/mock_render_process_host.h" | |
16 #include "chrome/browser/profile.h" | 18 #include "chrome/browser/profile.h" |
17 #include "chrome/browser/tab_contents/tab_contents.h" | 19 #include "chrome/browser/tab_contents/tab_contents.h" |
18 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
21 #include "chrome/browser/translate/translate_infobar_delegate.h" | |
22 #include "chrome/browser/translate/translate_manager.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" | |
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 |
25 class AutoFillTest : public InProcessBrowserTest { | 33 class AutoFillTest : public InProcessBrowserTest { |
34 TestURLFetcherFactory url_fetcher_factory_; | |
26 protected: | 35 protected: |
27 AutoFillTest() { | 36 AutoFillTest() { |
28 set_show_window(true); | 37 set_show_window(true); |
29 EnableDOMAutomation(); | 38 EnableDOMAutomation(); |
30 } | 39 } |
31 | 40 |
32 void SetUpProfile() { | 41 void SetUpProfile() { |
33 autofill_test::DisableSystemServices(browser()->profile()); | 42 autofill_test::DisableSystemServices(browser()->profile()); |
34 | 43 |
35 AutoFillProfile profile; | 44 AutoFillProfile profile; |
(...skipping 13 matching lines...) Expand all Loading... | |
49 | 58 |
50 void ExpectFieldValue(const std::wstring& field_name, | 59 void ExpectFieldValue(const std::wstring& field_name, |
51 const std::string& expected_value) { | 60 const std::string& expected_value) { |
52 std::string value; | 61 std::string value; |
53 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 62 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
54 browser()->GetSelectedTabContents()->render_view_host(), L"", | 63 browser()->GetSelectedTabContents()->render_view_host(), L"", |
55 L"window.domAutomationController.send(" | 64 L"window.domAutomationController.send(" |
56 L"document.getElementById('" + field_name + L"').value);", &value)); | 65 L"document.getElementById('" + field_name + L"').value);", &value)); |
57 EXPECT_EQ(expected_value, value); | 66 EXPECT_EQ(expected_value, value); |
58 } | 67 } |
68 | |
69 RenderViewHost* rvh() { | |
70 return browser()->GetSelectedTabContents()->render_view_host(); | |
71 } | |
72 | |
73 virtual void SetUp() { | |
74 URLFetcher::set_factory(&url_fetcher_factory_); | |
75 InProcessBrowserTest::SetUp(); | |
76 } | |
77 | |
78 void SimulateURLFetch(bool success) { | |
79 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | |
80 ASSERT_TRUE(fetcher); | |
81 URLRequestStatus status; | |
82 status.set_status(success ? URLRequestStatus::SUCCESS : | |
83 URLRequestStatus::FAILED); | |
84 | |
85 std::string script = " var google = {};" | |
86 "google.translate = (function() {" | |
87 " return {" | |
88 " TranslateService: function() {" | |
89 " return {" | |
90 " isAvailable : function() {" | |
91 " return true;" | |
92 " }," | |
93 " restore : function() {" | |
94 " return;" | |
95 " }," | |
96 " getDetectedLanguage : function() {" | |
97 " return \"ja\";" | |
98 " }," | |
99 " translatePage : function(originalLang, targetLang," | |
100 " onTranslateProgress) {" | |
101 " document.getElementsByTagName(\"body\")[0].innerHTML = '" | |
102 " <form action=\"http://www.google.com/\" method=\"POST\">" | |
103 " <label for=\"firstname\">First name:</label>" | |
104 " <input type=\"text\" id=\"firstname\"" | |
105 " onFocus=\"domAutomationController.send(true)\"" | |
106 " /><br />" | |
107 " <label for=\"lastname\">Last name:</label>" | |
108 " <input type=\"text\" id=\"lastname\" /><br />" | |
109 " <label for=\"address1\">Address line 1:</label>" | |
110 " <input type=\"text\" id=\"address1\" /><br />" | |
111 " <label for=\"address2\">Address line 2:</label>" | |
112 " <input type=\"text\" id=\"address2\" /><br />" | |
113 " <label for=\"city\">City:</label>" | |
114 " <input type=\"text\" id=\"city\" /><br />" | |
115 " <label for=\"state\">State:</label>" | |
116 " <select id=\"state\">" | |
117 " <option value=\"\" selected=\"yes\">--</option>" | |
118 " <option value=\"CA\">California</option>" | |
119 " <option value=\"TX\">Texas</option>" | |
120 " </select><br />" | |
121 " <label for=\"zip\">ZIP code:</label>" | |
122 " <input type=\"text\" id=\"zip\" /><br />" | |
123 " <label for=\"country\">Country:</label>" | |
124 " <select id=\"country\">" | |
125 " <option value=\"\" selected=\"yes\">--</option>" | |
126 " <option value=\"CA\">Canada</option>" | |
127 " <option value=\"US\">United States</option>" | |
128 " </select><br />" | |
129 " <label for=\"phone\">Phone number:</label>" | |
130 " <input type=\"text\" id=\"phone\" /><br />" | |
131 " </form>" | |
132 " ';" | |
133 " onTranslateProgress(100, true, false);" | |
134 " }" | |
135 " };" | |
136 " }" | |
137 " };" | |
138 "})();"; | |
139 | |
honten.org
2010/12/05 01:34:34
Is this Ok to include javascript code as text stri
James Hawkins
2010/12/06 19:02:05
Should be fine to inline it.
| |
140 fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(), | |
141 status, success ? 200 : 500, | |
142 ResponseCookies(), | |
143 script); | |
144 } | |
59 }; | 145 }; |
60 | 146 |
61 // Test that basic form fill is working. | 147 // Test that basic form fill is working. |
62 IN_PROC_BROWSER_TEST_F(AutoFillTest, BasicFormFill) { | 148 IN_PROC_BROWSER_TEST_F(AutoFillTest, BasicFormFill) { |
63 SetUpProfile(); | 149 SetUpProfile(); |
64 | 150 |
65 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 151 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
66 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( | 152 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
67 browser(), GURL("data:text/html;charset=utf-8," | 153 browser(), GURL("data:text/html;charset=utf-8," |
68 "<form action=\"http://www.google.com/\" method=\"POST\">" | 154 "<form action=\"http://www.google.com/\" method=\"POST\">" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 ExpectFieldValue(L"firstname", "Milton"); | 232 ExpectFieldValue(L"firstname", "Milton"); |
147 ExpectFieldValue(L"lastname", "Waddams"); | 233 ExpectFieldValue(L"lastname", "Waddams"); |
148 ExpectFieldValue(L"address1", "4120 Freidrich Lane"); | 234 ExpectFieldValue(L"address1", "4120 Freidrich Lane"); |
149 ExpectFieldValue(L"address2", "Basement"); | 235 ExpectFieldValue(L"address2", "Basement"); |
150 ExpectFieldValue(L"city", "Austin"); | 236 ExpectFieldValue(L"city", "Austin"); |
151 ExpectFieldValue(L"state", "TX"); | 237 ExpectFieldValue(L"state", "TX"); |
152 ExpectFieldValue(L"zip", "78744"); | 238 ExpectFieldValue(L"zip", "78744"); |
153 ExpectFieldValue(L"country", "US"); | 239 ExpectFieldValue(L"country", "US"); |
154 ExpectFieldValue(L"phone", "5125551234"); | 240 ExpectFieldValue(L"phone", "5125551234"); |
155 } | 241 } |
242 | |
243 // Test that basic form fill is working. | |
244 IN_PROC_BROWSER_TEST_F(AutoFillTest, TranslateAndFormFill) { | |
245 SetUpProfile(); | |
246 | |
247 GURL url("data:text/html;charset=utf-8," | |
248 "<form action=\"http://www.google.com/\" method=\"POST\">" | |
249 "<label for=\"firstname\">なまえ</label>" | |
250 " <input type=\"text\" id=\"firstname\"" | |
251 " onFocus=\"domAutomationController.send(true)\"" | |
252 " /><br />" | |
253 "<label for=\"lastname\">みょうじ</label>" | |
254 " <input type=\"text\" id=\"lastname\" /><br />" | |
honten.org
2010/12/05 01:34:34
Is is OK to use Japanese character here?
For now,
James Hawkins
2010/12/06 19:02:05
UTF8
| |
255 "<label for=\"address1\">Address line 1:</label>" | |
256 " <input type=\"text\" id=\"address1\" /><br />" | |
257 "<label for=\"address2\">Address line 2:</label>" | |
258 " <input type=\"text\" id=\"address2\" /><br />" | |
259 "<label for=\"city\">City:</label>" | |
260 " <input type=\"text\" id=\"city\" /><br />" | |
261 "<label for=\"state\">State:</label>" | |
262 " <select id=\"state\">" | |
263 " <option value=\"\" selected=\"yes\">--</option>" | |
264 " <option value=\"CA\">California</option>" | |
265 " <option value=\"TX\">Texas</option>" | |
266 " </select><br />" | |
267 "<label for=\"zip\">ZIP code:</label>" | |
268 " <input type=\"text\" id=\"zip\" /><br />" | |
269 "<label for=\"country\">Country:</label>" | |
270 " <select id=\"country\">" | |
271 " <option value=\"\" selected=\"yes\">--</option>" | |
272 " <option value=\"CA\">Canada</option>" | |
273 " <option value=\"US\">United States</option>" | |
274 " </select><br />" | |
275 "<label for=\"phone\">Phone number:</label>" | |
276 " <input type=\"text\" id=\"phone\" /><br />" | |
277 "</form>"); | |
278 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | |
279 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( | |
280 browser(), url)); | |
281 | |
282 // Get translation bar. | |
283 int page_id = browser()->GetSelectedTabContents()->controller(). | |
284 GetLastCommittedEntry()->page_id(); | |
285 | |
286 rvh()->OnMessageReceived(ViewHostMsg_PageContents(0, url, page_id, | |
287 UTF8ToUTF16("test"), "ja", true)); | |
288 TranslateInfoBarDelegate* infobar = browser()->GetSelectedTabContents()-> | |
289 GetInfoBarDelegateAt(0)->AsTranslateInfoBarDelegate(); | |
290 | |
291 ASSERT_TRUE(infobar != NULL); | |
292 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type()); | |
293 | |
294 // Simulate press translation button. | |
295 infobar->Translate(); | |
296 | |
297 // Simulate the translate script being retrieved. | |
298 // Pass fake google.translate lib as the translate script. | |
299 SimulateURLFetch(true); | |
300 | |
301 // Simulate translation to kick onTranslateElementLoad. | |
302 // But right now, the call stucks here. | |
303 // Once click the text field, it starts again. | |
304 bool result; | |
honten.org
2010/12/05 01:34:34
As I wrote as comment, this test stops at ExecuteJ
James Hawkins
2010/12/06 19:02:05
I don't have any ideas.
| |
305 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
306 rvh(), L"", L"cr.googleTranslate.onTranslateElementLoad();", &result)); | |
307 ASSERT_TRUE(result); | |
308 | |
309 // Simulate the render notifying the translation has been done. | |
310 rvh()->OnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "ja", "en", | |
311 TranslateErrors::NONE)); | |
312 | |
313 ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), | |
314 VIEW_ID_TAB_CONTAINER)); | |
315 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), | |
316 VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | |
317 | |
318 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
319 rvh(), L"", L"document.getElementById('firstname').focus();", &result)); | |
320 ASSERT_TRUE(result); | |
321 // Start filling the first name field with "M" and wait for the popup to be | |
322 // shown. | |
323 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | |
324 browser(), app::VKEY_M, false, true, false, false, | |
325 NotificationType::AUTOFILL_DID_SHOW_SUGGESTIONS, | |
326 Source<RenderViewHost>(rvh()))); | |
327 | |
328 // Press the down arrow to select the suggestion and preview the autofilled | |
329 // form. | |
330 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | |
331 browser(), app::VKEY_DOWN, false, false, false, false, | |
332 NotificationType::AUTOFILL_DID_FILL_FORM_DATA, | |
333 Source<RenderViewHost>(rvh()))); | |
334 | |
335 // The previewed values should not be accessible to JavaScript. | |
336 ExpectFieldValue(L"firstname", "M"); | |
337 ExpectFieldValue(L"lastname", ""); | |
338 ExpectFieldValue(L"address1", ""); | |
339 ExpectFieldValue(L"address2", ""); | |
340 ExpectFieldValue(L"city", ""); | |
341 ExpectFieldValue(L"state", ""); | |
342 ExpectFieldValue(L"zip", ""); | |
343 ExpectFieldValue(L"country", ""); | |
344 ExpectFieldValue(L"phone", ""); | |
345 // TODO(isherman): It would be nice to test that the previewed values are | |
346 // displayed: http://crbug.com/57220 | |
347 | |
348 // Press Enter to accept the autofill suggestions. | |
349 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( | |
350 browser(), app::VKEY_RETURN, false, false, false, false, | |
351 NotificationType::AUTOFILL_DID_FILL_FORM_DATA, | |
352 Source<RenderViewHost>(rvh()))); | |
353 | |
354 // The form should be filled. | |
355 ExpectFieldValue(L"firstname", "Milton"); | |
356 ExpectFieldValue(L"lastname", "Waddams"); | |
357 ExpectFieldValue(L"address1", "4120 Freidrich Lane"); | |
358 ExpectFieldValue(L"address2", "Basement"); | |
359 ExpectFieldValue(L"city", "Austin"); | |
360 ExpectFieldValue(L"state", "TX"); | |
361 ExpectFieldValue(L"zip", "78744"); | |
362 ExpectFieldValue(L"country", "US"); | |
363 ExpectFieldValue(L"phone", "5125551234"); | |
364 } | |
OLD | NEW |