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 23 matching lines...) Expand all Loading... | |
34 #include "third_party/WebKit/public/web/WebInputEvent.h" | 34 #include "third_party/WebKit/public/web/WebInputEvent.h" |
35 #include "ui/events/keycodes/keyboard_codes.h" | 35 #include "ui/events/keycodes/keyboard_codes.h" |
36 | 36 |
37 | 37 |
38 // NavigationObserver --------------------------------------------------------- | 38 // NavigationObserver --------------------------------------------------------- |
39 | 39 |
40 namespace { | 40 namespace { |
41 | 41 |
42 // Observer that waits for navigation to complete and for the password infobar | 42 // Observer that waits for navigation to complete and for the password infobar |
43 // to be shown. | 43 // to be shown. |
44 class NavigationObserver : public content::NotificationObserver, | 44 class NavigationObserver : public content::WebContentsObserver, |
45 public content::WebContentsObserver { | 45 public InfoBarManager::Observer { |
46 public: | 46 public: |
47 explicit NavigationObserver(content::WebContents* web_contents) | 47 explicit NavigationObserver(content::WebContents* web_contents) |
48 : content::WebContentsObserver(web_contents), | 48 : content::WebContentsObserver(web_contents), |
49 message_loop_runner_(new content::MessageLoopRunner), | 49 message_loop_runner_(new content::MessageLoopRunner), |
50 infobar_shown_(false), | 50 infobar_shown_(false), |
51 infobar_removed_(false), | 51 infobar_removed_(false), |
52 should_automatically_accept_infobar_(true), | 52 should_automatically_accept_infobar_(true), |
53 infobar_service_(InfoBarService::FromWebContents(web_contents)) { | 53 infobar_service_(InfoBarService::FromWebContents(web_contents)) { |
54 registrar_.Add(this, | 54 infobar_service_->AddObserver(this); |
55 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
56 content::Source<InfoBarService>(infobar_service_)); | |
57 registrar_.Add(this, | |
58 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
59 content::Source<InfoBarService>(infobar_service_)); | |
60 } | 55 } |
61 | 56 |
62 virtual ~NavigationObserver() {} | 57 virtual ~NavigationObserver() { |
58 if (infobar_service_) | |
59 infobar_service_->RemoveObserver(this); | |
60 } | |
63 | 61 |
64 // Normally Wait() will not return until a main frame navigation occurs. | 62 // Normally Wait() will not return until a main frame navigation occurs. |
65 // 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, |
66 // regardless of the frame that navigated. Useful for multi-frame pages. | 64 // regardless of the frame that navigated. Useful for multi-frame pages. |
67 void SetPathToWaitFor(const std::string& path) { | 65 void SetPathToWaitFor(const std::string& path) { |
68 wait_for_path_ = path; | 66 wait_for_path_ = path; |
69 } | 67 } |
70 | 68 |
71 // content::NotificationObserver: | |
72 virtual void Observe(int type, | |
73 const content::NotificationSource& source, | |
74 const content::NotificationDetails& details) OVERRIDE { | |
75 switch (type) { | |
76 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED: | |
77 if (should_automatically_accept_infobar_) { | |
78 infobar_service_->infobar_at(0)->delegate()-> | |
79 AsConfirmInfoBarDelegate()->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 } | |
90 } | |
91 | |
92 // content::WebContentsObserver: | 69 // content::WebContentsObserver: |
93 virtual void DidFinishLoad( | 70 virtual void DidFinishLoad( |
94 int64 frame_id, | 71 int64 frame_id, |
95 const GURL& validated_url, | 72 const GURL& validated_url, |
96 bool is_main_frame, | 73 bool is_main_frame, |
97 content::RenderViewHost* render_view_host) OVERRIDE { | 74 content::RenderViewHost* render_view_host) OVERRIDE { |
98 if (!wait_for_path_.empty()) { | 75 if (!wait_for_path_.empty()) { |
99 if (validated_url.path() == wait_for_path_) | 76 if (validated_url.path() == wait_for_path_) |
100 message_loop_runner_->Quit(); | 77 message_loop_runner_->Quit(); |
101 } else if (is_main_frame) { | 78 } else if (is_main_frame) { |
102 message_loop_runner_->Quit(); | 79 message_loop_runner_->Quit(); |
103 } | 80 } |
104 } | 81 } |
105 | 82 |
106 bool infobar_shown() const { return infobar_shown_; } | 83 bool infobar_shown() const { return infobar_shown_; } |
107 bool infobar_removed() const { return infobar_removed_; } | 84 bool infobar_removed() const { return infobar_removed_; } |
108 | 85 |
109 void disable_should_automatically_accept_infobar() { | 86 void disable_should_automatically_accept_infobar() { |
110 should_automatically_accept_infobar_ = false; | 87 should_automatically_accept_infobar_ = false; |
111 } | 88 } |
112 | 89 |
113 void Wait() { | 90 void Wait() { |
114 message_loop_runner_->Run(); | 91 message_loop_runner_->Run(); |
115 } | 92 } |
116 | 93 |
117 private: | 94 private: |
95 // InfoBarManager::Observer | |
Peter Kasting
2014/04/18 23:52:39
Nit: Add trailing colon
tfarina
2014/04/19 18:43:10
Done.
| |
96 virtual void OnInfoBarAdded(InfoBar* infobar) OVERRIDE { | |
97 if (should_automatically_accept_infobar_) { | |
98 infobar_service_->infobar_at(0) | |
99 ->delegate() | |
100 ->AsConfirmInfoBarDelegate() | |
101 ->Accept(); | |
Peter Kasting
2014/04/18 23:52:39
Nit: Wrap like the old code did
tfarina
2014/04/19 18:43:10
Done.
| |
102 } | |
103 infobar_shown_ = true; | |
104 } | |
Ilya Sherman
2014/04/19 04:21:27
nit: Please leave a blank line between method defi
| |
105 virtual void OnInfoBarRemoved(InfoBar* infobar, bool animate) OVERRIDE { | |
106 infobar_removed_ = true; | |
107 } | |
108 virtual void OnInfoBarReplaced(InfoBar* old_infobar, | |
109 InfoBar* new_infobar) OVERRIDE {} | |
Peter Kasting
2014/04/18 23:52:39
Subclasses shouldn't need to override methods they
tfarina
2014/04/19 18:43:10
Done.
| |
110 virtual void OnManagerShuttingDown(InfoBarManager* manager) OVERRIDE { | |
111 DCHECK_EQ(infobar_service_, manager); | |
Ilya Sherman
2014/04/19 04:21:27
nit: ASSERT_EQ is preferable in tests, as it doesn
tfarina
2014/04/19 18:43:10
Done.
| |
112 infobar_service_->RemoveObserver(this); | |
113 infobar_service_ = NULL; | |
114 } | |
115 | |
118 std::string wait_for_path_; | 116 std::string wait_for_path_; |
119 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | 117 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
120 bool infobar_shown_; | 118 bool infobar_shown_; |
121 bool infobar_removed_; | 119 bool infobar_removed_; |
122 // If |should_automatically_accept_infobar_| is true, then whenever the test | 120 // If |should_automatically_accept_infobar_| is true, then whenever the test |
123 // sees an infobar added, it will click its accepting button. Default = true. | 121 // sees an infobar added, it will click its accepting button. Default = true. |
124 bool should_automatically_accept_infobar_; | 122 bool should_automatically_accept_infobar_; |
125 content::NotificationRegistrar registrar_; | |
126 InfoBarService* infobar_service_; | 123 InfoBarService* infobar_service_; |
127 | 124 |
128 DISALLOW_COPY_AND_ASSIGN(NavigationObserver); | 125 DISALLOW_COPY_AND_ASSIGN(NavigationObserver); |
129 }; | 126 }; |
130 | 127 |
131 } // namespace | 128 } // namespace |
132 | 129 |
133 | 130 |
134 // PasswordManagerBrowserTest ------------------------------------------------- | 131 // PasswordManagerBrowserTest ------------------------------------------------- |
135 | 132 |
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
647 DontPromptForPasswordFormWithDefaultValue) { | 644 DontPromptForPasswordFormWithDefaultValue) { |
648 NavigateToFile("/password/password_form_with_default_value.html"); | 645 NavigateToFile("/password/password_form_with_default_value.html"); |
649 | 646 |
650 // Don't prompt if we navigate away even if there is a password value since | 647 // Don't prompt if we navigate away even if there is a password value since |
651 // it's not coming from the user. | 648 // it's not coming from the user. |
652 NavigationObserver observer(WebContents()); | 649 NavigationObserver observer(WebContents()); |
653 NavigateToFile("/password/done.html"); | 650 NavigateToFile("/password/done.html"); |
654 observer.Wait(); | 651 observer.Wait(); |
655 EXPECT_FALSE(observer.infobar_shown()); | 652 EXPECT_FALSE(observer.infobar_shown()); |
656 } | 653 } |
OLD | NEW |