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 "content/public/browser/web_contents.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 class MockPasswordFormManager
: public password_manager::PasswordFormManager
{
19 MOCK_METHOD0(PermanentlyBlacklist
, void());
21 MockPasswordFormManager(password_manager::StubPasswordManagerClient
* client
,
22 const autofill::PasswordForm
& form
)
23 : PasswordFormManager(
26 base::WeakPtr
<password_manager::PasswordManagerDriver
>(),
30 ~MockPasswordFormManager() override
{}
33 DISALLOW_COPY_AND_ASSIGN(MockPasswordFormManager
);
36 class TestSavePasswordInfobarDelegate
: public SavePasswordInfoBarDelegate
{
38 TestSavePasswordInfobarDelegate(
39 content::WebContents
* web_contents
,
40 scoped_ptr
<password_manager::PasswordFormManager
> form_to_save
,
41 password_manager::CredentialSourceType source_type
)
42 : SavePasswordInfoBarDelegate(web_contents
,
46 true /* is_smartlock_branding_enabled */) {}
47 ~TestSavePasswordInfobarDelegate() override
{}
52 class SavePasswordInfoBarDelegateTest
: public ChromeRenderViewHostTestHarness
{
54 SavePasswordInfoBarDelegateTest();
55 ~SavePasswordInfoBarDelegateTest() override
{};
57 void SetUp() override
;
58 void TearDown() override
;
60 const autofill::PasswordForm
& test_form() { return test_form_
; }
61 scoped_ptr
<MockPasswordFormManager
> CreateMockFormManager();
64 scoped_ptr
<ConfirmInfoBarDelegate
> CreateDelegate(
65 scoped_ptr
<password_manager::PasswordFormManager
> password_form_manager
,
66 password_manager::CredentialSourceType type
);
68 password_manager::StubPasswordManagerClient client_
;
69 autofill::PasswordForm test_form_
;
72 DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegateTest
);
75 SavePasswordInfoBarDelegateTest::SavePasswordInfoBarDelegateTest() {
76 test_form_
.origin
= GURL("http://example.com");
77 test_form_
.username_value
= base::ASCIIToUTF16("username");
78 test_form_
.password_value
= base::ASCIIToUTF16("12345");
81 scoped_ptr
<MockPasswordFormManager
>
82 SavePasswordInfoBarDelegateTest::CreateMockFormManager() {
83 return scoped_ptr
<MockPasswordFormManager
>(
84 new MockPasswordFormManager(&client_
, test_form()));
87 scoped_ptr
<ConfirmInfoBarDelegate
>
88 SavePasswordInfoBarDelegateTest::CreateDelegate(
89 scoped_ptr
<password_manager::PasswordFormManager
> password_form_manager
,
90 password_manager::CredentialSourceType type
) {
91 scoped_ptr
<ConfirmInfoBarDelegate
> delegate(
92 new TestSavePasswordInfobarDelegate(web_contents(),
93 password_form_manager
.Pass(), type
));
94 return delegate
.Pass();
97 void SavePasswordInfoBarDelegateTest::SetUp() {
98 ChromeRenderViewHostTestHarness::SetUp();
101 void SavePasswordInfoBarDelegateTest::TearDown() {
102 ChromeRenderViewHostTestHarness::TearDown();
105 TEST_F(SavePasswordInfoBarDelegateTest
, CancelTestCredentialSourceAPI
) {
106 scoped_ptr
<MockPasswordFormManager
> password_form_manager(
107 CreateMockFormManager());
108 EXPECT_CALL(*password_form_manager
.get(), PermanentlyBlacklist())
109 .Times(testing::Exactly(0));
110 scoped_ptr
<ConfirmInfoBarDelegate
> infobar(CreateDelegate(
111 password_form_manager
.Pass(),
112 password_manager::CredentialSourceType::CREDENTIAL_SOURCE_API
));
113 EXPECT_TRUE(infobar
->Cancel());
116 TEST_F(SavePasswordInfoBarDelegateTest
,
117 CancelTestCredentialSourcePasswordManager
) {
118 scoped_ptr
<MockPasswordFormManager
> password_form_manager(
119 CreateMockFormManager());
120 EXPECT_CALL(*password_form_manager
.get(), PermanentlyBlacklist())
121 .Times(testing::Exactly(1));
122 scoped_ptr
<ConfirmInfoBarDelegate
> infobar(CreateDelegate(
123 password_form_manager
.Pass(), password_manager::CredentialSourceType::
124 CREDENTIAL_SOURCE_PASSWORD_MANAGER
));
125 EXPECT_TRUE(infobar
->Cancel());