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 TestInternalComponentsFactory factory(InternalComponentsFactory::Switches(), | 115 TestInternalComponentsFactory factory(InternalComponentsFactory::Switches(), |
115 storage_option); | 116 storage_option, storage_option); |
116 | 117 |
117 base::RunLoop run_loop; | 118 base::RunLoop run_loop; |
118 manager->Init( | 119 manager->Init( |
119 temp_dir_.path(), | 120 temp_dir_.path(), |
120 MakeWeakHandle(base::WeakPtr<JsEventHandler>()), | 121 MakeWeakHandle(base::WeakPtr<JsEventHandler>()), |
121 GURL("https://example.com/"), | 122 GURL("https://example.com/"), |
122 scoped_ptr<HttpPostProviderFactory>().Pass(), | 123 scoped_ptr<HttpPostProviderFactory>().Pass(), |
123 std::vector<scoped_refptr<ModelSafeWorker> >(1, worker_.get()), | 124 std::vector<scoped_refptr<ModelSafeWorker> >(1, worker_.get()), |
124 NULL, | 125 NULL, |
125 delegate, | 126 delegate, |
126 SyncCredentials(), | 127 SyncCredentials(), |
127 "", | 128 "", |
128 "", | 129 "", |
129 "", | 130 "", |
130 &factory, | 131 &factory, |
131 NULL, | 132 NULL, |
132 scoped_ptr<UnrecoverableErrorHandler>().Pass(), | 133 scoped_ptr<UnrecoverableErrorHandler>().Pass(), |
133 NULL, | 134 NULL, |
134 NULL); | 135 NULL); |
135 loop_.PostTask(FROM_HERE, run_loop.QuitClosure()); | 136 loop_.PostTask(FROM_HERE, run_loop.QuitClosure()); |
136 run_loop.Run(); | 137 run_loop.Run(); |
137 } | 138 } |
138 | 139 |
139 // Create and persist an entry by unique tag in DB. | 140 // Create and persist an entry by unique tag in DB. |
140 void PrepopulateDb(ModelType type, const std::string& client_tag) { | 141 void PrepopulateDb(ModelType type, const std::string& client_tag) { |
141 SyncBackupManager backup_manager; | 142 SyncBackupManager backup_manager; |
142 TestChangeDelegate delegate; | 143 TestChangeDelegate delegate; |
143 InitManager(&backup_manager, ModelTypeSet(type), &delegate, | 144 InitManager(&backup_manager, ModelTypeSet(type), &delegate, |
144 STORAGE_ON_DISK); | 145 InternalComponentsFactory::STORAGE_ON_DISK_DEFERRED); |
145 CreateEntry(backup_manager.GetUserShare(), type, client_tag); | 146 CreateEntry(backup_manager.GetUserShare(), type, client_tag); |
146 backup_manager.ShutdownOnSyncThread(STOP_SYNC); | 147 backup_manager.ShutdownOnSyncThread(SWITCH_MODE_SYNC); |
147 } | 148 } |
148 | 149 |
149 // Verify entry with |client_tag| exists in sync directory. | 150 // Verify entry with |client_tag| exists in sync directory. |
150 bool VerifyEntry(UserShare* user_share, ModelType type, | 151 bool VerifyEntry(UserShare* user_share, ModelType type, |
151 const std::string& client_tag) { | 152 const std::string& client_tag) { |
152 ReadTransaction trans(FROM_HERE, user_share); | 153 ReadTransaction trans(FROM_HERE, user_share); |
153 ReadNode node(&trans); | 154 ReadNode node(&trans); |
154 return BaseNode::INIT_OK == node.InitByClientTagLookup(type, client_tag); | 155 return BaseNode::INIT_OK == node.InitByClientTagLookup(type, client_tag); |
155 } | 156 } |
156 | 157 |
(...skipping 30 matching lines...) Expand all Loading... |
187 bool IsRollbackDoneAction(SyncProtocolError e) { | 188 bool IsRollbackDoneAction(SyncProtocolError e) { |
188 return e.action == syncer::ROLLBACK_DONE; | 189 return e.action == syncer::ROLLBACK_DONE; |
189 } | 190 } |
190 | 191 |
191 TEST_F(SyncRollbackManagerTest, RollbackBasic) { | 192 TEST_F(SyncRollbackManagerTest, RollbackBasic) { |
192 PrepopulateDb(PREFERENCES, "pref1"); | 193 PrepopulateDb(PREFERENCES, "pref1"); |
193 | 194 |
194 TestChangeDelegate delegate; | 195 TestChangeDelegate delegate; |
195 SyncRollbackManager rollback_manager; | 196 SyncRollbackManager rollback_manager; |
196 InitManager(&rollback_manager, ModelTypeSet(PREFERENCES), &delegate, | 197 InitManager(&rollback_manager, ModelTypeSet(PREFERENCES), &delegate, |
197 STORAGE_ON_DISK); | 198 InternalComponentsFactory::STORAGE_ON_DISK); |
198 | 199 |
199 // Simulate a new entry added during type initialization. | 200 // Simulate a new entry added during type initialization. |
200 int64 new_pref_id = | 201 int64 new_pref_id = |
201 CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); | 202 CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); |
202 | 203 |
203 delegate.add_expected_delete(new_pref_id); | 204 delegate.add_expected_delete(new_pref_id); |
204 EXPECT_CALL(delegate, OnChangesApplied(_, _, _, _)) | 205 EXPECT_CALL(delegate, OnChangesApplied(_, _, _, _)) |
205 .Times(1) | 206 .Times(1) |
206 .WillOnce(DoDefault()); | 207 .WillOnce(DoDefault()); |
207 EXPECT_CALL(delegate, OnChangesComplete(_)).Times(1); | 208 EXPECT_CALL(delegate, OnChangesComplete(_)).Times(1); |
208 EXPECT_CALL(*this, OnActionableError(Truly(IsRollbackDoneAction))).Times(1); | 209 EXPECT_CALL(*this, OnActionableError(Truly(IsRollbackDoneAction))).Times(1); |
209 | 210 |
210 ModelSafeRoutingInfo routing_info; | 211 ModelSafeRoutingInfo routing_info; |
211 routing_info[PREFERENCES] = GROUP_UI; | 212 routing_info[PREFERENCES] = GROUP_UI; |
212 rollback_manager.StartSyncingNormally(routing_info); | 213 rollback_manager.StartSyncingNormally(routing_info); |
213 } | 214 } |
214 | 215 |
215 TEST_F(SyncRollbackManagerTest, NoRollbackOfTypesNotBackedUp) { | 216 TEST_F(SyncRollbackManagerTest, NoRollbackOfTypesNotBackedUp) { |
216 PrepopulateDb(PREFERENCES, "pref1"); | 217 PrepopulateDb(PREFERENCES, "pref1"); |
217 | 218 |
218 TestChangeDelegate delegate; | 219 TestChangeDelegate delegate; |
219 SyncRollbackManager rollback_manager; | 220 SyncRollbackManager rollback_manager; |
220 InitManager(&rollback_manager, ModelTypeSet(PREFERENCES, APPS), &delegate, | 221 InitManager(&rollback_manager, ModelTypeSet(PREFERENCES, APPS), &delegate, |
221 STORAGE_ON_DISK); | 222 InternalComponentsFactory::STORAGE_ON_DISK); |
222 | 223 |
223 // Simulate new entry added during type initialization. | 224 // Simulate new entry added during type initialization. |
224 int64 new_pref_id = | 225 int64 new_pref_id = |
225 CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); | 226 CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); |
226 CreateEntry(rollback_manager.GetUserShare(), APPS, "app1"); | 227 CreateEntry(rollback_manager.GetUserShare(), APPS, "app1"); |
227 | 228 |
228 delegate.add_expected_delete(new_pref_id); | 229 delegate.add_expected_delete(new_pref_id); |
229 EXPECT_CALL(delegate, OnChangesApplied(_, _, _, _)) | 230 EXPECT_CALL(delegate, OnChangesApplied(_, _, _, _)) |
230 .Times(1) | 231 .Times(1) |
231 .WillOnce(DoDefault()); | 232 .WillOnce(DoDefault()); |
232 EXPECT_CALL(delegate, OnChangesComplete(_)).Times(1); | 233 EXPECT_CALL(delegate, OnChangesComplete(_)).Times(1); |
233 | 234 |
234 ModelSafeRoutingInfo routing_info; | 235 ModelSafeRoutingInfo routing_info; |
235 routing_info[PREFERENCES] = GROUP_UI; | 236 routing_info[PREFERENCES] = GROUP_UI; |
236 rollback_manager.StartSyncingNormally(routing_info); | 237 rollback_manager.StartSyncingNormally(routing_info); |
237 | 238 |
238 // APP entry is still valid. | 239 // APP entry is still valid. |
239 EXPECT_TRUE(VerifyEntry(rollback_manager.GetUserShare(), APPS, "app1")); | 240 EXPECT_TRUE(VerifyEntry(rollback_manager.GetUserShare(), APPS, "app1")); |
240 } | 241 } |
241 | 242 |
242 TEST_F(SyncRollbackManagerTest, BackupDbNotChangedOnAbort) { | 243 TEST_F(SyncRollbackManagerTest, BackupDbNotChangedOnAbort) { |
243 PrepopulateDb(PREFERENCES, "pref1"); | 244 PrepopulateDb(PREFERENCES, "pref1"); |
244 | 245 |
245 TestChangeDelegate delegate; | 246 TestChangeDelegate delegate; |
246 scoped_ptr<SyncRollbackManager> rollback_manager( | 247 scoped_ptr<SyncRollbackManager> rollback_manager( |
247 new SyncRollbackManager); | 248 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 | 251 |
251 // Simulate a new entry added during type initialization. | 252 // Simulate a new entry added during type initialization. |
252 CreateEntry(rollback_manager->GetUserShare(), PREFERENCES, "pref2"); | 253 CreateEntry(rollback_manager->GetUserShare(), PREFERENCES, "pref2"); |
253 | 254 |
254 // Manager was shut down before sync starts. | 255 // Manager was shut down before sync starts. |
255 rollback_manager->ShutdownOnSyncThread(STOP_SYNC); | 256 rollback_manager->ShutdownOnSyncThread(STOP_SYNC); |
256 | 257 |
257 // Verify new entry was not persisted. | 258 // Verify new entry was not persisted. |
258 rollback_manager.reset(new SyncRollbackManager); | 259 rollback_manager.reset(new SyncRollbackManager); |
259 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES), &delegate, | 260 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES), &delegate, |
260 STORAGE_ON_DISK); | 261 InternalComponentsFactory::STORAGE_ON_DISK); |
261 EXPECT_FALSE(VerifyEntry(rollback_manager->GetUserShare(), PREFERENCES, | 262 EXPECT_FALSE(VerifyEntry(rollback_manager->GetUserShare(), PREFERENCES, |
262 "pref2")); | 263 "pref2")); |
263 } | 264 } |
264 | 265 |
265 TEST_F(SyncRollbackManagerTest, OnInitializationFailure) { | 266 TEST_F(SyncRollbackManagerTest, OnInitializationFailure) { |
266 // Test graceful shutdown on initialization failure. | 267 // Test graceful shutdown on initialization failure. |
267 scoped_ptr<SyncRollbackManager> rollback_manager( | 268 scoped_ptr<SyncRollbackManager> rollback_manager( |
268 new SyncRollbackManager); | 269 new SyncRollbackManager); |
269 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES), NULL, | 270 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES), NULL, |
270 STORAGE_ON_DISK); | 271 InternalComponentsFactory::STORAGE_ON_DISK); |
271 } | 272 } |
272 | 273 |
273 } // anonymous namespace | 274 } // anonymous namespace |
274 | 275 |
275 } // namespace syncer | 276 } // namespace syncer |
OLD | NEW |