[sql] Remove _HAS_EXCEPTIONS=0 from build info.
[chromium-blink-merge.git] / chrome / browser / password_manager / password_manager_test_base.cc
blob7afc1ecf52b287c7425f280dc2a0ad25346c9ef7
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/password_manager/password_manager_test_base.h"
7 #include "base/command_line.h"
8 #include "base/run_loop.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/infobars/infobar_service.h"
11 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
12 #include "chrome/browser/password_manager/password_store_factory.h"
13 #include "chrome/browser/password_manager/test_password_store_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "components/autofill/core/browser/autofill_test_utils.h"
20 #include "components/infobars/core/confirm_infobar_delegate.h"
21 #include "components/infobars/core/infobar.h"
22 #include "components/infobars/core/infobar_manager.h"
23 #include "components/password_manager/core/common/password_manager_switches.h"
24 #include "content/public/browser/render_frame_host.h"
25 #include "content/public/browser/render_view_host.h"
26 #include "content/public/test/browser_test_utils.h"
27 #include "content/public/test/test_utils.h"
28 #include "net/test/embedded_test_server/embedded_test_server.h"
30 NavigationObserver::NavigationObserver(content::WebContents* web_contents)
31 : content::WebContentsObserver(web_contents),
32 quit_on_entry_committed_(false),
33 message_loop_runner_(new content::MessageLoopRunner) {
35 NavigationObserver::~NavigationObserver() {
38 void NavigationObserver::DidFinishLoad(
39 content::RenderFrameHost* render_frame_host,
40 const GURL& validated_url) {
41 render_frame_host_ = render_frame_host;
42 if (!wait_for_path_.empty()) {
43 if (validated_url.path() == wait_for_path_)
44 message_loop_runner_->Quit();
45 } else if (!render_frame_host->GetParent()) {
46 message_loop_runner_->Quit();
50 void NavigationObserver::NavigationEntryCommitted(
51 const content::LoadCommittedDetails& load_details) {
52 if (quit_on_entry_committed_)
53 message_loop_runner_->Quit();
56 void NavigationObserver::Wait() {
57 message_loop_runner_->Run();
60 PromptObserver::PromptObserver() {
62 PromptObserver::~PromptObserver() {
65 bool PromptObserver::IsShowingUpdatePrompt() const {
66 // TODO(dvadym): Make this method pure virtual as soon as update UI is
67 // implemented for infobar. http://crbug.com/359315
68 return false;
71 void PromptObserver::Accept() const {
72 EXPECT_TRUE(IsShowingPrompt());
73 AcceptImpl();
76 void PromptObserver::AcceptUpdatePrompt(
77 const autofill::PasswordForm& form) const {
78 EXPECT_TRUE(IsShowingUpdatePrompt());
79 AcceptUpdatePromptImpl(form);
82 class InfoBarObserver : public PromptObserver,
83 public infobars::InfoBarManager::Observer {
84 public:
85 explicit InfoBarObserver(content::WebContents* web_contents)
86 : infobar_is_being_shown_(false),
87 infobar_service_(InfoBarService::FromWebContents(web_contents)) {
88 infobar_service_->AddObserver(this);
91 ~InfoBarObserver() override {
92 if (infobar_service_)
93 infobar_service_->RemoveObserver(this);
96 private:
97 // PromptObserver:
98 bool IsShowingPrompt() const override { return infobar_is_being_shown_; }
100 void AcceptImpl() const override {
101 EXPECT_EQ(1u, infobar_service_->infobar_count());
102 if (!infobar_service_->infobar_count())
103 return; // Let the test finish to gather possibly more diagnostics.
105 // ConfirmInfoBarDelegate::Accept returning true means the infobar is
106 // immediately closed. Checking the return value is preferred to testing
107 // IsShowingPrompt() here, for it avoids the delay until the closing
108 // notification is received.
109 EXPECT_TRUE(infobar_service_->infobar_at(0)
110 ->delegate()
111 ->AsConfirmInfoBarDelegate()
112 ->Accept());
115 // infobars::InfoBarManager::Observer:
116 void OnInfoBarAdded(infobars::InfoBar* infobar) override {
117 infobar_is_being_shown_ = true;
120 void OnInfoBarRemoved(infobars::InfoBar* infobar, bool animate) override {
121 infobar_is_being_shown_ = false;
124 void OnManagerShuttingDown(infobars::InfoBarManager* manager) override {
125 ASSERT_EQ(infobar_service_, manager);
126 infobar_service_->RemoveObserver(this);
127 infobar_service_ = nullptr;
130 bool infobar_is_being_shown_;
131 InfoBarService* infobar_service_;
133 DISALLOW_COPY_AND_ASSIGN(InfoBarObserver);
136 class BubbleObserver : public PromptObserver {
137 public:
138 explicit BubbleObserver(content::WebContents* web_contents)
139 : ui_controller_(
140 ManagePasswordsUIController::FromWebContents(web_contents)) {}
142 ~BubbleObserver() override {}
144 private:
145 // PromptObserver:
146 bool IsShowingPrompt() const override {
147 return ui_controller_->PasswordPendingUserDecision();
150 bool IsShowingUpdatePrompt() const override {
151 return ui_controller_->state() ==
152 password_manager::ui::PENDING_PASSWORD_UPDATE_STATE;
155 void AcceptImpl() const override {
156 ui_controller_->SavePassword();
157 EXPECT_FALSE(IsShowingPrompt());
160 void AcceptUpdatePromptImpl(
161 const autofill::PasswordForm& form) const override {
162 ui_controller_->UpdatePassword(form);
163 EXPECT_FALSE(IsShowingUpdatePrompt());
165 ManagePasswordsUIController* const ui_controller_;
167 DISALLOW_COPY_AND_ASSIGN(BubbleObserver);
170 scoped_ptr<PromptObserver> PromptObserver::Create(
171 content::WebContents* web_contents) {
172 if (ChromePasswordManagerClient::IsTheHotNewBubbleUIEnabled()) {
173 return scoped_ptr<PromptObserver>(new BubbleObserver(web_contents));
174 } else {
175 return scoped_ptr<PromptObserver>(new InfoBarObserver(web_contents));
179 PasswordManagerBrowserTestBase::PasswordManagerBrowserTestBase() {
181 PasswordManagerBrowserTestBase::~PasswordManagerBrowserTestBase() {
184 void PasswordManagerBrowserTestBase::SetUpOnMainThread() {
185 // Use TestPasswordStore to remove a possible race. Normally the
186 // PasswordStore does its database manipulation on the DB thread, which
187 // creates a possible race during navigation. Specifically the
188 // PasswordManager will ignore any forms in a page if the load from the
189 // PasswordStore has not completed.
190 PasswordStoreFactory::GetInstance()->SetTestingFactory(
191 browser()->profile(), TestPasswordStoreService::Build);
192 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
193 ASSERT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch(
194 password_manager::switches::kEnableAutomaticPasswordSaving));
197 void PasswordManagerBrowserTestBase::TearDownOnMainThread() {
198 ASSERT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
201 content::WebContents* PasswordManagerBrowserTestBase::WebContents() {
202 return browser()->tab_strip_model()->GetActiveWebContents();
205 content::RenderViewHost* PasswordManagerBrowserTestBase::RenderViewHost() {
206 return WebContents()->GetRenderViewHost();
209 void PasswordManagerBrowserTestBase::NavigateToFile(const std::string& path) {
210 NavigationObserver observer(WebContents());
211 GURL url = embedded_test_server()->GetURL(path);
212 ui_test_utils::NavigateToURL(browser(), url);
213 observer.Wait();
216 void PasswordManagerBrowserTestBase::WaitForElementValue(
217 const std::string& element_id,
218 const std::string& expected_value) {
219 WaitForElementValue("null", element_id, expected_value);
222 void PasswordManagerBrowserTestBase::WaitForElementValue(
223 const std::string& iframe_id,
224 const std::string& element_id,
225 const std::string& expected_value) {
226 enum ReturnCodes { // Possible results of the JavaScript code.
227 RETURN_CODE_OK,
228 RETURN_CODE_NO_ELEMENT,
229 RETURN_CODE_WRONG_VALUE,
230 RETURN_CODE_INVALID,
232 const std::string value_check_function = base::StringPrintf(
233 "function valueCheck() {"
234 " if (%s)"
235 " var element = document.getElementById("
236 " '%s').contentDocument.getElementById('%s');"
237 " else "
238 " var element = document.getElementById('%s');"
239 " return element && element.value == '%s';"
240 "}",
241 iframe_id.c_str(), iframe_id.c_str(), element_id.c_str(),
242 element_id.c_str(), expected_value.c_str());
243 const std::string script =
244 value_check_function +
245 base::StringPrintf(
246 "if (valueCheck()) {"
247 " /* Spin the event loop with setTimeout. */"
248 " setTimeout(window.domAutomationController.send(%d), 0);"
249 "} else {"
250 " if (%s)"
251 " var element = document.getElementById("
252 " '%s').contentDocument.getElementById('%s');"
253 " else "
254 " var element = document.getElementById('%s');"
255 " if (!element)"
256 " window.domAutomationController.send(%d);"
257 " element.onchange = function() {"
258 " if (valueCheck()) {"
259 " /* Spin the event loop with setTimeout. */"
260 " setTimeout(window.domAutomationController.send(%d), 0);"
261 " } else {"
262 " window.domAutomationController.send(%d);"
263 " }"
264 " };"
265 "}",
266 RETURN_CODE_OK, iframe_id.c_str(), iframe_id.c_str(),
267 element_id.c_str(), element_id.c_str(), RETURN_CODE_NO_ELEMENT,
268 RETURN_CODE_OK, RETURN_CODE_WRONG_VALUE);
269 int return_value = RETURN_CODE_INVALID;
270 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(RenderViewHost(), script,
271 &return_value));
272 EXPECT_EQ(RETURN_CODE_OK, return_value)
273 << "element_id = " << element_id
274 << ", expected_value = " << expected_value;
277 void PasswordManagerBrowserTestBase::CheckElementValue(
278 const std::string& element_id,
279 const std::string& expected_value) {
280 CheckElementValue("null", element_id, expected_value);
283 void PasswordManagerBrowserTestBase::CheckElementValue(
284 const std::string& iframe_id,
285 const std::string& element_id,
286 const std::string& expected_value) {
287 const std::string value_check_script = base::StringPrintf(
288 "if (%s)"
289 " var element = document.getElementById("
290 " '%s').contentDocument.getElementById('%s');"
291 "else "
292 " var element = document.getElementById('%s');"
293 "window.domAutomationController.send(element && element.value == '%s');",
294 iframe_id.c_str(), iframe_id.c_str(), element_id.c_str(),
295 element_id.c_str(), expected_value.c_str());
296 bool return_value = false;
297 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
298 RenderViewHost(), value_check_script, &return_value));
299 EXPECT_TRUE(return_value) << "element_id = " << element_id
300 << ", expected_value = " << expected_value;