Commit a0b8f6fc authored by Marc Treib's avatar Marc Treib Committed by Commit Bot

Various small cleanups in ProfileSyncServiceHarness

- Update some comments for accuracy
- Remove unused methods and includes
- Add some "const"s

Bug: none
Change-Id: Ia45237757b101af7ff0886dadff9ef9e622a2cc2
Reviewed-on: https://chromium-review.googlesource.com/1208526
Commit-Queue: Marc Treib <treib@chromium.org>
Reviewed-by: default avatarMohamed Amir Yosef <mamir@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589141}
parent a754378f
...@@ -5,14 +5,11 @@ ...@@ -5,14 +5,11 @@
#include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
#include <cstddef> #include <cstddef>
#include <iterator>
#include <ostream>
#include <sstream> #include <sstream>
#include "base/json/json_writer.h" #include "base/json/json_writer.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h" #include "chrome/browser/signin/identity_manager_factory.h"
...@@ -28,16 +25,11 @@ ...@@ -28,16 +25,11 @@
#include "chrome/browser/ui/webui/signin/login_ui_test_utils.h" #include "chrome/browser/ui/webui/signin/login_ui_test_utils.h"
#include "chrome/browser/unified_consent/unified_consent_service_factory.h" #include "chrome/browser/unified_consent/unified_consent_service_factory.h"
#include "chrome/common/channel_info.h" #include "chrome/common/channel_info.h"
#include "chrome/common/chrome_switches.h"
#include "components/invalidation/impl/p2p_invalidation_service.h"
#include "components/signin/core/browser/signin_manager.h" #include "components/signin/core/browser/signin_manager.h"
#include "components/signin/core/browser/signin_metrics.h"
#include "components/sync/base/progress_marker_map.h"
#include "components/sync/driver/about_sync_util.h" #include "components/sync/driver/about_sync_util.h"
#include "components/sync/engine/sync_string_conversions.h" #include "components/sync/engine/sync_string_conversions.h"
#include "components/unified_consent/feature.h" #include "components/unified_consent/feature.h"
#include "components/unified_consent/unified_consent_service.h" #include "components/unified_consent/unified_consent_service.h"
#include "google_apis/gaia/gaia_constants.h"
#include "services/identity/public/cpp/identity_manager.h" #include "services/identity/public/cpp/identity_manager.h"
#include "services/identity/public/cpp/identity_test_utils.h" #include "services/identity/public/cpp/identity_test_utils.h"
...@@ -124,34 +116,10 @@ ProfileSyncServiceHarness::ProfileSyncServiceHarness( ...@@ -124,34 +116,10 @@ ProfileSyncServiceHarness::ProfileSyncServiceHarness(
username_(username), username_(username),
password_(password), password_(password),
signin_type_(signin_type), signin_type_(signin_type),
oauth2_refesh_token_number_(0),
profile_debug_name_(profile->GetDebugName()) {} profile_debug_name_(profile->GetDebugName()) {}
ProfileSyncServiceHarness::~ProfileSyncServiceHarness() { } ProfileSyncServiceHarness::~ProfileSyncServiceHarness() { }
bool ProfileSyncServiceHarness::SetupSync() {
bool result = SetupSync(syncer::UserSelectableTypes(), false);
if (!result) {
LOG(ERROR) << profile_debug_name_ << ": SetupSync failed. Syncer status:\n"
<< GetServiceStatus();
} else {
DVLOG(1) << profile_debug_name_ << ": SetupSync successful.";
}
return result;
}
bool ProfileSyncServiceHarness::SetupSyncForClearingServerData() {
bool result = SetupSync(syncer::UserSelectableTypes(), true);
if (!result) {
LOG(ERROR) << profile_debug_name_
<< ": SetupSyncForClear failed. Syncer status:\n"
<< GetServiceStatus();
} else {
DVLOG(1) << profile_debug_name_ << ": SetupSyncForClear successful.";
}
return result;
}
bool ProfileSyncServiceHarness::SignIn() { bool ProfileSyncServiceHarness::SignIn() {
// TODO(crbug.com/871221): This function should distinguish primary account // TODO(crbug.com/871221): This function should distinguish primary account
// (aka sync account) from secondary accounts (content area signin). Let's // (aka sync account) from secondary accounts (content area signin). Let's
...@@ -203,12 +171,34 @@ bool ProfileSyncServiceHarness::SignIn() { ...@@ -203,12 +171,34 @@ bool ProfileSyncServiceHarness::SignIn() {
return false; return false;
} }
bool ProfileSyncServiceHarness::SetupSync() {
bool result = SetupSync(syncer::UserSelectableTypes(), false);
if (!result) {
LOG(ERROR) << profile_debug_name_ << ": SetupSync failed. Syncer status:\n"
<< GetServiceStatus();
} else {
DVLOG(1) << profile_debug_name_ << ": SetupSync successful.";
}
return result;
}
bool ProfileSyncServiceHarness::SetupSyncForClearingServerData() {
bool result = SetupSync(syncer::UserSelectableTypes(), true);
if (!result) {
LOG(ERROR) << profile_debug_name_
<< ": SetupSyncForClear failed. Syncer status:\n"
<< GetServiceStatus();
} else {
DVLOG(1) << profile_debug_name_ << ": SetupSyncForClear successful.";
}
return result;
}
bool ProfileSyncServiceHarness::SetupSync(syncer::ModelTypeSet synced_datatypes, bool ProfileSyncServiceHarness::SetupSync(syncer::ModelTypeSet synced_datatypes,
bool skip_passphrase_verification) { bool skip_passphrase_verification) {
DCHECK(!profile_->IsLegacySupervised()) DCHECK(!profile_->IsLegacySupervised())
<< "SetupSync should not be used for legacy supervised users."; << "SetupSync should not be used for legacy supervised users.";
// Initialize the sync client's profile sync service object.
if (service() == nullptr) { if (service() == nullptr) {
LOG(ERROR) << "SetupSync(): service() is null."; LOG(ERROR) << "SetupSync(): service() is null.";
return false; return false;
...@@ -235,7 +225,7 @@ bool ProfileSyncServiceHarness::SetupSync(syncer::ModelTypeSet synced_datatypes, ...@@ -235,7 +225,7 @@ bool ProfileSyncServiceHarness::SetupSync(syncer::ModelTypeSet synced_datatypes,
// When unified consent given is set to |true|, the unified consent service // When unified consent given is set to |true|, the unified consent service
// enables syncing all datatypes. // enables syncing all datatypes.
UnifiedConsentServiceFactory::GetForProfile(profile_) UnifiedConsentServiceFactory::GetForProfile(profile_)
->SetUnifiedConsentGiven(sync_everything); ->SetUnifiedConsentGiven(sync_everything);
if (!sync_everything) { if (!sync_everything) {
service()->OnUserChoseDatatypes(sync_everything, synced_datatypes); service()->OnUserChoseDatatypes(sync_everything, synced_datatypes);
} }
...@@ -291,6 +281,11 @@ bool ProfileSyncServiceHarness::SetupSync(syncer::ModelTypeSet synced_datatypes, ...@@ -291,6 +281,11 @@ bool ProfileSyncServiceHarness::SetupSync(syncer::ModelTypeSet synced_datatypes,
return true; return true;
} }
void ProfileSyncServiceHarness::FinishSyncSetup() {
sync_blocker_.reset();
service()->SetFirstSetupComplete();
}
void ProfileSyncServiceHarness::StopSyncService( void ProfileSyncServiceHarness::StopSyncService(
syncer::SyncService::SyncStopDataFate data_fate) { syncer::SyncService::SyncStopDataFate data_fate) {
DVLOG(1) << "Requesting stop for service."; DVLOG(1) << "Requesting stop for service.";
...@@ -334,11 +329,10 @@ bool ProfileSyncServiceHarness::StartSyncService() { ...@@ -334,11 +329,10 @@ bool ProfileSyncServiceHarness::StartSyncService() {
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
void ProfileSyncServiceHarness::SignoutSyncService() { void ProfileSyncServiceHarness::SignoutSyncService() {
DCHECK(!username_.empty()); DCHECK(!username_.empty());
// TODO(https://crbug.com/806781): This should go through IdentityManager once identity::ClearPrimaryAccount(
// that supports sign-out. SigninManagerFactory::GetForProfile(profile_),
SigninManagerFactory::GetForProfile(profile_)->SignOutAndRemoveAllAccounts( IdentityManagerFactory::GetForProfile(profile_),
signin_metrics::SIGNOUT_TEST, identity::ClearPrimaryAccountPolicy::REMOVE_ALL_ACCOUNTS);
signin_metrics::SignoutDelete::IGNORE_METRIC);
} }
#endif // !OS_CHROMEOS #endif // !OS_CHROMEOS
...@@ -362,19 +356,14 @@ bool ProfileSyncServiceHarness::AwaitMutualSyncCycleCompletion( ...@@ -362,19 +356,14 @@ bool ProfileSyncServiceHarness::AwaitMutualSyncCycleCompletion(
return AwaitQuiescence(harnesses); return AwaitQuiescence(harnesses);
} }
bool ProfileSyncServiceHarness::AwaitGroupSyncCycleCompletion(
const std::vector<ProfileSyncServiceHarness*>& partners) {
return AwaitQuiescence(partners);
}
// static // static
bool ProfileSyncServiceHarness::AwaitQuiescence( bool ProfileSyncServiceHarness::AwaitQuiescence(
const std::vector<ProfileSyncServiceHarness*>& clients) { const std::vector<ProfileSyncServiceHarness*>& clients) {
std::vector<ProfileSyncService*> services;
if (clients.empty()) { if (clients.empty()) {
return true; return true;
} }
std::vector<ProfileSyncService*> services;
for (const ProfileSyncServiceHarness* harness : clients) { for (const ProfileSyncServiceHarness* harness : clients) {
services.push_back(harness->service()); services.push_back(harness->service());
} }
...@@ -435,30 +424,6 @@ bool ProfileSyncServiceHarness::AwaitSyncSetupCompletion( ...@@ -435,30 +424,6 @@ bool ProfileSyncServiceHarness::AwaitSyncSetupCompletion(
return true; return true;
} }
std::string ProfileSyncServiceHarness::GenerateFakeOAuth2RefreshTokenString() {
return base::StringPrintf("oauth2_refresh_token_%d",
++oauth2_refesh_token_number_);
}
bool ProfileSyncServiceHarness::IsSyncEnabledByUser() const {
return service()->IsFirstSetupComplete() &&
!service()->HasDisableReason(
ProfileSyncService::DISABLE_REASON_USER_CHOICE);
}
void ProfileSyncServiceHarness::FinishSyncSetup() {
sync_blocker_.reset();
service()->SetFirstSetupComplete();
}
SyncCycleSnapshot ProfileSyncServiceHarness::GetLastCycleSnapshot() const {
DCHECK(service() != nullptr) << "Sync service has not yet been set up.";
if (service()->IsSyncActive()) {
return service()->GetLastCycleSnapshot();
}
return SyncCycleSnapshot();
}
bool ProfileSyncServiceHarness::EnableSyncForDatatype( bool ProfileSyncServiceHarness::EnableSyncForDatatype(
syncer::ModelType datatype) { syncer::ModelType datatype) {
DVLOG(1) << GetClientInfoString( DVLOG(1) << GetClientInfoString(
...@@ -591,6 +556,24 @@ bool ProfileSyncServiceHarness::DisableSyncForAllDatatypes() { ...@@ -591,6 +556,24 @@ bool ProfileSyncServiceHarness::DisableSyncForAllDatatypes() {
return true; return true;
} }
SyncCycleSnapshot ProfileSyncServiceHarness::GetLastCycleSnapshot() const {
DCHECK(service() != nullptr) << "Sync service has not yet been set up.";
if (service()->IsSyncActive()) {
return service()->GetLastCycleSnapshot();
}
return SyncCycleSnapshot();
}
std::string ProfileSyncServiceHarness::GetServiceStatus() {
std::unique_ptr<base::DictionaryValue> value(
syncer::sync_ui_util::ConstructAboutInformation(service(),
chrome::GetChannel()));
std::string service_status;
base::JSONWriter::WriteWithOptions(
*value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &service_status);
return service_status;
}
// TODO(sync): Clean up this method in a separate CL. Remove all snapshot fields // TODO(sync): Clean up this method in a separate CL. Remove all snapshot fields
// and log shorter, more meaningful messages. // and log shorter, more meaningful messages.
std::string ProfileSyncServiceHarness::GetClientInfoString( std::string ProfileSyncServiceHarness::GetClientInfoString(
...@@ -622,16 +605,8 @@ std::string ProfileSyncServiceHarness::GetClientInfoString( ...@@ -622,16 +605,8 @@ std::string ProfileSyncServiceHarness::GetClientInfoString(
return os.str(); return os.str();
} }
bool ProfileSyncServiceHarness::IsTypePreferred(syncer::ModelType type) { bool ProfileSyncServiceHarness::IsSyncEnabledByUser() const {
return service()->GetPreferredDataTypes().Has(type); return service()->IsFirstSetupComplete() &&
} !service()->HasDisableReason(
ProfileSyncService::DISABLE_REASON_USER_CHOICE);
std::string ProfileSyncServiceHarness::GetServiceStatus() {
std::unique_ptr<base::DictionaryValue> value(
syncer::sync_ui_util::ConstructAboutInformation(service(),
chrome::GetChannel()));
std::string service_status;
base::JSONWriter::WriteWithOptions(
*value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &service_status);
return service_status;
} }
...@@ -41,29 +41,33 @@ class ProfileSyncServiceHarness { ...@@ -41,29 +41,33 @@ class ProfileSyncServiceHarness {
const std::string& username, const std::string& username,
const std::string& password, const std::string& password,
SigninType signin_type); SigninType signin_type);
virtual ~ProfileSyncServiceHarness(); ~ProfileSyncServiceHarness();
// Creates a ProfileSyncService for the profile passed at construction and // Signs in to a primary account without actually enabling sync the feature.
// signs in without actually enabling sync the feature. // TODO(treib): Rename this to SignInToPrimaryAccount.
bool SignIn(); bool SignIn();
// Creates a ProfileSyncService for the profile passed at construction and // Enables and configures sync for all available datatypes. Returns true only
// enables sync for all available datatypes. Returns true only after sync has // after sync has been fully initialized and authenticated, and we are ready
// been fully initialized and authenticated, and we are ready to process // to process changes.
// changes.
bool SetupSync(); bool SetupSync();
// Setup sync without the authenticating through the passphrase encryption. // Sets up sync without authenticating through the passphrase encryption.
// Use this method when you need to setup a client that you're going to call // Use this method when you need to setup a client that you're going to call
// StopSyncService(), StartSyncService() directly after. // StopSyncService(), StartSyncService() directly after.
bool SetupSyncForClearingServerData(); bool SetupSyncForClearingServerData();
// Both SetupSync and SetupSyncForClear call into this method. // Both SetupSync and SetupSyncForClearingServerData call into this method.
// Same as the above method, but enables sync only for the datatypes contained // Same as the above method, but enables sync only for the datatypes contained
// in |synced_datatypes|. // in |synced_datatypes|.
bool SetupSync(syncer::ModelTypeSet synced_datatypes, bool SetupSync(syncer::ModelTypeSet synced_datatypes,
bool skip_passphrase_verification = false); bool skip_passphrase_verification = false);
// Signals that sync setup is complete, and that PSS may begin syncing.
// Typically SetupSync does this automatically, but if that returned false,
// then setup may have been left incomplete.
void FinishSyncSetup();
// Methods to stop and restart the sync service. // Methods to stop and restart the sync service.
// //
// For example, this can be used to simulate a sign-in/sign-out or can be // For example, this can be used to simulate a sign-in/sign-out or can be
...@@ -80,8 +84,10 @@ class ProfileSyncServiceHarness { ...@@ -80,8 +84,10 @@ class ProfileSyncServiceHarness {
bool StartSyncService(); bool StartSyncService();
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
// Sign out of sync service. ChromeOS doesn't have the concept of sign-out, // Signs out of the primary account. ChromeOS doesn't have the concept of
// so this only exists on other platforms. // sign-out, so this only exists on other platforms.
// TODO(treib): Rename this to SignOut() - it does nothing with the
// SyncService.
void SignoutSyncService(); void SignoutSyncService();
#endif // !OS_CHROMEOS #endif // !OS_CHROMEOS
...@@ -99,13 +105,6 @@ class ProfileSyncServiceHarness { ...@@ -99,13 +105,6 @@ class ProfileSyncServiceHarness {
// exactly one client is waiting to receive those changes. // exactly one client is waiting to receive those changes.
bool AwaitMutualSyncCycleCompletion(ProfileSyncServiceHarness* partner); bool AwaitMutualSyncCycleCompletion(ProfileSyncServiceHarness* partner);
// Blocks the caller until |this| completes its ongoing sync cycle and every
// other client in |partners| have achieved identical download progresses.
// Note: Use this method when exactly one client makes local change(s),
// and more than one client is waiting to receive those changes.
bool AwaitGroupSyncCycleCompletion(
const std::vector<ProfileSyncServiceHarness*>& partners);
// Blocks the caller until every client in |clients| completes its ongoing // Blocks the caller until every client in |clients| completes its ongoing
// sync cycle and all the clients' progress markers match. Note: Use this // sync cycle and all the clients' progress markers match. Note: Use this
// method when more than one client makes local change(s), and more than one // method when more than one client makes local change(s), and more than one
...@@ -145,21 +144,6 @@ class ProfileSyncServiceHarness { ...@@ -145,21 +144,6 @@ class ProfileSyncServiceHarness {
// Returns a snapshot of the current sync session. // Returns a snapshot of the current sync session.
syncer::SyncCycleSnapshot GetLastCycleSnapshot() const; syncer::SyncCycleSnapshot GetLastCycleSnapshot() const;
// Check if |type| is being synced.
bool IsTypePreferred(syncer::ModelType type);
// Returns a string that can be used as the value of an oauth2 refresh token.
// This function guarantees that a different string is returned each time
// it is called.
std::string GenerateFakeOAuth2RefreshTokenString();
// Returns a string with relevant info about client's sync state (if
// available), annotated with |message|. Useful for logging.
std::string GetClientInfoString(const std::string& message) const;
// Signals that sync setup is complete, and that PSS may begin syncing.
void FinishSyncSetup();
private: private:
ProfileSyncServiceHarness(Profile* profile, ProfileSyncServiceHarness(Profile* profile,
const std::string& username, const std::string& username,
...@@ -169,30 +153,30 @@ class ProfileSyncServiceHarness { ...@@ -169,30 +153,30 @@ class ProfileSyncServiceHarness {
// Gets detailed status from |service_| in pretty-printable form. // Gets detailed status from |service_| in pretty-printable form.
std::string GetServiceStatus(); std::string GetServiceStatus();
// Returns a string with relevant info about client's sync state (if
// available), annotated with |message|. Useful for logging.
std::string GetClientInfoString(const std::string& message) const;
// Returns true if the user has enabled and configured sync for this client. // Returns true if the user has enabled and configured sync for this client.
// Note that this does not imply sync is actually running. // Note that this does not imply sync is actually running.
bool IsSyncEnabledByUser() const; bool IsSyncEnabledByUser() const;
// Sync profile associated with this sync client. // Profile associated with this sync client.
Profile* profile_; Profile* const profile_;
// ProfileSyncService object associated with |profile_|. // ProfileSyncService object associated with |profile_|.
browser_sync::ProfileSyncService* service_; browser_sync::ProfileSyncService* const service_;
// Prevents Sync from running until configuration is complete. // Prevents Sync from running until configuration is complete.
std::unique_ptr<syncer::SyncSetupInProgressHandle> sync_blocker_; std::unique_ptr<syncer::SyncSetupInProgressHandle> sync_blocker_;
// Credentials used for GAIA authentication. // Credentials used for GAIA authentication.
std::string username_; const std::string username_;
std::string password_; const std::string password_;
// Used to decide what method of profile signin to use. // Used to decide what method of profile signin to use.
const SigninType signin_type_; const SigninType signin_type_;
// Number used by GenerateFakeOAuth2RefreshTokenString() to make sure that
// all refresh tokens used in the tests are different.
int oauth2_refesh_token_number_;
// Used for logging. // Used for logging.
const std::string profile_debug_name_; const std::string profile_debug_name_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment