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 "base/strings/utf_string_conversions.h"
6 #include "chrome/browser/password_manager/save_password_infobar_delegate.h"
7 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
8 #include "components/autofill/core/common/password_form.h"
9 #include "components/password_manager/core/browser/password_form_manager.h"
10 #include "components/password_manager/core/browser/password_manager_client.h"
11 #include "components/password_manager/core/browser/stub_password_manager_client.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
16 class MockPasswordFormManager
: public password_manager::PasswordFormManager
{
18 MOCK_METHOD0(PermanentlyBlacklist
, void());
20 MockPasswordFormManager(password_manager::StubPasswordManagerClient
* client
,
21 const autofill::PasswordForm
& form
)
22 : PasswordFormManager(
25 base::WeakPtr
<password_manager::PasswordManagerDriver
>(),
29 ~MockPasswordFormManager() override
{}
32 DISALLOW_COPY_AND_ASSIGN(MockPasswordFormManager
);
35 class TestSavePasswordInfobarDelegate
: public SavePasswordInfoBarDelegate
{
37 TestSavePasswordInfobarDelegate(
38 scoped_ptr
<password_manager::PasswordFormManager
> form_to_save
,
39 password_manager::CredentialSourceType source_type
)
40 : SavePasswordInfoBarDelegate(form_to_save
.Pass(),
43 true /* is_smartlock_branding_enabled */) {}
44 ~TestSavePasswordInfobarDelegate() override
{}
49 class SavePasswordInfoBarDelegateTest
: public ChromeRenderViewHostTestHarness
{
51 SavePasswordInfoBarDelegateTest();
52 ~SavePasswordInfoBarDelegateTest() override
{};
54 void SetUp() override
;
55 void TearDown() override
;
57 const autofill::PasswordForm
& test_form() { return test_form_
; }
58 scoped_ptr
<MockPasswordFormManager
> CreateMockFormManager();
61 scoped_ptr
<ConfirmInfoBarDelegate
> CreateDelegate(
62 scoped_ptr
<password_manager::PasswordFormManager
> password_form_manager
,
63 password_manager::CredentialSourceType type
);
65 password_manager::StubPasswordManagerClient client_
;
66 autofill::PasswordForm test_form_
;
69 DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegateTest
);
72 SavePasswordInfoBarDelegateTest::SavePasswordInfoBarDelegateTest() {
73 test_form_
.origin
= GURL("http://example.com");
74 test_form_
.username_value
= base::ASCIIToUTF16("username");
75 test_form_
.password_value
= base::ASCIIToUTF16("12345");
78 scoped_ptr
<MockPasswordFormManager
>
79 SavePasswordInfoBarDelegateTest::CreateMockFormManager() {
80 return scoped_ptr
<MockPasswordFormManager
>(
81 new MockPasswordFormManager(&client_
, test_form()));
84 scoped_ptr
<ConfirmInfoBarDelegate
>
85 SavePasswordInfoBarDelegateTest::CreateDelegate(
86 scoped_ptr
<password_manager::PasswordFormManager
> password_form_manager
,
87 password_manager::CredentialSourceType type
) {
88 scoped_ptr
<ConfirmInfoBarDelegate
> delegate(
89 new TestSavePasswordInfobarDelegate(password_form_manager
.Pass(), type
));
90 return delegate
.Pass();
93 void SavePasswordInfoBarDelegateTest::SetUp() {
94 ChromeRenderViewHostTestHarness::SetUp();
97 void SavePasswordInfoBarDelegateTest::TearDown() {
98 ChromeRenderViewHostTestHarness::TearDown();
101 TEST_F(SavePasswordInfoBarDelegateTest
, CancelTestCredentialSourceAPI
) {
102 scoped_ptr
<MockPasswordFormManager
> password_form_manager(
103 CreateMockFormManager());
104 EXPECT_CALL(*password_form_manager
.get(), PermanentlyBlacklist())
105 .Times(testing::Exactly(0));
106 scoped_ptr
<ConfirmInfoBarDelegate
> infobar(CreateDelegate(
107 password_form_manager
.Pass(),
108 password_manager::CredentialSourceType::CREDENTIAL_SOURCE_API
));
109 EXPECT_TRUE(infobar
->Cancel());
112 TEST_F(SavePasswordInfoBarDelegateTest
,
113 CancelTestCredentialSourcePasswordManager
) {
114 scoped_ptr
<MockPasswordFormManager
> password_form_manager(
115 CreateMockFormManager());
116 EXPECT_CALL(*password_form_manager
.get(), PermanentlyBlacklist())
117 .Times(testing::Exactly(1));
118 scoped_ptr
<ConfirmInfoBarDelegate
> infobar(CreateDelegate(
119 password_form_manager
.Pass(), password_manager::CredentialSourceType::
120 CREDENTIAL_SOURCE_PASSWORD_MANAGER
));
121 EXPECT_TRUE(infobar
->Cancel());