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