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/http_post_provider_factory.h"
     18 #include "sync/internal_api/public/internal_components_factory.h"
     19 #include "sync/internal_api/public/util/weak_handle.h"
     20 #include "sync/syncable/directory.h"
     21 #include "sync/test/fake_sync_encryption_handler.h"
     22 
     23 class GURL;
     24 
     25 namespace syncer {
     26 
     27 FakeSyncManager::FakeSyncManager(ModelTypeSet initial_sync_ended_types,
     28                                  ModelTypeSet progress_marker_types,
     29                                  ModelTypeSet configure_fail_types) :
     30     initial_sync_ended_types_(initial_sync_ended_types),
     31     progress_marker_types_(progress_marker_types),
     32     configure_fail_types_(configure_fail_types),
     33     last_configure_reason_(CONFIGURE_REASON_UNKNOWN) {
     34   fake_encryption_handler_.reset(new FakeSyncEncryptionHandler());
     35 }
     36 
     37 FakeSyncManager::~FakeSyncManager() {}
     38 
     39 ModelTypeSet FakeSyncManager::GetAndResetCleanedTypes() {
     40   ModelTypeSet cleaned_types = cleaned_types_;
     41   cleaned_types_.Clear();
     42   return cleaned_types;
     43 }
     44 
     45 ModelTypeSet FakeSyncManager::GetAndResetDownloadedTypes() {
     46   ModelTypeSet downloaded_types = downloaded_types_;
     47   downloaded_types_.Clear();
     48   return downloaded_types;
     49 }
     50 
     51 ModelTypeSet FakeSyncManager::GetAndResetEnabledTypes() {
     52   ModelTypeSet enabled_types = enabled_types_;
     53   enabled_types_.Clear();
     54   return enabled_types;
     55 }
     56 
     57 ConfigureReason FakeSyncManager::GetAndResetConfigureReason() {
     58   ConfigureReason reason = last_configure_reason_;
     59   last_configure_reason_ = CONFIGURE_REASON_UNKNOWN;
     60   return reason;
     61 }
     62 
     63 void FakeSyncManager::WaitForSyncThread() {
     64   // Post a task to |sync_task_runner_| and block until it runs.
     65   base::RunLoop run_loop;
     66   if (!sync_task_runner_->PostTaskAndReply(
     67       FROM_HERE,
     68       base::Bind(&base::DoNothing),
     69       run_loop.QuitClosure())) {
     70     NOTREACHED();
     71   }
     72   run_loop.Run();
     73 }
     74 
     75 void FakeSyncManager::Init(InitArgs* args) {
     76   sync_task_runner_ = base::ThreadTaskRunnerHandle::Get();
     77   PurgePartiallySyncedTypes();
     78 
     79   test_user_share_.SetUp();
     80   UserShare* share = test_user_share_.user_share();
     81   for (ModelTypeSet::Iterator it = initial_sync_ended_types_.First();
     82        it.Good(); it.Inc()) {
     83     TestUserShare::CreateRoot(it.Get(), share);
     84   }
     85 
     86   FOR_EACH_OBSERVER(SyncManager::Observer, observers_,
     87                     OnInitializationComplete(
     88                         WeakHandle<JsBackend>(),
     89                         WeakHandle<DataTypeDebugInfoListener>(),
     90                         true, initial_sync_ended_types_));
     91 }
     92 
     93 ModelTypeSet FakeSyncManager::InitialSyncEndedTypes() {
     94   return initial_sync_ended_types_;
     95 }
     96 
     97 ModelTypeSet FakeSyncManager::GetTypesWithEmptyProgressMarkerToken(
     98     ModelTypeSet types) {
     99   ModelTypeSet empty_types = types;
    100   empty_types.RemoveAll(progress_marker_types_);
    101   return empty_types;
    102 }
    103 
    104 bool FakeSyncManager::PurgePartiallySyncedTypes() {
    105   ModelTypeSet partial_types;
    106   for (ModelTypeSet::Iterator i = progress_marker_types_.First();
    107        i.Good(); i.Inc()) {
    108     if (!initial_sync_ended_types_.Has(i.Get()))
    109       partial_types.Put(i.Get());
    110   }
    111   progress_marker_types_.RemoveAll(partial_types);
    112   cleaned_types_.PutAll(partial_types);
    113   return true;
    114 }
    115 
    116 void FakeSyncManager::UpdateCredentials(const SyncCredentials& credentials) {
    117   NOTIMPLEMENTED();
    118 }
    119 
    120 void FakeSyncManager::StartSyncingNormally(
    121       const ModelSafeRoutingInfo& routing_info) {
    122   // Do nothing.
    123 }
    124 
    125 void FakeSyncManager::ConfigureSyncer(
    126     ConfigureReason reason,
    127     ModelTypeSet to_download,
    128     ModelTypeSet to_purge,
    129     ModelTypeSet to_journal,
    130     ModelTypeSet to_unapply,
    131     const ModelSafeRoutingInfo& new_routing_info,
    132     const base::Closure& ready_task,
    133     const base::Closure& retry_task) {
    134   last_configure_reason_ = reason;
    135   enabled_types_ = GetRoutingInfoTypes(new_routing_info);
    136   ModelTypeSet success_types = to_download;
    137   success_types.RemoveAll(configure_fail_types_);
    138 
    139   DVLOG(1) << "Faking configuration. Downloading: "
    140            << ModelTypeSetToString(success_types) << ". Cleaning: "
    141            << ModelTypeSetToString(to_purge);
    142 
    143   // Update our fake directory by clearing and fake-downloading as necessary.
    144   UserShare* share = GetUserShare();
    145   share->directory->PurgeEntriesWithTypeIn(to_purge,
    146                                            to_journal,
    147                                            to_unapply);
    148   for (ModelTypeSet::Iterator it = success_types.First(); it.Good(); it.Inc()) {
    149     // We must be careful to not create the same root node twice.
    150     if (!initial_sync_ended_types_.Has(it.Get())) {
    151       TestUserShare::CreateRoot(it.Get(), share);
    152     }
    153   }
    154 
    155   // Simulate cleaning up disabled types.
    156   // TODO(sync): consider only cleaning those types that were recently disabled,
    157   // if this isn't the first cleanup, which more accurately reflects the
    158   // behavior of the real cleanup logic.
    159   initial_sync_ended_types_.RemoveAll(to_purge);
    160   progress_marker_types_.RemoveAll(to_purge);
    161   cleaned_types_.PutAll(to_purge);
    162 
    163   // Now simulate the actual configuration for those types that successfully
    164   // download + apply.
    165   progress_marker_types_.PutAll(success_types);
    166   initial_sync_ended_types_.PutAll(success_types);
    167   downloaded_types_.PutAll(success_types);
    168 
    169   ready_task.Run();
    170 }
    171 
    172 void FakeSyncManager::AddObserver(Observer* observer) {
    173   observers_.AddObserver(observer);
    174 }
    175 
    176 void FakeSyncManager::RemoveObserver(Observer* observer) {
    177   observers_.RemoveObserver(observer);
    178 }
    179 
    180 SyncStatus FakeSyncManager::GetDetailedStatus() const {
    181   NOTIMPLEMENTED();
    182   return SyncStatus();
    183 }
    184 
    185 void FakeSyncManager::SaveChanges() {
    186   // Do nothing.
    187 }
    188 
    189 void FakeSyncManager::ShutdownOnSyncThread(ShutdownReason reason) {
    190   DCHECK(sync_task_runner_->RunsTasksOnCurrentThread());
    191   test_user_share_.TearDown();
    192 }
    193 
    194 UserShare* FakeSyncManager::GetUserShare() {
    195   return test_user_share_.user_share();
    196 }
    197 
    198 syncer::SyncContextProxy* FakeSyncManager::GetSyncContextProxy() {
    199   return &null_sync_context_proxy_;
    200 }
    201 
    202 const std::string FakeSyncManager::cache_guid() {
    203   return test_user_share_.user_share()->directory->cache_guid();
    204 }
    205 
    206 bool FakeSyncManager::ReceivedExperiment(Experiments* experiments) {
    207   return false;
    208 }
    209 
    210 bool FakeSyncManager::HasUnsyncedItems() {
    211   NOTIMPLEMENTED();
    212   return false;
    213 }
    214 
    215 SyncEncryptionHandler* FakeSyncManager::GetEncryptionHandler() {
    216   return fake_encryption_handler_.get();
    217 }
    218 
    219 ScopedVector<syncer::ProtocolEvent>
    220 FakeSyncManager::GetBufferedProtocolEvents() {
    221   return ScopedVector<syncer::ProtocolEvent>();
    222 }
    223 
    224 scoped_ptr<base::ListValue> FakeSyncManager::GetAllNodesForType(
    225     syncer::ModelType type) {
    226   return scoped_ptr<base::ListValue>(new base::ListValue());
    227 }
    228 
    229 void FakeSyncManager::RefreshTypes(ModelTypeSet types) {
    230   last_refresh_request_types_ = types;
    231 }
    232 
    233 void FakeSyncManager::RegisterDirectoryTypeDebugInfoObserver(
    234     syncer::TypeDebugInfoObserver* observer) {}
    235 
    236 void FakeSyncManager::UnregisterDirectoryTypeDebugInfoObserver(
    237     syncer::TypeDebugInfoObserver* observer) {}
    238 
    239 bool FakeSyncManager::HasDirectoryTypeDebugInfoObserver(
    240     syncer::TypeDebugInfoObserver* observer) {
    241   return false;
    242 }
    243 
    244 void FakeSyncManager::RequestEmitDebugInfo() {}
    245 
    246 void FakeSyncManager::OnIncomingInvalidation(
    247     syncer::ModelType type,
    248     scoped_ptr<InvalidationInterface> invalidation) {
    249   // Do nothing.
    250 }
    251 
    252 ModelTypeSet FakeSyncManager::GetLastRefreshRequestTypes() {
    253   return last_refresh_request_types_;
    254 }
    255 
    256 void FakeSyncManager::SetInvalidatorEnabled(bool invalidator_enabled) {
    257   // Do nothing.
    258 }
    259 
    260 }  // namespace syncer
    261