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

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: include infobar_manager.h it is now in components/infobars/ 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"
11 #include "chrome/browser/infobars/confirm_infobar_delegate.h" 11 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
12 #include "chrome/browser/infobars/infobar_service.h" 12 #include "chrome/browser/infobars/infobar_service.h"
13 #include "chrome/browser/password_manager/password_store_factory.h" 13 #include "chrome/browser/password_manager/password_store_factory.h"
14 #include "chrome/browser/password_manager/test_password_store_service.h" 14 #include "chrome/browser/password_manager/test_password_store_service.h"
15 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" 16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/test/base/in_process_browser_test.h" 17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/test_switches.h" 18 #include "chrome/test/base/test_switches.h"
19 #include "chrome/test/base/ui_test_utils.h" 19 #include "chrome/test/base/ui_test_utils.h"
20 #include "components/autofill/core/browser/autofill_test_utils.h" 20 #include "components/autofill/core/browser/autofill_test_utils.h"
21 #include "components/infobars/core/infobar.h" 21 #include "components/infobars/core/infobar.h"
22 #include "components/infobars/core/infobar_manager.h"
22 #include "components/password_manager/core/browser/test_password_store.h" 23 #include "components/password_manager/core/browser/test_password_store.h"
23 #include "content/public/browser/notification_observer.h" 24 #include "content/public/browser/notification_observer.h"
24 #include "content/public/browser/notification_registrar.h" 25 #include "content/public/browser/notification_registrar.h"
25 #include "content/public/browser/notification_service.h" 26 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/render_view_host.h" 27 #include "content/public/browser/render_view_host.h"
27 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
28 #include "content/public/browser/web_contents_observer.h" 29 #include "content/public/browser/web_contents_observer.h"
29 #include "content/public/test/browser_test_utils.h" 30 #include "content/public/test/browser_test_utils.h"
30 #include "content/public/test/test_utils.h" 31 #include "content/public/test/test_utils.h"
31 #include "net/test/embedded_test_server/embedded_test_server.h" 32 #include "net/test/embedded_test_server/embedded_test_server.h"
32 #include "net/url_request/test_url_fetcher_factory.h" 33 #include "net/url_request/test_url_fetcher_factory.h"
33 #include "testing/gmock/include/gmock/gmock.h" 34 #include "testing/gmock/include/gmock/gmock.h"
34 #include "third_party/WebKit/public/web/WebInputEvent.h" 35 #include "third_party/WebKit/public/web/WebInputEvent.h"
35 #include "ui/events/keycodes/keyboard_codes.h" 36 #include "ui/events/keycodes/keyboard_codes.h"
36 37
37 38
38 // NavigationObserver --------------------------------------------------------- 39 // NavigationObserver ---------------------------------------------------------
39 40
40 namespace { 41 namespace {
41 42
42 // Observer that waits for navigation to complete and for the password infobar 43 // Observer that waits for navigation to complete and for the password infobar
43 // to be shown. 44 // to be shown.
44 class NavigationObserver : public content::NotificationObserver, 45 class NavigationObserver : public content::WebContentsObserver,
45 public content::WebContentsObserver { 46 public InfoBarManager::Observer {
46 public: 47 public:
47 explicit NavigationObserver(content::WebContents* web_contents) 48 explicit NavigationObserver(content::WebContents* web_contents)
48 : content::WebContentsObserver(web_contents), 49 : content::WebContentsObserver(web_contents),
49 message_loop_runner_(new content::MessageLoopRunner), 50 message_loop_runner_(new content::MessageLoopRunner),
50 infobar_shown_(false), 51 infobar_shown_(false),
51 infobar_removed_(false), 52 infobar_removed_(false),
52 should_automatically_accept_infobar_(true), 53 should_automatically_accept_infobar_(true),
53 infobar_service_(InfoBarService::FromWebContents(web_contents)) { 54 infobar_service_(InfoBarService::FromWebContents(web_contents)) {
54 registrar_.Add(this, 55 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 } 56 }
61 57
62 virtual ~NavigationObserver() {} 58 virtual ~NavigationObserver() {
59 if (infobar_service_)
60 infobar_service_->RemoveObserver(this);
61 }
63 62
64 // Normally Wait() will not return until a main frame navigation occurs. 63 // 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, 64 // 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. 65 // regardless of the frame that navigated. Useful for multi-frame pages.
67 void SetPathToWaitFor(const std::string& path) { 66 void SetPathToWaitFor(const std::string& path) {
68 wait_for_path_ = path; 67 wait_for_path_ = path;
69 } 68 }
70 69
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: 70 // content::WebContentsObserver:
93 virtual void DidFinishLoad( 71 virtual void DidFinishLoad(
94 int64 frame_id, 72 int64 frame_id,
95 const GURL& validated_url, 73 const GURL& validated_url,
96 bool is_main_frame, 74 bool is_main_frame,
97 content::RenderViewHost* render_view_host) OVERRIDE { 75 content::RenderViewHost* render_view_host) OVERRIDE {
98 if (!wait_for_path_.empty()) { 76 if (!wait_for_path_.empty()) {
99 if (validated_url.path() == wait_for_path_) 77 if (validated_url.path() == wait_for_path_)
100 message_loop_runner_->Quit(); 78 message_loop_runner_->Quit();
101 } else if (is_main_frame) { 79 } else if (is_main_frame) {
102 message_loop_runner_->Quit(); 80 message_loop_runner_->Quit();
103 } 81 }
104 } 82 }
105 83
106 bool infobar_shown() const { return infobar_shown_; } 84 bool infobar_shown() const { return infobar_shown_; }
107 bool infobar_removed() const { return infobar_removed_; } 85 bool infobar_removed() const { return infobar_removed_; }
108 86
109 void disable_should_automatically_accept_infobar() { 87 void disable_should_automatically_accept_infobar() {
110 should_automatically_accept_infobar_ = false; 88 should_automatically_accept_infobar_ = false;
111 } 89 }
112 90
113 void Wait() { 91 void Wait() {
114 message_loop_runner_->Run(); 92 message_loop_runner_->Run();
115 } 93 }
116 94
117 private: 95 private:
96 // InfoBarManager::Observer:
97 virtual void OnInfoBarAdded(InfoBar* infobar) OVERRIDE {
98 if (should_automatically_accept_infobar_) {
99 infobar_service_->infobar_at(0)
100 ->delegate()
101 ->AsConfirmInfoBarDelegate()
102 ->Accept();
Peter Kasting 2014/04/21 20:34:06 Nit: This still isn't wrapped like the old code wa
tfarina 2014/04/21 20:56:40 I managed to get this right in patch set 2, then I
103 }
104 infobar_shown_ = true;
105 }
106
107 virtual void OnInfoBarRemoved(InfoBar* infobar, bool animate) OVERRIDE {
108 infobar_removed_ = true;
109 }
110
111 virtual void OnManagerShuttingDown(InfoBarManager* manager) OVERRIDE {
112 ASSERT_EQ(infobar_service_, manager);
113 infobar_service_->RemoveObserver(this);
114 infobar_service_ = NULL;
115 }
116
118 std::string wait_for_path_; 117 std::string wait_for_path_;
119 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; 118 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
120 bool infobar_shown_; 119 bool infobar_shown_;
121 bool infobar_removed_; 120 bool infobar_removed_;
122 // If |should_automatically_accept_infobar_| is true, then whenever the test 121 // If |should_automatically_accept_infobar_| is true, then whenever the test
123 // sees an infobar added, it will click its accepting button. Default = true. 122 // sees an infobar added, it will click its accepting button. Default = true.
124 bool should_automatically_accept_infobar_; 123 bool should_automatically_accept_infobar_;
125 content::NotificationRegistrar registrar_;
126 InfoBarService* infobar_service_; 124 InfoBarService* infobar_service_;
127 125
128 DISALLOW_COPY_AND_ASSIGN(NavigationObserver); 126 DISALLOW_COPY_AND_ASSIGN(NavigationObserver);
129 }; 127 };
130 128
131 } // namespace 129 } // namespace
132 130
133 131
134 // PasswordManagerBrowserTest ------------------------------------------------- 132 // PasswordManagerBrowserTest -------------------------------------------------
135 133
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 DontPromptForPasswordFormWithDefaultValue) { 645 DontPromptForPasswordFormWithDefaultValue) {
648 NavigateToFile("/password/password_form_with_default_value.html"); 646 NavigateToFile("/password/password_form_with_default_value.html");
649 647
650 // Don't prompt if we navigate away even if there is a password value since 648 // Don't prompt if we navigate away even if there is a password value since
651 // it's not coming from the user. 649 // it's not coming from the user.
652 NavigationObserver observer(WebContents()); 650 NavigationObserver observer(WebContents());
653 NavigateToFile("/password/done.html"); 651 NavigateToFile("/password/done.html");
654 observer.Wait(); 652 observer.Wait();
655 EXPECT_FALSE(observer.infobar_shown()); 653 EXPECT_FALSE(observer.infobar_shown());
656 } 654 }
OLDNEW
« no previous file with comments | « no previous file | components/infobars/core/infobar_manager.h » ('j') | components/infobars/core/infobar_manager.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698