| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 #include "content/public/browser/web_contents.h" | 45 #include "content/public/browser/web_contents.h" |
| 46 #include "content/public/test/browser_test_utils.h" | 46 #include "content/public/test/browser_test_utils.h" |
| 47 #include "content/public/test/test_renderer_host.h" | 47 #include "content/public/test/test_renderer_host.h" |
| 48 #include "content/public/test/test_utils.h" | 48 #include "content/public/test/test_utils.h" |
| 49 #include "net/base/net_errors.h" | 49 #include "net/base/net_errors.h" |
| 50 #include "net/test/embedded_test_server/embedded_test_server.h" | 50 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 51 #include "net/url_request/test_url_fetcher_factory.h" | 51 #include "net/url_request/test_url_fetcher_factory.h" |
| 52 #include "net/url_request/url_request_status.h" | 52 #include "net/url_request/url_request_status.h" |
| 53 #include "testing/gmock/include/gmock/gmock.h" | 53 #include "testing/gmock/include/gmock/gmock.h" |
| 54 #include "testing/gtest/include/gtest/gtest.h" | 54 #include "testing/gtest/include/gtest/gtest.h" |
| 55 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
| 55 #include "ui/events/keycodes/keyboard_codes.h" | 56 #include "ui/events/keycodes/keyboard_codes.h" |
| 56 | 57 |
| 57 using base::ASCIIToUTF16; | 58 using base::ASCIIToUTF16; |
| 58 | 59 |
| 59 namespace autofill { | 60 namespace autofill { |
| 60 | 61 |
| 61 static const char kDataURIPrefix[] = "data:text/html;charset=utf-8,"; | 62 static const char kDataURIPrefix[] = "data:text/html;charset=utf-8,"; |
| 62 static const char kTestFormString[] = | 63 static const char kTestFormString[] = |
| 63 "<form action=\"http://www.example.com/\" method=\"POST\">" | 64 "<form action=\"http://www.example.com/\" method=\"POST\">" |
| 64 "<label for=\"firstname\">First name:</label>" | 65 "<label for=\"firstname\">First name:</label>" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 82 " <input type=\"text\" id=\"zip\"><br>" | 83 " <input type=\"text\" id=\"zip\"><br>" |
| 83 "<label for=\"country\">Country:</label>" | 84 "<label for=\"country\">Country:</label>" |
| 84 " <select id=\"country\">" | 85 " <select id=\"country\">" |
| 85 " <option value=\"\" selected=\"yes\">--</option>" | 86 " <option value=\"\" selected=\"yes\">--</option>" |
| 86 " <option value=\"CA\">Canada</option>" | 87 " <option value=\"CA\">Canada</option>" |
| 87 " <option value=\"US\">United States</option>" | 88 " <option value=\"US\">United States</option>" |
| 88 " </select><br>" | 89 " </select><br>" |
| 89 "<label for=\"phone\">Phone number:</label>" | 90 "<label for=\"phone\">Phone number:</label>" |
| 90 " <input type=\"text\" id=\"phone\"><br>" | 91 " <input type=\"text\" id=\"phone\"><br>" |
| 91 "</form>"; | 92 "</form>"; |
| 92 | 93 static const char kTestPasswordFormString[] = |
| 94 "<form>" |
| 95 "<label for=\"user\">User:</label>" |
| 96 " <input id=\"user\" type=\"text\" name=\"name\"" |
| 97 "onfocus=\"domAutomationController.send(true)\">" |
| 98 "<br>" |
| 99 "<label for=\"password\">Password:</label>" |
| 100 " <input id=\"password\" type=\"password\" name=\"password\"" |
| 101 "onfocus=\"domAutomationController.send(true)\">" |
| 102 "<br>" |
| 103 "<input type=\"submit\" value=\"Submit\">" |
| 104 "</form>"; |
| 93 | 105 |
| 94 // AutofillManagerTestDelegateImpl -------------------------------------------- | 106 // AutofillManagerTestDelegateImpl -------------------------------------------- |
| 95 | 107 |
| 96 class AutofillManagerTestDelegateImpl | 108 class AutofillManagerTestDelegateImpl |
| 97 : public autofill::AutofillManagerTestDelegate { | 109 : public autofill::AutofillManagerTestDelegate { |
| 98 public: | 110 public: |
| 99 AutofillManagerTestDelegateImpl() {} | 111 AutofillManagerTestDelegateImpl() |
| 112 : waiting_for_text_change_(false) {} |
| 100 ~AutofillManagerTestDelegateImpl() override {} | 113 ~AutofillManagerTestDelegateImpl() override {} |
| 101 | 114 |
| 102 // autofill::AutofillManagerTestDelegate: | 115 // autofill::AutofillManagerTestDelegate: |
| 103 void DidPreviewFormData() override { | 116 void DidPreviewFormData() override { |
| 104 ASSERT_TRUE(loop_runner_->loop_running()); | 117 ASSERT_TRUE(loop_runner_->loop_running()); |
| 105 loop_runner_->Quit(); | 118 loop_runner_->Quit(); |
| 106 } | 119 } |
| 107 | 120 |
| 108 void DidFillFormData() override { | 121 void DidFillFormData() override { |
| 109 ASSERT_TRUE(loop_runner_->loop_running()); | 122 ASSERT_TRUE(loop_runner_->loop_running()); |
| 110 loop_runner_->Quit(); | 123 loop_runner_->Quit(); |
| 111 } | 124 } |
| 112 | 125 |
| 113 void DidShowSuggestions() override { | 126 void DidShowSuggestions() override { |
| 114 ASSERT_TRUE(loop_runner_->loop_running()); | 127 ASSERT_TRUE(loop_runner_->loop_running()); |
| 115 loop_runner_->Quit(); | 128 loop_runner_->Quit(); |
| 116 } | 129 } |
| 117 | 130 |
| 131 void OnTextFieldChanged() override { |
| 132 if (!waiting_for_text_change_) |
| 133 return; |
| 134 waiting_for_text_change_ = false; |
| 135 ASSERT_TRUE(loop_runner_->loop_running()); |
| 136 loop_runner_->Quit(); |
| 137 } |
| 138 |
| 118 void Reset() { | 139 void Reset() { |
| 119 loop_runner_ = new content::MessageLoopRunner(); | 140 loop_runner_ = new content::MessageLoopRunner(); |
| 120 } | 141 } |
| 121 | 142 |
| 122 void Wait() { | 143 void Wait() { |
| 123 loop_runner_->Run(); | 144 loop_runner_->Run(); |
| 124 } | 145 } |
| 125 | 146 |
| 147 void WaitForTextChange() { |
| 148 waiting_for_text_change_ = true; |
| 149 loop_runner_->Run(); |
| 150 } |
| 151 |
| 126 private: | 152 private: |
| 127 scoped_refptr<content::MessageLoopRunner> loop_runner_; | 153 scoped_refptr<content::MessageLoopRunner> loop_runner_; |
| 154 bool waiting_for_text_change_; |
| 128 | 155 |
| 129 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTestDelegateImpl); | 156 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTestDelegateImpl); |
| 130 }; | 157 }; |
| 131 | 158 |
| 132 // AutofillInteractiveTest ---------------------------------------------------- | 159 // AutofillInteractiveTest ---------------------------------------------------- |
| 133 | 160 |
| 134 class AutofillInteractiveTest : public InProcessBrowserTest { | 161 class AutofillInteractiveTest : public InProcessBrowserTest { |
| 135 protected: | 162 protected: |
| 136 AutofillInteractiveTest() : | 163 AutofillInteractiveTest() : |
| 137 key_press_event_sink_( | 164 key_press_event_sink_( |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 "})();" | 283 "})();" |
| 257 "cr.googleTranslate.onTranslateElementLoad();"; | 284 "cr.googleTranslate.onTranslateElementLoad();"; |
| 258 | 285 |
| 259 fetcher->set_url(fetcher->GetOriginalURL()); | 286 fetcher->set_url(fetcher->GetOriginalURL()); |
| 260 fetcher->set_status(net::URLRequestStatus::FromError(error)); | 287 fetcher->set_status(net::URLRequestStatus::FromError(error)); |
| 261 fetcher->set_response_code(success ? 200 : 500); | 288 fetcher->set_response_code(success ? 200 : 500); |
| 262 fetcher->SetResponseString(script); | 289 fetcher->SetResponseString(script); |
| 263 fetcher->delegate()->OnURLFetchComplete(fetcher); | 290 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 264 } | 291 } |
| 265 | 292 |
| 293 void FocusFieldByName(const std::string& name) { |
| 294 bool result = false; |
| 295 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 296 GetRenderViewHost(), |
| 297 "if (document.readyState === 'complete')" |
| 298 " document.getElementById('" + name + "').focus();" |
| 299 "else" |
| 300 " domAutomationController.send(false);", |
| 301 &result)); |
| 302 ASSERT_TRUE(result); |
| 303 } |
| 304 |
| 266 void FocusFirstNameField() { | 305 void FocusFirstNameField() { |
| 267 bool result = false; | 306 bool result = false; |
| 268 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | 307 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 269 GetRenderViewHost(), | 308 GetRenderViewHost(), |
| 270 "if (document.readyState === 'complete')" | 309 "if (document.readyState === 'complete')" |
| 271 " document.getElementById('firstname').focus();" | 310 " document.getElementById('firstname').focus();" |
| 272 "else" | 311 "else" |
| 273 " domAutomationController.send(false);", | 312 " domAutomationController.send(false);", |
| 274 &result)); | 313 &result)); |
| 275 ASSERT_TRUE(result); | 314 ASSERT_TRUE(result); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 ExpectFieldValue("phone", "5125551234"); | 365 ExpectFieldValue("phone", "5125551234"); |
| 327 } | 366 } |
| 328 | 367 |
| 329 void SendKeyToPageAndWait(ui::KeyboardCode key) { | 368 void SendKeyToPageAndWait(ui::KeyboardCode key) { |
| 330 test_delegate_.Reset(); | 369 test_delegate_.Reset(); |
| 331 content::SimulateKeyPress( | 370 content::SimulateKeyPress( |
| 332 GetWebContents(), key, false, false, false, false); | 371 GetWebContents(), key, false, false, false, false); |
| 333 test_delegate_.Wait(); | 372 test_delegate_.Wait(); |
| 334 } | 373 } |
| 335 | 374 |
| 375 void PasteStringAndWait(const std::string& pastedata) { |
| 376 { |
| 377 ui::ScopedClipboardWriter writer(ui::CLIPBOARD_TYPE_COPY_PASTE); |
| 378 writer.WriteText(base::ASCIIToUTF16(pastedata)); |
| 379 } |
| 380 test_delegate_.Reset(); |
| 381 GetWebContents()->Paste(); |
| 382 test_delegate_.WaitForTextChange(); |
| 383 } |
| 384 |
| 336 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event) { | 385 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event) { |
| 337 return true; | 386 return true; |
| 338 } | 387 } |
| 339 | 388 |
| 340 void SendKeyToPopupAndWait(ui::KeyboardCode key) { | 389 void SendKeyToPopupAndWait(ui::KeyboardCode key) { |
| 341 // Route popup-targeted key presses via the render view host. | 390 // Route popup-targeted key presses via the render view host. |
| 342 content::NativeWebKeyboardEvent event; | 391 content::NativeWebKeyboardEvent event; |
| 343 event.windowsKeyCode = key; | 392 event.windowsKeyCode = key; |
| 344 event.type = blink::WebKeyboardEvent::RawKeyDown; | 393 event.type = blink::WebKeyboardEvent::RawKeyDown; |
| 345 test_delegate_.Reset(); | 394 test_delegate_.Reset(); |
| (...skipping 1043 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1389 // the active field. | 1438 // the active field. |
| 1390 ASSERT_TRUE(content::ExecuteScript( | 1439 ASSERT_TRUE(content::ExecuteScript( |
| 1391 GetRenderViewHost(), | 1440 GetRenderViewHost(), |
| 1392 "document.querySelector('input').autocomplete = 'off';")); | 1441 "document.querySelector('input').autocomplete = 'off';")); |
| 1393 | 1442 |
| 1394 // Press the down arrow to select the suggestion and attempt to preview the | 1443 // Press the down arrow to select the suggestion and attempt to preview the |
| 1395 // autofilled form. | 1444 // autofilled form. |
| 1396 SendKeyToPopupAndWait(ui::VKEY_DOWN); | 1445 SendKeyToPopupAndWait(ui::VKEY_DOWN); |
| 1397 } | 1446 } |
| 1398 | 1447 |
| 1448 IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, |
| 1449 PastedPasswordIsSaved) { |
| 1450 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), |
| 1451 GURL(std::string(kDataURIPrefix) + kTestPasswordFormString))); |
| 1452 ASSERT_TRUE(content::ExecuteScript( |
| 1453 GetRenderViewHost(), |
| 1454 "document.getElementById('user').value = 'user';")); |
| 1455 FocusFieldByName("password"); |
| 1456 PasteStringAndWait("foobar"); |
| 1457 } |
| 1458 |
| 1399 } // namespace autofill | 1459 } // namespace autofill |
| OLD | NEW |