1 // Copyright 2014 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/ui/passwords/manage_passwords_test.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_command_controller.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/interactive_test_utils.h"
17 #include "components/autofill/core/common/password_form.h"
18 #include "components/password_manager/core/browser/password_form_manager.h"
19 #include "components/password_manager/core/browser/password_manager_metrics_util.h"
20 #include "components/password_manager/core/browser/stub_password_manager_client.h"
21 #include "components/password_manager/core/browser/stub_password_manager_driver.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 ManagePasswordsTest::ManagePasswordsTest() {
27 ManagePasswordsTest::~ManagePasswordsTest() {
30 void ManagePasswordsTest::SetUpOnMainThread() {
31 AddTabAtIndex(0, GURL("http://example.com/"), ui::PAGE_TRANSITION_TYPED
);
34 void ManagePasswordsTest::ExecuteManagePasswordsCommand() {
35 // Show the window to ensure that it's active.
36 browser()->window()->Show();
38 CommandUpdater
* updater
= browser()->command_controller()->command_updater();
39 EXPECT_TRUE(updater
->IsCommandEnabled(IDC_MANAGE_PASSWORDS_FOR_PAGE
));
40 EXPECT_TRUE(updater
->ExecuteCommand(IDC_MANAGE_PASSWORDS_FOR_PAGE
));
42 // Wait for the command execution to pop up the bubble.
43 content::RunAllPendingInMessageLoop();
46 void ManagePasswordsTest::SetupManagingPasswords() {
47 base::string16 kTestUsername
= base::ASCIIToUTF16("test_username");
48 autofill::PasswordFormMap map
;
49 map
.insert(kTestUsername
,
50 make_scoped_ptr(new autofill::PasswordForm(*test_form())));
51 GetController()->OnPasswordAutofilled(map
);
54 void ManagePasswordsTest::SetupPendingPassword() {
55 password_manager::StubPasswordManagerClient client
;
56 password_manager::StubPasswordManagerDriver driver
;
57 scoped_ptr
<password_manager::PasswordFormManager
> test_form_manager(
58 new password_manager::PasswordFormManager(
59 NULL
, &client
, driver
.AsWeakPtr(), *test_form(), false));
60 test_form_manager
->SimulateFetchMatchingLoginsFromPasswordStore();
61 ScopedVector
<autofill::PasswordForm
> best_matches
;
62 test_form_manager
->OnGetPasswordStoreResults(best_matches
.Pass());
63 GetController()->OnPasswordSubmitted(test_form_manager
.Pass());
66 void ManagePasswordsTest::SetupAutomaticPassword() {
67 password_manager::StubPasswordManagerClient client
;
68 password_manager::StubPasswordManagerDriver driver
;
69 scoped_ptr
<password_manager::PasswordFormManager
> test_form_manager(
70 new password_manager::PasswordFormManager(
71 NULL
, &client
, driver
.AsWeakPtr(), *test_form(), false));
72 GetController()->OnAutomaticPasswordSave(test_form_manager
.Pass());
75 void ManagePasswordsTest::SetupBlackistedPassword() {
76 test_form()->blacklisted_by_user
= true;
77 test_form()->username_value
.clear();
78 autofill::PasswordFormMap map
;
79 map
.insert(test_form()->username_value
,
80 make_scoped_ptr(new autofill::PasswordForm(*test_form())));
81 GetController()->OnBlacklistBlockedAutofill(map
);
84 void ManagePasswordsTest::SetupChooseCredentials(
85 ScopedVector
<autofill::PasswordForm
> local_credentials
,
86 ScopedVector
<autofill::PasswordForm
> federated_credentials
,
88 base::string16 kTestUsername
= base::ASCIIToUTF16("test_username");
89 autofill::PasswordFormMap map
;
90 map
.insert(kTestUsername
,
91 make_scoped_ptr(new autofill::PasswordForm(*test_form())));
92 GetController()->OnChooseCredentials(
93 local_credentials
.Pass(), federated_credentials
.Pass(), origin
,
94 base::Bind(&ManagePasswordsTest::OnChooseCredential
, this));
97 void ManagePasswordsTest::SetupAutoSignin(
98 ScopedVector
<autofill::PasswordForm
> local_credentials
) {
99 GetController()->OnAutoSignin(local_credentials
.Pass());
102 scoped_ptr
<base::HistogramSamples
> ManagePasswordsTest::GetSamples(
103 const char* histogram
) {
104 // Ensure that everything has been properly recorded before pulling samples.
105 content::RunAllPendingInMessageLoop();
106 return histogram_tester_
.GetHistogramSamplesSinceCreation(histogram
).Pass();
109 ManagePasswordsUIController
* ManagePasswordsTest::GetController() {
110 return ManagePasswordsUIController::FromWebContents(
111 browser()->tab_strip_model()->GetActiveWebContents());