OLD | NEW |
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" | |
15 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/web_data_service_factory.h" | 15 #include "chrome/browser/web_data_service_factory.h" |
17 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
18 #include "components/autofill/core/browser/autofill_profile.h" | 17 #include "components/autofill/core/browser/autofill_profile.h" |
19 #include "components/autofill/core/browser/autofill_test_utils.h" | 18 #include "components/autofill/core/browser/autofill_test_utils.h" |
20 #include "components/autofill/core/browser/autofill_type.h" | 19 #include "components/autofill/core/browser/autofill_type.h" |
21 #include "components/autofill/core/browser/credit_card.h" | 20 #include "components/autofill/core/browser/credit_card.h" |
22 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 21 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
23 #include "components/browser_sync/profile_sync_service.h" | |
24 #include "components/browsing_data/core/browsing_data_utils.h" | 22 #include "components/browsing_data/core/browsing_data_utils.h" |
25 #include "components/browsing_data/core/pref_names.h" | 23 #include "components/browsing_data/core/pref_names.h" |
26 #include "components/prefs/pref_service.h" | 24 #include "components/prefs/pref_service.h" |
27 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
28 | 26 |
29 namespace { | 27 namespace { |
30 | 28 |
31 // TODO(crbug.com/553421): Only RestartOnSyncChange is a SyncTest. | 29 class AutofillCounterTest : public InProcessBrowserTest { |
32 // Extract it together with HistoryCounterTest.RestartOnSyncChange. | |
33 class AutofillCounterTest : public SyncTest { | |
34 public: | 30 public: |
35 AutofillCounterTest() : SyncTest(SINGLE_CLIENT) {} | 31 AutofillCounterTest() {} |
36 ~AutofillCounterTest() override {} | 32 ~AutofillCounterTest() override {} |
37 | 33 |
38 void SetUpOnMainThread() override { | 34 void SetUpOnMainThread() override { |
39 web_data_service_ = WebDataServiceFactory::GetAutofillWebDataForProfile( | 35 web_data_service_ = WebDataServiceFactory::GetAutofillWebDataForProfile( |
40 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS); | 36 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS); |
41 | 37 |
42 SetAutofillDeletionPref(true); | 38 SetAutofillDeletionPref(true); |
43 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME); | 39 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME); |
44 } | 40 } |
45 | 41 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 return web_data_service_; | 145 return web_data_service_; |
150 } | 146 } |
151 | 147 |
152 // Callback and result retrieval --------------------------------------------- | 148 // Callback and result retrieval --------------------------------------------- |
153 | 149 |
154 void WaitForCounting() { | 150 void WaitForCounting() { |
155 run_loop_.reset(new base::RunLoop()); | 151 run_loop_.reset(new base::RunLoop()); |
156 run_loop_->Run(); | 152 run_loop_->Run(); |
157 } | 153 } |
158 | 154 |
159 bool CountingFinishedSinceLastAsked() { | |
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 | |
173 browsing_data::BrowsingDataCounter::ResultInt GetNumSuggestions() { | 155 browsing_data::BrowsingDataCounter::ResultInt GetNumSuggestions() { |
174 DCHECK(finished_); | 156 DCHECK(finished_); |
175 return num_suggestions_; | 157 return num_suggestions_; |
176 } | 158 } |
177 | 159 |
178 browsing_data::BrowsingDataCounter::ResultInt GetNumCreditCards() { | 160 browsing_data::BrowsingDataCounter::ResultInt GetNumCreditCards() { |
179 DCHECK(finished_); | 161 DCHECK(finished_); |
180 return num_credit_cards_; | 162 return num_credit_cards_; |
181 } | 163 } |
182 | 164 |
183 browsing_data::BrowsingDataCounter::ResultInt GetNumAddresses() { | 165 browsing_data::BrowsingDataCounter::ResultInt GetNumAddresses() { |
184 DCHECK(finished_); | 166 DCHECK(finished_); |
185 return num_addresses_; | 167 return num_addresses_; |
186 } | 168 } |
187 | 169 |
188 bool AutofillSyncEnabled() { return autofill_sync_enabled_; } | |
189 | |
190 void Callback( | 170 void Callback( |
191 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { | 171 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { |
192 finished_ = result->Finished(); | 172 finished_ = result->Finished(); |
193 | 173 |
194 if (finished_) { | 174 if (finished_) { |
195 browsing_data::AutofillCounter::AutofillResult* autofill_result = | 175 browsing_data::AutofillCounter::AutofillResult* autofill_result = |
196 static_cast<browsing_data::AutofillCounter::AutofillResult*>( | 176 static_cast<browsing_data::AutofillCounter::AutofillResult*>( |
197 result.get()); | 177 result.get()); |
198 | 178 |
199 num_suggestions_ = autofill_result->Value(); | 179 num_suggestions_ = autofill_result->Value(); |
200 num_credit_cards_ = autofill_result->num_credit_cards(); | 180 num_credit_cards_ = autofill_result->num_credit_cards(); |
201 num_addresses_ = autofill_result->num_addresses(); | 181 num_addresses_ = autofill_result->num_addresses(); |
202 autofill_sync_enabled_ = autofill_result->autofill_sync_enabled(); | |
203 } | 182 } |
204 | 183 |
205 if (run_loop_ && finished_) | 184 if (run_loop_ && finished_) |
206 run_loop_->Quit(); | 185 run_loop_->Quit(); |
207 } | 186 } |
208 | 187 |
209 private: | 188 private: |
210 std::unique_ptr<base::RunLoop> run_loop_; | 189 std::unique_ptr<base::RunLoop> run_loop_; |
211 | 190 |
212 std::vector<std::string> credit_card_ids_; | 191 std::vector<std::string> credit_card_ids_; |
213 std::vector<std::string> address_ids_; | 192 std::vector<std::string> address_ids_; |
214 | 193 |
215 scoped_refptr<autofill::AutofillWebDataService> web_data_service_; | 194 scoped_refptr<autofill::AutofillWebDataService> web_data_service_; |
216 | 195 |
217 bool finished_; | 196 bool finished_; |
218 browsing_data::BrowsingDataCounter::ResultInt num_suggestions_; | 197 browsing_data::BrowsingDataCounter::ResultInt num_suggestions_; |
219 browsing_data::BrowsingDataCounter::ResultInt num_credit_cards_; | 198 browsing_data::BrowsingDataCounter::ResultInt num_credit_cards_; |
220 browsing_data::BrowsingDataCounter::ResultInt num_addresses_; | 199 browsing_data::BrowsingDataCounter::ResultInt num_addresses_; |
221 bool autofill_sync_enabled_; | |
222 | 200 |
223 DISALLOW_COPY_AND_ASSIGN(AutofillCounterTest); | 201 DISALLOW_COPY_AND_ASSIGN(AutofillCounterTest); |
224 }; | 202 }; |
225 | 203 |
226 // Tests that we count the correct number of autocomplete suggestions. | 204 // Tests that we count the correct number of autocomplete suggestions. |
227 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, AutocompleteSuggestions) { | 205 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, AutocompleteSuggestions) { |
228 Profile* profile = browser()->profile(); | 206 Profile* profile = browser()->profile(); |
229 browsing_data::AutofillCounter counter(GetWebDataService(), nullptr); | 207 browsing_data::AutofillCounter counter(GetWebDataService(), nullptr); |
230 counter.Init( | 208 counter.Init( |
231 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, | 209 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 for (const TestCase& test_case : test_cases) { | 393 for (const TestCase& test_case : test_cases) { |
416 counter.SetPeriodStartForTesting(test_case.period_start); | 394 counter.SetPeriodStartForTesting(test_case.period_start); |
417 counter.Restart(); | 395 counter.Restart(); |
418 WaitForCounting(); | 396 WaitForCounting(); |
419 EXPECT_EQ(test_case.expected_num_suggestions, GetNumSuggestions()); | 397 EXPECT_EQ(test_case.expected_num_suggestions, GetNumSuggestions()); |
420 EXPECT_EQ(test_case.expected_num_credit_cards, GetNumCreditCards()); | 398 EXPECT_EQ(test_case.expected_num_credit_cards, GetNumCreditCards()); |
421 EXPECT_EQ(test_case.expected_num_addresses, GetNumAddresses()); | 399 EXPECT_EQ(test_case.expected_num_addresses, GetNumAddresses()); |
422 } | 400 } |
423 } | 401 } |
424 | 402 |
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 | |
492 } // namespace | 403 } // namespace |
OLD | NEW |