1 // Copyright (c) 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 "chrome/browser/browsing_data/passwords_counter.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/password_manager/password_store_factory.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/common/pref_names.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "components/autofill/core/common/password_form.h"
18 using autofill::PasswordForm
;
20 class PasswordsCounterTest
: public InProcessBrowserTest
{
22 void AddLogin(const std::string
& origin
,
23 const std::string
& username
,
25 PasswordStoreFactory::GetForProfile(
26 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS
)->
27 AddLogin(CreateCredentials(origin
, username
, blacklisted
));
28 base::RunLoop().RunUntilIdle();
31 void RemoveLogin(const std::string
& origin
,
32 const std::string
& username
,
34 PasswordStoreFactory::GetForProfile(
35 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS
)->
36 RemoveLogin(CreateCredentials(origin
, username
, blacklisted
));
37 base::RunLoop().RunUntilIdle();
40 void SetPasswordsDeletionPref(bool value
) {
41 browser()->profile()->GetPrefs()->SetBoolean(
42 prefs::kDeletePasswords
, value
);
45 void WaitForCounting() {
46 run_loop_
.reset(new base::RunLoop());
55 void Callback(bool finished
, uint32 count
) {
58 if (run_loop_
&& finished
)
63 PasswordForm
CreateCredentials(const std::string
& origin
,
64 const std::string
& username
,
67 result
.signon_realm
= origin
;
68 result
.origin
= GURL(origin
);
69 result
.username_value
= base::ASCIIToUTF16(username
);
70 result
.password_value
= base::ASCIIToUTF16("hunter2");
71 result
.blacklisted_by_user
= blacklisted
;
75 scoped_ptr
<base::RunLoop
> run_loop_
;
81 // Tests that the counter correctly counts each individual credential on
83 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest
, SameDomain
) {
84 SetPasswordsDeletionPref(true);
85 AddLogin("https://www.google.com", "user1", false);
86 AddLogin("https://www.google.com", "user2", false);
87 AddLogin("https://www.google.com", "user3", false);
88 AddLogin("https://www.chrome.com", "user1", false);
89 AddLogin("https://www.chrome.com", "user2", false);
91 PasswordsCounter counter
;
92 counter
.Init(browser()->profile(),
93 base::Bind(&PasswordsCounterTest::Callback
,
94 base::Unretained(this)));
97 EXPECT_EQ(5u, GetResult());
100 // Tests that the counter doesn't count blacklisted entries.
101 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest
, Blacklisted
) {
102 SetPasswordsDeletionPref(true);
103 AddLogin("https://www.google.com", "user1", false);
104 AddLogin("https://www.google.com", "user2", true);
105 AddLogin("https://www.chrome.com", "user3", true);
107 PasswordsCounter counter
;
108 counter
.Init(browser()->profile(),
109 base::Bind(&PasswordsCounterTest::Callback
,
110 base::Unretained(this)));
113 EXPECT_EQ(1u, GetResult());
116 // Tests that the counter starts counting automatically when the deletion
117 // pref changes to true.
118 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest
, PrefChanged
) {
119 SetPasswordsDeletionPref(false);
120 AddLogin("https://www.google.com", "user", false);
121 AddLogin("https://www.chrome.com", "user", false);
123 PasswordsCounter counter
;
124 counter
.Init(browser()->profile(),
125 base::Bind(&PasswordsCounterTest::Callback
,
126 base::Unretained(this)));
127 SetPasswordsDeletionPref(true);
130 EXPECT_EQ(2u, GetResult());
133 // Tests that the counter does not count passwords if the deletion
134 // preference is false.
135 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest
, PrefIsFalse
) {
136 SetPasswordsDeletionPref(false);
137 AddLogin("https://www.google.com", "user", false);
139 PasswordsCounter counter
;
140 counter
.Init(browser()->profile(),
141 base::Bind(&PasswordsCounterTest::Callback
,
142 base::Unretained(this)));
144 EXPECT_FALSE(counter
.cancelable_task_tracker()->HasTrackedTasks());
147 // Tests that the counter starts counting automatically when
148 // the password store changes.
149 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest
, StoreChanged
) {
150 SetPasswordsDeletionPref(true);
151 AddLogin("https://www.google.com", "user", false);
153 PasswordsCounter counter
;
154 counter
.Init(browser()->profile(),
155 base::Bind(&PasswordsCounterTest::Callback
,
156 base::Unretained(this)));
159 EXPECT_EQ(1u, GetResult());
161 AddLogin("https://www.chrome.com", "user", false);
163 EXPECT_EQ(2u, GetResult());
165 RemoveLogin("https://www.chrome.com", "user", false);
167 EXPECT_EQ(1u, GetResult());