Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: chrome/browser/password_manager/password_manager_browsertest.cc

Issue 243953002: Update PasswordManagerBrowserTest to use the new Observer class from InfoBarManager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleanup Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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:
96 virtual void OnInfoBarAdded(InfoBar* infobar) OVERRIDE;
97 virtual void OnInfoBarRemoved(InfoBar* infobar, bool animate) OVERRIDE;
98 virtual void OnManagerShuttingDown(InfoBarManager* manager) OVERRIDE;
99
118 std::string wait_for_path_; 100 std::string wait_for_path_;
119 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; 101 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
120 bool infobar_shown_; 102 bool infobar_shown_;
121 bool infobar_removed_; 103 bool infobar_removed_;
122 // If |should_automatically_accept_infobar_| is true, then whenever the test 104 // If |should_automatically_accept_infobar_| is true, then whenever the test
123 // sees an infobar added, it will click its accepting button. Default = true. 105 // sees an infobar added, it will click its accepting button. Default = true.
124 bool should_automatically_accept_infobar_; 106 bool should_automatically_accept_infobar_;
125 content::NotificationRegistrar registrar_;
126 InfoBarService* infobar_service_; 107 InfoBarService* infobar_service_;
127 108
128 DISALLOW_COPY_AND_ASSIGN(NavigationObserver); 109 DISALLOW_COPY_AND_ASSIGN(NavigationObserver);
129 }; 110 };
130 111
112 void NavigationObserver::OnInfoBarAdded(InfoBar* infobar) {
113 if (should_automatically_accept_infobar_) {
114 infobar_service_->infobar_at(0)->delegate()->
115 AsConfirmInfoBarDelegate()->Accept();
116 }
117 infobar_shown_ = true;
118 }
119
120 void NavigationObserver::OnInfoBarRemoved(InfoBar* infobar, bool animate) {
121 infobar_removed_ = true;
122 }
123
124 void NavigationObserver::OnManagerShuttingDown(InfoBarManager* manager) {
125 ASSERT_EQ(infobar_service_, manager);
126 infobar_service_->RemoveObserver(this);
127 infobar_service_ = NULL;
128 }
Ilya Sherman 2014/04/19 19:46:04 nit: Please inline these implementations, to match
tfarina 2014/04/19 22:03:23 Done.
129
131 } // namespace 130 } // namespace
132 131
133 132
134 // PasswordManagerBrowserTest ------------------------------------------------- 133 // PasswordManagerBrowserTest -------------------------------------------------
135 134
136 class PasswordManagerBrowserTest : public InProcessBrowserTest { 135 class PasswordManagerBrowserTest : public InProcessBrowserTest {
137 public: 136 public:
138 PasswordManagerBrowserTest() {} 137 PasswordManagerBrowserTest() {}
139 virtual ~PasswordManagerBrowserTest() {} 138 virtual ~PasswordManagerBrowserTest() {}
140 139
(...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 DontPromptForPasswordFormWithDefaultValue) { 646 DontPromptForPasswordFormWithDefaultValue) {
648 NavigateToFile("/password/password_form_with_default_value.html"); 647 NavigateToFile("/password/password_form_with_default_value.html");
649 648
650 // Don't prompt if we navigate away even if there is a password value since 649 // Don't prompt if we navigate away even if there is a password value since
651 // it's not coming from the user. 650 // it's not coming from the user.
652 NavigationObserver observer(WebContents()); 651 NavigationObserver observer(WebContents());
653 NavigateToFile("/password/done.html"); 652 NavigateToFile("/password/done.html");
654 observer.Wait(); 653 observer.Wait();
655 EXPECT_FALSE(observer.infobar_shown()); 654 EXPECT_FALSE(observer.infobar_shown());
656 } 655 }
OLDNEW
« chrome/browser/infobars/infobar_manager.h ('K') | « chrome/browser/infobars/infobar_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698