mock_remote_file_sync_service.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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 "chrome/browser/sync_file_system/mock_remote_file_sync_service.h" 6 7#include <string> 8 9#include "base/bind.h" 10#include "base/location.h" 11#include "base/message_loop_proxy.h" 12#include "googleurl/src/gurl.h" 13#include "webkit/fileapi/file_system_url.h" 14 15using ::testing::_; 16using ::testing::Invoke; 17using ::testing::Return; 18 19namespace sync_file_system { 20 21const char MockRemoteFileSyncService::kServiceName[] = "mock_sync_service"; 22 23MockRemoteFileSyncService::MockRemoteFileSyncService() 24 : conflict_resolution_policy_(CONFLICT_RESOLUTION_MANUAL) { 25 typedef MockRemoteFileSyncService self; 26 ON_CALL(*this, AddServiceObserver(_)) 27 .WillByDefault(Invoke(this, &self::AddServiceObserverStub)); 28 ON_CALL(*this, AddFileStatusObserver(_)) 29 .WillByDefault(Invoke(this, &self::AddFileStatusObserverStub)); 30 ON_CALL(*this, RegisterOriginForTrackingChanges(_, _)) 31 .WillByDefault(Invoke(this, &self::RegisterOriginForTrackingChangesStub)); 32 ON_CALL(*this, UnregisterOriginForTrackingChanges(_, _)) 33 .WillByDefault( 34 Invoke(this, &self::UnregisterOriginForTrackingChangesStub)); 35 ON_CALL(*this, UninstallOrigin(_, _)) 36 .WillByDefault( 37 Invoke(this, &self::DeleteOriginDirectoryStub)); 38 ON_CALL(*this, ProcessRemoteChange(_)) 39 .WillByDefault(Invoke(this, &self::ProcessRemoteChangeStub)); 40 ON_CALL(*this, GetLocalChangeProcessor()) 41 .WillByDefault(Return(&mock_local_change_processor_)); 42 ON_CALL(*this, IsConflicting(_)) 43 .WillByDefault(Return(false)); 44 ON_CALL(*this, GetRemoteFileMetadata(_, _)) 45 .WillByDefault(Invoke(this, &self::GetRemoteFileMetadataStub)); 46 ON_CALL(*this, GetCurrentState()) 47 .WillByDefault(Return(REMOTE_SERVICE_OK)); 48 ON_CALL(*this, GetServiceName()) 49 .WillByDefault(Return(kServiceName)); 50 ON_CALL(*this, SetConflictResolutionPolicy(_)) 51 .WillByDefault(Invoke(this, &self::SetConflictResolutionPolicyStub)); 52 ON_CALL(*this, GetConflictResolutionPolicy()) 53 .WillByDefault(Invoke(this, &self::GetConflictResolutionPolicyStub)); 54} 55 56MockRemoteFileSyncService::~MockRemoteFileSyncService() { 57} 58 59void MockRemoteFileSyncService::NotifyRemoteChangeQueueUpdated( 60 int64 pending_changes) { 61 FOR_EACH_OBSERVER(Observer, service_observers_, 62 OnRemoteChangeQueueUpdated(pending_changes)); 63} 64 65void MockRemoteFileSyncService::NotifyRemoteServiceStateUpdated( 66 RemoteServiceState state, 67 const std::string& description) { 68 FOR_EACH_OBSERVER(Observer, service_observers_, 69 OnRemoteServiceStateUpdated(state, description)); 70} 71 72void MockRemoteFileSyncService::NotifyFileStatusChanged( 73 const fileapi::FileSystemURL& url, 74 SyncFileStatus sync_status, 75 SyncAction action_taken, 76 SyncDirection direction) { 77 FOR_EACH_OBSERVER(FileStatusObserver, file_status_observers_, 78 OnFileStatusChanged(url, sync_status, 79 action_taken, direction)); 80} 81 82void MockRemoteFileSyncService::AddServiceObserverStub(Observer* observer) { 83 service_observers_.AddObserver(observer); 84} 85 86void MockRemoteFileSyncService::AddFileStatusObserverStub( 87 FileStatusObserver* observer) { 88 file_status_observers_.AddObserver(observer); 89} 90 91void MockRemoteFileSyncService::RegisterOriginForTrackingChangesStub( 92 const GURL& origin, 93 const SyncStatusCallback& callback) { 94 base::MessageLoopProxy::current()->PostTask( 95 FROM_HERE, 96 base::Bind(callback, SYNC_STATUS_OK)); 97} 98 99void MockRemoteFileSyncService::UnregisterOriginForTrackingChangesStub( 100 const GURL& origin, 101 const SyncStatusCallback& callback) { 102 base::MessageLoopProxy::current()->PostTask( 103 FROM_HERE, 104 base::Bind(callback, SYNC_STATUS_OK)); 105} 106 107void MockRemoteFileSyncService::DeleteOriginDirectoryStub( 108 const GURL& origin, 109 const SyncStatusCallback& callback) { 110 base::MessageLoopProxy::current()->PostTask( 111 FROM_HERE, 112 base::Bind(callback, SYNC_STATUS_OK)); 113} 114 115void MockRemoteFileSyncService::ProcessRemoteChangeStub( 116 const SyncFileCallback& callback) { 117 base::MessageLoopProxy::current()->PostTask( 118 FROM_HERE, 119 base::Bind(callback, SYNC_STATUS_NO_CHANGE_TO_SYNC, 120 fileapi::FileSystemURL())); 121} 122 123void MockRemoteFileSyncService::GetRemoteFileMetadataStub( 124 const fileapi::FileSystemURL& url, 125 const SyncFileMetadataCallback& callback) { 126 FileMetadataMap::iterator iter = conflict_file_metadata_.find(url); 127 if (iter == conflict_file_metadata_.end()) { 128 base::MessageLoopProxy::current()->PostTask( 129 FROM_HERE, 130 base::Bind(callback, SYNC_FILE_ERROR_NOT_FOUND, 131 SyncFileMetadata())); 132 return; 133 } 134 base::MessageLoopProxy::current()->PostTask( 135 FROM_HERE, base::Bind(callback, SYNC_STATUS_OK, iter->second)); 136} 137 138SyncStatusCode MockRemoteFileSyncService::SetConflictResolutionPolicyStub( 139 ConflictResolutionPolicy policy) { 140 conflict_resolution_policy_ = policy; 141 return SYNC_STATUS_OK; 142} 143 144ConflictResolutionPolicy 145MockRemoteFileSyncService::GetConflictResolutionPolicyStub() const { 146 return conflict_resolution_policy_; 147} 148 149} // namespace sync_file_system 150