Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: components/sync_driver/glue/sync_backend_host_impl_unittest.cc

Issue 2203673002: [Sync] Move //components/sync_driver to //components/sync/driver. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sd-a
Patch Set: Full change rebased on static lib. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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/glue/sync_backend_host_impl.h"
6
7 #include <stdint.h>
8
9 #include <cstddef>
10 #include <map>
11 #include <memory>
12 #include <utility>
13
14 #include "base/files/file_util.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/location.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/run_loop.h"
19 #include "base/synchronization/waitable_event.h"
20 #include "base/test/test_timeouts.h"
21 #include "base/threading/thread_task_runner_handle.h"
22 #include "base/time/time.h"
23 #include "components/invalidation/impl/invalidator_storage.h"
24 #include "components/invalidation/impl/profile_invalidation_provider.h"
25 #include "components/invalidation/public/invalidator_state.h"
26 #include "components/invalidation/public/object_id_invalidation_map.h"
27 #include "components/sync/base/experiments.h"
28 #include "components/sync/base/model_type.h"
29 #include "components/sync/base/test_unrecoverable_error_handler.h"
30 #include "components/sync/core/http_bridge_network_resources.h"
31 #include "components/sync/core/network_resources.h"
32 #include "components/sync/core/sync_manager_factory.h"
33 #include "components/sync/core/test/fake_sync_manager.h"
34 #include "components/sync/engine/model_safe_worker.h"
35 #include "components/sync/engine/passive_model_worker.h"
36 #include "components/sync/protocol/encryption.pb.h"
37 #include "components/sync/protocol/sync_protocol_error.h"
38 #include "components/sync/sessions/commit_counters.h"
39 #include "components/sync/sessions/status_counters.h"
40 #include "components/sync/sessions/update_counters.h"
41 #include "components/sync/test/callback_counter.h"
42 #include "components/sync_driver/device_info.h"
43 #include "components/sync_driver/fake_sync_client.h"
44 #include "components/sync_driver/sync_frontend.h"
45 #include "components/sync_driver/sync_prefs.h"
46 #include "components/syncable_prefs/pref_service_syncable.h"
47 #include "components/syncable_prefs/testing_pref_service_syncable.h"
48 #include "google/cacheinvalidation/include/types.h"
49 #include "google_apis/gaia/gaia_constants.h"
50 #include "net/url_request/test_url_fetcher_factory.h"
51 #include "net/url_request/url_request_context_getter.h"
52 #include "testing/gmock/include/gmock/gmock.h"
53 #include "testing/gtest/include/gtest/gtest.h"
54 #include "url/gurl.h"
55
56 using syncer::FakeSyncManager;
57 using syncer::SyncManager;
58 using ::testing::InvokeWithoutArgs;
59 using ::testing::StrictMock;
60 using ::testing::_;
61
62 namespace browser_sync {
63
64 namespace {
65
66 static const base::FilePath::CharType kTestSyncDir[] =
67 FILE_PATH_LITERAL("sync-test");
68
69 ACTION_P(Signal, event) {
70 event->Signal();
71 }
72
73 void EmptyNetworkTimeUpdate(const base::Time&,
74 const base::TimeDelta&,
75 const base::TimeDelta&) {}
76
77 void QuitMessageLoop() {
78 base::MessageLoop::current()->QuitWhenIdle();
79 }
80
81 class MockSyncFrontend : public sync_driver::SyncFrontend {
82 public:
83 virtual ~MockSyncFrontend() {}
84
85 MOCK_METHOD4(
86 OnBackendInitialized,
87 void(const syncer::WeakHandle<syncer::JsBackend>&,
88 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>&,
89 const std::string&,
90 bool));
91 MOCK_METHOD0(OnSyncCycleCompleted, void());
92 MOCK_METHOD1(OnConnectionStatusChange,
93 void(syncer::ConnectionStatus status));
94 MOCK_METHOD0(OnClearServerDataSucceeded, void());
95 MOCK_METHOD0(OnClearServerDataFailed, void());
96 MOCK_METHOD2(OnPassphraseRequired,
97 void(syncer::PassphraseRequiredReason,
98 const sync_pb::EncryptedData&));
99 MOCK_METHOD0(OnPassphraseAccepted, void());
100 MOCK_METHOD2(OnEncryptedTypesChanged,
101 void(syncer::ModelTypeSet, bool));
102 MOCK_METHOD0(OnEncryptionComplete, void());
103 MOCK_METHOD1(OnMigrationNeededForTypes, void(syncer::ModelTypeSet));
104 MOCK_METHOD1(OnProtocolEvent, void(const syncer::ProtocolEvent&));
105 MOCK_METHOD2(OnDirectoryTypeCommitCounterUpdated,
106 void(syncer::ModelType, const syncer::CommitCounters&));
107 MOCK_METHOD2(OnDirectoryTypeUpdateCounterUpdated,
108 void(syncer::ModelType, const syncer::UpdateCounters&));
109 MOCK_METHOD2(OnDirectoryTypeStatusCounterUpdated,
110 void(syncer::ModelType, const syncer::StatusCounters&));
111 MOCK_METHOD1(OnExperimentsChanged,
112 void(const syncer::Experiments&));
113 MOCK_METHOD1(OnActionableError,
114 void(const syncer::SyncProtocolError& sync_error));
115 MOCK_METHOD0(OnSyncConfigureRetry, void());
116 MOCK_METHOD1(
117 OnLocalSetPassphraseEncryption,
118 void(const syncer::SyncEncryptionHandler::NigoriState& nigori_state));
119 };
120
121 class FakeSyncManagerFactory : public syncer::SyncManagerFactory {
122 public:
123 explicit FakeSyncManagerFactory(FakeSyncManager** fake_manager)
124 : fake_manager_(fake_manager) {
125 *fake_manager_ = NULL;
126 }
127 ~FakeSyncManagerFactory() override {}
128
129 // SyncManagerFactory implementation. Called on the sync thread.
130 std::unique_ptr<SyncManager> CreateSyncManager(
131 const std::string& /* name */) override {
132 *fake_manager_ = new FakeSyncManager(initial_sync_ended_types_,
133 progress_marker_types_,
134 configure_fail_types_);
135 return std::unique_ptr<SyncManager>(*fake_manager_);
136 }
137
138 void set_initial_sync_ended_types(syncer::ModelTypeSet types) {
139 initial_sync_ended_types_ = types;
140 }
141
142 void set_progress_marker_types(syncer::ModelTypeSet types) {
143 progress_marker_types_ = types;
144 }
145
146 void set_configure_fail_types(syncer::ModelTypeSet types) {
147 configure_fail_types_ = types;
148 }
149
150 private:
151 syncer::ModelTypeSet initial_sync_ended_types_;
152 syncer::ModelTypeSet progress_marker_types_;
153 syncer::ModelTypeSet configure_fail_types_;
154 FakeSyncManager** fake_manager_;
155 };
156
157 class BackendSyncClient : public sync_driver::FakeSyncClient {
158 public:
159 scoped_refptr<syncer::ModelSafeWorker> CreateModelWorkerForGroup(
160 syncer::ModelSafeGroup group,
161 syncer::WorkerLoopDestructionObserver* observer) override {
162 switch (group) {
163 case syncer::GROUP_PASSIVE:
164 return new syncer::PassiveModelWorker(observer);
165 default:
166 return nullptr;
167 }
168 }
169 };
170
171 class SyncBackendHostTest : public testing::Test {
172 protected:
173 SyncBackendHostTest()
174 : fake_manager_(NULL) {}
175
176 ~SyncBackendHostTest() override {}
177
178 void SetUp() override {
179 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
180
181 sync_driver::SyncPrefs::RegisterProfilePrefs(pref_service_.registry());
182
183 sync_prefs_.reset(new sync_driver::SyncPrefs(&pref_service_));
184 backend_.reset(new SyncBackendHostImpl(
185 "dummyDebugName", &sync_client_,
186 base::ThreadTaskRunnerHandle::Get(),
187 nullptr,
188 sync_prefs_->AsWeakPtr(),
189 temp_dir_.path().Append(base::FilePath(kTestSyncDir))));
190 credentials_.account_id = "user@example.com";
191 credentials_.email = "user@example.com";
192 credentials_.sync_token = "sync_token";
193 credentials_.scope_set.insert(GaiaConstants::kChromeSyncOAuth2Scope);
194
195 fake_manager_factory_.reset(new FakeSyncManagerFactory(&fake_manager_));
196
197 // These types are always implicitly enabled.
198 enabled_types_.PutAll(syncer::ControlTypes());
199
200 // NOTE: We can't include Passwords or Typed URLs due to the Sync Backend
201 // Registrar removing them if it can't find their model workers.
202 enabled_types_.Put(syncer::BOOKMARKS);
203 enabled_types_.Put(syncer::PREFERENCES);
204 enabled_types_.Put(syncer::SESSIONS);
205 enabled_types_.Put(syncer::SEARCH_ENGINES);
206 enabled_types_.Put(syncer::AUTOFILL);
207
208 network_resources_.reset(new syncer::HttpBridgeNetworkResources());
209 }
210
211 void TearDown() override {
212 if (backend_) {
213 backend_->StopSyncingForShutdown();
214 backend_->Shutdown(syncer::STOP_SYNC);
215 }
216 backend_.reset();
217 sync_prefs_.reset();
218 // Pump messages posted by the sync thread.
219 base::RunLoop().RunUntilIdle();
220 }
221
222 // Synchronously initializes the backend.
223 void InitializeBackend(bool expect_success) {
224 EXPECT_CALL(mock_frontend_, OnBackendInitialized(_, _, _, expect_success)).
225 WillOnce(InvokeWithoutArgs(QuitMessageLoop));
226 SyncBackendHost::HttpPostProviderFactoryGetter
227 http_post_provider_factory_getter =
228 base::Bind(&syncer::NetworkResources::GetHttpPostProviderFactory,
229 base::Unretained(network_resources_.get()),
230 nullptr,
231 base::Bind(&EmptyNetworkTimeUpdate));
232 backend_->Initialize(
233 &mock_frontend_, std::unique_ptr<base::Thread>(),
234 base::ThreadTaskRunnerHandle::Get(),
235 base::ThreadTaskRunnerHandle::Get(),
236 syncer::WeakHandle<syncer::JsEventHandler>(), GURL(std::string()),
237 std::string(), credentials_, true, std::move(fake_manager_factory_),
238 MakeWeakHandle(test_unrecoverable_error_handler_.GetWeakPtr()),
239 base::Closure(), http_post_provider_factory_getter,
240 std::move(saved_nigori_state_));
241 base::RunLoop run_loop;
242 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE,
243 run_loop.QuitClosure(),
244 TestTimeouts::action_timeout());
245 run_loop.Run();
246 // |fake_manager_factory_|'s fake_manager() is set on the sync
247 // thread, but we can rely on the message loop barriers to
248 // guarantee that we see the updated value.
249 DCHECK(fake_manager_);
250 }
251
252 // Synchronously configures the backend's datatypes.
253 syncer::ModelTypeSet ConfigureDataTypes(
254 syncer::ModelTypeSet types_to_add,
255 syncer::ModelTypeSet types_to_remove,
256 syncer::ModelTypeSet types_to_unapply) {
257 sync_driver::BackendDataTypeConfigurer::DataTypeConfigStateMap
258 config_state_map;
259 sync_driver::BackendDataTypeConfigurer::SetDataTypesState(
260 sync_driver::BackendDataTypeConfigurer::CONFIGURE_ACTIVE,
261 types_to_add,
262 &config_state_map);
263 sync_driver::BackendDataTypeConfigurer::SetDataTypesState(
264 sync_driver::BackendDataTypeConfigurer::DISABLED,
265 types_to_remove, &config_state_map);
266 sync_driver::BackendDataTypeConfigurer::SetDataTypesState(
267 sync_driver::BackendDataTypeConfigurer::UNREADY,
268 types_to_unapply, &config_state_map);
269
270 types_to_add.PutAll(syncer::ControlTypes());
271 syncer::ModelTypeSet ready_types = backend_->ConfigureDataTypes(
272 syncer::CONFIGURE_REASON_RECONFIGURATION, config_state_map,
273 base::Bind(&SyncBackendHostTest::DownloadReady, base::Unretained(this)),
274 base::Bind(&SyncBackendHostTest::OnDownloadRetry,
275 base::Unretained(this)));
276 base::RunLoop run_loop;
277 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE,
278 run_loop.QuitClosure(),
279 TestTimeouts::action_timeout());
280 run_loop.Run();
281 return ready_types;
282 }
283
284 protected:
285 void DownloadReady(syncer::ModelTypeSet succeeded_types,
286 syncer::ModelTypeSet failed_types) {
287 base::MessageLoop::current()->QuitWhenIdle();
288 }
289
290 void OnDownloadRetry() {
291 NOTIMPLEMENTED();
292 }
293
294 base::MessageLoop message_loop_;
295 base::ScopedTempDir temp_dir_;
296 syncable_prefs::TestingPrefServiceSyncable pref_service_;
297 StrictMock<MockSyncFrontend> mock_frontend_;
298 syncer::SyncCredentials credentials_;
299 BackendSyncClient sync_client_;
300 syncer::TestUnrecoverableErrorHandler test_unrecoverable_error_handler_;
301 std::unique_ptr<sync_driver::SyncPrefs> sync_prefs_;
302 std::unique_ptr<SyncBackendHostImpl> backend_;
303 std::unique_ptr<FakeSyncManagerFactory> fake_manager_factory_;
304 FakeSyncManager* fake_manager_;
305 syncer::ModelTypeSet enabled_types_;
306 std::unique_ptr<syncer::NetworkResources> network_resources_;
307 std::unique_ptr<syncer::SyncEncryptionHandler::NigoriState>
308 saved_nigori_state_;
309 };
310
311 // Test basic initialization with no initial types (first time initialization).
312 // Only the nigori should be configured.
313 TEST_F(SyncBackendHostTest, InitShutdown) {
314 InitializeBackend(true);
315 EXPECT_EQ(syncer::ControlTypes(),
316 fake_manager_->GetAndResetDownloadedTypes());
317 EXPECT_EQ(syncer::ControlTypes(), fake_manager_->InitialSyncEndedTypes());
318 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
319 syncer::ControlTypes()).Empty());
320 }
321
322 // Test first time sync scenario. All types should be properly configured.
323 TEST_F(SyncBackendHostTest, FirstTimeSync) {
324 InitializeBackend(true);
325 EXPECT_EQ(syncer::ControlTypes(),
326 fake_manager_->GetAndResetDownloadedTypes());
327 EXPECT_EQ(syncer::ControlTypes(), fake_manager_->InitialSyncEndedTypes());
328 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
329 syncer::ControlTypes()).Empty());
330
331 syncer::ModelTypeSet ready_types = ConfigureDataTypes(
332 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
333 syncer::ModelTypeSet());
334 // Nigori is always downloaded so won't be ready.
335 EXPECT_EQ(syncer::Difference(syncer::ControlTypes(),
336 syncer::ModelTypeSet(syncer::NIGORI)),
337 ready_types);
338 EXPECT_TRUE(fake_manager_->GetAndResetDownloadedTypes().HasAll(
339 Difference(enabled_types_, syncer::ControlTypes())));
340 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
341 EXPECT_EQ(enabled_types_, fake_manager_->GetAndResetEnabledTypes());
342 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
343 enabled_types_).Empty());
344 }
345
346 // Test the restart after setting up sync scenario. No enabled types should be
347 // downloaded or cleaned.
348 TEST_F(SyncBackendHostTest, Restart) {
349 sync_prefs_->SetFirstSetupComplete();
350 syncer::ModelTypeSet all_but_nigori = enabled_types_;
351 fake_manager_factory_->set_progress_marker_types(enabled_types_);
352 fake_manager_factory_->set_initial_sync_ended_types(enabled_types_);
353 InitializeBackend(true);
354 EXPECT_TRUE(fake_manager_->GetAndResetDownloadedTypes().Empty());
355 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(),
356 enabled_types_).Empty());
357 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
358 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
359 enabled_types_).Empty());
360
361 syncer::ModelTypeSet ready_types = ConfigureDataTypes(
362 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
363 syncer::ModelTypeSet());
364 EXPECT_EQ(enabled_types_, ready_types);
365 EXPECT_TRUE(fake_manager_->GetAndResetDownloadedTypes().Empty());
366 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(),
367 enabled_types_).Empty());
368 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
369 EXPECT_EQ(enabled_types_, fake_manager_->GetAndResetEnabledTypes());
370 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
371 enabled_types_).Empty());
372 }
373
374 // Test a sync restart scenario where some types had never finished configuring.
375 // The partial types should be purged, then reconfigured properly.
376 TEST_F(SyncBackendHostTest, PartialTypes) {
377 sync_prefs_->SetFirstSetupComplete();
378 // Set sync manager behavior before passing it down. All types have progress
379 // markers, but nigori and bookmarks are missing initial sync ended.
380 syncer::ModelTypeSet partial_types(syncer::NIGORI, syncer::BOOKMARKS);
381 syncer::ModelTypeSet full_types =
382 Difference(enabled_types_, partial_types);
383 fake_manager_factory_->set_progress_marker_types(enabled_types_);
384 fake_manager_factory_->set_initial_sync_ended_types(full_types);
385
386 // Bringing up the backend should purge all partial types, then proceed to
387 // download the Nigori.
388 InitializeBackend(true);
389 EXPECT_EQ(syncer::ModelTypeSet(syncer::NIGORI),
390 fake_manager_->GetAndResetDownloadedTypes());
391 EXPECT_TRUE(fake_manager_->GetAndResetCleanedTypes().HasAll(partial_types));
392 EXPECT_EQ(Union(full_types, syncer::ModelTypeSet(syncer::NIGORI)),
393 fake_manager_->InitialSyncEndedTypes());
394 EXPECT_EQ(
395 Difference(partial_types, syncer::ModelTypeSet(syncer::NIGORI)),
396 fake_manager_->GetTypesWithEmptyProgressMarkerToken(enabled_types_));
397
398 // Now do the actual configuration, which should download and apply bookmarks.
399 syncer::ModelTypeSet ready_types = ConfigureDataTypes(
400 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
401 syncer::ModelTypeSet());
402 EXPECT_EQ(full_types, ready_types);
403 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(),
404 enabled_types_).Empty());
405 EXPECT_EQ(partial_types, fake_manager_->GetAndResetDownloadedTypes());
406 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
407 EXPECT_EQ(enabled_types_, fake_manager_->GetAndResetEnabledTypes());
408 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
409 enabled_types_).Empty());
410 }
411
412 // Test the behavior when we lose the sync db. Although we already have types
413 // enabled, we should re-download all of them because we lost their data.
414 TEST_F(SyncBackendHostTest, LostDB) {
415 sync_prefs_->SetFirstSetupComplete();
416 // Initialization should fetch the Nigori node. Everything else should be
417 // left untouched.
418 InitializeBackend(true);
419 EXPECT_EQ(syncer::ModelTypeSet(syncer::ControlTypes()),
420 fake_manager_->GetAndResetDownloadedTypes());
421 EXPECT_EQ(syncer::ModelTypeSet(syncer::ControlTypes()),
422 fake_manager_->InitialSyncEndedTypes());
423 EXPECT_EQ(
424 Difference(enabled_types_, syncer::ControlTypes()),
425 fake_manager_->GetTypesWithEmptyProgressMarkerToken(enabled_types_));
426
427 // The database was empty, so any cleaning is entirely optional. We want to
428 // reset this value before running the next part of the test, though.
429 fake_manager_->GetAndResetCleanedTypes();
430
431 // The actual configuration should redownload and apply all the enabled types.
432 syncer::ModelTypeSet ready_types = ConfigureDataTypes(
433 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
434 syncer::ModelTypeSet());
435 // Nigori is always downloaded so won't be ready.
436 EXPECT_EQ(syncer::Difference(syncer::ControlTypes(),
437 syncer::ModelTypeSet(syncer::NIGORI)),
438 ready_types);
439 EXPECT_TRUE(fake_manager_->GetAndResetDownloadedTypes().HasAll(
440 Difference(enabled_types_, syncer::ControlTypes())));
441 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(),
442 enabled_types_).Empty());
443 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
444 EXPECT_EQ(enabled_types_, fake_manager_->GetAndResetEnabledTypes());
445 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
446 enabled_types_).Empty());
447 }
448
449 TEST_F(SyncBackendHostTest, DisableTypes) {
450 // Simulate first time sync.
451 InitializeBackend(true);
452 fake_manager_->GetAndResetCleanedTypes();
453 syncer::ModelTypeSet ready_types = ConfigureDataTypes(
454 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
455 syncer::ModelTypeSet());
456 // Nigori is always downloaded so won't be ready.
457 EXPECT_EQ(syncer::Difference(syncer::ControlTypes(),
458 syncer::ModelTypeSet(syncer::NIGORI)),
459 ready_types);
460 EXPECT_EQ(enabled_types_, fake_manager_->GetAndResetDownloadedTypes());
461 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(),
462 enabled_types_).Empty());
463 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
464 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
465 enabled_types_).Empty());
466
467 // Then disable two datatypes.
468 syncer::ModelTypeSet disabled_types(syncer::BOOKMARKS,
469 syncer::SEARCH_ENGINES);
470 syncer::ModelTypeSet old_types = enabled_types_;
471 enabled_types_.RemoveAll(disabled_types);
472 ready_types = ConfigureDataTypes(
473 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
474 syncer::ModelTypeSet());
475
476 // Only those datatypes disabled should be cleaned. Nothing should be
477 // downloaded.
478 EXPECT_EQ(enabled_types_, ready_types);
479 EXPECT_TRUE(fake_manager_->GetAndResetDownloadedTypes().Empty());
480 EXPECT_EQ(disabled_types,
481 Intersection(fake_manager_->GetAndResetCleanedTypes(), old_types));
482 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
483 EXPECT_EQ(enabled_types_, fake_manager_->GetAndResetEnabledTypes());
484 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
485 enabled_types_).Empty());
486 }
487
488 TEST_F(SyncBackendHostTest, AddTypes) {
489 // Simulate first time sync.
490 InitializeBackend(true);
491 fake_manager_->GetAndResetCleanedTypes();
492 syncer::ModelTypeSet ready_types = ConfigureDataTypes(
493 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
494 syncer::ModelTypeSet());
495 // Nigori is always downloaded so won't be ready.
496 EXPECT_EQ(syncer::Difference(syncer::ControlTypes(),
497 syncer::ModelTypeSet(syncer::NIGORI)),
498 ready_types);
499 EXPECT_EQ(enabled_types_, fake_manager_->GetAndResetDownloadedTypes());
500 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(),
501 enabled_types_).Empty());
502 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
503 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
504 enabled_types_).Empty());
505
506 // Then add two datatypes.
507 syncer::ModelTypeSet new_types(syncer::EXTENSIONS,
508 syncer::APPS);
509 enabled_types_.PutAll(new_types);
510 ready_types = ConfigureDataTypes(
511 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
512 syncer::ModelTypeSet());
513
514 // Only those datatypes added should be downloaded (plus nigori). Nothing
515 // should be cleaned aside from the disabled types.
516 new_types.Put(syncer::NIGORI);
517 EXPECT_EQ(syncer::Difference(enabled_types_, new_types), ready_types);
518 EXPECT_EQ(new_types, fake_manager_->GetAndResetDownloadedTypes());
519 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(),
520 enabled_types_).Empty());
521 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
522 EXPECT_EQ(enabled_types_, fake_manager_->GetAndResetEnabledTypes());
523 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
524 enabled_types_).Empty());
525 }
526
527 // And and disable in the same configuration.
528 TEST_F(SyncBackendHostTest, AddDisableTypes) {
529 // Simulate first time sync.
530 InitializeBackend(true);
531 fake_manager_->GetAndResetCleanedTypes();
532 syncer::ModelTypeSet ready_types = ConfigureDataTypes(
533 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
534 syncer::ModelTypeSet());
535 // Nigori is always downloaded so won't be ready.
536 EXPECT_EQ(syncer::Difference(syncer::ControlTypes(),
537 syncer::ModelTypeSet(syncer::NIGORI)),
538 ready_types);
539 EXPECT_EQ(enabled_types_, fake_manager_->GetAndResetDownloadedTypes());
540 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(),
541 enabled_types_).Empty());
542 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
543 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
544 enabled_types_).Empty());
545
546 // Then add two datatypes.
547 syncer::ModelTypeSet old_types = enabled_types_;
548 syncer::ModelTypeSet disabled_types(syncer::BOOKMARKS,
549 syncer::SEARCH_ENGINES);
550 syncer::ModelTypeSet new_types(syncer::EXTENSIONS,
551 syncer::APPS);
552 enabled_types_.PutAll(new_types);
553 enabled_types_.RemoveAll(disabled_types);
554 ready_types = ConfigureDataTypes(
555 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
556 syncer::ModelTypeSet());
557
558 // Only those datatypes added should be downloaded (plus nigori). Nothing
559 // should be cleaned aside from the disabled types.
560 new_types.Put(syncer::NIGORI);
561 EXPECT_EQ(syncer::Difference(enabled_types_, new_types), ready_types);
562 EXPECT_EQ(new_types, fake_manager_->GetAndResetDownloadedTypes());
563 EXPECT_EQ(disabled_types,
564 Intersection(fake_manager_->GetAndResetCleanedTypes(), old_types));
565 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
566 EXPECT_EQ(enabled_types_, fake_manager_->GetAndResetEnabledTypes());
567 EXPECT_EQ(disabled_types,
568 fake_manager_->GetTypesWithEmptyProgressMarkerToken(old_types));
569 }
570
571 // Test restarting the browser to newly supported datatypes. The new datatypes
572 // should be downloaded on the configuration after backend initialization.
573 TEST_F(SyncBackendHostTest, NewlySupportedTypes) {
574 sync_prefs_->SetFirstSetupComplete();
575 // Set sync manager behavior before passing it down. All types have progress
576 // markers and initial sync ended except the new types.
577 syncer::ModelTypeSet old_types = enabled_types_;
578 fake_manager_factory_->set_progress_marker_types(old_types);
579 fake_manager_factory_->set_initial_sync_ended_types(old_types);
580 syncer::ModelTypeSet new_types(syncer::APP_SETTINGS,
581 syncer::EXTENSION_SETTINGS);
582 enabled_types_.PutAll(new_types);
583
584 // Does nothing.
585 InitializeBackend(true);
586 EXPECT_TRUE(fake_manager_->GetAndResetDownloadedTypes().Empty());
587 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(),
588 old_types).Empty());
589 EXPECT_EQ(old_types, fake_manager_->InitialSyncEndedTypes());
590 EXPECT_EQ(new_types, fake_manager_->GetTypesWithEmptyProgressMarkerToken(
591 enabled_types_));
592
593 // Downloads and applies the new types (plus nigori).
594 syncer::ModelTypeSet ready_types = ConfigureDataTypes(
595 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
596 syncer::ModelTypeSet());
597
598 new_types.Put(syncer::NIGORI);
599 EXPECT_EQ(syncer::Difference(old_types, syncer::ModelTypeSet(syncer::NIGORI)),
600 ready_types);
601 EXPECT_EQ(new_types, fake_manager_->GetAndResetDownloadedTypes());
602 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(),
603 enabled_types_).Empty());
604 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
605 EXPECT_EQ(enabled_types_, fake_manager_->GetAndResetEnabledTypes());
606 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
607 enabled_types_).Empty());
608 }
609
610 // Test the newly supported types scenario, but with the presence of partial
611 // types as well. Both partial and newly supported types should be downloaded
612 // the configuration.
613 TEST_F(SyncBackendHostTest, NewlySupportedTypesWithPartialTypes) {
614 sync_prefs_->SetFirstSetupComplete();
615 // Set sync manager behavior before passing it down. All types have progress
616 // markers and initial sync ended except the new types.
617 syncer::ModelTypeSet old_types = enabled_types_;
618 syncer::ModelTypeSet partial_types(syncer::NIGORI, syncer::BOOKMARKS);
619 syncer::ModelTypeSet full_types =
620 Difference(enabled_types_, partial_types);
621 fake_manager_factory_->set_progress_marker_types(old_types);
622 fake_manager_factory_->set_initial_sync_ended_types(full_types);
623 syncer::ModelTypeSet new_types(syncer::APP_SETTINGS,
624 syncer::EXTENSION_SETTINGS);
625 enabled_types_.PutAll(new_types);
626
627 // Purge the partial types. The nigori will be among the purged types, but
628 // the syncer will re-download it by the time the initialization is complete.
629 InitializeBackend(true);
630 EXPECT_EQ(syncer::ModelTypeSet(syncer::NIGORI),
631 fake_manager_->GetAndResetDownloadedTypes());
632 EXPECT_TRUE(fake_manager_->GetAndResetCleanedTypes().HasAll(partial_types));
633 EXPECT_EQ(syncer::Union(full_types, syncer::ModelTypeSet(syncer::NIGORI)),
634 fake_manager_->InitialSyncEndedTypes());
635 EXPECT_EQ(
636 Union(new_types,
637 Difference(partial_types, syncer::ModelTypeSet(syncer::NIGORI))),
638 fake_manager_->GetTypesWithEmptyProgressMarkerToken(enabled_types_));
639
640 // Downloads and applies the new types and partial types (which includes
641 // nigori anyways).
642 syncer::ModelTypeSet ready_types = ConfigureDataTypes(
643 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
644 syncer::ModelTypeSet());
645 EXPECT_EQ(full_types, ready_types);
646 EXPECT_EQ(Union(new_types, partial_types),
647 fake_manager_->GetAndResetDownloadedTypes());
648 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(),
649 enabled_types_).Empty());
650 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
651 EXPECT_EQ(enabled_types_, fake_manager_->GetAndResetEnabledTypes());
652 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
653 enabled_types_).Empty());
654 }
655
656 // Verify that downloading control types only downloads those types that do
657 // not have initial sync ended set.
658 TEST_F(SyncBackendHostTest, DownloadControlTypes) {
659 sync_prefs_->SetFirstSetupComplete();
660 // Set sync manager behavior before passing it down. Experiments and device
661 // info are new types without progress markers or initial sync ended, while
662 // all other types have been fully downloaded and applied.
663 syncer::ModelTypeSet new_types(syncer::EXPERIMENTS, syncer::NIGORI);
664 syncer::ModelTypeSet old_types =
665 Difference(enabled_types_, new_types);
666 fake_manager_factory_->set_progress_marker_types(old_types);
667 fake_manager_factory_->set_initial_sync_ended_types(old_types);
668
669 // Bringing up the backend should download the new types without downloading
670 // any old types.
671 InitializeBackend(true);
672 EXPECT_EQ(new_types, fake_manager_->GetAndResetDownloadedTypes());
673 EXPECT_EQ(Difference(syncer::ModelTypeSet::All(), enabled_types_),
674 fake_manager_->GetAndResetCleanedTypes());
675 EXPECT_EQ(enabled_types_, fake_manager_->InitialSyncEndedTypes());
676 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
677 enabled_types_).Empty());
678 }
679
680 // Fail to download control types. It's believed that there is a server bug
681 // which can allow this to happen (crbug.com/164288). The sync backend host
682 // should detect this condition and fail to initialize the backend.
683 //
684 // The failure is "silent" in the sense that the GetUpdates request appears to
685 // be successful, but it returned no results. This means that the usual
686 // download retry logic will not be invoked.
687 TEST_F(SyncBackendHostTest, SilentlyFailToDownloadControlTypes) {
688 fake_manager_factory_->set_configure_fail_types(syncer::ModelTypeSet::All());
689 InitializeBackend(false);
690 }
691
692 // Test that local refresh requests are delivered to sync.
693 TEST_F(SyncBackendHostTest, ForwardLocalRefreshRequest) {
694 InitializeBackend(true);
695
696 syncer::ModelTypeSet set1 = syncer::ModelTypeSet::All();
697 backend_->TriggerRefresh(set1);
698 fake_manager_->WaitForSyncThread();
699 EXPECT_EQ(set1, fake_manager_->GetLastRefreshRequestTypes());
700
701 syncer::ModelTypeSet set2 = syncer::ModelTypeSet(syncer::SESSIONS);
702 backend_->TriggerRefresh(set2);
703 fake_manager_->WaitForSyncThread();
704 EXPECT_EQ(set2, fake_manager_->GetLastRefreshRequestTypes());
705 }
706
707 // Test that configuration on signin sends the proper GU source.
708 TEST_F(SyncBackendHostTest, DownloadControlTypesNewClient) {
709 InitializeBackend(true);
710 EXPECT_EQ(syncer::CONFIGURE_REASON_NEW_CLIENT,
711 fake_manager_->GetAndResetConfigureReason());
712 }
713
714 // Test that configuration on restart sends the proper GU source.
715 TEST_F(SyncBackendHostTest, DownloadControlTypesRestart) {
716 sync_prefs_->SetFirstSetupComplete();
717 fake_manager_factory_->set_progress_marker_types(enabled_types_);
718 fake_manager_factory_->set_initial_sync_ended_types(enabled_types_);
719 InitializeBackend(true);
720 EXPECT_EQ(syncer::CONFIGURE_REASON_NEWLY_ENABLED_DATA_TYPE,
721 fake_manager_->GetAndResetConfigureReason());
722 }
723
724 // It is SyncBackendHostCore responsibility to cleanup Sync Data folder if sync
725 // setup hasn't been completed. This test ensures that cleanup happens.
726 TEST_F(SyncBackendHostTest, TestStartupWithOldSyncData) {
727 const char* nonsense = "slon";
728 base::FilePath temp_directory = temp_dir_.path().Append(
729 base::FilePath(kTestSyncDir));
730 base::FilePath sync_file = temp_directory.AppendASCII("SyncData.sqlite3");
731 ASSERT_TRUE(base::CreateDirectory(temp_directory));
732 ASSERT_NE(-1, base::WriteFile(sync_file, nonsense, strlen(nonsense)));
733
734 InitializeBackend(true);
735
736 EXPECT_FALSE(base::PathExists(sync_file));
737 }
738
739 // If bookmarks encounter an error that results in disabling without purging
740 // (such as when the type is unready), and then is explicitly disabled, the
741 // SyncBackendHost needs to tell the manager to purge the type, even though
742 // it's already disabled (crbug.com/386778).
743 TEST_F(SyncBackendHostTest, DisableThenPurgeType) {
744 syncer::ModelTypeSet error_types(syncer::BOOKMARKS);
745
746 InitializeBackend(true);
747
748 // First enable the types.
749 syncer::ModelTypeSet ready_types = ConfigureDataTypes(
750 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
751 syncer::ModelTypeSet());
752
753 // Nigori is always downloaded so won't be ready.
754 EXPECT_EQ(syncer::Difference(syncer::ControlTypes(),
755 syncer::ModelTypeSet(syncer::NIGORI)),
756 ready_types);
757
758 // Then mark the error types as unready (disables without purging).
759 ready_types = ConfigureDataTypes(
760 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
761 error_types);
762 EXPECT_EQ(syncer::Difference(enabled_types_, error_types), ready_types);
763 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
764 error_types).Empty());
765
766 // Lastly explicitly disable the error types, which should result in a purge.
767 enabled_types_.RemoveAll(error_types);
768 ready_types = ConfigureDataTypes(
769 enabled_types_, Difference(syncer::ModelTypeSet::All(), enabled_types_),
770 syncer::ModelTypeSet());
771 EXPECT_EQ(syncer::Difference(enabled_types_, error_types), ready_types);
772 EXPECT_FALSE(fake_manager_->GetTypesWithEmptyProgressMarkerToken(
773 error_types).Empty());
774 }
775
776 // Test that a call to ClearServerData is forwarded to the underlying
777 // SyncManager.
778 TEST_F(SyncBackendHostTest, ClearServerDataCallsAreForwarded) {
779 InitializeBackend(true);
780 syncer::CallbackCounter callback_counter;
781 backend_->ClearServerData(base::Bind(&syncer::CallbackCounter::Callback,
782 base::Unretained(&callback_counter)));
783 fake_manager_->WaitForSyncThread();
784 EXPECT_EQ(1, callback_counter.times_called());
785 }
786
787 // Ensure that redundant invalidations are ignored and that the most recent
788 // set of invalidation version is persisted across restarts.
789 TEST_F(SyncBackendHostTest, IgnoreOldInvalidations) {
790 // Set up some old persisted invalidations.
791 std::map<syncer::ModelType, int64_t> invalidation_versions;
792 invalidation_versions[syncer::BOOKMARKS] = 20;
793 sync_prefs_->UpdateInvalidationVersions(invalidation_versions);
794 InitializeBackend(true);
795 EXPECT_EQ(0, fake_manager_->GetInvalidationCount());
796
797 // Receiving an invalidation with an old version should do nothing.
798 syncer::ObjectIdInvalidationMap invalidation_map;
799 std::string notification_type;
800 syncer::RealModelTypeToNotificationType(syncer::BOOKMARKS,
801 &notification_type);
802 invalidation_map.Insert(syncer::Invalidation::Init(
803 invalidation::ObjectId(0, notification_type), 10, "payload"));
804 backend_->OnIncomingInvalidation(invalidation_map);
805 fake_manager_->WaitForSyncThread();
806 EXPECT_EQ(0, fake_manager_->GetInvalidationCount());
807
808 // Invalidations with new versions should be acted upon.
809 invalidation_map.Insert(syncer::Invalidation::Init(
810 invalidation::ObjectId(0, notification_type), 30, "payload"));
811 backend_->OnIncomingInvalidation(invalidation_map);
812 fake_manager_->WaitForSyncThread();
813 EXPECT_EQ(1, fake_manager_->GetInvalidationCount());
814
815 // Invalidation for new data types should be acted on.
816 syncer::RealModelTypeToNotificationType(syncer::SESSIONS, &notification_type);
817 invalidation_map.Insert(syncer::Invalidation::Init(
818 invalidation::ObjectId(0, notification_type), 10, "payload"));
819 backend_->OnIncomingInvalidation(invalidation_map);
820 fake_manager_->WaitForSyncThread();
821 EXPECT_EQ(2, fake_manager_->GetInvalidationCount());
822
823 // But redelivering that same invalidation should be ignored.
824 backend_->OnIncomingInvalidation(invalidation_map);
825 fake_manager_->WaitForSyncThread();
826 EXPECT_EQ(2, fake_manager_->GetInvalidationCount());
827
828 // If an invalidation with an unknown version is received, it should be
829 // acted on, but should not affect the persisted versions.
830 invalidation_map.Insert(syncer::Invalidation::InitUnknownVersion(
831 invalidation::ObjectId(0, notification_type)));
832 backend_->OnIncomingInvalidation(invalidation_map);
833 fake_manager_->WaitForSyncThread();
834 EXPECT_EQ(3, fake_manager_->GetInvalidationCount());
835
836 // Verify that the invalidation versions were updated in the prefs.
837 invalidation_versions[syncer::BOOKMARKS] = 30;
838 invalidation_versions[syncer::SESSIONS] = 10;
839 std::map<syncer::ModelType, int64_t> persisted_invalidation_versions;
840 sync_prefs_->GetInvalidationVersions(&persisted_invalidation_versions);
841 EXPECT_EQ(invalidation_versions.size(),
842 persisted_invalidation_versions.size());
843 for (auto iter : persisted_invalidation_versions) {
844 EXPECT_EQ(invalidation_versions[iter.first], iter.second);
845 }
846 }
847
848 // Tests that SyncBackendHostImpl retains ModelTypeConnector after call to
849 // StopSyncingForShutdown. This is needed for datatype deactivation during
850 // DataTypeManager shutdown.
851 TEST_F(SyncBackendHostTest, ModelTypeConnectorValidDuringShutdown) {
852 InitializeBackend(true);
853 backend_->StopSyncingForShutdown();
854 // Verify that call to DeactivateNonBlockingDataType doesn't assert.
855 backend_->DeactivateNonBlockingDataType(syncer::AUTOFILL);
856 backend_->Shutdown(syncer::STOP_SYNC);
857 backend_.reset();
858 }
859
860 } // namespace
861
862 } // namespace browser_sync
OLDNEW
« no previous file with comments | « components/sync_driver/glue/sync_backend_host_impl.cc ('k') | components/sync_driver/glue/sync_backend_host_mock.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698