Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / password_manager / save_password_infobar_delegate_unittest.cc
blobeb994fa52f31dcf28e3ec47ca0080b32e7a74df7
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/prefs/pref_service.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/password_manager/save_password_infobar_delegate.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
10 #include "components/autofill/core/common/password_form.h"
11 #include "components/password_manager/core/browser/password_form_manager.h"
12 #include "components/password_manager/core/browser/password_manager_client.h"
13 #include "components/password_manager/core/browser/stub_password_manager_client.h"
14 #include "components/password_manager/core/common/password_manager_pref_names.h"
15 #include "content/public/browser/web_contents.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace {
21 class MockPasswordFormManager : public password_manager::PasswordFormManager {
22 public:
23 MOCK_METHOD0(PermanentlyBlacklist, void());
25 MockPasswordFormManager(password_manager::StubPasswordManagerClient* client,
26 const autofill::PasswordForm& form)
27 : PasswordFormManager(
28 nullptr,
29 client,
30 base::WeakPtr<password_manager::PasswordManagerDriver>(),
31 form,
32 false) {}
34 ~MockPasswordFormManager() override {}
36 private:
37 DISALLOW_COPY_AND_ASSIGN(MockPasswordFormManager);
40 class TestSavePasswordInfobarDelegate : public SavePasswordInfoBarDelegate {
41 public:
42 TestSavePasswordInfobarDelegate(
43 content::WebContents* web_contents,
44 scoped_ptr<password_manager::PasswordFormManager> form_to_save,
45 password_manager::CredentialSourceType source_type,
46 bool should_show_first_run_experience)
47 : SavePasswordInfoBarDelegate(web_contents,
48 form_to_save.Pass(),
49 std::string(),
50 source_type,
51 true /* is_smartlock_branding_enabled */,
52 should_show_first_run_experience) {}
54 ~TestSavePasswordInfobarDelegate() override {}
57 } // namespace
59 class SavePasswordInfoBarDelegateTest : public ChromeRenderViewHostTestHarness {
60 public:
61 SavePasswordInfoBarDelegateTest();
62 ~SavePasswordInfoBarDelegateTest() override{};
64 void SetUp() override;
65 void TearDown() override;
67 PrefService* prefs();
68 const autofill::PasswordForm& test_form() { return test_form_; }
69 scoped_ptr<MockPasswordFormManager> CreateMockFormManager();
71 protected:
72 scoped_ptr<ConfirmInfoBarDelegate> CreateDelegate(
73 scoped_ptr<password_manager::PasswordFormManager> password_form_manager,
74 password_manager::CredentialSourceType type,
75 bool should_show_first_run_experience);
77 password_manager::StubPasswordManagerClient client_;
78 autofill::PasswordForm test_form_;
80 private:
81 DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegateTest);
84 SavePasswordInfoBarDelegateTest::SavePasswordInfoBarDelegateTest() {
85 test_form_.origin = GURL("http://example.com");
86 test_form_.username_value = base::ASCIIToUTF16("username");
87 test_form_.password_value = base::ASCIIToUTF16("12345");
90 PrefService* SavePasswordInfoBarDelegateTest::prefs() {
91 Profile* profile =
92 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
93 return profile->GetPrefs();
96 scoped_ptr<MockPasswordFormManager>
97 SavePasswordInfoBarDelegateTest::CreateMockFormManager() {
98 return scoped_ptr<MockPasswordFormManager>(
99 new MockPasswordFormManager(&client_, test_form()));
102 scoped_ptr<ConfirmInfoBarDelegate>
103 SavePasswordInfoBarDelegateTest::CreateDelegate(
104 scoped_ptr<password_manager::PasswordFormManager> password_form_manager,
105 password_manager::CredentialSourceType type,
106 bool should_show_first_run_experience) {
107 scoped_ptr<ConfirmInfoBarDelegate> delegate(
108 new TestSavePasswordInfobarDelegate(web_contents(),
109 password_form_manager.Pass(), type,
110 should_show_first_run_experience));
111 return delegate.Pass();
114 void SavePasswordInfoBarDelegateTest::SetUp() {
115 ChromeRenderViewHostTestHarness::SetUp();
118 void SavePasswordInfoBarDelegateTest::TearDown() {
119 ChromeRenderViewHostTestHarness::TearDown();
122 TEST_F(SavePasswordInfoBarDelegateTest, CancelTestCredentialSourceAPI) {
123 scoped_ptr<MockPasswordFormManager> password_form_manager(
124 CreateMockFormManager());
125 EXPECT_CALL(*password_form_manager.get(), PermanentlyBlacklist())
126 .Times(testing::Exactly(0));
127 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
128 password_form_manager.Pass(),
129 password_manager::CredentialSourceType::CREDENTIAL_SOURCE_API, false));
130 EXPECT_TRUE(infobar->Cancel());
133 TEST_F(SavePasswordInfoBarDelegateTest,
134 CancelTestCredentialSourcePasswordManager) {
135 scoped_ptr<MockPasswordFormManager> password_form_manager(
136 CreateMockFormManager());
137 EXPECT_CALL(*password_form_manager.get(), PermanentlyBlacklist())
138 .Times(testing::Exactly(1));
139 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
140 password_form_manager.Pass(), password_manager::CredentialSourceType::
141 CREDENTIAL_SOURCE_PASSWORD_MANAGER,
142 false));
143 EXPECT_TRUE(infobar->Cancel());
146 TEST_F(SavePasswordInfoBarDelegateTest,
147 CheckResetOfPrefAfterFirstRunMessageWasShown) {
148 using password_manager::CredentialSourceType;
149 prefs()->SetBoolean(
150 password_manager::prefs::kWasSavePrompFirstRunExperienceShown, false);
151 scoped_ptr<MockPasswordFormManager> password_form_manager(
152 CreateMockFormManager());
153 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
154 password_form_manager.Pass(), password_manager::CredentialSourceType::
155 CREDENTIAL_SOURCE_PASSWORD_MANAGER,
156 true));
157 EXPECT_TRUE(infobar->Cancel());
158 infobar.reset();
159 EXPECT_TRUE(prefs()->GetBoolean(
160 password_manager::prefs::kWasSavePrompFirstRunExperienceShown));
163 TEST_F(SavePasswordInfoBarDelegateTest,
164 CheckNoStateOfPrefChangeWhenNoFirstRunExperienceShown) {
165 using password_manager::CredentialSourceType;
166 prefs()->SetBoolean(
167 password_manager::prefs::kWasSavePrompFirstRunExperienceShown, false);
168 scoped_ptr<MockPasswordFormManager> password_form_manager(
169 CreateMockFormManager());
170 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
171 password_form_manager.Pass(), password_manager::CredentialSourceType::
172 CREDENTIAL_SOURCE_PASSWORD_MANAGER,
173 false));
174 EXPECT_TRUE(infobar->Cancel());
175 infobar.reset();
176 EXPECT_FALSE(prefs()->GetBoolean(
177 password_manager::prefs::kWasSavePrompFirstRunExperienceShown));