summaryrefslogtreecommitdiffstats
path: root/sync
diff options
context:
space:
mode:
authorzea@chromium.org <zea@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-18 00:42:21 +0000
committerzea@chromium.org <zea@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-18 00:42:21 +0000
commitfda9cb33dea6f4d67afe79939913bb75b30c8313 (patch)
tree14e6c1d202a9a0b68a1b9cbd900cdfa55d6a8d06 /sync
parent4f3c8a4dbaaf25b87ae23dd464fce828ca0c7866 (diff)
downloadchromium_src-fda9cb33dea6f4d67afe79939913bb75b30c8313.zip
chromium_src-fda9cb33dea6f4d67afe79939913bb75b30c8313.tar.gz
chromium_src-fda9cb33dea6f4d67afe79939913bb75b30c8313.tar.bz2
[Sync] Refactor SyncBackendHost unit tests.
We introduce a fake sync manager to let us properly unit test the SyncBackendHost and a bunch of tests verifying Initialize/Configure behavior. BUG=none TEST=self Review URL: https://chromiumcodereview.appspot.com/10790018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@147146 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sync')
-rw-r--r--sync/internal_api/public/test/fake_sync_manager.h129
-rw-r--r--sync/internal_api/test/fake_sync_manager.cc211
-rw-r--r--sync/sync.gyp2
3 files changed, 342 insertions, 0 deletions
diff --git a/sync/internal_api/public/test/fake_sync_manager.h b/sync/internal_api/public/test/fake_sync_manager.h
new file mode 100644
index 0000000..4c1ae91
--- /dev/null
+++ b/sync/internal_api/public/test/fake_sync_manager.h
@@ -0,0 +1,129 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SYNC_INTERNAL_API_PUBLIC_TEST_FAKE_SYNC_MANAGER_H_
+#define SYNC_INTERNAL_API_PUBLIC_TEST_FAKE_SYNC_MANAGER_H_
+
+#include <string>
+
+#include "sync/internal_api/public/sync_manager.h"
+
+#include "base/observer_list.h"
+
+class MessageLoop;
+
+namespace syncer {
+
+class FakeSyncManager : public SyncManager {
+ public:
+ explicit FakeSyncManager();
+ virtual ~FakeSyncManager();
+
+ // The set of types that have initial_sync_ended set to true. This value will
+ // be used by InitialSyncEndedTypes() until the next configuration is
+ // performed.
+ void set_initial_sync_ended_types(syncer::ModelTypeSet types);
+
+ // The set of types that have valid progress markers. This will be used by
+ // GetTypesWithEmptyProgressMarkerToken() until the next configuration is
+ // performed.
+ void set_progress_marker_types(syncer::ModelTypeSet types);
+
+ // The set of types that will fail configuration. Once ConfigureSyncer is
+ // called, the |initial_sync_ended_types_| and
+ // |progress_marker_types_| will be updated to include those types
+ // that didn't fail.
+ void set_configure_fail_types(syncer::ModelTypeSet types);
+
+ // Returns those types that have been cleaned (purged from the directory)
+ // since the last call to GetAndResetCleanedTypes(), or since startup if never
+ // called.
+ syncer::ModelTypeSet GetAndResetCleanedTypes();
+
+ // Returns those types that have been downloaded since the last call to
+ // GetAndResetDownloadedTypes(), or since startup if never called.
+ syncer::ModelTypeSet GetAndResetDownloadedTypes();
+
+ // SyncManager implementation.
+ // Note: we treat whatever message loop this is called from as the sync
+ // loop for purposes of callbacks.
+ virtual bool Init(
+ const FilePath& database_location,
+ const syncer::WeakHandle<syncer::JsEventHandler>& event_handler,
+ const std::string& sync_server_and_path,
+ int sync_server_port,
+ bool use_ssl,
+ const scoped_refptr<base::TaskRunner>& blocking_task_runner,
+ scoped_ptr<HttpPostProviderFactory> post_factory,
+ const syncer::ModelSafeRoutingInfo& model_safe_routing_info,
+ const std::vector<syncer::ModelSafeWorker*>& workers,
+ syncer::ExtensionsActivityMonitor*
+ extensions_activity_monitor,
+ ChangeDelegate* change_delegate,
+ const SyncCredentials& credentials,
+ scoped_ptr<syncer::SyncNotifier> sync_notifier,
+ const std::string& restored_key_for_bootstrapping,
+ TestingMode testing_mode,
+ syncer::Encryptor* encryptor,
+ syncer::UnrecoverableErrorHandler* unrecoverable_error_handler,
+ syncer::ReportUnrecoverableErrorFunction
+ report_unrecoverable_error_function) OVERRIDE;
+ virtual void ThrowUnrecoverableError() OVERRIDE;
+ virtual syncer::ModelTypeSet InitialSyncEndedTypes() OVERRIDE;
+ virtual syncer::ModelTypeSet GetTypesWithEmptyProgressMarkerToken(
+ syncer::ModelTypeSet types) OVERRIDE;
+ virtual bool PurgePartiallySyncedTypes() OVERRIDE;
+ virtual void UpdateCredentials(const SyncCredentials& credentials) OVERRIDE;
+ virtual void UpdateEnabledTypes(const syncer::ModelTypeSet& types) OVERRIDE;
+ virtual void StartSyncingNormally(
+ const syncer::ModelSafeRoutingInfo& routing_info) OVERRIDE;
+ virtual void SetEncryptionPassphrase(const std::string& passphrase,
+ bool is_explicit) OVERRIDE;
+ virtual void SetDecryptionPassphrase(const std::string& passphrase) OVERRIDE;
+ virtual void ConfigureSyncer(
+ ConfigureReason reason,
+ const syncer::ModelTypeSet& types_to_config,
+ const syncer::ModelSafeRoutingInfo& new_routing_info,
+ const base::Closure& ready_task,
+ const base::Closure& retry_task) OVERRIDE;
+ virtual void AddObserver(Observer* observer) OVERRIDE;
+ virtual void RemoveObserver(Observer* observer) OVERRIDE;
+ virtual SyncStatus GetDetailedStatus() const OVERRIDE;
+ virtual bool IsUsingExplicitPassphrase() OVERRIDE;
+ virtual void SaveChanges() OVERRIDE;
+ virtual void StopSyncingForShutdown(const base::Closure& callback) OVERRIDE;
+ virtual void ShutdownOnSyncThread() OVERRIDE;
+ virtual UserShare* GetUserShare() const OVERRIDE;
+ virtual void RefreshNigori(const std::string& chrome_version,
+ const base::Closure& done_callback) OVERRIDE;
+ virtual void EnableEncryptEverything() OVERRIDE;
+ virtual bool ReceivedExperiment(
+ syncer::Experiments* experiments) const OVERRIDE;
+ virtual bool HasUnsyncedItems() const OVERRIDE;
+
+ private:
+ ObserverList<SyncManager::Observer> observers_;
+
+ // Faked directory state.
+ syncer::ModelTypeSet initial_sync_ended_types_;
+ syncer::ModelTypeSet progress_marker_types_;
+
+ // Test specific state.
+ // The types that should fail configuration attempts. These types will not
+ // have their progress markers or initial_sync_ended bits set.
+ syncer::ModelTypeSet configure_fail_types_;
+ // The set of types that have been cleaned up.
+ syncer::ModelTypeSet cleaned_types_;
+ // The set of types that have been downloaded.
+ syncer::ModelTypeSet downloaded_types_;
+
+ // For StopSyncingForShutdown's callback.
+ MessageLoop* sync_loop_;
+
+ DISALLOW_COPY_AND_ASSIGN(FakeSyncManager);
+};
+
+} // namespace syncer
+
+#endif // SYNC_INTERNAL_API_PUBLIC_TEST_FAKE_SYNC_MANAGER_H_
diff --git a/sync/internal_api/test/fake_sync_manager.cc b/sync/internal_api/test/fake_sync_manager.cc
new file mode 100644
index 0000000..ab6bfab
--- /dev/null
+++ b/sync/internal_api/test/fake_sync_manager.cc
@@ -0,0 +1,211 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "sync/internal_api/public/test/fake_sync_manager.h"
+
+#include "base/message_loop.h"
+#include "sync/internal_api/public/http_post_provider_factory.h"
+#include "sync/internal_api/public/util/weak_handle.h"
+#include "sync/notifier/sync_notifier.h"
+
+namespace syncer {
+
+FakeSyncManager::FakeSyncManager() {
+}
+
+FakeSyncManager::~FakeSyncManager() {
+}
+
+void FakeSyncManager::set_initial_sync_ended_types(
+ syncer::ModelTypeSet types) {
+ initial_sync_ended_types_ = types;
+}
+
+void FakeSyncManager::set_progress_marker_types(
+ syncer::ModelTypeSet types) {
+ progress_marker_types_ = types;
+}
+
+void FakeSyncManager::set_configure_fail_types(syncer::ModelTypeSet types) {
+ configure_fail_types_ = types;
+}
+
+syncer::ModelTypeSet FakeSyncManager::GetAndResetCleanedTypes() {
+ syncer::ModelTypeSet cleaned_types = cleaned_types_;
+ cleaned_types_.Clear();
+ return cleaned_types;
+}
+
+syncer::ModelTypeSet FakeSyncManager::GetAndResetDownloadedTypes() {
+ syncer::ModelTypeSet downloaded_types = downloaded_types_;
+ downloaded_types_.Clear();
+ return downloaded_types;
+}
+
+bool FakeSyncManager::Init(
+ const FilePath& database_location,
+ const syncer::WeakHandle<syncer::JsEventHandler>& event_handler,
+ const std::string& sync_server_and_path,
+ int sync_server_port,
+ bool use_ssl,
+ const scoped_refptr<base::TaskRunner>& blocking_task_runner,
+ scoped_ptr<HttpPostProviderFactory> post_factory,
+ const syncer::ModelSafeRoutingInfo& model_safe_routing_info,
+ const std::vector<syncer::ModelSafeWorker*>& workers,
+ syncer::ExtensionsActivityMonitor* extensions_activity_monitor,
+ ChangeDelegate* change_delegate,
+ const SyncCredentials& credentials,
+ scoped_ptr<syncer::SyncNotifier> sync_notifier,
+ const std::string& restored_key_for_bootstrapping,
+ TestingMode testing_mode,
+ syncer::Encryptor* encryptor,
+ syncer::UnrecoverableErrorHandler* unrecoverable_error_handler,
+ syncer::ReportUnrecoverableErrorFunction
+ report_unrecoverable_error_function) {
+ sync_loop_ = MessageLoop::current();
+ PurgePartiallySyncedTypes();
+ FOR_EACH_OBSERVER(SyncManager::Observer, observers_,
+ OnInitializationComplete(
+ syncer::WeakHandle<syncer::JsBackend>(),
+ true));
+ return true;
+}
+
+void FakeSyncManager::ThrowUnrecoverableError() {
+ NOTIMPLEMENTED();
+}
+
+syncer::ModelTypeSet FakeSyncManager::InitialSyncEndedTypes() {
+ return initial_sync_ended_types_;
+}
+
+syncer::ModelTypeSet FakeSyncManager::GetTypesWithEmptyProgressMarkerToken(
+ syncer::ModelTypeSet types) {
+ syncer::ModelTypeSet empty_types = types;
+ empty_types.RemoveAll(progress_marker_types_);
+ return empty_types;
+}
+
+bool FakeSyncManager::PurgePartiallySyncedTypes() {
+ ModelTypeSet partial_types;
+ for (syncer::ModelTypeSet::Iterator i = progress_marker_types_.First();
+ i.Good(); i.Inc()) {
+ if (!initial_sync_ended_types_.Has(i.Get()))
+ partial_types.Put(i.Get());
+ }
+ progress_marker_types_.RemoveAll(partial_types);
+ return true;
+}
+
+void FakeSyncManager::UpdateCredentials(const SyncCredentials& credentials) {
+ NOTIMPLEMENTED();
+}
+
+void FakeSyncManager::UpdateEnabledTypes(const syncer::ModelTypeSet& types) {
+ // Do nothing.
+}
+
+void FakeSyncManager::StartSyncingNormally(
+ const syncer::ModelSafeRoutingInfo& routing_info) {
+ // Do nothing.
+}
+
+void FakeSyncManager::SetEncryptionPassphrase(const std::string& passphrase,
+ bool is_explicit) {
+ NOTIMPLEMENTED();
+}
+
+void FakeSyncManager::SetDecryptionPassphrase(const std::string& passphrase) {
+ NOTIMPLEMENTED();
+}
+
+void FakeSyncManager::ConfigureSyncer(
+ ConfigureReason reason,
+ const syncer::ModelTypeSet& types_to_config,
+ const syncer::ModelSafeRoutingInfo& new_routing_info,
+ const base::Closure& ready_task,
+ const base::Closure& retry_task) {
+ syncer::ModelTypeSet enabled_types = GetRoutingInfoTypes(new_routing_info);
+ syncer::ModelTypeSet disabled_types = Difference(
+ syncer::ModelTypeSet::All(), enabled_types);
+ syncer::ModelTypeSet success_types = types_to_config;
+ success_types.RemoveAll(configure_fail_types_);
+
+ DVLOG(1) << "Faking configuration. Downloading: "
+ << syncer::ModelTypeSetToString(success_types) << ". Cleaning: "
+ << syncer::ModelTypeSetToString(disabled_types);
+
+ // Simulate cleaning up disabled types.
+ // TODO(sync): consider only cleaning those types that were recently disabled,
+ // if this isn't the first cleanup, which more accurately reflects the
+ // behavior of the real cleanup logic.
+ initial_sync_ended_types_.RemoveAll(disabled_types);
+ progress_marker_types_.RemoveAll(disabled_types);
+ cleaned_types_.PutAll(disabled_types);
+
+ // Now simulate the actual configuration for those types that successfully
+ // download + apply.
+ progress_marker_types_.PutAll(success_types);
+ initial_sync_ended_types_.PutAll(success_types);
+ downloaded_types_.PutAll(success_types);
+
+ ready_task.Run();
+}
+
+void FakeSyncManager::AddObserver(Observer* observer) {
+ observers_.AddObserver(observer);
+}
+
+void FakeSyncManager::RemoveObserver(Observer* observer) {
+ observers_.RemoveObserver(observer);
+}
+
+SyncStatus FakeSyncManager::GetDetailedStatus() const {
+ NOTIMPLEMENTED();
+ return SyncStatus();
+}
+
+bool FakeSyncManager::IsUsingExplicitPassphrase() {
+ NOTIMPLEMENTED();
+ return false;
+}
+
+void FakeSyncManager::SaveChanges() {
+ // Do nothing.
+}
+
+void FakeSyncManager::StopSyncingForShutdown(const base::Closure& callback) {
+ sync_loop_->PostTask(FROM_HERE, callback);
+}
+
+void FakeSyncManager::ShutdownOnSyncThread() {
+ // Do nothing.
+}
+
+UserShare* FakeSyncManager::GetUserShare() const {
+ NOTIMPLEMENTED();
+ return NULL;
+}
+
+void FakeSyncManager::RefreshNigori(const std::string& chrome_version,
+ const base::Closure& done_callback) {
+ done_callback.Run();
+}
+
+void FakeSyncManager::EnableEncryptEverything() {
+ NOTIMPLEMENTED();
+}
+
+bool FakeSyncManager::ReceivedExperiment(
+ syncer::Experiments* experiments) const {
+ return false;
+}
+
+bool FakeSyncManager::HasUnsyncedItems() const {
+ NOTIMPLEMENTED();
+ return false;
+}
+
+} // namespace syncer
+
diff --git a/sync/sync.gyp b/sync/sync.gyp
index f4286fb..d88327b 100644
--- a/sync/sync.gyp
+++ b/sync/sync.gyp
@@ -469,8 +469,10 @@
'test_support_sync',
],
'sources': [
+ 'internal_api/public/test/fake_sync_manager.h',
'internal_api/public/test/test_entry_factory.h',
'internal_api/public/test/test_user_share.h',
+ 'internal_api/test/fake_sync_manager.cc',
'internal_api/test/test_entry_factory.cc',
'internal_api/test/test_user_share.cc',
],