Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
|
Ilya Sherman
2013/03/22 03:26:48
ultra nit: Omit the "(c)"
Garrett Casto
2013/03/22 21:05:05
Done.
| |
| 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/api/infobars/confirm_infobar_delegate.h" | |
| 8 #include "chrome/browser/api/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 "testing/gmock/include/gmock/gmock.h" | |
| 22 #include "ui/base/keycodes/keyboard_codes.h" | |
| 23 | |
| 24 class NavigationObserver : public content::NotificationObserver, | |
| 25 public content::WebContentsObserver { | |
| 26 public: | |
| 27 explicit NavigationObserver(Browser* browser) | |
|
Ilya Sherman
2013/03/22 03:26:48
nit: Can you pass in just the WebContents, rather
Garrett Casto
2013/03/22 21:05:05
Done.
| |
| 28 : content::WebContentsObserver( | |
| 29 browser->tab_strip_model()->GetActiveWebContents()), | |
| 30 has_run_message_loop_(false), | |
| 31 done_loading_(false), | |
| 32 info_bar_shown_(false), | |
| 33 browser_(browser), | |
| 34 infobar_service_(NULL) { | |
| 35 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
| 36 content::NotificationService::AllSources()); | |
|
Ilya Sherman
2013/03/22 03:26:48
nit: Can you listen to notifications from a specif
Garrett Casto
2013/03/22 21:05:05
Changed to use a specific source. I don't think th
| |
| 37 } | |
| 38 | |
| 39 virtual ~NavigationObserver() { | |
| 40 if (!infobar_service_) | |
| 41 return; | |
| 42 | |
| 43 InfoBarDelegate* infobar = NULL; | |
| 44 if (infobar_service_->GetInfoBarCount() > 0 && | |
| 45 (infobar = infobar_service_->GetInfoBarDelegateAt(0))) { | |
| 46 infobar_service_->RemoveInfoBar(infobar); | |
|
Ilya Sherman
2013/03/22 03:26:48
Hmm, why do you need to do this manually? Doesn't
Garrett Casto
2013/03/22 21:05:05
I actually just pulled this from autofill_browsert
| |
| 47 } | |
| 48 } | |
| 49 | |
| 50 void Wait() { | |
| 51 if (!done_loading_) { | |
| 52 has_run_message_loop_ = true; | |
| 53 content::RunMessageLoop(); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 bool InfoBarWasShown() { | |
| 58 return info_bar_shown_; | |
| 59 } | |
| 60 | |
| 61 // content::NotificationObserver: | |
| 62 virtual void Observe(int type, | |
| 63 const content::NotificationSource& source, | |
| 64 const content::NotificationDetails& details) OVERRIDE { | |
| 65 // Accept in the infobar. | |
| 66 infobar_service_ = InfoBarService::FromWebContents( | |
| 67 browser_->tab_strip_model()->GetActiveWebContents()); | |
| 68 InfoBarDelegate* infobar = infobar_service_->GetInfoBarDelegateAt(0); | |
| 69 | |
| 70 ConfirmInfoBarDelegate* confirm_infobar = | |
| 71 infobar->AsConfirmInfoBarDelegate(); | |
| 72 confirm_infobar->Accept(); | |
| 73 info_bar_shown_ = true; | |
| 74 } | |
| 75 | |
| 76 // content::WebContentsObserver | |
| 77 virtual void DidFinishLoad(int64 frame_id, | |
| 78 const GURL& validated_url, | |
| 79 bool is_main_frame, | |
| 80 content::RenderViewHost* render_view_host) { | |
| 81 done_loading_ = true; | |
| 82 if (has_run_message_loop_) { | |
| 83 MessageLoopForUI::current()->Quit(); | |
| 84 has_run_message_loop_ = false; | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 bool has_run_message_loop_; | |
|
Ilya Sherman
2013/03/22 03:26:48
Use content::MessageLoopRunner instead.
Garrett Casto
2013/03/22 21:05:05
Done.
| |
| 90 bool done_loading_; | |
| 91 bool info_bar_shown_; | |
| 92 content::NotificationRegistrar registrar_; | |
| 93 Browser* browser_; | |
| 94 InfoBarService* infobar_service_; | |
| 95 }; | |
|
Ilya Sherman
2013/03/22 03:26:48
nit: Tuck this into an anonymous namespace.
Garrett Casto
2013/03/22 21:05:05
Done.
| |
| 96 | |
| 97 class PasswordManagerBrowserTest : public InProcessBrowserTest { | |
| 98 protected: | |
| 99 content::RenderViewHost* render_view_host() { | |
|
Ilya Sherman
2013/03/22 03:26:48
nit: This should use MixedCaps rather than hacker_
Garrett Casto
2013/03/22 21:05:05
Done.
| |
| 100 return browser()->tab_strip_model()->GetActiveWebContents()-> | |
| 101 GetRenderViewHost(); | |
| 102 } | |
| 103 }; | |
| 104 | |
| 105 IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, PromptForXHRSubmit) { | |
|
Ilya Sherman
2013/03/22 03:26:48
As long as you're adding test coverage (thanks!),
Garrett Casto
2013/03/22 21:05:05
There is actually a full pyauto test suite for the
Ilya Sherman
2013/03/23 01:10:58
Ok, fair enough :)
| |
| 106 ASSERT_TRUE(test_server()->Start()); | |
| 107 | |
| 108 GURL url = test_server()->GetURL("files/password/password_xhr_submit.html"); | |
| 109 ui_test_utils::NavigateToURL(browser(), url); | |
| 110 | |
| 111 // Verify that we show the save password prompt if a form returns false | |
| 112 // in it's onsubmit handler but instead logs in/navigates via XHR. | |
|
Ilya Sherman
2013/03/22 03:26:48
nit: "it's" -> "its"
Garrett Casto
2013/03/22 21:05:05
Done.
| |
| 113 // Note that calling 'submit()' on a form with javascript doesn't call | |
| 114 // the onsubmit handler, so we click the submit button instead. | |
| 115 NavigationObserver observer(browser()); | |
| 116 std::string fill_and_submit = | |
| 117 "document.getElementById('username_field').value = 'temp';" | |
| 118 "document.getElementById('password_field').value = 'random';" | |
| 119 "document.getElementById('submit_button').click()"; | |
| 120 ASSERT_TRUE(content::ExecuteScript(render_view_host(), fill_and_submit)); | |
|
Ilya Sherman
2013/03/22 03:26:48
Can you have the loaded page send a result when it
Garrett Casto
2013/03/22 21:05:05
I'm not sure what exactly you have in mind here. I
Ilya Sherman
2013/03/23 01:10:58
Hmm, I kind of ignored the fact that the JavaScrip
| |
| 121 observer.Wait(); | |
| 122 ASSERT_TRUE(observer.InfoBarWasShown()); | |
|
Ilya Sherman
2013/03/22 03:26:48
nit: I think this can be EXPECT_TRUE.
Garrett Casto
2013/03/22 21:05:05
Done.
| |
| 123 } | |
| 124 | |
| 125 IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, NoPromptForOtherXHR) { | |
| 126 ASSERT_TRUE(test_server()->Start()); | |
| 127 | |
| 128 GURL url = test_server()->GetURL("files/password/password_xhr_submit.html"); | |
| 129 ui_test_utils::NavigateToURL(browser(), url); | |
| 130 | |
| 131 // Verify that if random XHR navigation occurs, we don't try and save the | |
| 132 // password. | |
| 133 // | |
| 134 // We may want to change this functionality in the future to account for | |
| 135 // cases where the element that users click on isn't a submit button. | |
| 136 NavigationObserver observer(browser()); | |
| 137 std::string fill_and_navigate = | |
| 138 "document.getElementById('username_field').value = 'temp';" | |
| 139 "document.getElementById('password_field').value = 'random';" | |
| 140 "send_xhr()"; | |
| 141 ASSERT_TRUE(content::ExecuteScript(render_view_host(), fill_and_navigate)); | |
| 142 observer.Wait(); | |
| 143 ASSERT_FALSE(observer.InfoBarWasShown()); | |
|
Ilya Sherman
2013/03/22 03:26:48
nit: I think this can be EXPECT_FALSE.
Garrett Casto
2013/03/22 21:05:05
Done.
| |
| 144 } | |
| OLD | NEW |