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

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

Issue 2828083003: Show autofill sync status in CBD (Closed)
Patch Set: fix history test Created 3 years, 7 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/autofill_counter.h" 5 #include "components/browsing_data/core/counters/autofill_counter.h"
6 6
7 #include "base/guid.h" 7 #include "base/guid.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "base/threading/platform_thread.h" 12 #include "base/threading/platform_thread.h"
13 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/sync/test/integration/sync_test.h"
14 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/web_data_service_factory.h" 16 #include "chrome/browser/web_data_service_factory.h"
16 #include "chrome/test/base/in_process_browser_test.h" 17 #include "chrome/test/base/in_process_browser_test.h"
17 #include "components/autofill/core/browser/autofill_profile.h" 18 #include "components/autofill/core/browser/autofill_profile.h"
18 #include "components/autofill/core/browser/autofill_test_utils.h" 19 #include "components/autofill/core/browser/autofill_test_utils.h"
19 #include "components/autofill/core/browser/autofill_type.h" 20 #include "components/autofill/core/browser/autofill_type.h"
20 #include "components/autofill/core/browser/credit_card.h" 21 #include "components/autofill/core/browser/credit_card.h"
21 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" 22 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
23 #include "components/browser_sync/profile_sync_service.h"
22 #include "components/browsing_data/core/browsing_data_utils.h" 24 #include "components/browsing_data/core/browsing_data_utils.h"
23 #include "components/browsing_data/core/pref_names.h" 25 #include "components/browsing_data/core/pref_names.h"
24 #include "components/prefs/pref_service.h" 26 #include "components/prefs/pref_service.h"
25 #include "content/public/browser/browser_thread.h" 27 #include "content/public/browser/browser_thread.h"
26 28
27 namespace { 29 namespace {
28 30
29 class AutofillCounterTest : public InProcessBrowserTest { 31 // TODO(crbug.com/553421): Only RestartOnSyncChange is a SyncTest.
32 // Extract it together with HistoryCounterTest.RestartOnSyncChange.
33 class AutofillCounterTest : public SyncTest {
30 public: 34 public:
31 AutofillCounterTest() {} 35 AutofillCounterTest() : SyncTest(SINGLE_CLIENT) {}
32 ~AutofillCounterTest() override {} 36 ~AutofillCounterTest() override {}
33 37
34 void SetUpOnMainThread() override { 38 void SetUpOnMainThread() override {
35 web_data_service_ = WebDataServiceFactory::GetAutofillWebDataForProfile( 39 web_data_service_ = WebDataServiceFactory::GetAutofillWebDataForProfile(
36 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS); 40 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS);
37 41
38 SetAutofillDeletionPref(true); 42 SetAutofillDeletionPref(true);
39 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME); 43 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME);
40 } 44 }
41 45
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 return web_data_service_; 149 return web_data_service_;
146 } 150 }
147 151
148 // Callback and result retrieval --------------------------------------------- 152 // Callback and result retrieval ---------------------------------------------
149 153
150 void WaitForCounting() { 154 void WaitForCounting() {
151 run_loop_.reset(new base::RunLoop()); 155 run_loop_.reset(new base::RunLoop());
152 run_loop_->Run(); 156 run_loop_->Run();
153 } 157 }
154 158
159 bool CountingFinishedSinceLastAsked() {
msramek 2017/04/28 19:13:13 This is starting to look like we'll need BrowsingD
160 bool result = finished_;
161 finished_ = false;
162 return result;
163 }
164
165 void WaitForCountingOrConfirmFinished() {
166 if (CountingFinishedSinceLastAsked())
167 return;
168
169 WaitForCounting();
170 CountingFinishedSinceLastAsked();
171 }
172
155 browsing_data::BrowsingDataCounter::ResultInt GetNumSuggestions() { 173 browsing_data::BrowsingDataCounter::ResultInt GetNumSuggestions() {
156 DCHECK(finished_); 174 DCHECK(finished_);
157 return num_suggestions_; 175 return num_suggestions_;
158 } 176 }
159 177
160 browsing_data::BrowsingDataCounter::ResultInt GetNumCreditCards() { 178 browsing_data::BrowsingDataCounter::ResultInt GetNumCreditCards() {
161 DCHECK(finished_); 179 DCHECK(finished_);
162 return num_credit_cards_; 180 return num_credit_cards_;
163 } 181 }
164 182
165 browsing_data::BrowsingDataCounter::ResultInt GetNumAddresses() { 183 browsing_data::BrowsingDataCounter::ResultInt GetNumAddresses() {
166 DCHECK(finished_); 184 DCHECK(finished_);
167 return num_addresses_; 185 return num_addresses_;
168 } 186 }
169 187
188 bool AutofillSyncEnabled() { return autofill_sync_enabled_; }
189
170 void Callback( 190 void Callback(
171 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { 191 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
172 finished_ = result->Finished(); 192 finished_ = result->Finished();
173 193
174 if (finished_) { 194 if (finished_) {
175 browsing_data::AutofillCounter::AutofillResult* autofill_result = 195 browsing_data::AutofillCounter::AutofillResult* autofill_result =
176 static_cast<browsing_data::AutofillCounter::AutofillResult*>( 196 static_cast<browsing_data::AutofillCounter::AutofillResult*>(
177 result.get()); 197 result.get());
178 198
179 num_suggestions_ = autofill_result->Value(); 199 num_suggestions_ = autofill_result->Value();
180 num_credit_cards_ = autofill_result->num_credit_cards(); 200 num_credit_cards_ = autofill_result->num_credit_cards();
181 num_addresses_ = autofill_result->num_addresses(); 201 num_addresses_ = autofill_result->num_addresses();
202 autofill_sync_enabled_ = autofill_result->autofill_sync_enabled();
182 } 203 }
183 204
184 if (run_loop_ && finished_) 205 if (run_loop_ && finished_)
185 run_loop_->Quit(); 206 run_loop_->Quit();
186 } 207 }
187 208
188 private: 209 private:
189 std::unique_ptr<base::RunLoop> run_loop_; 210 std::unique_ptr<base::RunLoop> run_loop_;
190 211
191 std::vector<std::string> credit_card_ids_; 212 std::vector<std::string> credit_card_ids_;
192 std::vector<std::string> address_ids_; 213 std::vector<std::string> address_ids_;
193 214
194 scoped_refptr<autofill::AutofillWebDataService> web_data_service_; 215 scoped_refptr<autofill::AutofillWebDataService> web_data_service_;
195 216
196 bool finished_; 217 bool finished_;
197 browsing_data::BrowsingDataCounter::ResultInt num_suggestions_; 218 browsing_data::BrowsingDataCounter::ResultInt num_suggestions_;
198 browsing_data::BrowsingDataCounter::ResultInt num_credit_cards_; 219 browsing_data::BrowsingDataCounter::ResultInt num_credit_cards_;
199 browsing_data::BrowsingDataCounter::ResultInt num_addresses_; 220 browsing_data::BrowsingDataCounter::ResultInt num_addresses_;
221 bool autofill_sync_enabled_;
200 222
201 DISALLOW_COPY_AND_ASSIGN(AutofillCounterTest); 223 DISALLOW_COPY_AND_ASSIGN(AutofillCounterTest);
202 }; 224 };
203 225
204 // Tests that we count the correct number of autocomplete suggestions. 226 // Tests that we count the correct number of autocomplete suggestions.
205 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, AutocompleteSuggestions) { 227 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, AutocompleteSuggestions) {
206 Profile* profile = browser()->profile(); 228 Profile* profile = browser()->profile();
207 browsing_data::AutofillCounter counter(GetWebDataService()); 229 browsing_data::AutofillCounter counter(GetWebDataService(), nullptr);
208 counter.Init( 230 counter.Init(
209 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 231 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
210 base::Bind(&AutofillCounterTest::Callback, base::Unretained(this))); 232 base::Bind(&AutofillCounterTest::Callback, base::Unretained(this)));
211 counter.Restart(); 233 counter.Restart();
212 WaitForCounting(); 234 WaitForCounting();
213 EXPECT_EQ(0, GetNumSuggestions()); 235 EXPECT_EQ(0, GetNumSuggestions());
214 236
215 AddAutocompleteSuggestion("email", "example@example.com"); 237 AddAutocompleteSuggestion("email", "example@example.com");
216 counter.Restart(); 238 counter.Restart();
217 WaitForCounting(); 239 WaitForCounting();
(...skipping 16 matching lines...) Expand all
234 256
235 ClearAutocompleteSuggestions(); 257 ClearAutocompleteSuggestions();
236 counter.Restart(); 258 counter.Restart();
237 WaitForCounting(); 259 WaitForCounting();
238 EXPECT_EQ(0, GetNumSuggestions()); 260 EXPECT_EQ(0, GetNumSuggestions());
239 } 261 }
240 262
241 // Tests that we count the correct number of credit cards. 263 // Tests that we count the correct number of credit cards.
242 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, CreditCards) { 264 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, CreditCards) {
243 Profile* profile = browser()->profile(); 265 Profile* profile = browser()->profile();
244 browsing_data::AutofillCounter counter(GetWebDataService()); 266 browsing_data::AutofillCounter counter(GetWebDataService(), nullptr);
245 counter.Init( 267 counter.Init(
246 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 268 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
247 base::Bind(&AutofillCounterTest::Callback, base::Unretained(this))); 269 base::Bind(&AutofillCounterTest::Callback, base::Unretained(this)));
248 counter.Restart(); 270 counter.Restart();
249 WaitForCounting(); 271 WaitForCounting();
250 EXPECT_EQ(0, GetNumCreditCards()); 272 EXPECT_EQ(0, GetNumCreditCards());
251 273
252 AddCreditCard("0000-0000-0000-0000", "1", "2015"); 274 AddCreditCard("0000-0000-0000-0000", "1", "2015");
253 counter.Restart(); 275 counter.Restart();
254 WaitForCounting(); 276 WaitForCounting();
(...skipping 16 matching lines...) Expand all
271 293
272 ClearCreditCardsAndAddresses(); 294 ClearCreditCardsAndAddresses();
273 counter.Restart(); 295 counter.Restart();
274 WaitForCounting(); 296 WaitForCounting();
275 EXPECT_EQ(0, GetNumCreditCards()); 297 EXPECT_EQ(0, GetNumCreditCards());
276 } 298 }
277 299
278 // Tests that we count the correct number of addresses. 300 // Tests that we count the correct number of addresses.
279 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, Addresses) { 301 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, Addresses) {
280 Profile* profile = browser()->profile(); 302 Profile* profile = browser()->profile();
281 browsing_data::AutofillCounter counter(GetWebDataService()); 303 browsing_data::AutofillCounter counter(GetWebDataService(), nullptr);
282 counter.Init( 304 counter.Init(
283 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 305 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
284 base::Bind(&AutofillCounterTest::Callback, base::Unretained(this))); 306 base::Bind(&AutofillCounterTest::Callback, base::Unretained(this)));
285 counter.Restart(); 307 counter.Restart();
286 WaitForCounting(); 308 WaitForCounting();
287 EXPECT_EQ(0, GetNumAddresses()); 309 EXPECT_EQ(0, GetNumAddresses());
288 310
289 AddAddress("John", "Doe", "Main Street 12345"); 311 AddAddress("John", "Doe", "Main Street 12345");
290 counter.Restart(); 312 counter.Restart();
291 WaitForCounting(); 313 WaitForCounting();
(...skipping 30 matching lines...) Expand all
322 AddAutocompleteSuggestion("city", "Munich"); 344 AddAutocompleteSuggestion("city", "Munich");
323 345
324 AddCreditCard("0000-0000-0000-0000", "1", "2015"); 346 AddCreditCard("0000-0000-0000-0000", "1", "2015");
325 AddCreditCard("1211-1098-7654-3210", "10", "2030"); 347 AddCreditCard("1211-1098-7654-3210", "10", "2030");
326 348
327 AddAddress("John", "Doe", "Main Street 12345"); 349 AddAddress("John", "Doe", "Main Street 12345");
328 AddAddress("Jane", "Smith", "Main Street 12346"); 350 AddAddress("Jane", "Smith", "Main Street 12346");
329 AddAddress("John", "Smith", "Side Street 47"); 351 AddAddress("John", "Smith", "Side Street 47");
330 352
331 Profile* profile = browser()->profile(); 353 Profile* profile = browser()->profile();
332 browsing_data::AutofillCounter counter(GetWebDataService()); 354 browsing_data::AutofillCounter counter(GetWebDataService(), nullptr);
333 counter.Init( 355 counter.Init(
334 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 356 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
335 base::Bind(&AutofillCounterTest::Callback, base::Unretained(this))); 357 base::Bind(&AutofillCounterTest::Callback, base::Unretained(this)));
336 counter.Restart(); 358 counter.Restart();
337 WaitForCounting(); 359 WaitForCounting();
338 EXPECT_EQ(5, GetNumSuggestions()); 360 EXPECT_EQ(5, GetNumSuggestions());
339 EXPECT_EQ(2, GetNumCreditCards()); 361 EXPECT_EQ(2, GetNumCreditCards());
340 EXPECT_EQ(3, GetNumAddresses()); 362 EXPECT_EQ(3, GetNumAddresses());
341 } 363 }
342 364
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 expected_num_credit_cards; 400 expected_num_credit_cards;
379 const browsing_data::BrowsingDataCounter::ResultInt expected_num_addresses; 401 const browsing_data::BrowsingDataCounter::ResultInt expected_num_addresses;
380 } test_cases[] = { 402 } test_cases[] = {
381 { base::Time(), 2, 3, 3}, 403 { base::Time(), 2, 3, 3},
382 { time1, 2, 3, 3}, 404 { time1, 2, 3, 3},
383 { time2, 1, 2, 2}, 405 { time2, 1, 2, 2},
384 { time3, 1, 1, 0} 406 { time3, 1, 1, 0}
385 }; 407 };
386 408
387 Profile* profile = browser()->profile(); 409 Profile* profile = browser()->profile();
388 browsing_data::AutofillCounter counter(GetWebDataService()); 410 browsing_data::AutofillCounter counter(GetWebDataService(), nullptr);
389 counter.Init( 411 counter.Init(
390 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, 412 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
391 base::Bind(&AutofillCounterTest::Callback, base::Unretained(this))); 413 base::Bind(&AutofillCounterTest::Callback, base::Unretained(this)));
392 414
393 for (const TestCase& test_case : test_cases) { 415 for (const TestCase& test_case : test_cases) {
394 counter.SetPeriodStartForTesting(test_case.period_start); 416 counter.SetPeriodStartForTesting(test_case.period_start);
395 counter.Restart(); 417 counter.Restart();
396 WaitForCounting(); 418 WaitForCounting();
397 EXPECT_EQ(test_case.expected_num_suggestions, GetNumSuggestions()); 419 EXPECT_EQ(test_case.expected_num_suggestions, GetNumSuggestions());
398 EXPECT_EQ(test_case.expected_num_credit_cards, GetNumCreditCards()); 420 EXPECT_EQ(test_case.expected_num_credit_cards, GetNumCreditCards());
399 EXPECT_EQ(test_case.expected_num_addresses, GetNumAddresses()); 421 EXPECT_EQ(test_case.expected_num_addresses, GetNumAddresses());
400 } 422 }
401 } 423 }
402 424
425 // Test that the counting restarts when autofill sync state changes.
426 // TODO(crbug.com/553421): Move this to the sync/test/integration directory?
427 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, RestartOnSyncChange) {
428 // Set up the Sync client.
429 ASSERT_TRUE(SetupClients());
430 static const int kFirstProfileIndex = 0;
431 browser_sync::ProfileSyncService* sync_service =
432 GetSyncService(kFirstProfileIndex);
433 Profile* profile = GetProfile(kFirstProfileIndex);
434 // Set up the counter.
435 browsing_data::AutofillCounter counter(
436 WebDataServiceFactory::GetAutofillWebDataForProfile(
437 profile, ServiceAccessType::IMPLICIT_ACCESS),
438 sync_service);
439
440 counter.Init(
441 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
442 base::Bind(&AutofillCounterTest::Callback, base::Unretained(this)));
443
444 // Note that some Sync operations notify observers immediately (and thus there
445 // is no need to call |WaitForCounting()|; in fact, it would block the test),
446 // while other operations only post the task on UI thread's message loop
447 // (which requires calling |WaitForCounting()| for them to run). Therefore,
448 // this test always checks if the callback has already run and only waits
449 // if it has not.
450
451 // We sync all datatypes by default, so starting Sync means that we start
452 // syncing autofill, and this should restart the counter.
453 ASSERT_TRUE(SetupSync());
454 ASSERT_TRUE(sync_service->IsSyncActive());
455 ASSERT_TRUE(sync_service->GetActiveDataTypes().Has(syncer::AUTOFILL));
456 WaitForCountingOrConfirmFinished();
457 EXPECT_TRUE(AutofillSyncEnabled());
458
459 // We stop syncing autofill in particular. This restarts the counter.
460 syncer::ModelTypeSet everything_except_autofill =
461 syncer::UserSelectableTypes();
462 everything_except_autofill.Remove(syncer::AUTOFILL);
463 auto sync_blocker = sync_service->GetSetupInProgressHandle();
464 sync_service->OnUserChoseDatatypes(/*sync_everything=*/false,
465 everything_except_autofill);
466 ASSERT_FALSE(sync_service->GetPreferredDataTypes().Has(syncer::AUTOFILL));
467 sync_blocker.reset();
468 WaitForCountingOrConfirmFinished();
469 ASSERT_FALSE(sync_service->GetActiveDataTypes().Has(syncer::AUTOFILL));
470 EXPECT_FALSE(AutofillSyncEnabled());
471
472 // If autofill sync is not affected, the counter is not restarted.
473 syncer::ModelTypeSet only_history(syncer::HISTORY_DELETE_DIRECTIVES);
474 sync_blocker = sync_service->GetSetupInProgressHandle();
475 sync_service->ChangePreferredDataTypes(only_history);
476 sync_blocker.reset();
477 EXPECT_FALSE(CountingFinishedSinceLastAsked());
478
479 // We start syncing autofill again. This restarts the counter.
480 sync_blocker = sync_service->GetSetupInProgressHandle();
481 sync_service->ChangePreferredDataTypes(syncer::ModelTypeSet::All());
482 sync_blocker.reset();
483 WaitForCountingOrConfirmFinished();
484 EXPECT_TRUE(AutofillSyncEnabled());
485
486 // Stopping the Sync service triggers a restart.
487 sync_service->RequestStop(syncer::SyncService::CLEAR_DATA);
488 WaitForCountingOrConfirmFinished();
489 EXPECT_FALSE(AutofillSyncEnabled());
490 }
491
403 } // namespace 492 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698