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/sync/test/integration/passwords_helper.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "components/autofill/core/common/password_form.h"
19 using autofill::PasswordForm
;
21 class PasswordsCounterTest
: public InProcessBrowserTest
,
22 public password_manager::PasswordStore::Observer
{
24 void SetUpOnMainThread() override
{
25 time_
= base::Time::Now();
26 store_
= PasswordStoreFactory::GetForProfile(
27 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS
);
28 SetPasswordsDeletionPref(true);
29 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING
);
32 void AddLogin(const std::string
& origin
,
33 const std::string
& username
,
35 // Add login and wait until the password store actually changes.
36 // on the database thread.
37 passwords_helper::AddLogin(
38 store_
.get(), CreateCredentials(origin
, username
, blacklisted
));
39 base::RunLoop().RunUntilIdle();
40 // Even after the store changes on the database thread, we must wait until
41 // the listeners are notified on this thread.
42 run_loop_
.reset(new base::RunLoop());
43 run_loop_
->RunUntilIdle();
46 void RemoveLogin(const std::string
& origin
,
47 const std::string
& username
,
49 // Remove login and wait until the password store actually changes
50 // on the database thread.
51 passwords_helper::RemoveLogin(
52 store_
.get(), CreateCredentials(origin
, username
, blacklisted
));
53 // Even after the store changes on the database thread, we must wait until
54 // the listeners are notified on this thread.
55 run_loop_
.reset(new base::RunLoop());
56 run_loop_
->RunUntilIdle();
60 const password_manager::PasswordStoreChangeList
& changes
) override
{
64 void SetPasswordsDeletionPref(bool value
) {
65 browser()->profile()->GetPrefs()->SetBoolean(
66 prefs::kDeletePasswords
, value
);
69 void SetDeletionPeriodPref(BrowsingDataRemover::TimePeriod period
) {
70 browser()->profile()->GetPrefs()->SetInteger(
71 prefs::kDeleteTimePeriod
, static_cast<int>(period
));
74 void RevertTimeInDays(int days
) {
75 time_
-= base::TimeDelta::FromDays(days
);
78 void WaitForCounting() {
81 run_loop_
.reset(new base::RunLoop());
90 void Callback(bool finished
, uint32 count
) {
93 if (run_loop_
&& finished
)
98 PasswordForm
CreateCredentials(const std::string
& origin
,
99 const std::string
& username
,
102 result
.signon_realm
= origin
;
103 result
.origin
= GURL(origin
);
104 result
.username_value
= base::ASCIIToUTF16(username
);
105 result
.password_value
= base::ASCIIToUTF16("hunter2");
106 result
.blacklisted_by_user
= blacklisted
;
107 result
.date_created
= time_
;
111 scoped_refptr
<password_manager::PasswordStore
> store_
;
113 scoped_ptr
<base::RunLoop
> run_loop_
;
120 // Tests that the counter correctly counts each individual credential on
122 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest
, SameDomain
) {
123 AddLogin("https://www.google.com", "user1", false);
124 AddLogin("https://www.google.com", "user2", false);
125 AddLogin("https://www.google.com", "user3", false);
126 AddLogin("https://www.chrome.com", "user1", false);
127 AddLogin("https://www.chrome.com", "user2", false);
129 PasswordsCounter counter
;
130 counter
.Init(browser()->profile(),
131 base::Bind(&PasswordsCounterTest::Callback
,
132 base::Unretained(this)));
135 EXPECT_EQ(5u, GetResult());
138 // Tests that the counter doesn't count blacklisted entries.
139 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest
, Blacklisted
) {
140 AddLogin("https://www.google.com", "user1", false);
141 AddLogin("https://www.google.com", "user2", true);
142 AddLogin("https://www.chrome.com", "user3", true);
144 PasswordsCounter counter
;
145 counter
.Init(browser()->profile(),
146 base::Bind(&PasswordsCounterTest::Callback
,
147 base::Unretained(this)));
150 EXPECT_EQ(1u, GetResult());
153 // Tests that the counter starts counting automatically when the deletion
154 // pref changes to true.
155 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest
, PrefChanged
) {
156 SetPasswordsDeletionPref(false);
157 AddLogin("https://www.google.com", "user", false);
158 AddLogin("https://www.chrome.com", "user", false);
160 PasswordsCounter counter
;
161 counter
.Init(browser()->profile(),
162 base::Bind(&PasswordsCounterTest::Callback
,
163 base::Unretained(this)));
164 SetPasswordsDeletionPref(true);
167 EXPECT_EQ(2u, GetResult());
170 // Tests that the counter does not count passwords if the deletion
171 // preference is false.
172 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest
, PrefIsFalse
) {
173 SetPasswordsDeletionPref(false);
174 AddLogin("https://www.google.com", "user", false);
176 PasswordsCounter counter
;
177 counter
.Init(browser()->profile(),
178 base::Bind(&PasswordsCounterTest::Callback
,
179 base::Unretained(this)));
181 EXPECT_FALSE(counter
.cancelable_task_tracker()->HasTrackedTasks());
184 // Tests that the counter starts counting automatically when
185 // the password store changes.
186 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest
, StoreChanged
) {
187 AddLogin("https://www.google.com", "user", false);
189 PasswordsCounter counter
;
190 counter
.Init(browser()->profile(),
191 base::Bind(&PasswordsCounterTest::Callback
,
192 base::Unretained(this)));
195 EXPECT_EQ(1u, GetResult());
197 AddLogin("https://www.chrome.com", "user", false);
199 EXPECT_EQ(2u, GetResult());
201 RemoveLogin("https://www.chrome.com", "user", false);
203 EXPECT_EQ(1u, GetResult());
206 // Tests that changing the deletion period restarts the counting, and that
207 // the result takes login creation dates into account.
208 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest
, PeriodChanged
) {
209 AddLogin("https://www.google.com", "user", false);
211 AddLogin("https://example.com", "user1", false);
213 AddLogin("https://example.com", "user2", false);
214 RevertTimeInDays(30);
215 AddLogin("https://www.chrome.com", "user", false);
217 PasswordsCounter counter
;
218 counter
.Init(browser()->profile(),
219 base::Bind(&PasswordsCounterTest::Callback
,
220 base::Unretained(this)));
222 SetDeletionPeriodPref(BrowsingDataRemover::LAST_HOUR
);
224 EXPECT_EQ(1u, GetResult());
226 SetDeletionPeriodPref(BrowsingDataRemover::LAST_DAY
);
228 EXPECT_EQ(1u, GetResult());
230 SetDeletionPeriodPref(BrowsingDataRemover::LAST_WEEK
);
232 EXPECT_EQ(3u, GetResult());
234 SetDeletionPeriodPref(BrowsingDataRemover::FOUR_WEEKS
);
236 EXPECT_EQ(3u, GetResult());
238 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING
);
240 EXPECT_EQ(4u, GetResult());