OLD | NEW |
| (Empty) |
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 "components/sync_driver/backend_migrator.h" | |
6 | |
7 #include "base/message_loop/message_loop.h" | |
8 #include "base/run_loop.h" | |
9 #include "base/tracked_objects.h" | |
10 #include "components/sync/base/model_type_test_util.h" | |
11 #include "components/sync/core/test/test_user_share.h" | |
12 #include "components/sync/core/write_transaction.h" | |
13 #include "components/sync/protocol/sync.pb.h" | |
14 #include "components/sync/syncable/directory.h" // TODO(tim): Remove. Bug 13113
0. | |
15 #include "components/sync_driver/data_type_manager_mock.h" | |
16 #include "components/sync_driver/fake_sync_service.h" | |
17 #include "testing/gmock/include/gmock/gmock.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 using ::testing::_; | |
21 using ::testing::Eq; | |
22 using ::testing::Mock; | |
23 using ::testing::NiceMock; | |
24 using ::testing::Return; | |
25 using sync_driver::DataTypeManager; | |
26 using sync_driver::DataTypeManagerMock; | |
27 | |
28 namespace browser_sync { | |
29 | |
30 using syncer::sessions::SyncSessionSnapshot; | |
31 | |
32 class SyncBackendMigratorTest : public testing::Test { | |
33 public: | |
34 SyncBackendMigratorTest() { } | |
35 virtual ~SyncBackendMigratorTest() { } | |
36 | |
37 virtual void SetUp() { | |
38 test_user_share_.SetUp(); | |
39 Mock::VerifyAndClear(manager()); | |
40 preferred_types_.Put(syncer::BOOKMARKS); | |
41 preferred_types_.Put(syncer::PREFERENCES); | |
42 preferred_types_.Put(syncer::AUTOFILL); | |
43 | |
44 migrator_.reset( | |
45 new BackendMigrator( | |
46 "Profile0", test_user_share_.user_share(), service(), manager(), | |
47 base::Closure())); | |
48 SetUnsyncedTypes(syncer::ModelTypeSet()); | |
49 } | |
50 | |
51 virtual void TearDown() { | |
52 migrator_.reset(); | |
53 test_user_share_.TearDown(); | |
54 } | |
55 | |
56 // Marks all types in |unsynced_types| as unsynced and all other | |
57 // types as synced. | |
58 void SetUnsyncedTypes(syncer::ModelTypeSet unsynced_types) { | |
59 syncer::WriteTransaction trans(FROM_HERE, | |
60 test_user_share_.user_share()); | |
61 for (int i = syncer::FIRST_REAL_MODEL_TYPE; | |
62 i < syncer::MODEL_TYPE_COUNT; ++i) { | |
63 syncer::ModelType type = syncer::ModelTypeFromInt(i); | |
64 sync_pb::DataTypeProgressMarker progress_marker; | |
65 if (!unsynced_types.Has(type)) { | |
66 progress_marker.set_token("dummy"); | |
67 } | |
68 trans.GetDirectory()->SetDownloadProgress(type, progress_marker); | |
69 } | |
70 } | |
71 | |
72 void SendConfigureDone(DataTypeManager::ConfigureStatus status, | |
73 syncer::ModelTypeSet requested_types) { | |
74 if (status == DataTypeManager::OK) { | |
75 DataTypeManager::ConfigureResult result(status, requested_types); | |
76 migrator_->OnConfigureDone(result); | |
77 } else { | |
78 DataTypeManager::ConfigureResult result( | |
79 status, | |
80 requested_types); | |
81 migrator_->OnConfigureDone(result); | |
82 } | |
83 base::RunLoop run_loop; | |
84 run_loop.RunUntilIdle(); | |
85 } | |
86 | |
87 sync_driver::SyncService* service() { return &service_; } | |
88 DataTypeManagerMock* manager() { return &manager_; } | |
89 syncer::ModelTypeSet preferred_types() { return preferred_types_; } | |
90 BackendMigrator* migrator() { return migrator_.get(); } | |
91 void RemovePreferredType(syncer::ModelType type) { | |
92 preferred_types_.Remove(type); | |
93 } | |
94 | |
95 private: | |
96 base::MessageLoop message_loop_; | |
97 syncer::ModelTypeSet preferred_types_; | |
98 sync_driver::FakeSyncService service_; | |
99 NiceMock<DataTypeManagerMock> manager_; | |
100 syncer::TestUserShare test_user_share_; | |
101 std::unique_ptr<BackendMigrator> migrator_; | |
102 }; | |
103 | |
104 class MockMigrationObserver : public MigrationObserver { | |
105 public: | |
106 virtual ~MockMigrationObserver() {} | |
107 | |
108 MOCK_METHOD0(OnMigrationStateChange, void()); | |
109 }; | |
110 | |
111 // Test that in the normal case a migration does transition through each state | |
112 // and wind up back in IDLE. | |
113 TEST_F(SyncBackendMigratorTest, Sanity) { | |
114 MockMigrationObserver migration_observer; | |
115 migrator()->AddMigrationObserver(&migration_observer); | |
116 EXPECT_CALL(migration_observer, OnMigrationStateChange()).Times(4); | |
117 | |
118 syncer::ModelTypeSet to_migrate, difference; | |
119 to_migrate.Put(syncer::PREFERENCES); | |
120 difference.Put(syncer::AUTOFILL); | |
121 difference.Put(syncer::BOOKMARKS); | |
122 | |
123 EXPECT_CALL(*manager(), state()) | |
124 .WillOnce(Return(DataTypeManager::CONFIGURED)); | |
125 EXPECT_CALL( | |
126 *manager(), | |
127 PurgeForMigration(_, syncer::CONFIGURE_REASON_MIGRATION)).Times(1); | |
128 EXPECT_CALL( | |
129 *manager(), | |
130 Configure(_, syncer::CONFIGURE_REASON_MIGRATION)).Times(1); | |
131 | |
132 migrator()->MigrateTypes(to_migrate); | |
133 EXPECT_EQ(BackendMigrator::DISABLING_TYPES, migrator()->state()); | |
134 | |
135 SetUnsyncedTypes(to_migrate); | |
136 SendConfigureDone(DataTypeManager::OK, difference); | |
137 EXPECT_EQ(BackendMigrator::REENABLING_TYPES, migrator()->state()); | |
138 | |
139 SetUnsyncedTypes(syncer::ModelTypeSet()); | |
140 SendConfigureDone(DataTypeManager::OK, preferred_types()); | |
141 EXPECT_EQ(BackendMigrator::IDLE, migrator()->state()); | |
142 | |
143 migrator()->RemoveMigrationObserver(&migration_observer); | |
144 } | |
145 | |
146 // Test that in the normal case with Nigori a migration transitions through | |
147 // each state and wind up back in IDLE. | |
148 TEST_F(SyncBackendMigratorTest, MigrateNigori) { | |
149 syncer::ModelTypeSet to_migrate, difference; | |
150 to_migrate.Put(syncer::NIGORI); | |
151 difference.Put(syncer::AUTOFILL); | |
152 difference.Put(syncer::BOOKMARKS); | |
153 | |
154 EXPECT_CALL(*manager(), state()) | |
155 .WillOnce(Return(DataTypeManager::CONFIGURED)); | |
156 | |
157 EXPECT_CALL(*manager(), PurgeForMigration(_, | |
158 syncer::CONFIGURE_REASON_MIGRATION)); | |
159 | |
160 migrator()->MigrateTypes(to_migrate); | |
161 EXPECT_EQ(BackendMigrator::DISABLING_TYPES, migrator()->state()); | |
162 | |
163 SetUnsyncedTypes(to_migrate); | |
164 SendConfigureDone(DataTypeManager::OK, difference); | |
165 EXPECT_EQ(BackendMigrator::REENABLING_TYPES, migrator()->state()); | |
166 | |
167 SetUnsyncedTypes(syncer::ModelTypeSet()); | |
168 SendConfigureDone(DataTypeManager::OK, preferred_types()); | |
169 EXPECT_EQ(BackendMigrator::IDLE, migrator()->state()); | |
170 } | |
171 | |
172 | |
173 // Test that the migrator waits for the data type manager to be idle before | |
174 // starting a migration. | |
175 TEST_F(SyncBackendMigratorTest, WaitToStart) { | |
176 syncer::ModelTypeSet to_migrate; | |
177 to_migrate.Put(syncer::PREFERENCES); | |
178 | |
179 EXPECT_CALL(*manager(), state()) | |
180 .WillOnce(Return(DataTypeManager::CONFIGURING)); | |
181 EXPECT_CALL(*manager(), Configure(_, _)).Times(0); | |
182 migrator()->MigrateTypes(to_migrate); | |
183 EXPECT_EQ(BackendMigrator::WAITING_TO_START, migrator()->state()); | |
184 | |
185 Mock::VerifyAndClearExpectations(manager()); | |
186 EXPECT_CALL(*manager(), state()) | |
187 .WillOnce(Return(DataTypeManager::CONFIGURED)); | |
188 EXPECT_CALL(*manager(), | |
189 PurgeForMigration(_, syncer::CONFIGURE_REASON_MIGRATION)); | |
190 SetUnsyncedTypes(syncer::ModelTypeSet()); | |
191 SendConfigureDone(DataTypeManager::OK, syncer::ModelTypeSet()); | |
192 | |
193 EXPECT_EQ(BackendMigrator::DISABLING_TYPES, migrator()->state()); | |
194 } | |
195 | |
196 // Test that the migrator can cope with a migration request while a migration | |
197 // is in progress. | |
198 TEST_F(SyncBackendMigratorTest, RestartMigration) { | |
199 syncer::ModelTypeSet to_migrate1, to_migrate2, to_migrate_union, bookmarks; | |
200 to_migrate1.Put(syncer::PREFERENCES); | |
201 to_migrate2.Put(syncer::AUTOFILL); | |
202 to_migrate_union.Put(syncer::PREFERENCES); | |
203 to_migrate_union.Put(syncer::AUTOFILL); | |
204 bookmarks.Put(syncer::BOOKMARKS); | |
205 | |
206 EXPECT_CALL(*manager(), state()) | |
207 .WillOnce(Return(DataTypeManager::CONFIGURED)); | |
208 EXPECT_CALL( | |
209 *manager(), | |
210 PurgeForMigration(_, syncer::CONFIGURE_REASON_MIGRATION)).Times(2); | |
211 migrator()->MigrateTypes(to_migrate1); | |
212 | |
213 EXPECT_EQ(BackendMigrator::DISABLING_TYPES, migrator()->state()); | |
214 migrator()->MigrateTypes(to_migrate2); | |
215 | |
216 const syncer::ModelTypeSet difference1 = | |
217 Difference(preferred_types(), to_migrate1); | |
218 | |
219 Mock::VerifyAndClearExpectations(manager()); | |
220 EXPECT_CALL( | |
221 *manager(), | |
222 PurgeForMigration(_, syncer::CONFIGURE_REASON_MIGRATION)).Times(1); | |
223 EXPECT_CALL(*manager(), Configure(_, syncer::CONFIGURE_REASON_MIGRATION)) | |
224 .Times(1); | |
225 SetUnsyncedTypes(to_migrate1); | |
226 SendConfigureDone(DataTypeManager::OK, difference1); | |
227 EXPECT_EQ(BackendMigrator::DISABLING_TYPES, migrator()->state()); | |
228 | |
229 SetUnsyncedTypes(to_migrate_union); | |
230 SendConfigureDone(DataTypeManager::OK, bookmarks); | |
231 EXPECT_EQ(BackendMigrator::REENABLING_TYPES, migrator()->state()); | |
232 } | |
233 | |
234 // Test that an external invocation of Configure(...) during a migration results | |
235 // in a migration reattempt. | |
236 TEST_F(SyncBackendMigratorTest, InterruptedWhileDisablingTypes) { | |
237 syncer::ModelTypeSet to_migrate; | |
238 syncer::ModelTypeSet difference; | |
239 to_migrate.Put(syncer::PREFERENCES); | |
240 difference.Put(syncer::AUTOFILL); | |
241 difference.Put(syncer::BOOKMARKS); | |
242 | |
243 EXPECT_CALL(*manager(), state()) | |
244 .WillOnce(Return(DataTypeManager::CONFIGURED)); | |
245 EXPECT_CALL(*manager(), PurgeForMigration(HasModelTypes(to_migrate), | |
246 syncer::CONFIGURE_REASON_MIGRATION)); | |
247 migrator()->MigrateTypes(to_migrate); | |
248 EXPECT_EQ(BackendMigrator::DISABLING_TYPES, migrator()->state()); | |
249 | |
250 Mock::VerifyAndClearExpectations(manager()); | |
251 EXPECT_CALL(*manager(), PurgeForMigration(HasModelTypes(to_migrate), | |
252 syncer::CONFIGURE_REASON_MIGRATION)); | |
253 SetUnsyncedTypes(syncer::ModelTypeSet()); | |
254 SendConfigureDone(DataTypeManager::OK, preferred_types()); | |
255 | |
256 EXPECT_EQ(BackendMigrator::DISABLING_TYPES, migrator()->state()); | |
257 } | |
258 | |
259 // Test that spurious OnConfigureDone events don't confuse the | |
260 // migrator while it's waiting for disabled types to have been purged | |
261 // from the sync db. | |
262 TEST_F(SyncBackendMigratorTest, WaitingForPurge) { | |
263 syncer::ModelTypeSet to_migrate, difference; | |
264 to_migrate.Put(syncer::PREFERENCES); | |
265 to_migrate.Put(syncer::AUTOFILL); | |
266 difference.Put(syncer::BOOKMARKS); | |
267 | |
268 EXPECT_CALL(*manager(), state()) | |
269 .WillOnce(Return(DataTypeManager::CONFIGURED)); | |
270 EXPECT_CALL( | |
271 *manager(), | |
272 PurgeForMigration(_, syncer::CONFIGURE_REASON_MIGRATION)).Times(1); | |
273 EXPECT_CALL( | |
274 *manager(), | |
275 Configure(_, syncer::CONFIGURE_REASON_MIGRATION)).Times(1); | |
276 | |
277 migrator()->MigrateTypes(to_migrate); | |
278 EXPECT_EQ(BackendMigrator::DISABLING_TYPES, migrator()->state()); | |
279 | |
280 SendConfigureDone(DataTypeManager::OK, difference); | |
281 EXPECT_EQ(BackendMigrator::DISABLING_TYPES, migrator()->state()); | |
282 | |
283 syncer::ModelTypeSet prefs; | |
284 prefs.Put(syncer::PREFERENCES); | |
285 SetUnsyncedTypes(prefs); | |
286 SendConfigureDone(DataTypeManager::OK, difference); | |
287 EXPECT_EQ(BackendMigrator::DISABLING_TYPES, migrator()->state()); | |
288 | |
289 SetUnsyncedTypes(to_migrate); | |
290 SendConfigureDone(DataTypeManager::OK, difference); | |
291 EXPECT_EQ(BackendMigrator::REENABLING_TYPES, migrator()->state()); | |
292 } | |
293 | |
294 TEST_F(SyncBackendMigratorTest, MigratedTypeDisabledByUserDuringMigration) { | |
295 syncer::ModelTypeSet to_migrate; | |
296 to_migrate.Put(syncer::PREFERENCES); | |
297 | |
298 EXPECT_CALL(*manager(), state()) | |
299 .WillOnce(Return(DataTypeManager::CONFIGURED)); | |
300 EXPECT_CALL( | |
301 *manager(), | |
302 PurgeForMigration(_, syncer::CONFIGURE_REASON_MIGRATION)).Times(1); | |
303 EXPECT_CALL( | |
304 *manager(), | |
305 Configure(_, syncer::CONFIGURE_REASON_MIGRATION)).Times(1); | |
306 migrator()->MigrateTypes(to_migrate); | |
307 | |
308 RemovePreferredType(syncer::PREFERENCES); | |
309 SetUnsyncedTypes(to_migrate); | |
310 SendConfigureDone(DataTypeManager::OK, preferred_types()); | |
311 EXPECT_EQ(BackendMigrator::REENABLING_TYPES, migrator()->state()); | |
312 SetUnsyncedTypes(syncer::ModelTypeSet()); | |
313 SendConfigureDone(DataTypeManager::OK, preferred_types()); | |
314 EXPECT_EQ(BackendMigrator::IDLE, migrator()->state()); | |
315 } | |
316 | |
317 TEST_F(SyncBackendMigratorTest, ConfigureFailure) { | |
318 syncer::ModelTypeSet to_migrate; | |
319 to_migrate.Put(syncer::PREFERENCES); | |
320 | |
321 EXPECT_CALL(*manager(), state()) | |
322 .WillOnce(Return(DataTypeManager::CONFIGURED)); | |
323 EXPECT_CALL( | |
324 *manager(), | |
325 PurgeForMigration(_, syncer::CONFIGURE_REASON_MIGRATION)).Times(1); | |
326 migrator()->MigrateTypes(to_migrate); | |
327 SetUnsyncedTypes(syncer::ModelTypeSet()); | |
328 SendConfigureDone(DataTypeManager::ABORTED, syncer::ModelTypeSet()); | |
329 EXPECT_EQ(BackendMigrator::IDLE, migrator()->state()); | |
330 } | |
331 | |
332 }; // namespace browser_sync | |
OLD | NEW |