Chromium Code Reviews| Index: chrome/browser/browsing_data/autofill_counter_browsertest.cc |
| diff --git a/chrome/browser/browsing_data/autofill_counter_browsertest.cc b/chrome/browser/browsing_data/autofill_counter_browsertest.cc |
| index 3db7c8f6a8838bf8e0956cb05d602fdf771f6185..e7a660e886398341d21b26eaab8178d455937c44 100644 |
| --- a/chrome/browser/browsing_data/autofill_counter_browsertest.cc |
| +++ b/chrome/browser/browsing_data/autofill_counter_browsertest.cc |
| @@ -11,6 +11,7 @@ |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/threading/platform_thread.h" |
| #include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/sync/test/integration/sync_test.h" |
| #include "chrome/browser/ui/browser.h" |
| #include "chrome/browser/web_data_service_factory.h" |
| #include "chrome/test/base/in_process_browser_test.h" |
| @@ -19,6 +20,7 @@ |
| #include "components/autofill/core/browser/autofill_type.h" |
| #include "components/autofill/core/browser/credit_card.h" |
| #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
| +#include "components/browser_sync/profile_sync_service.h" |
| #include "components/browsing_data/core/browsing_data_utils.h" |
| #include "components/browsing_data/core/pref_names.h" |
| #include "components/prefs/pref_service.h" |
| @@ -26,9 +28,11 @@ |
| namespace { |
| -class AutofillCounterTest : public InProcessBrowserTest { |
| +// TODO(crbug.com/553421): Only RestartOnSyncChange is a SyncTest. |
| +// Extract it together with HistoryCounterTest.RestartOnSyncChange. |
| +class AutofillCounterTest : public SyncTest { |
| public: |
| - AutofillCounterTest() {} |
| + AutofillCounterTest() : SyncTest(SINGLE_CLIENT) {} |
| ~AutofillCounterTest() override {} |
| void SetUpOnMainThread() override { |
| @@ -152,6 +156,20 @@ class AutofillCounterTest : public InProcessBrowserTest { |
| run_loop_->Run(); |
| } |
| + bool CountingFinishedSinceLastAsked() { |
|
msramek
2017/04/28 19:13:13
This is starting to look like we'll need BrowsingD
|
| + bool result = finished_; |
| + finished_ = false; |
| + return result; |
| + } |
| + |
| + void WaitForCountingOrConfirmFinished() { |
| + if (CountingFinishedSinceLastAsked()) |
| + return; |
| + |
| + WaitForCounting(); |
| + CountingFinishedSinceLastAsked(); |
| + } |
| + |
| browsing_data::BrowsingDataCounter::ResultInt GetNumSuggestions() { |
| DCHECK(finished_); |
| return num_suggestions_; |
| @@ -167,6 +185,8 @@ class AutofillCounterTest : public InProcessBrowserTest { |
| return num_addresses_; |
| } |
| + bool AutofillSyncEnabled() { return autofill_sync_enabled_; } |
| + |
| void Callback( |
| std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { |
| finished_ = result->Finished(); |
| @@ -179,6 +199,7 @@ class AutofillCounterTest : public InProcessBrowserTest { |
| num_suggestions_ = autofill_result->Value(); |
| num_credit_cards_ = autofill_result->num_credit_cards(); |
| num_addresses_ = autofill_result->num_addresses(); |
| + autofill_sync_enabled_ = autofill_result->autofill_sync_enabled(); |
| } |
| if (run_loop_ && finished_) |
| @@ -197,6 +218,7 @@ class AutofillCounterTest : public InProcessBrowserTest { |
| browsing_data::BrowsingDataCounter::ResultInt num_suggestions_; |
| browsing_data::BrowsingDataCounter::ResultInt num_credit_cards_; |
| browsing_data::BrowsingDataCounter::ResultInt num_addresses_; |
| + bool autofill_sync_enabled_; |
| DISALLOW_COPY_AND_ASSIGN(AutofillCounterTest); |
| }; |
| @@ -204,7 +226,7 @@ class AutofillCounterTest : public InProcessBrowserTest { |
| // Tests that we count the correct number of autocomplete suggestions. |
| IN_PROC_BROWSER_TEST_F(AutofillCounterTest, AutocompleteSuggestions) { |
| Profile* profile = browser()->profile(); |
| - browsing_data::AutofillCounter counter(GetWebDataService()); |
| + browsing_data::AutofillCounter counter(GetWebDataService(), nullptr); |
| counter.Init( |
| profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| base::Bind(&AutofillCounterTest::Callback, base::Unretained(this))); |
| @@ -241,7 +263,7 @@ IN_PROC_BROWSER_TEST_F(AutofillCounterTest, AutocompleteSuggestions) { |
| // Tests that we count the correct number of credit cards. |
| IN_PROC_BROWSER_TEST_F(AutofillCounterTest, CreditCards) { |
| Profile* profile = browser()->profile(); |
| - browsing_data::AutofillCounter counter(GetWebDataService()); |
| + browsing_data::AutofillCounter counter(GetWebDataService(), nullptr); |
| counter.Init( |
| profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| base::Bind(&AutofillCounterTest::Callback, base::Unretained(this))); |
| @@ -278,7 +300,7 @@ IN_PROC_BROWSER_TEST_F(AutofillCounterTest, CreditCards) { |
| // Tests that we count the correct number of addresses. |
| IN_PROC_BROWSER_TEST_F(AutofillCounterTest, Addresses) { |
| Profile* profile = browser()->profile(); |
| - browsing_data::AutofillCounter counter(GetWebDataService()); |
| + browsing_data::AutofillCounter counter(GetWebDataService(), nullptr); |
| counter.Init( |
| profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| base::Bind(&AutofillCounterTest::Callback, base::Unretained(this))); |
| @@ -329,7 +351,7 @@ IN_PROC_BROWSER_TEST_F(AutofillCounterTest, ComplexResult) { |
| AddAddress("John", "Smith", "Side Street 47"); |
| Profile* profile = browser()->profile(); |
| - browsing_data::AutofillCounter counter(GetWebDataService()); |
| + browsing_data::AutofillCounter counter(GetWebDataService(), nullptr); |
| counter.Init( |
| profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| base::Bind(&AutofillCounterTest::Callback, base::Unretained(this))); |
| @@ -385,7 +407,7 @@ IN_PROC_BROWSER_TEST_F(AutofillCounterTest, TimeRanges) { |
| }; |
| Profile* profile = browser()->profile(); |
| - browsing_data::AutofillCounter counter(GetWebDataService()); |
| + browsing_data::AutofillCounter counter(GetWebDataService(), nullptr); |
| counter.Init( |
| profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| base::Bind(&AutofillCounterTest::Callback, base::Unretained(this))); |
| @@ -400,4 +422,71 @@ IN_PROC_BROWSER_TEST_F(AutofillCounterTest, TimeRanges) { |
| } |
| } |
| +// Test that the counting restarts when autofill sync state changes. |
| +// TODO(crbug.com/553421): Move this to the sync/test/integration directory? |
| +IN_PROC_BROWSER_TEST_F(AutofillCounterTest, RestartOnSyncChange) { |
| + // Set up the Sync client. |
| + ASSERT_TRUE(SetupClients()); |
| + static const int kFirstProfileIndex = 0; |
| + browser_sync::ProfileSyncService* sync_service = |
| + GetSyncService(kFirstProfileIndex); |
| + Profile* profile = GetProfile(kFirstProfileIndex); |
| + // Set up the counter. |
| + browsing_data::AutofillCounter counter( |
| + WebDataServiceFactory::GetAutofillWebDataForProfile( |
| + profile, ServiceAccessType::IMPLICIT_ACCESS), |
| + sync_service); |
| + |
| + counter.Init( |
| + profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| + base::Bind(&AutofillCounterTest::Callback, base::Unretained(this))); |
| + |
| + // Note that some Sync operations notify observers immediately (and thus there |
| + // is no need to call |WaitForCounting()|; in fact, it would block the test), |
| + // while other operations only post the task on UI thread's message loop |
| + // (which requires calling |WaitForCounting()| for them to run). Therefore, |
| + // this test always checks if the callback has already run and only waits |
| + // if it has not. |
| + |
| + // We sync all datatypes by default, so starting Sync means that we start |
| + // syncing autofill, and this should restart the counter. |
| + ASSERT_TRUE(SetupSync()); |
| + ASSERT_TRUE(sync_service->IsSyncActive()); |
| + ASSERT_TRUE(sync_service->GetActiveDataTypes().Has(syncer::AUTOFILL)); |
| + WaitForCountingOrConfirmFinished(); |
| + EXPECT_TRUE(AutofillSyncEnabled()); |
| + |
| + // We stop syncing autofill in particular. This restarts the counter. |
| + syncer::ModelTypeSet everything_except_autofill = |
| + syncer::UserSelectableTypes(); |
| + everything_except_autofill.Remove(syncer::AUTOFILL); |
| + auto sync_blocker = sync_service->GetSetupInProgressHandle(); |
| + sync_service->OnUserChoseDatatypes(/*sync_everything=*/false, |
| + everything_except_autofill); |
| + ASSERT_FALSE(sync_service->GetPreferredDataTypes().Has(syncer::AUTOFILL)); |
| + sync_blocker.reset(); |
| + WaitForCountingOrConfirmFinished(); |
| + ASSERT_FALSE(sync_service->GetActiveDataTypes().Has(syncer::AUTOFILL)); |
| + EXPECT_FALSE(AutofillSyncEnabled()); |
| + |
| + // If autofill sync is not affected, the counter is not restarted. |
| + syncer::ModelTypeSet only_history(syncer::HISTORY_DELETE_DIRECTIVES); |
| + sync_blocker = sync_service->GetSetupInProgressHandle(); |
| + sync_service->ChangePreferredDataTypes(only_history); |
| + sync_blocker.reset(); |
| + EXPECT_FALSE(CountingFinishedSinceLastAsked()); |
| + |
| + // We start syncing autofill again. This restarts the counter. |
| + sync_blocker = sync_service->GetSetupInProgressHandle(); |
| + sync_service->ChangePreferredDataTypes(syncer::ModelTypeSet::All()); |
| + sync_blocker.reset(); |
| + WaitForCountingOrConfirmFinished(); |
| + EXPECT_TRUE(AutofillSyncEnabled()); |
| + |
| + // Stopping the Sync service triggers a restart. |
| + sync_service->RequestStop(syncer::SyncService::CLEAR_DATA); |
| + WaitForCountingOrConfirmFinished(); |
| + EXPECT_FALSE(AutofillSyncEnabled()); |
| +} |
| + |
| } // namespace |