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
[kTestUsername
] = test_form();
50 GetController()->OnPasswordAutofilled(map
);
53 void ManagePasswordsTest::SetupPendingPassword() {
54 password_manager::StubPasswordManagerClient client
;
55 password_manager::StubPasswordManagerDriver driver
;
56 scoped_ptr
<password_manager::PasswordFormManager
> test_form_manager(
57 new password_manager::PasswordFormManager(
58 NULL
, &client
, driver
.AsWeakPtr(), *test_form(), false));
59 GetController()->OnPasswordSubmitted(test_form_manager
.Pass());
62 void ManagePasswordsTest::SetupAutomaticPassword() {
63 password_manager::StubPasswordManagerClient client
;
64 password_manager::StubPasswordManagerDriver driver
;
65 scoped_ptr
<password_manager::PasswordFormManager
> test_form_manager(
66 new password_manager::PasswordFormManager(
67 NULL
, &client
, driver
.AsWeakPtr(), *test_form(), false));
68 GetController()->OnAutomaticPasswordSave(test_form_manager
.Pass());
71 void ManagePasswordsTest::SetupBlackistedPassword() {
72 test_form()->blacklisted_by_user
= true;
73 test_form()->username_value
.clear();
74 autofill::PasswordFormMap map
;
75 map
[test_form()->username_value
] = test_form();
76 GetController()->OnBlacklistBlockedAutofill(map
);
79 void ManagePasswordsTest::SetupChooseCredentials(
80 ScopedVector
<autofill::PasswordForm
> local_credentials
,
81 ScopedVector
<autofill::PasswordForm
> federated_credentials
,
83 base::string16 kTestUsername
= base::ASCIIToUTF16("test_username");
84 autofill::PasswordFormMap map
;
85 map
[kTestUsername
] = test_form();
86 GetController()->OnChooseCredentials(
87 local_credentials
.Pass(), federated_credentials
.Pass(), origin
,
88 base::Bind(&ManagePasswordsTest::OnChooseCredential
, this));
91 void ManagePasswordsTest::SetupAutoSignin(
92 ScopedVector
<autofill::PasswordForm
> local_credentials
) {
93 GetController()->OnAutoSignin(local_credentials
.Pass());
96 base::HistogramSamples
* ManagePasswordsTest::GetSamples(
97 const char* histogram
) {
98 // Ensure that everything has been properly recorded before pulling samples.
99 content::RunAllPendingInMessageLoop();
100 return histogram_tester_
.GetHistogramSamplesSinceCreation(histogram
)
104 ManagePasswordsUIController
* ManagePasswordsTest::GetController() {
105 return ManagePasswordsUIController::FromWebContents(
106 browser()->tab_strip_model()->GetActiveWebContents());