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