| Index: chrome/browser/browsing_data/passwords_counter_browsertest.cc | 
| diff --git a/chrome/browser/browsing_data/passwords_counter_browsertest.cc b/chrome/browser/browsing_data/passwords_counter_browsertest.cc | 
| index 8f5b994da687be2fdc53987fe361d39cde258687..38d647ff2ac1f1fb04a9720c9cf012e6e887beef 100644 | 
| --- a/chrome/browser/browsing_data/passwords_counter_browsertest.cc | 
| +++ b/chrome/browser/browsing_data/passwords_counter_browsertest.cc | 
| @@ -8,10 +8,14 @@ | 
| #include "base/strings/utf_string_conversions.h" | 
| #include "base/synchronization/waitable_event.h" | 
| #include "chrome/browser/password_manager/password_store_factory.h" | 
| +#include "chrome/browser/sync/profile_sync_service_factory.h" | 
| #include "chrome/browser/sync/test/integration/passwords_helper.h" | 
| +#include "chrome/browser/sync/test/integration/sync_test.h" | 
| #include "chrome/browser/ui/browser.h" | 
| #include "chrome/test/base/in_process_browser_test.h" | 
| #include "components/autofill/core/common/password_form.h" | 
| +#include "components/browser_sync/profile_sync_service.h" | 
| +#include "components/browser_sync/profile_sync_service_mock.h" | 
| #include "components/browsing_data/core/browsing_data_utils.h" | 
| #include "components/browsing_data/core/pref_names.h" | 
| #include "components/prefs/pref_service.h" | 
| @@ -21,9 +25,14 @@ namespace { | 
|  | 
| using autofill::PasswordForm; | 
|  | 
| -class PasswordsCounterTest : public InProcessBrowserTest { | 
| +// TODO(crbug.com/553421): Only RestartOnSyncChange is a SyncTest. | 
| +// Extract it together with HistoryCounterTest.RestartOnSyncChange. | 
| +class PasswordsCounterTest : public SyncTest { | 
| public: | 
| +  PasswordsCounterTest() : SyncTest(SINGLE_CLIENT) {} | 
| + | 
| void SetUpOnMainThread() override { | 
| +    finished_ = false; | 
| time_ = base::Time::Now(); | 
| store_ = PasswordStoreFactory::GetForProfile( | 
| browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS); | 
| @@ -79,23 +88,41 @@ class PasswordsCounterTest : public InProcessBrowserTest { | 
| run_loop_->Run(); | 
| } | 
|  | 
| +  bool CountingFinishedSinceLastAsked() { | 
| +    bool result = finished_; | 
| +    finished_ = false; | 
| +    return result; | 
| +  } | 
| + | 
| +  void WaitForCountingOrConfirmFinished() { | 
| +    if (CountingFinishedSinceLastAsked()) | 
| +      return; | 
| + | 
| +    WaitForCounting(); | 
| +    CountingFinishedSinceLastAsked(); | 
| +  } | 
| + | 
| browsing_data::BrowsingDataCounter::ResultInt GetResult() { | 
| DCHECK(finished_); | 
| return result_; | 
| } | 
|  | 
| +  bool PasswordSyncEnabled() { return password_sync_enabled_; } | 
| + | 
| void Callback( | 
| std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { | 
| +    DCHECK(result); | 
| finished_ = result->Finished(); | 
|  | 
| if (finished_) { | 
| -      result_ = | 
| -          static_cast<browsing_data::BrowsingDataCounter::FinishedResult*>( | 
| -              result.get()) | 
| -              ->Value(); | 
| -    } | 
| +      auto* password_result = | 
| +          static_cast<browsing_data::PasswordsCounter::PasswordResult*>( | 
| +              result.get()); | 
| +      result_ = password_result->Value(); | 
|  | 
| -    if (finished_) | 
| +      password_sync_enabled_ = password_result->password_sync_enabled(); | 
| +    } | 
| +    if (run_loop_ && finished_) | 
| run_loop_->Quit(); | 
| } | 
|  | 
| @@ -125,6 +152,7 @@ class PasswordsCounterTest : public InProcessBrowserTest { | 
|  | 
| bool finished_; | 
| browsing_data::BrowsingDataCounter::ResultInt result_; | 
| +  bool password_sync_enabled_; | 
| }; | 
|  | 
| // Tests that the counter correctly counts each individual credential on | 
| @@ -138,8 +166,10 @@ IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, SameDomain) { | 
| WaitForUICallbacksFromAddingLogins(); | 
|  | 
| Profile* profile = browser()->profile(); | 
| -  browsing_data::PasswordsCounter counter(PasswordStoreFactory::GetForProfile( | 
| -      profile, ServiceAccessType::EXPLICIT_ACCESS)); | 
| +  browsing_data::PasswordsCounter counter( | 
| +      PasswordStoreFactory::GetForProfile(profile, | 
| +                                          ServiceAccessType::EXPLICIT_ACCESS), | 
| +      ProfileSyncServiceFactory::GetForProfile(profile)); | 
| counter.Init( | 
| profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, | 
| base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); | 
| @@ -147,6 +177,7 @@ IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, SameDomain) { | 
|  | 
| WaitForCounting(); | 
| EXPECT_EQ(5u, GetResult()); | 
| +  EXPECT_FALSE(PasswordSyncEnabled()); | 
| } | 
|  | 
| // Tests that the counter doesn't count blacklisted entries. | 
| @@ -157,8 +188,10 @@ IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, Blacklisted) { | 
| WaitForUICallbacksFromAddingLogins(); | 
|  | 
| Profile* profile = browser()->profile(); | 
| -  browsing_data::PasswordsCounter counter(PasswordStoreFactory::GetForProfile( | 
| -      profile, ServiceAccessType::EXPLICIT_ACCESS)); | 
| +  browsing_data::PasswordsCounter counter( | 
| +      PasswordStoreFactory::GetForProfile(profile, | 
| +                                          ServiceAccessType::EXPLICIT_ACCESS), | 
| +      ProfileSyncServiceFactory::GetForProfile(profile)); | 
|  | 
| counter.Init( | 
| profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, | 
| @@ -167,6 +200,7 @@ IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, Blacklisted) { | 
|  | 
| WaitForCounting(); | 
| EXPECT_EQ(1u, GetResult()); | 
| +  EXPECT_FALSE(PasswordSyncEnabled()); | 
| } | 
|  | 
| // Tests that the counter starts counting automatically when the deletion | 
| @@ -178,8 +212,10 @@ IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefChanged) { | 
| WaitForUICallbacksFromAddingLogins(); | 
|  | 
| Profile* profile = browser()->profile(); | 
| -  browsing_data::PasswordsCounter counter(PasswordStoreFactory::GetForProfile( | 
| -      profile, ServiceAccessType::EXPLICIT_ACCESS)); | 
| +  browsing_data::PasswordsCounter counter( | 
| +      PasswordStoreFactory::GetForProfile(profile, | 
| +                                          ServiceAccessType::EXPLICIT_ACCESS), | 
| +      ProfileSyncServiceFactory::GetForProfile(profile)); | 
| counter.Init( | 
| profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, | 
| base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); | 
| @@ -187,6 +223,7 @@ IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefChanged) { | 
|  | 
| WaitForCounting(); | 
| EXPECT_EQ(2u, GetResult()); | 
| +  EXPECT_FALSE(PasswordSyncEnabled()); | 
| } | 
|  | 
| // Tests that the counter starts counting automatically when | 
| @@ -196,8 +233,10 @@ IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, StoreChanged) { | 
| WaitForUICallbacksFromAddingLogins(); | 
|  | 
| Profile* profile = browser()->profile(); | 
| -  browsing_data::PasswordsCounter counter(PasswordStoreFactory::GetForProfile( | 
| -      profile, ServiceAccessType::EXPLICIT_ACCESS)); | 
| +  browsing_data::PasswordsCounter counter( | 
| +      PasswordStoreFactory::GetForProfile(profile, | 
| +                                          ServiceAccessType::EXPLICIT_ACCESS), | 
| +      ProfileSyncServiceFactory::GetForProfile(profile)); | 
| counter.Init( | 
| profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, | 
| base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); | 
| @@ -228,8 +267,10 @@ IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PeriodChanged) { | 
| WaitForUICallbacksFromAddingLogins(); | 
|  | 
| Profile* profile = browser()->profile(); | 
| -  browsing_data::PasswordsCounter counter(PasswordStoreFactory::GetForProfile( | 
| -      profile, ServiceAccessType::EXPLICIT_ACCESS)); | 
| +  browsing_data::PasswordsCounter counter( | 
| +      PasswordStoreFactory::GetForProfile(profile, | 
| +                                          ServiceAccessType::EXPLICIT_ACCESS), | 
| +      ProfileSyncServiceFactory::GetForProfile(profile)); | 
| counter.Init( | 
| profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, | 
| base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); | 
| @@ -255,4 +296,69 @@ IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PeriodChanged) { | 
| EXPECT_EQ(4u, GetResult()); | 
| } | 
|  | 
| +// Test that the counting restarts when password sync state changes. | 
| +// TODO(crbug.com/553421): Move this to the sync/test/integration directory? | 
| +IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, 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::PasswordsCounter counter( | 
| +      PasswordStoreFactory::GetForProfile(profile, | 
| +                                          ServiceAccessType::EXPLICIT_ACCESS), | 
| +      sync_service); | 
| + | 
| +  counter.Init( | 
| +      profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, | 
| +      base::Bind(&PasswordsCounterTest::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 passwords, and this should restart the counter. | 
| +  ASSERT_TRUE(SetupSync()); | 
| +  ASSERT_TRUE(sync_service->IsSyncActive()); | 
| +  ASSERT_TRUE(sync_service->GetPreferredDataTypes().Has(syncer::PASSWORDS)); | 
| +  WaitForCountingOrConfirmFinished(); | 
| +  EXPECT_TRUE(PasswordSyncEnabled()); | 
| + | 
| +  // We stop syncing passwords in particular. This restarts the counter. | 
| +  syncer::ModelTypeSet everything_except_passwords = | 
| +      syncer::ModelTypeSet::All(); | 
| +  everything_except_passwords.Remove(syncer::PASSWORDS); | 
| +  auto sync_blocker = sync_service->GetSetupInProgressHandle(); | 
| +  sync_service->ChangePreferredDataTypes(everything_except_passwords); | 
| +  sync_blocker.reset(); | 
| +  WaitForCountingOrConfirmFinished(); | 
| +  EXPECT_FALSE(PasswordSyncEnabled()); | 
| + | 
| +  // If password sync is not affected, the counter is not restarted. | 
| +  syncer::ModelTypeSet only_history(syncer::HISTORY_DELETE_DIRECTIVES); | 
| +  sync_service->ChangePreferredDataTypes(only_history); | 
| +  sync_blocker = sync_service->GetSetupInProgressHandle(); | 
| +  sync_service->ChangePreferredDataTypes(only_history); | 
| +  sync_blocker.reset(); | 
| +  EXPECT_FALSE(CountingFinishedSinceLastAsked()); | 
| + | 
| +  // We start syncing passwords again. This restarts the counter. | 
| +  sync_blocker = sync_service->GetSetupInProgressHandle(); | 
| +  sync_service->ChangePreferredDataTypes(syncer::ModelTypeSet::All()); | 
| +  sync_blocker.reset(); | 
| +  WaitForCountingOrConfirmFinished(); | 
| +  EXPECT_TRUE(PasswordSyncEnabled()); | 
| + | 
| +  // Stopping the Sync service triggers a restart. | 
| +  sync_service->RequestStop(syncer::SyncService::CLEAR_DATA); | 
| +  WaitForCountingOrConfirmFinished(); | 
| +  EXPECT_FALSE(PasswordSyncEnabled()); | 
| +} | 
| + | 
| }  // namespace | 
|  |