OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/metrics/histogram_samples.h" | 8 #include "base/metrics/histogram_samples.h" |
9 #include "base/metrics/statistics_recorder.h" | 9 #include "base/metrics/statistics_recorder.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 | 39 |
40 // Observer that waits for navigation to complete and for the password infobar | 40 // Observer that waits for navigation to complete and for the password infobar |
41 // to be shown. | 41 // to be shown. |
42 class NavigationObserver : public content::NotificationObserver, | 42 class NavigationObserver : public content::NotificationObserver, |
43 public content::WebContentsObserver { | 43 public content::WebContentsObserver { |
44 public: | 44 public: |
45 explicit NavigationObserver(content::WebContents* web_contents) | 45 explicit NavigationObserver(content::WebContents* web_contents) |
46 : content::WebContentsObserver(web_contents), | 46 : content::WebContentsObserver(web_contents), |
47 message_loop_runner_(new content::MessageLoopRunner), | 47 message_loop_runner_(new content::MessageLoopRunner), |
48 infobar_shown_(false), | 48 infobar_shown_(false), |
| 49 infobar_removed_(false), |
| 50 should_automatically_accept_infobar_(true), |
49 infobar_service_(InfoBarService::FromWebContents(web_contents)) { | 51 infobar_service_(InfoBarService::FromWebContents(web_contents)) { |
50 registrar_.Add(this, | 52 registrar_.Add(this, |
51 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | 53 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, |
52 content::Source<InfoBarService>(infobar_service_)); | 54 content::Source<InfoBarService>(infobar_service_)); |
| 55 registrar_.Add(this, |
| 56 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
| 57 content::Source<InfoBarService>(infobar_service_)); |
53 } | 58 } |
54 | 59 |
55 virtual ~NavigationObserver() {} | 60 virtual ~NavigationObserver() {} |
56 | 61 |
57 // Normally Wait() will not return until a main frame navigation occurs. | 62 // Normally Wait() will not return until a main frame navigation occurs. |
58 // If a path is set, Wait() will return after this path has been seen, | 63 // If a path is set, Wait() will return after this path has been seen, |
59 // regardless of the frame that navigated. Useful for multi-frame pages. | 64 // regardless of the frame that navigated. Useful for multi-frame pages. |
60 void SetPathToWaitFor(const std::string& path) { | 65 void SetPathToWaitFor(const std::string& path) { |
61 wait_for_path_ = path; | 66 wait_for_path_ = path; |
62 } | 67 } |
63 | 68 |
64 // content::NotificationObserver: | 69 // content::NotificationObserver: |
65 virtual void Observe(int type, | 70 virtual void Observe(int type, |
66 const content::NotificationSource& source, | 71 const content::NotificationSource& source, |
67 const content::NotificationDetails& details) OVERRIDE { | 72 const content::NotificationDetails& details) OVERRIDE { |
68 infobar_service_->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate()-> | 73 switch (type) { |
69 Accept(); | 74 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED: |
70 infobar_shown_ = true; | 75 if (should_automatically_accept_infobar_) { |
| 76 infobar_service_->infobar_at(0) |
| 77 ->delegate() |
| 78 ->AsConfirmInfoBarDelegate() |
| 79 ->Accept(); |
| 80 } |
| 81 infobar_shown_ = true; |
| 82 return; |
| 83 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED: |
| 84 infobar_removed_ = true; |
| 85 return; |
| 86 default: |
| 87 NOTREACHED(); |
| 88 return; |
| 89 } |
71 } | 90 } |
72 | 91 |
73 // content::WebContentsObserver: | 92 // content::WebContentsObserver: |
74 virtual void DidFinishLoad( | 93 virtual void DidFinishLoad( |
75 int64 frame_id, | 94 int64 frame_id, |
76 const GURL& validated_url, | 95 const GURL& validated_url, |
77 bool is_main_frame, | 96 bool is_main_frame, |
78 content::RenderViewHost* render_view_host) OVERRIDE { | 97 content::RenderViewHost* render_view_host) OVERRIDE { |
79 if (!wait_for_path_.empty()) { | 98 if (!wait_for_path_.empty()) { |
80 if (validated_url.path() == wait_for_path_) | 99 if (validated_url.path() == wait_for_path_) |
81 message_loop_runner_->Quit(); | 100 message_loop_runner_->Quit(); |
82 } else if (is_main_frame) { | 101 } else if (is_main_frame) { |
83 message_loop_runner_->Quit(); | 102 message_loop_runner_->Quit(); |
84 } | 103 } |
85 } | 104 } |
86 | 105 |
87 bool infobar_shown() const { return infobar_shown_; } | 106 bool infobar_shown() const { return infobar_shown_; } |
| 107 bool infobar_removed() const { return infobar_removed_; } |
| 108 |
| 109 void disable_should_automatically_accept_infobar() { |
| 110 should_automatically_accept_infobar_ = false; |
| 111 } |
88 | 112 |
89 void Wait() { | 113 void Wait() { |
90 message_loop_runner_->Run(); | 114 message_loop_runner_->Run(); |
91 } | 115 } |
92 | 116 |
93 private: | 117 private: |
94 std::string wait_for_path_; | 118 std::string wait_for_path_; |
95 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | 119 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
96 bool infobar_shown_; | 120 bool infobar_shown_; |
| 121 bool infobar_removed_; |
| 122 // If |should_automatically_accept_infobar_| is true, then whenever the test |
| 123 // sees an infobar added, it will click its accepting button. Default = true. |
| 124 bool should_automatically_accept_infobar_; |
97 content::NotificationRegistrar registrar_; | 125 content::NotificationRegistrar registrar_; |
98 InfoBarService* infobar_service_; | 126 InfoBarService* infobar_service_; |
99 | 127 |
100 DISALLOW_COPY_AND_ASSIGN(NavigationObserver); | 128 DISALLOW_COPY_AND_ASSIGN(NavigationObserver); |
101 }; | 129 }; |
102 | 130 |
103 } // namespace | 131 } // namespace |
104 | 132 |
105 | 133 |
106 // PasswordManagerBrowserTest ------------------------------------------------- | 134 // PasswordManagerBrowserTest ------------------------------------------------- |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 NavigationObserver observer(WebContents()); | 203 NavigationObserver observer(WebContents()); |
176 std::string fill_and_submit = | 204 std::string fill_and_submit = |
177 "document.getElementById('username_field').value = 'temp';" | 205 "document.getElementById('username_field').value = 'temp';" |
178 "document.getElementById('password_field').value = 'random';" | 206 "document.getElementById('password_field').value = 'random';" |
179 "document.getElementById('input_submit_button').click()"; | 207 "document.getElementById('input_submit_button').click()"; |
180 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit)); | 208 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit)); |
181 observer.Wait(); | 209 observer.Wait(); |
182 EXPECT_TRUE(observer.infobar_shown()); | 210 EXPECT_TRUE(observer.infobar_shown()); |
183 } | 211 } |
184 | 212 |
| 213 IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, Redirects) { |
| 214 NavigateToFile("/password/password_form.html"); |
| 215 |
| 216 // Fill a form and submit through a <input type="submit"> button. The form |
| 217 // points to a redirection page. |
| 218 NavigationObserver observer(WebContents()); |
| 219 std::string fill_and_submit = |
| 220 "document.getElementById('username_redirect').value = 'temp';" |
| 221 "document.getElementById('password_redirect').value = 'random';" |
| 222 "document.getElementById('submit_redirect').click()"; |
| 223 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit)); |
| 224 observer.disable_should_automatically_accept_infobar(); |
| 225 observer.Wait(); |
| 226 EXPECT_TRUE(observer.infobar_shown()); |
| 227 |
| 228 // The redirection page now redirects via Javascript. We check that the |
| 229 // infobar stays. |
| 230 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), |
| 231 "window.location.href = 'done.html';")); |
| 232 observer.Wait(); |
| 233 EXPECT_FALSE(observer.infobar_removed()); |
| 234 } |
| 235 |
185 IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, | 236 IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, |
186 PromptForSubmitUsingJavaScript) { | 237 PromptForSubmitUsingJavaScript) { |
187 NavigateToFile("/password/password_form.html"); | 238 NavigateToFile("/password/password_form.html"); |
188 | 239 |
189 // Fill a form and submit using <button> that calls submit() on the form. | 240 // Fill a form and submit using <button> that calls submit() on the form. |
190 // This should work regardless of the type of element, as long as submit() is | 241 // This should work regardless of the type of element, as long as submit() is |
191 // called. | 242 // called. |
192 NavigationObserver observer(WebContents()); | 243 NavigationObserver observer(WebContents()); |
193 std::string fill_and_submit = | 244 std::string fill_and_submit = |
194 "document.getElementById('username_field').value = 'temp';" | 245 "document.getElementById('username_field').value = 'temp';" |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 "document.getElementById('username_field').value = 'temp';" | 520 "document.getElementById('username_field').value = 'temp';" |
470 "document.getElementById('password_field').value = 'random';" | 521 "document.getElementById('password_field').value = 'random';" |
471 "document.getElementById('input_submit_button').click();" | 522 "document.getElementById('input_submit_button').click();" |
472 "window.location.href = 'done.html';"; | 523 "window.location.href = 'done.html';"; |
473 | 524 |
474 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), save_and_remove)); | 525 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), save_and_remove)); |
475 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), navigate_frame)); | 526 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), navigate_frame)); |
476 observer.Wait(); | 527 observer.Wait(); |
477 // The only thing we check here is that there is no use-after-free reported. | 528 // The only thing we check here is that there is no use-after-free reported. |
478 } | 529 } |
OLD | NEW |