| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_ |
| 6 #define CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_ | 6 #define CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "chrome/browser/invalidation/invalidator_storage.h" | |
| 14 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 13 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 16 #include "chrome/browser/sync/glue/data_type_manager_impl.h" | 14 #include "chrome/browser/sync/glue/data_type_manager_impl.h" |
| 17 #include "chrome/browser/sync/glue/sync_backend_host_impl.h" | 15 #include "chrome/browser/sync/glue/sync_backend_host_impl.h" |
| 18 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" | |
| 19 #include "chrome/browser/sync/profile_sync_service.h" | 16 #include "chrome/browser/sync/profile_sync_service.h" |
| 20 #include "chrome/browser/sync/sync_prefs.h" | 17 #include "chrome/browser/sync/sync_prefs.h" |
| 21 #include "chrome/test/base/profile_mock.h" | |
| 22 #include "google_apis/gaia/oauth2_token_service.h" | |
| 23 #include "sync/internal_api/public/test/test_internal_components_factory.h" | |
| 24 #include "sync/test/engine/test_id_factory.h" | 18 #include "sync/test/engine/test_id_factory.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 26 | 20 |
| 27 class Profile; | 21 class Profile; |
| 22 class ProfileOAuth2TokenService; |
| 23 class ProfileSyncComponentsFactory; |
| 24 class ProfileSyncComponentsFactoryMock; |
| 28 | 25 |
| 29 ACTION(ReturnNewDataTypeManager) { | 26 ACTION(ReturnNewDataTypeManager) { |
| 30 return new browser_sync::DataTypeManagerImpl(arg0, | 27 return new browser_sync::DataTypeManagerImpl(arg0, |
| 31 arg1, | 28 arg1, |
| 32 arg2, | 29 arg2, |
| 33 arg3, | 30 arg3, |
| 34 arg4, | 31 arg4, |
| 35 arg5); | 32 arg5); |
| 36 } | 33 } |
| 37 | 34 |
| 38 namespace browser_sync { | 35 namespace browser_sync { |
| 39 | 36 |
| 40 class SyncBackendHostForProfileSyncTest : public SyncBackendHostImpl { | 37 class SyncBackendHostForProfileSyncTest : public SyncBackendHostImpl { |
| 41 public: | 38 public: |
| 42 // |synchronous_init| causes initialization to block until the syncapi has | |
| 43 // completed setting itself up and called us back. | |
| 44 // TOOD(akalin): Remove |synchronous_init| (http://crbug.com/140354). | |
| 45 SyncBackendHostForProfileSyncTest( | 39 SyncBackendHostForProfileSyncTest( |
| 46 Profile* profile, | 40 Profile* profile, |
| 47 const base::WeakPtr<SyncPrefs>& sync_prefs, | 41 const base::WeakPtr<SyncPrefs>& sync_prefs, |
| 48 base::Closure& callback, | 42 base::Closure& callback); |
| 49 bool set_initial_sync_ended_on_init, | |
| 50 bool synchronous_init, | |
| 51 bool fail_initial_download, | |
| 52 syncer::StorageOption storage_option); | |
| 53 virtual ~SyncBackendHostForProfileSyncTest(); | 43 virtual ~SyncBackendHostForProfileSyncTest(); |
| 54 | 44 |
| 55 MOCK_METHOD1(RequestNudge, void(const tracked_objects::Location&)); | |
| 56 | |
| 57 virtual void UpdateCredentials( | |
| 58 const syncer::SyncCredentials& credentials) OVERRIDE; | |
| 59 | |
| 60 virtual void RequestConfigureSyncer( | 45 virtual void RequestConfigureSyncer( |
| 61 syncer::ConfigureReason reason, | 46 syncer::ConfigureReason reason, |
| 62 syncer::ModelTypeSet to_download, | 47 syncer::ModelTypeSet to_download, |
| 63 syncer::ModelTypeSet to_purge, | 48 syncer::ModelTypeSet to_purge, |
| 64 syncer::ModelTypeSet to_journal, | 49 syncer::ModelTypeSet to_journal, |
| 65 syncer::ModelTypeSet to_unapply, | 50 syncer::ModelTypeSet to_unapply, |
| 66 syncer::ModelTypeSet to_ignore, | 51 syncer::ModelTypeSet to_ignore, |
| 67 const syncer::ModelSafeRoutingInfo& routing_info, | 52 const syncer::ModelSafeRoutingInfo& routing_info, |
| 68 const base::Callback<void(syncer::ModelTypeSet, | 53 const base::Callback<void(syncer::ModelTypeSet, |
| 69 syncer::ModelTypeSet)>& ready_task, | 54 syncer::ModelTypeSet)>& ready_task, |
| 70 const base::Closure& retry_callback) OVERRIDE; | 55 const base::Closure& retry_callback) OVERRIDE; |
| 71 | 56 |
| 72 virtual void HandleInitializationSuccessOnFrontendLoop( | |
| 73 const syncer::WeakHandle<syncer::JsBackend> js_backend, | |
| 74 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener> | |
| 75 debug_info_listener) OVERRIDE; | |
| 76 | |
| 77 protected: | 57 protected: |
| 78 virtual void InitCore(scoped_ptr<DoInitializeOptions> options) OVERRIDE; | 58 virtual void InitCore(scoped_ptr<DoInitializeOptions> options) OVERRIDE; |
| 79 | 59 |
| 80 private: | 60 private: |
| 81 | |
| 82 | |
| 83 // Invoked at the start of HandleSyncManagerInitializationOnFrontendLoop. | 61 // Invoked at the start of HandleSyncManagerInitializationOnFrontendLoop. |
| 84 // Allows extra initialization work to be performed before the backend comes | 62 // Allows extra initialization work to be performed before the backend comes |
| 85 // up. | 63 // up. |
| 86 base::Closure& callback_; | 64 base::Closure& callback_; |
| 87 | |
| 88 // Saved closure in case we failed the initial download but then received | |
| 89 // new credentials. Holds the results of | |
| 90 // HandleSyncManagerInitializationOnFrontendLoop, and if | |
| 91 // |fail_initial_download_| was true, finishes the initialization process | |
| 92 // once we receive new credentials. | |
| 93 base::Closure initial_download_closure_; | |
| 94 bool fail_initial_download_; | |
| 95 | |
| 96 bool set_initial_sync_ended_on_init_; | |
| 97 bool synchronous_init_; | |
| 98 syncer::StorageOption storage_option_; | |
| 99 | |
| 100 base::WeakPtrFactory<SyncBackendHostForProfileSyncTest> weak_ptr_factory_; | |
| 101 }; | 65 }; |
| 102 | 66 |
| 103 } // namespace browser_sync | 67 } // namespace browser_sync |
| 104 | 68 |
| 105 class TestProfileSyncService : public ProfileSyncService { | 69 class TestProfileSyncService : public ProfileSyncService { |
| 106 public: | 70 public: |
| 107 // TODO(tim): Remove |synchronous_backend_initialization|, and add ability to | |
| 108 // inject TokenService alongside SigninManager. | |
| 109 TestProfileSyncService( | 71 TestProfileSyncService( |
| 110 ProfileSyncComponentsFactory* factory, | 72 ProfileSyncComponentsFactory* factory, |
| 111 Profile* profile, | 73 Profile* profile, |
| 112 SigninManagerBase* signin, | 74 SigninManagerBase* signin, |
| 113 ProfileOAuth2TokenService* oauth2_token_service, | 75 ProfileOAuth2TokenService* oauth2_token_service, |
| 114 ProfileSyncService::StartBehavior behavior, | 76 ProfileSyncService::StartBehavior behavior); |
| 115 bool synchronous_backend_initialization); | |
| 116 | 77 |
| 117 virtual ~TestProfileSyncService(); | 78 virtual ~TestProfileSyncService(); |
| 118 | 79 |
| 119 virtual void RequestAccessToken() OVERRIDE; | |
| 120 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
| 121 const std::string& access_token, | |
| 122 const base::Time& expiration_time) OVERRIDE; | |
| 123 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
| 124 const GoogleServiceAuthError& error) OVERRIDE; | |
| 125 | |
| 126 virtual void OnBackendInitialized( | |
| 127 const syncer::WeakHandle<syncer::JsBackend>& backend, | |
| 128 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>& | |
| 129 debug_info_listener, | |
| 130 bool success) OVERRIDE; | |
| 131 | |
| 132 virtual void OnConfigureDone( | 80 virtual void OnConfigureDone( |
| 133 const browser_sync::DataTypeManager::ConfigureResult& result) OVERRIDE; | 81 const browser_sync::DataTypeManager::ConfigureResult& result) OVERRIDE; |
| 134 | 82 |
| 135 // We implement our own version to avoid some DCHECKs. | 83 // We implement our own version to avoid some DCHECKs. |
| 136 virtual syncer::UserShare* GetUserShare() const OVERRIDE; | 84 virtual syncer::UserShare* GetUserShare() const OVERRIDE; |
| 137 | 85 |
| 138 static BrowserContextKeyedService* BuildAutoStartAsyncInit( | 86 static BrowserContextKeyedService* BuildAutoStartAsyncInit( |
| 139 content::BrowserContext* profile); | 87 content::BrowserContext* profile); |
| 140 | 88 |
| 141 ProfileSyncComponentsFactoryMock* components_factory_mock(); | 89 ProfileSyncComponentsFactoryMock* components_factory_mock(); |
| 142 | 90 |
| 143 // If this is called, configuring data types will require a syncer | |
| 144 // nudge. | |
| 145 void dont_set_initial_sync_ended_on_init(); | |
| 146 void set_synchronous_sync_configuration(); | |
| 147 | |
| 148 // Fails initial download until a new auth token is provided. | |
| 149 void fail_initial_download(); | |
| 150 | |
| 151 void set_storage_option(syncer::StorageOption option); | |
| 152 | |
| 153 // |callback| can be used to populate nodes before the OnBackendInitialized | 91 // |callback| can be used to populate nodes before the OnBackendInitialized |
| 154 // callback fires. | 92 // callback fires. |
| 155 void set_backend_init_callback(const base::Closure& callback) { | 93 void set_backend_init_callback(const base::Closure& callback) { |
| 156 callback_ = callback; | 94 callback_ = callback; |
| 157 } | 95 } |
| 158 | 96 |
| 159 syncer::TestIdFactory* id_factory(); | 97 syncer::TestIdFactory* id_factory(); |
| 160 | 98 |
| 161 // Override of ProfileSyncService::GetBackendForTest() with a more | |
| 162 // specific return type (since C++ supports covariant return types) | |
| 163 // that is made public. | |
| 164 virtual browser_sync::SyncBackendHostForProfileSyncTest* | |
| 165 GetBackendForTest() OVERRIDE; | |
| 166 | |
| 167 protected: | 99 protected: |
| 168 virtual void CreateBackend() OVERRIDE; | 100 virtual void CreateBackend() OVERRIDE; |
| 169 | 101 |
| 170 // Return NULL handle to use in backend initialization to avoid receiving | 102 // Return NULL handle to use in backend initialization to avoid receiving |
| 171 // js messages on UI loop when it's being destroyed, which are not deleted | 103 // js messages on UI loop when it's being destroyed, which are not deleted |
| 172 // and cause memory leak in test. | 104 // and cause memory leak in test. |
| 173 virtual syncer::WeakHandle<syncer::JsEventHandler> GetJsEventHandler() | 105 virtual syncer::WeakHandle<syncer::JsEventHandler> GetJsEventHandler() |
| 174 OVERRIDE; | 106 OVERRIDE; |
| 175 | 107 |
| 176 private: | 108 private: |
| 177 syncer::TestIdFactory id_factory_; | 109 syncer::TestIdFactory id_factory_; |
| 178 | 110 |
| 179 bool synchronous_backend_initialization_; | |
| 180 | |
| 181 // Set to true when a mock data type manager is being used and the configure | |
| 182 // step is performed synchronously. | |
| 183 bool synchronous_sync_configuration_; | |
| 184 | |
| 185 base::Closure callback_; | 111 base::Closure callback_; |
| 186 bool set_initial_sync_ended_on_init_; | |
| 187 | |
| 188 bool fail_initial_download_; | |
| 189 syncer::StorageOption storage_option_; | |
| 190 }; | 112 }; |
| 191 | 113 |
| 192 #endif // CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_ | 114 #endif // CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_ |
| OLD | NEW |