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 ~TestSavePasswordInfobarDelegate() override
{}
48 class SavePasswordInfoBarDelegateTest
: public ChromeRenderViewHostTestHarness
{
50 SavePasswordInfoBarDelegateTest();
51 ~SavePasswordInfoBarDelegateTest() override
{};
53 void SetUp() override
;
54 void TearDown() override
;
56 const autofill::PasswordForm
& test_form() { return test_form_
; }
57 MockPasswordFormManager
* CreateMockFormManager();
60 scoped_ptr
<ConfirmInfoBarDelegate
> CreateDelegate(
61 MockPasswordFormManager
* password_form_manager
,
62 password_manager::CredentialSourceType type
);
64 password_manager::StubPasswordManagerClient client_
;
65 autofill::PasswordForm test_form_
;
68 DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegateTest
);
71 SavePasswordInfoBarDelegateTest::SavePasswordInfoBarDelegateTest() {
72 test_form_
.origin
= GURL("http://example.com");
73 test_form_
.username_value
= base::ASCIIToUTF16("username");
74 test_form_
.password_value
= base::ASCIIToUTF16("12345");
77 MockPasswordFormManager
*
78 SavePasswordInfoBarDelegateTest::CreateMockFormManager() {
79 return new MockPasswordFormManager(&client_
, test_form());
82 scoped_ptr
<ConfirmInfoBarDelegate
>
83 SavePasswordInfoBarDelegateTest::CreateDelegate(
84 MockPasswordFormManager
* password_form_manager
,
85 password_manager::CredentialSourceType type
) {
86 scoped_ptr
<ConfirmInfoBarDelegate
> delegate(
87 new TestSavePasswordInfobarDelegate(
88 scoped_ptr
<password_manager::PasswordFormManager
>(
89 password_form_manager
),
91 return delegate
.Pass();
94 void SavePasswordInfoBarDelegateTest::SetUp() {
95 ChromeRenderViewHostTestHarness::SetUp();
98 void SavePasswordInfoBarDelegateTest::TearDown() {
99 ChromeRenderViewHostTestHarness::TearDown();
102 TEST_F(SavePasswordInfoBarDelegateTest
, CancelTestCredentialSourceAPI
) {
103 // SavePasswordInfoBarDelegate::Create takes ownership of
104 // password_form_manager_ptr;
105 MockPasswordFormManager
* password_form_manager_ptr
= CreateMockFormManager();
106 scoped_ptr
<ConfirmInfoBarDelegate
> infobar(CreateDelegate(
107 password_form_manager_ptr
,
108 password_manager::CredentialSourceType::CREDENTIAL_SOURCE_API
));
109 EXPECT_CALL(*password_form_manager_ptr
, PermanentlyBlacklist())
110 .Times(testing::Exactly(0));
111 EXPECT_TRUE(infobar
->Cancel());
114 TEST_F(SavePasswordInfoBarDelegateTest
,
115 CancelTestCredentialSourcePasswordManager
) {
116 // SavePasswordInfoBarDelegate::Create takes ownership of
117 // password_form_manager_ptr;
118 MockPasswordFormManager
* password_form_manager_ptr
= CreateMockFormManager();
119 scoped_ptr
<ConfirmInfoBarDelegate
> infobar(CreateDelegate(
120 password_form_manager_ptr
, password_manager::CredentialSourceType::
121 CREDENTIAL_SOURCE_PASSWORD_MANAGER
));
122 EXPECT_CALL(*password_form_manager_ptr
, PermanentlyBlacklist())
123 .Times(testing::Exactly(1));
124 EXPECT_TRUE(infobar
->Cancel());