Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: chrome/browser/browsing_data/passwords_counter_browsertest.cc

Issue 2798243004: Show password sync status in CBD (Closed)
Patch Set: add sync test Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/browsing_data/core/counters/passwords_counter.h" 5 #include "components/browsing_data/core/counters/passwords_counter.h"
6 6
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "chrome/browser/password_manager/password_store_factory.h" 10 #include "chrome/browser/password_manager/password_store_factory.h"
11 #include "chrome/browser/sync/profile_sync_service_factory.h"
11 #include "chrome/browser/sync/test/integration/passwords_helper.h" 12 #include "chrome/browser/sync/test/integration/passwords_helper.h"
13 #include "chrome/browser/sync/test/integration/sync_test.h"
12 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser.h"
13 #include "chrome/test/base/in_process_browser_test.h" 15 #include "chrome/test/base/in_process_browser_test.h"
14 #include "components/autofill/core/common/password_form.h" 16 #include "components/autofill/core/common/password_form.h"
17 #include "components/browser_sync/profile_sync_service.h"
18 #include "components/browser_sync/profile_sync_service_mock.h"
15 #include "components/browsing_data/core/browsing_data_utils.h" 19 #include "components/browsing_data/core/browsing_data_utils.h"
16 #include "components/browsing_data/core/pref_names.h" 20 #include "components/browsing_data/core/pref_names.h"
17 #include "components/prefs/pref_service.h" 21 #include "components/prefs/pref_service.h"
18 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
19 23
20 namespace { 24 namespace {
21 25
22 using autofill::PasswordForm; 26 using autofill::PasswordForm;
23 27
24 class PasswordsCounterTest : public InProcessBrowserTest { 28 // TODO(crbug.com/553421): Only RestartOnSyncChange is a SyncTest.
29 // Extract it together with HistoryCounterTest.RestartOnSyncChange.
30 class PasswordsCounterTest : public SyncTest {
25 public: 31 public:
32 PasswordsCounterTest() : SyncTest(SINGLE_CLIENT) {}
33
26 void SetUpOnMainThread() override { 34 void SetUpOnMainThread() override {
35 finished_ = false;
27 time_ = base::Time::Now(); 36 time_ = base::Time::Now();
28 store_ = PasswordStoreFactory::GetForProfile( 37 store_ = PasswordStoreFactory::GetForProfile(
29 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS); 38 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS);
30 SetPasswordsDeletionPref(true); 39 SetPasswordsDeletionPref(true);
31 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME); 40 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME);
32 } 41 }
33 42
34 void AddLogin(const std::string& origin, 43 void AddLogin(const std::string& origin,
35 const std::string& username, 44 const std::string& username,
36 bool blacklisted) { 45 bool blacklisted) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 base::Unretained(&waitable_event))); 81 base::Unretained(&waitable_event)));
73 waitable_event.Wait(); 82 waitable_event.Wait();
74 83
75 // At this point, the calculation on DB thread should have finished, and 84 // At this point, the calculation on DB thread should have finished, and
76 // a callback should be scheduled on the UI thread. Process the tasks until 85 // a callback should be scheduled on the UI thread. Process the tasks until
77 // we get a finished result. 86 // we get a finished result.
78 run_loop_.reset(new base::RunLoop()); 87 run_loop_.reset(new base::RunLoop());
79 run_loop_->Run(); 88 run_loop_->Run();
80 } 89 }
81 90
91 bool CountingFinishedSinceLastAsked() {
92 bool result = finished_;
93 finished_ = false;
94 return result;
95 }
96
97 void WaitForCountingOrConfirmFinished() {
98 if (CountingFinishedSinceLastAsked())
99 return;
100
101 WaitForCounting();
102 CountingFinishedSinceLastAsked();
103 }
104
82 browsing_data::BrowsingDataCounter::ResultInt GetResult() { 105 browsing_data::BrowsingDataCounter::ResultInt GetResult() {
83 DCHECK(finished_); 106 DCHECK(finished_);
84 return result_; 107 return result_;
85 } 108 }
86 109
110 bool PasswordSyncEnabled() { return password_sync_enabled_; }
111
87 void Callback( 112 void Callback(
88 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { 113 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
114 DCHECK(result);
89 finished_ = result->Finished(); 115 finished_ = result->Finished();
90 116
91 if (finished_) { 117 if (finished_) {
92 result_ = 118 auto* password_result =
93 static_cast<browsing_data::BrowsingDataCounter::FinishedResult*>( 119 static_cast<browsing_data::PasswordsCounter::PasswordResult*>(
94 result.get()) 120 result.get());
95 ->Value(); 121 result_ = password_result->Value();
122
123 password_sync_enabled_ = password_result->password_sync_enabled();
96 } 124 }
97 125 if (run_loop_ && finished_)
98 if (finished_)
99 run_loop_->Quit(); 126 run_loop_->Quit();
100 } 127 }
101 128
102 void WaitForUICallbacksFromAddingLogins() { 129 void WaitForUICallbacksFromAddingLogins() {
103 base::RunLoop loop; 130 base::RunLoop loop;
104 loop.RunUntilIdle(); 131 loop.RunUntilIdle();
105 } 132 }
106 133
107 private: 134 private:
108 PasswordForm CreateCredentials(const std::string& origin, 135 PasswordForm CreateCredentials(const std::string& origin,
109 const std::string& username, 136 const std::string& username,
110 bool blacklisted) { 137 bool blacklisted) {
111 PasswordForm result; 138 PasswordForm result;
112 result.signon_realm = origin; 139 result.signon_realm = origin;
113 result.origin = GURL(origin); 140 result.origin = GURL(origin);
114 result.username_value = base::ASCIIToUTF16(username); 141 result.username_value = base::ASCIIToUTF16(username);
115 result.password_value = base::ASCIIToUTF16("hunter2"); 142 result.password_value = base::ASCIIToUTF16("hunter2");
116 result.blacklisted_by_user = blacklisted; 143 result.blacklisted_by_user = blacklisted;
117 result.date_created = time_; 144 result.date_created = time_;
118 return result; 145 return result;
119 } 146 }
120 147
121 scoped_refptr<password_manager::PasswordStore> store_; 148 scoped_refptr<password_manager::PasswordStore> store_;
122 149
123 std::unique_ptr<base::RunLoop> run_loop_; 150 std::unique_ptr<base::RunLoop> run_loop_;
124 base::Time time_; 151 base::Time time_;
125 152
126 bool finished_; 153 bool finished_;
127 browsing_data::BrowsingDataCounter::ResultInt result_; 154 browsing_data::BrowsingDataCounter::ResultInt result_;
155 bool password_sync_enabled_;
128 }; 156 };
129 157
130 // Tests that the counter correctly counts each individual credential on 158 // Tests that the counter correctly counts each individual credential on
131 // the same domain. 159 // the same domain.
132 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, SameDomain) { 160 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, SameDomain) {
133 AddLogin("https://www.google.com", "user1", false); 161 AddLogin("https://www.google.com", "user1", false);
134 AddLogin("https://www.google.com", "user2", false); 162 AddLogin("https://www.google.com", "user2", false);
135 AddLogin("https://www.google.com", "user3", false); 163 AddLogin("https://www.google.com", "user3", false);
136 AddLogin("https://www.chrome.com", "user1", false); 164 AddLogin("https://www.chrome.com", "user1", false);
137 AddLogin("https://www.chrome.com", "user2", false); 165 AddLogin("https://www.chrome.com", "user2", false);
138 WaitForUICallbacksFromAddingLogins(); 166 WaitForUICallbacksFromAddingLogins();
139 167
140 Profile* profile = browser()->profile(); 168 Profile* profile = browser()->profile();
141 browsing_data::PasswordsCounter counter(PasswordStoreFactory::GetForProfile( 169 browsing_data::PasswordsCounter counter(
142 profile, ServiceAccessType::EXPLICIT_ACCESS)); 170 PasswordStoreFactory::GetForProfile(profile,
171 ServiceAccessType::EXPLICIT_ACCESS),
172 ProfileSyncServiceFactory::GetForProfile(profile));
143 counter.Init( 173 counter.Init(
144 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 174 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
145 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); 175 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this)));
146 counter.Restart(); 176 counter.Restart();
147 177
148 WaitForCounting(); 178 WaitForCounting();
149 EXPECT_EQ(5u, GetResult()); 179 EXPECT_EQ(5u, GetResult());
180 EXPECT_FALSE(PasswordSyncEnabled());
150 } 181 }
151 182
152 // Tests that the counter doesn't count blacklisted entries. 183 // Tests that the counter doesn't count blacklisted entries.
153 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, Blacklisted) { 184 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, Blacklisted) {
154 AddLogin("https://www.google.com", "user1", false); 185 AddLogin("https://www.google.com", "user1", false);
155 AddLogin("https://www.google.com", "user2", true); 186 AddLogin("https://www.google.com", "user2", true);
156 AddLogin("https://www.chrome.com", "user3", true); 187 AddLogin("https://www.chrome.com", "user3", true);
157 WaitForUICallbacksFromAddingLogins(); 188 WaitForUICallbacksFromAddingLogins();
158 189
159 Profile* profile = browser()->profile(); 190 Profile* profile = browser()->profile();
160 browsing_data::PasswordsCounter counter(PasswordStoreFactory::GetForProfile( 191 browsing_data::PasswordsCounter counter(
161 profile, ServiceAccessType::EXPLICIT_ACCESS)); 192 PasswordStoreFactory::GetForProfile(profile,
193 ServiceAccessType::EXPLICIT_ACCESS),
194 ProfileSyncServiceFactory::GetForProfile(profile));
162 195
163 counter.Init( 196 counter.Init(
164 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 197 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
165 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); 198 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this)));
166 counter.Restart(); 199 counter.Restart();
167 200
168 WaitForCounting(); 201 WaitForCounting();
169 EXPECT_EQ(1u, GetResult()); 202 EXPECT_EQ(1u, GetResult());
203 EXPECT_FALSE(PasswordSyncEnabled());
170 } 204 }
171 205
172 // Tests that the counter starts counting automatically when the deletion 206 // Tests that the counter starts counting automatically when the deletion
173 // pref changes to true. 207 // pref changes to true.
174 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefChanged) { 208 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefChanged) {
175 SetPasswordsDeletionPref(false); 209 SetPasswordsDeletionPref(false);
176 AddLogin("https://www.google.com", "user", false); 210 AddLogin("https://www.google.com", "user", false);
177 AddLogin("https://www.chrome.com", "user", false); 211 AddLogin("https://www.chrome.com", "user", false);
178 WaitForUICallbacksFromAddingLogins(); 212 WaitForUICallbacksFromAddingLogins();
179 213
180 Profile* profile = browser()->profile(); 214 Profile* profile = browser()->profile();
181 browsing_data::PasswordsCounter counter(PasswordStoreFactory::GetForProfile( 215 browsing_data::PasswordsCounter counter(
182 profile, ServiceAccessType::EXPLICIT_ACCESS)); 216 PasswordStoreFactory::GetForProfile(profile,
217 ServiceAccessType::EXPLICIT_ACCESS),
218 ProfileSyncServiceFactory::GetForProfile(profile));
183 counter.Init( 219 counter.Init(
184 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 220 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
185 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); 221 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this)));
186 SetPasswordsDeletionPref(true); 222 SetPasswordsDeletionPref(true);
187 223
188 WaitForCounting(); 224 WaitForCounting();
189 EXPECT_EQ(2u, GetResult()); 225 EXPECT_EQ(2u, GetResult());
226 EXPECT_FALSE(PasswordSyncEnabled());
190 } 227 }
191 228
192 // Tests that the counter starts counting automatically when 229 // Tests that the counter starts counting automatically when
193 // the password store changes. 230 // the password store changes.
194 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, StoreChanged) { 231 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, StoreChanged) {
195 AddLogin("https://www.google.com", "user", false); 232 AddLogin("https://www.google.com", "user", false);
196 WaitForUICallbacksFromAddingLogins(); 233 WaitForUICallbacksFromAddingLogins();
197 234
198 Profile* profile = browser()->profile(); 235 Profile* profile = browser()->profile();
199 browsing_data::PasswordsCounter counter(PasswordStoreFactory::GetForProfile( 236 browsing_data::PasswordsCounter counter(
200 profile, ServiceAccessType::EXPLICIT_ACCESS)); 237 PasswordStoreFactory::GetForProfile(profile,
238 ServiceAccessType::EXPLICIT_ACCESS),
239 ProfileSyncServiceFactory::GetForProfile(profile));
201 counter.Init( 240 counter.Init(
202 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 241 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
203 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); 242 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this)));
204 counter.Restart(); 243 counter.Restart();
205 244
206 WaitForCounting(); 245 WaitForCounting();
207 EXPECT_EQ(1u, GetResult()); 246 EXPECT_EQ(1u, GetResult());
208 247
209 AddLogin("https://www.chrome.com", "user", false); 248 AddLogin("https://www.chrome.com", "user", false);
210 WaitForCounting(); 249 WaitForCounting();
(...skipping 10 matching lines...) Expand all
221 AddLogin("https://www.google.com", "user", false); 260 AddLogin("https://www.google.com", "user", false);
222 RevertTimeInDays(2); 261 RevertTimeInDays(2);
223 AddLogin("https://example.com", "user1", false); 262 AddLogin("https://example.com", "user1", false);
224 RevertTimeInDays(3); 263 RevertTimeInDays(3);
225 AddLogin("https://example.com", "user2", false); 264 AddLogin("https://example.com", "user2", false);
226 RevertTimeInDays(30); 265 RevertTimeInDays(30);
227 AddLogin("https://www.chrome.com", "user", false); 266 AddLogin("https://www.chrome.com", "user", false);
228 WaitForUICallbacksFromAddingLogins(); 267 WaitForUICallbacksFromAddingLogins();
229 268
230 Profile* profile = browser()->profile(); 269 Profile* profile = browser()->profile();
231 browsing_data::PasswordsCounter counter(PasswordStoreFactory::GetForProfile( 270 browsing_data::PasswordsCounter counter(
232 profile, ServiceAccessType::EXPLICIT_ACCESS)); 271 PasswordStoreFactory::GetForProfile(profile,
272 ServiceAccessType::EXPLICIT_ACCESS),
273 ProfileSyncServiceFactory::GetForProfile(profile));
233 counter.Init( 274 counter.Init(
234 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 275 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
235 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); 276 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this)));
236 277
237 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_HOUR); 278 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_HOUR);
238 WaitForCounting(); 279 WaitForCounting();
239 EXPECT_EQ(1u, GetResult()); 280 EXPECT_EQ(1u, GetResult());
240 281
241 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_DAY); 282 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_DAY);
242 WaitForCounting(); 283 WaitForCounting();
243 EXPECT_EQ(1u, GetResult()); 284 EXPECT_EQ(1u, GetResult());
244 285
245 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_WEEK); 286 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_WEEK);
246 WaitForCounting(); 287 WaitForCounting();
247 EXPECT_EQ(3u, GetResult()); 288 EXPECT_EQ(3u, GetResult());
248 289
249 SetDeletionPeriodPref(browsing_data::TimePeriod::FOUR_WEEKS); 290 SetDeletionPeriodPref(browsing_data::TimePeriod::FOUR_WEEKS);
250 WaitForCounting(); 291 WaitForCounting();
251 EXPECT_EQ(3u, GetResult()); 292 EXPECT_EQ(3u, GetResult());
252 293
253 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME); 294 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME);
254 WaitForCounting(); 295 WaitForCounting();
255 EXPECT_EQ(4u, GetResult()); 296 EXPECT_EQ(4u, GetResult());
256 } 297 }
257 298
299 // Test that the counting restarts when password sync state changes.
300 // TODO(crbug.com/553421): Move this to the sync/test/integration directory?
301 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, RestartOnSyncChange) {
302 // Set up the Sync client.
303 ASSERT_TRUE(SetupClients());
304 static const int kFirstProfileIndex = 0;
305 browser_sync::ProfileSyncService* sync_service =
306 GetSyncService(kFirstProfileIndex);
307 Profile* profile = GetProfile(kFirstProfileIndex);
308 // Set up the counter.
309 browsing_data::PasswordsCounter counter(
310 PasswordStoreFactory::GetForProfile(profile,
311 ServiceAccessType::EXPLICIT_ACCESS),
312 sync_service);
313
314 counter.Init(
315 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
316 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this)));
317
318 // Note that some Sync operations notify observers immediately (and thus there
319 // is no need to call |WaitForCounting()|; in fact, it would block the test),
320 // while other operations only post the task on UI thread's message loop
321 // (which requires calling |WaitForCounting()| for them to run). Therefore,
322 // this test always checks if the callback has already run and only waits
323 // if it has not.
324
325 // We sync all datatypes by default, so starting Sync means that we start
326 // syncing passwords, and this should restart the counter.
327 ASSERT_TRUE(SetupSync());
328 ASSERT_TRUE(sync_service->IsSyncActive());
329 ASSERT_TRUE(sync_service->GetPreferredDataTypes().Has(syncer::PASSWORDS));
330 WaitForCountingOrConfirmFinished();
331 EXPECT_TRUE(PasswordSyncEnabled());
332
333 // We stop syncing passwords in particular. This restarts the counter.
334 syncer::ModelTypeSet everything_except_passwords =
335 syncer::ModelTypeSet::All();
336 everything_except_passwords.Remove(syncer::PASSWORDS);
337 auto sync_blocker = sync_service->GetSetupInProgressHandle();
338 sync_service->ChangePreferredDataTypes(everything_except_passwords);
339 sync_blocker.reset();
340 WaitForCountingOrConfirmFinished();
341 EXPECT_FALSE(PasswordSyncEnabled());
342
343 // If password sync is not affected, the counter is not restarted.
344 syncer::ModelTypeSet only_history(syncer::HISTORY_DELETE_DIRECTIVES);
345 sync_service->ChangePreferredDataTypes(only_history);
346 sync_blocker = sync_service->GetSetupInProgressHandle();
347 sync_service->ChangePreferredDataTypes(only_history);
348 sync_blocker.reset();
349 EXPECT_FALSE(CountingFinishedSinceLastAsked());
350
351 // We start syncing passwords again. This restarts the counter.
352 sync_blocker = sync_service->GetSetupInProgressHandle();
353 sync_service->ChangePreferredDataTypes(syncer::ModelTypeSet::All());
354 sync_blocker.reset();
355 WaitForCountingOrConfirmFinished();
356 EXPECT_TRUE(PasswordSyncEnabled());
357
358 // Stopping the Sync service triggers a restart.
359 sync_service->RequestStop(syncer::SyncService::CLEAR_DATA);
360 WaitForCountingOrConfirmFinished();
361 EXPECT_FALSE(PasswordSyncEnabled());
362 }
363
258 } // namespace 364 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/browsing_data/history_counter_browsertest.cc ('k') | components/browsing_data/core/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698