Home | History | Annotate | Download | only in test
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "sync/internal_api/public/test/fake_sync_manager.h"
      6 
      7 #include <cstddef>
      8 
      9 #include "base/bind.h"
     10 #include "base/bind_helpers.h"
     11 #include "base/location.h"
     12 #include "base/logging.h"
     13 #include "base/run_loop.h"
     14 #include "base/sequenced_task_runner.h"
     15 #include "base/single_thread_task_runner.h"
     16 #include "base/thread_task_runner_handle.h"
     17 #include "sync/internal_api/public/base/invalidator_state.h"
     18 #include "sync/internal_api/public/http_post_provider_factory.h"
     19 #include "sync/internal_api/public/internal_components_factory.h"
     20 #include "sync/internal_api/public/util/weak_handle.h"
     21 #include "sync/notifier/invalidator.h"
     22 #include "sync/notifier/object_id_invalidation_map.h"
     23 #include "sync/syncable/directory.h"
     24 #include "sync/test/fake_sync_encryption_handler.h"
     25 
     26 namespace syncer {
     27 
     28 FakeSyncManager::FakeSyncManager(ModelTypeSet initial_sync_ended_types,
     29                                  ModelTypeSet progress_marker_types,
     30                                  ModelTypeSet configure_fail_types) :
     31     initial_sync_ended_types_(initial_sync_ended_types),
     32     progress_marker_types_(progress_marker_types),
     33     configure_fail_types_(configure_fail_types),
     34     last_configure_reason_(CONFIGURE_REASON_UNKNOWN) {
     35   fake_encryption_handler_.reset(new FakeSyncEncryptionHandler());
     36 }
     37 
     38 FakeSyncManager::~FakeSyncManager() {}
     39 
     40 ModelTypeSet FakeSyncManager::GetAndResetCleanedTypes() {
     41   ModelTypeSet cleaned_types = cleaned_types_;
     42   cleaned_types_.Clear();
     43   return cleaned_types;
     44 }
     45 
     46 ModelTypeSet FakeSyncManager::GetAndResetDownloadedTypes() {
     47   ModelTypeSet downloaded_types = downloaded_types_;
     48   downloaded_types_.Clear();
     49   return downloaded_types;
     50 }
     51 
     52 ModelTypeSet FakeSyncManager::GetAndResetEnabledTypes() {
     53   ModelTypeSet enabled_types = enabled_types_;
     54   enabled_types_.Clear();
     55   return enabled_types;
     56 }
     57 
     58 ConfigureReason FakeSyncManager::GetAndResetConfigureReason() {
     59   ConfigureReason reason = last_configure_reason_;
     60   last_configure_reason_ = CONFIGURE_REASON_UNKNOWN;
     61   return reason;
     62 }
     63 
     64 void FakeSyncManager::WaitForSyncThread() {
     65   // Post a task to |sync_task_runner_| and block until it runs.
     66   base::RunLoop run_loop;
     67   if (!sync_task_runner_->PostTaskAndReply(
     68       FROM_HERE,
     69       base::Bind(&base::DoNothing),
     70       run_loop.QuitClosure())) {
     71     NOTREACHED();
     72   }
     73   run_loop.Run();
     74 }
     75 
     76 void FakeSyncManager::Init(
     77     const base::FilePath& database_location,
     78     const WeakHandle<JsEventHandler>& event_handler,
     79     const std::string& sync_server_and_path,
     80     int sync_server_port,
     81     bool use_ssl,
     82     scoped_ptr<HttpPostProviderFactory> post_factory,
     83     const std::vector<scoped_refptr<ModelSafeWorker> >& workers,
     84     ExtensionsActivity* extensions_activity,
     85     ChangeDelegate* change_delegate,
     86     const SyncCredentials& credentials,
     87     const std::string& invalidator_client_id,
     88     const std::string& restored_key_for_bootstrapping,
     89     const std::string& restored_keystore_key_for_bootstrapping,
     90     InternalComponentsFactory* internal_components_factory,
     91     Encryptor* encryptor,
     92     scoped_ptr<UnrecoverableErrorHandler> unrecoverable_error_handler,
     93     ReportUnrecoverableErrorFunction report_unrecoverable_error_function,
     94     CancelationSignal* cancelation_signal) {
     95   sync_task_runner_ = base::ThreadTaskRunnerHandle::Get();
     96   PurgePartiallySyncedTypes();
     97 
     98   test_user_share_.SetUp();
     99   UserShare* share = test_user_share_.user_share();
    100   for (ModelTypeSet::Iterator it = initial_sync_ended_types_.First();
    101        it.Good(); it.Inc()) {
    102     TestUserShare::CreateRoot(it.Get(), share);
    103   }
    104 
    105   FOR_EACH_OBSERVER(SyncManager::Observer, observers_,
    106                     OnInitializationComplete(
    107                         WeakHandle<JsBackend>(),
    108                         WeakHandle<DataTypeDebugInfoListener>(),
    109                         true, initial_sync_ended_types_));
    110 }
    111 
    112 ModelTypeSet FakeSyncManager::InitialSyncEndedTypes() {
    113   return initial_sync_ended_types_;
    114 }
    115 
    116 ModelTypeSet FakeSyncManager::GetTypesWithEmptyProgressMarkerToken(
    117     ModelTypeSet types) {
    118   ModelTypeSet empty_types = types;
    119   empty_types.RemoveAll(progress_marker_types_);
    120   return empty_types;
    121 }
    122 
    123 bool FakeSyncManager::PurgePartiallySyncedTypes() {
    124   ModelTypeSet partial_types;
    125   for (ModelTypeSet::Iterator i = progress_marker_types_.First();
    126        i.Good(); i.Inc()) {
    127     if (!initial_sync_ended_types_.Has(i.Get()))
    128       partial_types.Put(i.Get());
    129   }
    130   progress_marker_types_.RemoveAll(partial_types);
    131   cleaned_types_.PutAll(partial_types);
    132   return true;
    133 }
    134 
    135 void FakeSyncManager::UpdateCredentials(const SyncCredentials& credentials) {
    136   NOTIMPLEMENTED();
    137 }
    138 
    139 void FakeSyncManager::StartSyncingNormally(
    140       const ModelSafeRoutingInfo& routing_info) {
    141   // Do nothing.
    142 }
    143 
    144 void FakeSyncManager::ConfigureSyncer(
    145     ConfigureReason reason,
    146     ModelTypeSet to_download,
    147     ModelTypeSet to_purge,
    148     ModelTypeSet to_journal,
    149     ModelTypeSet to_unapply,
    150     const ModelSafeRoutingInfo& new_routing_info,
    151     const base::Closure& ready_task,
    152     const base::Closure& retry_task) {
    153   last_configure_reason_ = reason;
    154   enabled_types_ = GetRoutingInfoTypes(new_routing_info);
    155   ModelTypeSet success_types = to_download;
    156   success_types.RemoveAll(configure_fail_types_);
    157 
    158   DVLOG(1) << "Faking configuration. Downloading: "
    159            << ModelTypeSetToString(success_types) << ". Cleaning: "
    160            << ModelTypeSetToString(to_purge);
    161 
    162   // Update our fake directory by clearing and fake-downloading as necessary.
    163   UserShare* share = GetUserShare();
    164   share->directory->PurgeEntriesWithTypeIn(to_purge,
    165                                            to_journal,
    166                                            to_unapply);
    167   for (ModelTypeSet::Iterator it = success_types.First(); it.Good(); it.Inc()) {
    168     // We must be careful to not create the same root node twice.
    169     if (!initial_sync_ended_types_.Has(it.Get())) {
    170       TestUserShare::CreateRoot(it.Get(), share);
    171     }
    172   }
    173 
    174   // Simulate cleaning up disabled types.
    175   // TODO(sync): consider only cleaning those types that were recently disabled,
    176   // if this isn't the first cleanup, which more accurately reflects the
    177   // behavior of the real cleanup logic.
    178   initial_sync_ended_types_.RemoveAll(to_purge);
    179   progress_marker_types_.RemoveAll(to_purge);
    180   cleaned_types_.PutAll(to_purge);
    181 
    182   // Now simulate the actual configuration for those types that successfully
    183   // download + apply.
    184   progress_marker_types_.PutAll(success_types);
    185   initial_sync_ended_types_.PutAll(success_types);
    186   downloaded_types_.PutAll(success_types);
    187 
    188   ready_task.Run();
    189 }
    190 
    191 void FakeSyncManager::AddObserver(Observer* observer) {
    192   observers_.AddObserver(observer);
    193 }
    194 
    195 void FakeSyncManager::RemoveObserver(Observer* observer) {
    196   observers_.RemoveObserver(observer);
    197 }
    198 
    199 SyncStatus FakeSyncManager::GetDetailedStatus() const {
    200   NOTIMPLEMENTED();
    201   return SyncStatus();
    202 }
    203 
    204 void FakeSyncManager::SaveChanges() {
    205   // Do nothing.
    206 }
    207 
    208 void FakeSyncManager::ShutdownOnSyncThread() {
    209   DCHECK(sync_task_runner_->RunsTasksOnCurrentThread());
    210   test_user_share_.TearDown();
    211 }
    212 
    213 UserShare* FakeSyncManager::GetUserShare() {
    214   return test_user_share_.user_share();
    215 }
    216 
    217 syncer::SyncCoreProxy* FakeSyncManager::GetSyncCoreProxy() {
    218   return &null_sync_core_proxy_;
    219 }
    220 
    221 const std::string FakeSyncManager::cache_guid() {
    222   return test_user_share_.user_share()->directory->cache_guid();
    223 }
    224 
    225 bool FakeSyncManager::ReceivedExperiment(Experiments* experiments) {
    226   return false;
    227 }
    228 
    229 bool FakeSyncManager::HasUnsyncedItems() {
    230   NOTIMPLEMENTED();
    231   return false;
    232 }
    233 
    234 SyncEncryptionHandler* FakeSyncManager::GetEncryptionHandler() {
    235   return fake_encryption_handler_.get();
    236 }
    237 
    238 ScopedVector<syncer::ProtocolEvent>
    239 FakeSyncManager::GetBufferedProtocolEvents() {
    240   return ScopedVector<syncer::ProtocolEvent>();
    241 }
    242 
    243 scoped_ptr<base::ListValue> FakeSyncManager::GetAllNodesForType(
    244     syncer::ModelType type) {
    245   return scoped_ptr<base::ListValue>(new base::ListValue());
    246 }
    247 
    248 void FakeSyncManager::RefreshTypes(ModelTypeSet types) {
    249   last_refresh_request_types_ = types;
    250 }
    251 
    252 void FakeSyncManager::RegisterDirectoryTypeDebugInfoObserver(
    253     syncer::TypeDebugInfoObserver* observer) {}
    254 
    255 void FakeSyncManager::UnregisterDirectoryTypeDebugInfoObserver(
    256     syncer::TypeDebugInfoObserver* observer) {}
    257 
    258 bool FakeSyncManager::HasDirectoryTypeDebugInfoObserver(
    259     syncer::TypeDebugInfoObserver* observer) {
    260   return false;
    261 }
    262 
    263 void FakeSyncManager::RequestEmitDebugInfo() {}
    264 
    265 void FakeSyncManager::OnIncomingInvalidation(
    266       const ObjectIdInvalidationMap& invalidation_map) {
    267   // Do nothing.
    268 }
    269 
    270 ModelTypeSet FakeSyncManager::GetLastRefreshRequestTypes() {
    271   return last_refresh_request_types_;
    272 }
    273 
    274 void FakeSyncManager::OnInvalidatorStateChange(InvalidatorState state) {
    275   // Do nothing.
    276 }
    277 
    278 std::string FakeSyncManager::GetOwnerName() const { return "FakeSyncManager"; }
    279 
    280 }  // namespace syncer
    281