OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "sync/internal_api/sync_rollback_manager.h" | 5 #include "sync/internal_api/sync_rollback_manager.h" |
6 | 6 |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "sync/internal_api/public/read_node.h" | 9 #include "sync/internal_api/public/read_node.h" |
10 #include "sync/internal_api/public/read_transaction.h" | 10 #include "sync/internal_api/public/read_transaction.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 ReadNode type_root(&trans); | 95 ReadNode type_root(&trans); |
96 EXPECT_EQ(BaseNode::INIT_OK, type_root.InitTypeRoot(type)); | 96 EXPECT_EQ(BaseNode::INIT_OK, type_root.InitTypeRoot(type)); |
97 | 97 |
98 WriteNode node(&trans); | 98 WriteNode node(&trans); |
99 EXPECT_EQ(WriteNode::INIT_SUCCESS, | 99 EXPECT_EQ(WriteNode::INIT_SUCCESS, |
100 node.InitUniqueByCreation(type, type_root, client_tag)); | 100 node.InitUniqueByCreation(type, type_root, client_tag)); |
101 return node.GetEntry()->GetMetahandle(); | 101 return node.GetEntry()->GetMetahandle(); |
102 } | 102 } |
103 | 103 |
104 void InitManager(SyncManager* manager, ModelTypeSet types, | 104 void InitManager(SyncManager* manager, ModelTypeSet types, |
105 TestChangeDelegate* delegate, StorageOption storage_option) { | 105 TestChangeDelegate* delegate, |
| 106 InternalComponentsFactory::StorageOption storage_option) { |
106 manager_ = manager; | 107 manager_ = manager; |
107 types_ = types; | 108 types_ = types; |
108 | 109 |
109 EXPECT_CALL(*this, OnInitializationComplete(_, _, _, _)) | 110 EXPECT_CALL(*this, OnInitializationComplete(_, _, _, _)) |
110 .WillOnce(WithArgs<2>(Invoke(this, | 111 .WillOnce(WithArgs<2>(Invoke(this, |
111 &SyncRollbackManagerTest::HandleInit))); | 112 &SyncRollbackManagerTest::HandleInit))); |
112 | 113 |
113 manager->AddObserver(this); | 114 manager->AddObserver(this); |
114 | 115 |
115 base::RunLoop run_loop; | 116 base::RunLoop run_loop; |
116 SyncManager::InitArgs args; | 117 SyncManager::InitArgs args; |
117 args.database_location = temp_dir_.path(); | 118 args.database_location = temp_dir_.path(); |
118 args.service_url = GURL("https://example.com/"); | 119 args.service_url = GURL("https://example.com/"); |
119 args.workers.push_back(worker_); | 120 args.workers.push_back(worker_); |
120 args.change_delegate = delegate; | 121 args.change_delegate = delegate; |
121 args.internal_components_factory.reset(new TestInternalComponentsFactory( | 122 args.internal_components_factory.reset(new TestInternalComponentsFactory( |
122 InternalComponentsFactory::Switches(), storage_option)); | 123 InternalComponentsFactory::Switches(), storage_option, storage_option)); |
123 manager->Init(&args); | 124 manager->Init(&args); |
124 loop_.PostTask(FROM_HERE, run_loop.QuitClosure()); | 125 loop_.PostTask(FROM_HERE, run_loop.QuitClosure()); |
125 run_loop.Run(); | 126 run_loop.Run(); |
126 } | 127 } |
127 | 128 |
128 // Create and persist an entry by unique tag in DB. | 129 // Create and persist an entry by unique tag in DB. |
129 void PrepopulateDb(ModelType type, const std::string& client_tag) { | 130 void PrepopulateDb(ModelType type, const std::string& client_tag) { |
130 SyncBackupManager backup_manager; | 131 SyncBackupManager backup_manager; |
131 TestChangeDelegate delegate; | 132 TestChangeDelegate delegate; |
132 InitManager(&backup_manager, ModelTypeSet(type), &delegate, | 133 InitManager(&backup_manager, ModelTypeSet(type), &delegate, |
133 STORAGE_ON_DISK); | 134 InternalComponentsFactory::STORAGE_ON_DISK_DEFERRED); |
134 CreateEntry(backup_manager.GetUserShare(), type, client_tag); | 135 CreateEntry(backup_manager.GetUserShare(), type, client_tag); |
135 backup_manager.ShutdownOnSyncThread(STOP_SYNC); | 136 backup_manager.ShutdownOnSyncThread(SWITCH_MODE_SYNC); |
136 } | 137 } |
137 | 138 |
138 // Verify entry with |client_tag| exists in sync directory. | 139 // Verify entry with |client_tag| exists in sync directory. |
139 bool VerifyEntry(UserShare* user_share, ModelType type, | 140 bool VerifyEntry(UserShare* user_share, ModelType type, |
140 const std::string& client_tag) { | 141 const std::string& client_tag) { |
141 ReadTransaction trans(FROM_HERE, user_share); | 142 ReadTransaction trans(FROM_HERE, user_share); |
142 ReadNode node(&trans); | 143 ReadNode node(&trans); |
143 return BaseNode::INIT_OK == node.InitByClientTagLookup(type, client_tag); | 144 return BaseNode::INIT_OK == node.InitByClientTagLookup(type, client_tag); |
144 } | 145 } |
145 | 146 |
(...skipping 30 matching lines...) Expand all Loading... |
176 bool IsRollbackDoneAction(SyncProtocolError e) { | 177 bool IsRollbackDoneAction(SyncProtocolError e) { |
177 return e.action == syncer::ROLLBACK_DONE; | 178 return e.action == syncer::ROLLBACK_DONE; |
178 } | 179 } |
179 | 180 |
180 TEST_F(SyncRollbackManagerTest, RollbackBasic) { | 181 TEST_F(SyncRollbackManagerTest, RollbackBasic) { |
181 PrepopulateDb(PREFERENCES, "pref1"); | 182 PrepopulateDb(PREFERENCES, "pref1"); |
182 | 183 |
183 TestChangeDelegate delegate; | 184 TestChangeDelegate delegate; |
184 SyncRollbackManager rollback_manager; | 185 SyncRollbackManager rollback_manager; |
185 InitManager(&rollback_manager, ModelTypeSet(PREFERENCES), &delegate, | 186 InitManager(&rollback_manager, ModelTypeSet(PREFERENCES), &delegate, |
186 STORAGE_ON_DISK); | 187 InternalComponentsFactory::STORAGE_ON_DISK); |
187 | 188 |
188 // Simulate a new entry added during type initialization. | 189 // Simulate a new entry added during type initialization. |
189 int64 new_pref_id = | 190 int64 new_pref_id = |
190 CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); | 191 CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); |
191 | 192 |
192 delegate.add_expected_delete(new_pref_id); | 193 delegate.add_expected_delete(new_pref_id); |
193 EXPECT_CALL(delegate, OnChangesApplied(_, _, _, _)) | 194 EXPECT_CALL(delegate, OnChangesApplied(_, _, _, _)) |
194 .Times(1) | 195 .Times(1) |
195 .WillOnce(DoDefault()); | 196 .WillOnce(DoDefault()); |
196 EXPECT_CALL(delegate, OnChangesComplete(_)).Times(1); | 197 EXPECT_CALL(delegate, OnChangesComplete(_)).Times(1); |
197 EXPECT_CALL(*this, OnActionableError(Truly(IsRollbackDoneAction))).Times(1); | 198 EXPECT_CALL(*this, OnActionableError(Truly(IsRollbackDoneAction))).Times(1); |
198 | 199 |
199 ModelSafeRoutingInfo routing_info; | 200 ModelSafeRoutingInfo routing_info; |
200 routing_info[PREFERENCES] = GROUP_UI; | 201 routing_info[PREFERENCES] = GROUP_UI; |
201 rollback_manager.StartSyncingNormally(routing_info); | 202 rollback_manager.StartSyncingNormally(routing_info); |
202 } | 203 } |
203 | 204 |
204 TEST_F(SyncRollbackManagerTest, NoRollbackOfTypesNotBackedUp) { | 205 TEST_F(SyncRollbackManagerTest, NoRollbackOfTypesNotBackedUp) { |
205 PrepopulateDb(PREFERENCES, "pref1"); | 206 PrepopulateDb(PREFERENCES, "pref1"); |
206 | 207 |
207 TestChangeDelegate delegate; | 208 TestChangeDelegate delegate; |
208 SyncRollbackManager rollback_manager; | 209 SyncRollbackManager rollback_manager; |
209 InitManager(&rollback_manager, ModelTypeSet(PREFERENCES, APPS), &delegate, | 210 InitManager(&rollback_manager, ModelTypeSet(PREFERENCES, APPS), &delegate, |
210 STORAGE_ON_DISK); | 211 InternalComponentsFactory::STORAGE_ON_DISK); |
211 | 212 |
212 // Simulate new entry added during type initialization. | 213 // Simulate new entry added during type initialization. |
213 int64 new_pref_id = | 214 int64 new_pref_id = |
214 CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); | 215 CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); |
215 CreateEntry(rollback_manager.GetUserShare(), APPS, "app1"); | 216 CreateEntry(rollback_manager.GetUserShare(), APPS, "app1"); |
216 | 217 |
217 delegate.add_expected_delete(new_pref_id); | 218 delegate.add_expected_delete(new_pref_id); |
218 EXPECT_CALL(delegate, OnChangesApplied(_, _, _, _)) | 219 EXPECT_CALL(delegate, OnChangesApplied(_, _, _, _)) |
219 .Times(1) | 220 .Times(1) |
220 .WillOnce(DoDefault()); | 221 .WillOnce(DoDefault()); |
221 EXPECT_CALL(delegate, OnChangesComplete(_)).Times(1); | 222 EXPECT_CALL(delegate, OnChangesComplete(_)).Times(1); |
222 | 223 |
223 ModelSafeRoutingInfo routing_info; | 224 ModelSafeRoutingInfo routing_info; |
224 routing_info[PREFERENCES] = GROUP_UI; | 225 routing_info[PREFERENCES] = GROUP_UI; |
225 rollback_manager.StartSyncingNormally(routing_info); | 226 rollback_manager.StartSyncingNormally(routing_info); |
226 | 227 |
227 // APP entry is still valid. | 228 // APP entry is still valid. |
228 EXPECT_TRUE(VerifyEntry(rollback_manager.GetUserShare(), APPS, "app1")); | 229 EXPECT_TRUE(VerifyEntry(rollback_manager.GetUserShare(), APPS, "app1")); |
229 } | 230 } |
230 | 231 |
231 TEST_F(SyncRollbackManagerTest, BackupDbNotChangedOnAbort) { | 232 TEST_F(SyncRollbackManagerTest, BackupDbNotChangedOnAbort) { |
232 PrepopulateDb(PREFERENCES, "pref1"); | 233 PrepopulateDb(PREFERENCES, "pref1"); |
233 | 234 |
234 TestChangeDelegate delegate; | 235 TestChangeDelegate delegate; |
235 scoped_ptr<SyncRollbackManager> rollback_manager( | 236 scoped_ptr<SyncRollbackManager> rollback_manager( |
236 new SyncRollbackManager); | 237 new SyncRollbackManager); |
237 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES), &delegate, | 238 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES), &delegate, |
238 STORAGE_ON_DISK); | 239 InternalComponentsFactory::STORAGE_ON_DISK); |
239 | 240 |
240 // Simulate a new entry added during type initialization. | 241 // Simulate a new entry added during type initialization. |
241 CreateEntry(rollback_manager->GetUserShare(), PREFERENCES, "pref2"); | 242 CreateEntry(rollback_manager->GetUserShare(), PREFERENCES, "pref2"); |
242 | 243 |
243 // Manager was shut down before sync starts. | 244 // Manager was shut down before sync starts. |
244 rollback_manager->ShutdownOnSyncThread(STOP_SYNC); | 245 rollback_manager->ShutdownOnSyncThread(STOP_SYNC); |
245 | 246 |
246 // Verify new entry was not persisted. | 247 // Verify new entry was not persisted. |
247 rollback_manager.reset(new SyncRollbackManager); | 248 rollback_manager.reset(new SyncRollbackManager); |
248 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES), &delegate, | 249 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES), &delegate, |
249 STORAGE_ON_DISK); | 250 InternalComponentsFactory::STORAGE_ON_DISK); |
250 EXPECT_FALSE(VerifyEntry(rollback_manager->GetUserShare(), PREFERENCES, | 251 EXPECT_FALSE(VerifyEntry(rollback_manager->GetUserShare(), PREFERENCES, |
251 "pref2")); | 252 "pref2")); |
252 } | 253 } |
253 | 254 |
254 TEST_F(SyncRollbackManagerTest, OnInitializationFailure) { | 255 TEST_F(SyncRollbackManagerTest, OnInitializationFailure) { |
255 // Test graceful shutdown on initialization failure. | 256 // Test graceful shutdown on initialization failure. |
256 scoped_ptr<SyncRollbackManager> rollback_manager( | 257 scoped_ptr<SyncRollbackManager> rollback_manager( |
257 new SyncRollbackManager); | 258 new SyncRollbackManager); |
258 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES), NULL, | 259 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES), NULL, |
259 STORAGE_ON_DISK); | 260 InternalComponentsFactory::STORAGE_ON_DISK); |
260 } | 261 } |
261 | 262 |
262 } // anonymous namespace | 263 } // anonymous namespace |
263 | 264 |
264 } // namespace syncer | 265 } // namespace syncer |
OLD | NEW |