Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / chrome / browser / browsing_data / passwords_counter_browsertest.cc
blob99f1546032880a095da0392381fd3234f26793f7
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"
17 namespace {
19 using autofill::PasswordForm;
21 class PasswordsCounterTest : public InProcessBrowserTest,
22 public password_manager::PasswordStore::Observer {
23 public:
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,
34 bool blacklisted) {
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,
48 bool blacklisted) {
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();
59 void OnLoginsChanged(
60 const password_manager::PasswordStoreChangeList& changes) override {
61 run_loop_->Quit();
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() {
79 if (finished_)
80 return;
81 run_loop_.reset(new base::RunLoop());
82 run_loop_->Run();
85 uint32 GetResult() {
86 DCHECK(finished_);
87 return result_;
90 void Callback(bool finished, uint32 count) {
91 finished_ = finished;
92 result_ = count;
93 if (run_loop_ && finished)
94 run_loop_->Quit();
97 private:
98 PasswordForm CreateCredentials(const std::string& origin,
99 const std::string& username,
100 bool blacklisted) {
101 PasswordForm result;
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_;
108 return result;
111 scoped_refptr<password_manager::PasswordStore> store_;
113 scoped_ptr<base::RunLoop> run_loop_;
114 base::Time time_;
116 bool finished_;
117 uint32 result_;
120 // Tests that the counter correctly counts each individual credential on
121 // the same domain.
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)));
133 counter.Restart();
135 WaitForCounting();
136 EXPECT_EQ(5u, GetResult());
139 // Tests that the counter doesn't count blacklisted entries.
140 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, Blacklisted) {
141 AddLogin("https://www.google.com", "user1", false);
142 AddLogin("https://www.google.com", "user2", true);
143 AddLogin("https://www.chrome.com", "user3", true);
145 PasswordsCounter counter;
146 counter.Init(browser()->profile(),
147 base::Bind(&PasswordsCounterTest::Callback,
148 base::Unretained(this)));
149 counter.Restart();
151 WaitForCounting();
152 EXPECT_EQ(1u, GetResult());
155 // Tests that the counter starts counting automatically when the deletion
156 // pref changes to true.
157 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefChanged) {
158 SetPasswordsDeletionPref(false);
159 AddLogin("https://www.google.com", "user", false);
160 AddLogin("https://www.chrome.com", "user", false);
162 PasswordsCounter counter;
163 counter.Init(browser()->profile(),
164 base::Bind(&PasswordsCounterTest::Callback,
165 base::Unretained(this)));
166 SetPasswordsDeletionPref(true);
168 WaitForCounting();
169 EXPECT_EQ(2u, GetResult());
172 // Tests that the counter does not count passwords if the deletion
173 // preference is false.
174 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefIsFalse) {
175 SetPasswordsDeletionPref(false);
176 AddLogin("https://www.google.com", "user", false);
178 PasswordsCounter counter;
179 counter.Init(browser()->profile(),
180 base::Bind(&PasswordsCounterTest::Callback,
181 base::Unretained(this)));
182 counter.Restart();
184 EXPECT_FALSE(counter.cancelable_task_tracker()->HasTrackedTasks());
187 // Tests that the counter starts counting automatically when
188 // the password store changes.
189 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, StoreChanged) {
190 AddLogin("https://www.google.com", "user", false);
192 PasswordsCounter counter;
193 counter.Init(browser()->profile(),
194 base::Bind(&PasswordsCounterTest::Callback,
195 base::Unretained(this)));
196 counter.Restart();
198 WaitForCounting();
199 EXPECT_EQ(1u, GetResult());
201 AddLogin("https://www.chrome.com", "user", false);
202 WaitForCounting();
203 EXPECT_EQ(2u, GetResult());
205 RemoveLogin("https://www.chrome.com", "user", false);
206 WaitForCounting();
207 EXPECT_EQ(1u, GetResult());
210 // Tests that changing the deletion period restarts the counting, and that
211 // the result takes login creation dates into account.
212 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PeriodChanged) {
213 AddLogin("https://www.google.com", "user", false);
214 RevertTimeInDays(2);
215 AddLogin("https://example.com", "user1", false);
216 RevertTimeInDays(3);
217 AddLogin("https://example.com", "user2", false);
218 RevertTimeInDays(30);
219 AddLogin("https://www.chrome.com", "user", false);
221 PasswordsCounter counter;
222 counter.Init(browser()->profile(),
223 base::Bind(&PasswordsCounterTest::Callback,
224 base::Unretained(this)));
226 SetDeletionPeriodPref(BrowsingDataRemover::LAST_HOUR);
227 WaitForCounting();
228 EXPECT_EQ(1u, GetResult());
230 SetDeletionPeriodPref(BrowsingDataRemover::LAST_DAY);
231 WaitForCounting();
232 EXPECT_EQ(1u, GetResult());
234 SetDeletionPeriodPref(BrowsingDataRemover::LAST_WEEK);
235 WaitForCounting();
236 EXPECT_EQ(3u, GetResult());
238 SetDeletionPeriodPref(BrowsingDataRemover::FOUR_WEEKS);
239 WaitForCounting();
240 EXPECT_EQ(3u, GetResult());
242 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING);
243 WaitForCounting();
244 EXPECT_EQ(4u, GetResult());
247 } // namespace