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