| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "chrome/browser/infobars/confirm_infobar_delegate.h" |
| 8 #include "chrome/browser/infobars/infobar_service.h" |
| 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 11 #include "chrome/common/chrome_notification_types.h" |
| 12 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "chrome/test/base/ui_test_utils.h" |
| 14 #include "content/public/browser/notification_observer.h" |
| 15 #include "content/public/browser/notification_registrar.h" |
| 16 #include "content/public/browser/notification_service.h" |
| 17 #include "content/public/browser/render_view_host.h" |
| 18 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/browser/web_contents_observer.h" |
| 20 #include "content/public/test/browser_test_utils.h" |
| 21 #include "content/public/test/test_utils.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "ui/base/keycodes/keyboard_codes.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 class NavigationObserver : public content::NotificationObserver, |
| 28 public content::WebContentsObserver { |
| 29 public: |
| 30 explicit NavigationObserver(content::WebContents* web_contents) |
| 31 : content::WebContentsObserver(web_contents), |
| 32 message_loop_runner_(new content::MessageLoopRunner), |
| 33 info_bar_shown_(false), |
| 34 infobar_service_(InfoBarService::FromWebContents(web_contents)) { |
| 35 registrar_.Add(this, |
| 36 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, |
| 37 content::Source<InfoBarService>(infobar_service_)); |
| 38 } |
| 39 |
| 40 virtual ~NavigationObserver() {} |
| 41 |
| 42 void Wait() { |
| 43 message_loop_runner_->Run(); |
| 44 } |
| 45 |
| 46 bool InfoBarWasShown() { |
| 47 return info_bar_shown_; |
| 48 } |
| 49 |
| 50 // content::NotificationObserver: |
| 51 virtual void Observe(int type, |
| 52 const content::NotificationSource& source, |
| 53 const content::NotificationDetails& details) OVERRIDE { |
| 54 // Accept in the infobar. |
| 55 InfoBarDelegate* infobar = infobar_service_->GetInfoBarDelegateAt(0); |
| 56 ConfirmInfoBarDelegate* confirm_infobar = |
| 57 infobar->AsConfirmInfoBarDelegate(); |
| 58 confirm_infobar->Accept(); |
| 59 info_bar_shown_ = true; |
| 60 } |
| 61 |
| 62 // content::WebContentsObserver |
| 63 virtual void DidFinishLoad(int64 frame_id, |
| 64 const GURL& validated_url, |
| 65 bool is_main_frame, |
| 66 content::RenderViewHost* render_view_host) { |
| 67 message_loop_runner_->Quit(); |
| 68 } |
| 69 |
| 70 private: |
| 71 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 72 bool info_bar_shown_; |
| 73 content::NotificationRegistrar registrar_; |
| 74 InfoBarService* infobar_service_; |
| 75 }; |
| 76 |
| 77 } // namespace |
| 78 |
| 79 class PasswordManagerBrowserTest : public InProcessBrowserTest { |
| 80 protected: |
| 81 content::WebContents* WebContents() { |
| 82 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 83 } |
| 84 |
| 85 content::RenderViewHost* RenderViewHost() { |
| 86 return WebContents()->GetRenderViewHost(); |
| 87 } |
| 88 }; |
| 89 |
| 90 IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, PromptForXHRSubmit) { |
| 91 ASSERT_TRUE(test_server()->Start()); |
| 92 |
| 93 GURL url = test_server()->GetURL("files/password/password_xhr_submit.html"); |
| 94 ui_test_utils::NavigateToURL(browser(), url); |
| 95 |
| 96 // Verify that we show the save password prompt if a form returns false |
| 97 // in its onsubmit handler but instead logs in/navigates via XHR. |
| 98 // Note that calling 'submit()' on a form with javascript doesn't call |
| 99 // the onsubmit handler, so we click the submit button instead. |
| 100 NavigationObserver observer(WebContents()); |
| 101 std::string fill_and_submit = |
| 102 "document.getElementById('username_field').value = 'temp';" |
| 103 "document.getElementById('password_field').value = 'random';" |
| 104 "document.getElementById('submit_button').click()"; |
| 105 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit)); |
| 106 observer.Wait(); |
| 107 EXPECT_TRUE(observer.InfoBarWasShown()); |
| 108 } |
| 109 |
| 110 IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, NoPromptForOtherXHR) { |
| 111 ASSERT_TRUE(test_server()->Start()); |
| 112 |
| 113 GURL url = test_server()->GetURL("files/password/password_xhr_submit.html"); |
| 114 ui_test_utils::NavigateToURL(browser(), url); |
| 115 |
| 116 // Verify that if random XHR navigation occurs, we don't try and save the |
| 117 // password. |
| 118 // |
| 119 // We may want to change this functionality in the future to account for |
| 120 // cases where the element that users click on isn't a submit button. |
| 121 NavigationObserver observer(WebContents()); |
| 122 std::string fill_and_navigate = |
| 123 "document.getElementById('username_field').value = 'temp';" |
| 124 "document.getElementById('password_field').value = 'random';" |
| 125 "send_xhr()"; |
| 126 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_navigate)); |
| 127 observer.Wait(); |
| 128 EXPECT_FALSE(observer.InfoBarWasShown()); |
| 129 } |
| OLD | NEW |