| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/invalidation/invalidation_service_factory.h" |
| 11 #include "chrome/browser/signin/signin_manager.h" | 12 #include "chrome/browser/signin/signin_manager.h" |
| 12 #include "chrome/browser/signin/signin_manager_factory.h" | 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 13 #include "chrome/browser/signin/token_service.h" | 14 #include "chrome/browser/signin/token_service.h" |
| 14 #include "chrome/browser/signin/token_service_factory.h" | 15 #include "chrome/browser/signin/token_service_factory.h" |
| 15 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" | 16 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" |
| 16 #include "chrome/browser/sync/glue/data_type_controller.h" | 17 #include "chrome/browser/sync/glue/data_type_controller.h" |
| 17 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" | 18 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" |
| 18 #include "chrome/browser/sync/test_profile_sync_service.h" | 19 #include "chrome/browser/sync/test_profile_sync_service.h" |
| 19 #include "chrome/common/chrome_version_info.h" | 20 #include "chrome/common/chrome_version_info.h" |
| 20 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 21 #include "chrome/test/base/testing_pref_service_syncable.h" | 22 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 22 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
| 23 #include "content/public/test/test_browser_thread.h" | 24 #include "content/public/test/test_browser_thread.h" |
| 24 #include "google/cacheinvalidation/include/types.h" | 25 #include "google/cacheinvalidation/include/types.h" |
| 25 #include "google_apis/gaia/gaia_constants.h" | 26 #include "google_apis/gaia/gaia_constants.h" |
| 26 #include "sync/js/js_arg_list.h" | 27 #include "sync/js/js_arg_list.h" |
| 27 #include "sync/js/js_event_details.h" | 28 #include "sync/js/js_event_details.h" |
| 28 #include "sync/js/js_test_util.h" | 29 #include "sync/js/js_test_util.h" |
| 29 #include "sync/notifier/fake_invalidation_handler.h" | |
| 30 #include "sync/notifier/invalidator.h" | |
| 31 #include "sync/notifier/invalidator_test_template.h" | |
| 32 #include "sync/notifier/object_id_invalidation_map_test_util.h" | |
| 33 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 35 | 32 |
| 36 // TODO(akalin): Add tests here that exercise the whole | 33 // TODO(akalin): Add tests here that exercise the whole |
| 37 // ProfileSyncService/SyncBackendHost stack while mocking out as | 34 // ProfileSyncService/SyncBackendHost stack while mocking out as |
| 38 // little as possible. | 35 // little as possible. |
| 39 | 36 |
| 40 namespace browser_sync { | 37 namespace browser_sync { |
| 41 | 38 |
| 42 namespace { | 39 namespace { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 57 file_thread_(BrowserThread::FILE), | 54 file_thread_(BrowserThread::FILE), |
| 58 io_thread_(BrowserThread::IO) {} | 55 io_thread_(BrowserThread::IO) {} |
| 59 | 56 |
| 60 ~ProfileSyncServiceTestHarness() {} | 57 ~ProfileSyncServiceTestHarness() {} |
| 61 | 58 |
| 62 void SetUp() { | 59 void SetUp() { |
| 63 file_thread_.Start(); | 60 file_thread_.Start(); |
| 64 io_thread_.StartIOThread(); | 61 io_thread_.StartIOThread(); |
| 65 profile.reset(new TestingProfile()); | 62 profile.reset(new TestingProfile()); |
| 66 profile->CreateRequestContext(); | 63 profile->CreateRequestContext(); |
| 64 invalidation::InvalidationServiceFactory::GetInstance()-> |
| 65 SetFakeInvalidationServiceForTest(profile.get()); |
| 67 } | 66 } |
| 68 | 67 |
| 69 void TearDown() { | 68 void TearDown() { |
| 70 // Kill the service before the profile. | 69 // Kill the service before the profile. |
| 71 if (service) { | 70 if (service) { |
| 72 service->Shutdown(); | 71 service->Shutdown(); |
| 73 } | 72 } |
| 74 service.reset(); | 73 service.reset(); |
| 75 profile.reset(); | 74 profile.reset(); |
| 76 // Pump messages posted by the sync thread (which may end up | 75 // Pump messages posted by the sync thread (which may end up |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 // these tests). The sync_backend_host will notice this and inform the profile | 453 // these tests). The sync_backend_host will notice this and inform the profile |
| 455 // sync service of the failure to initialize the backed. | 454 // sync service of the failure to initialize the backed. |
| 456 TEST_F(ProfileSyncServiceTest, FailToDownloadControlTypes) { | 455 TEST_F(ProfileSyncServiceTest, FailToDownloadControlTypes) { |
| 457 harness_.StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, | 456 harness_.StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, |
| 458 syncer::STORAGE_IN_MEMORY); | 457 syncer::STORAGE_IN_MEMORY); |
| 459 | 458 |
| 460 // The backend is not ready. Ensure the PSS knows this. | 459 // The backend is not ready. Ensure the PSS knows this. |
| 461 EXPECT_FALSE(harness_.service->sync_initialized()); | 460 EXPECT_FALSE(harness_.service->sync_initialized()); |
| 462 } | 461 } |
| 463 | 462 |
| 464 // Register a handler with the ProfileSyncService, and disable and | |
| 465 // reenable sync. The handler should get notified of the state | |
| 466 // changes. | |
| 467 // Flaky on all platforms. http://crbug.com/154491 | |
| 468 TEST_F(ProfileSyncServiceTest, DISABLED_DisableInvalidationsOnStop) { | |
| 469 harness_.StartSyncServiceAndSetInitialSyncEnded( | |
| 470 true, true, true, true, syncer::STORAGE_IN_MEMORY); | |
| 471 | |
| 472 syncer::FakeInvalidationHandler handler; | |
| 473 harness_.service->RegisterInvalidationHandler(&handler); | |
| 474 | |
| 475 SyncBackendHostForProfileSyncTest* const backend = | |
| 476 harness_.service->GetBackendForTest(); | |
| 477 | |
| 478 backend->EmitOnInvalidatorStateChange(syncer::INVALIDATIONS_ENABLED); | |
| 479 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); | |
| 480 | |
| 481 harness_.service->StopAndSuppress(); | |
| 482 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR, | |
| 483 handler.GetInvalidatorState()); | |
| 484 | |
| 485 harness_.service->UnsuppressAndStart(); | |
| 486 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); | |
| 487 | |
| 488 harness_.service->UnregisterInvalidationHandler(&handler); | |
| 489 } | |
| 490 | |
| 491 // Register for some IDs with the ProfileSyncService, restart sync, | |
| 492 // and trigger some invalidation messages. They should still be | |
| 493 // received by the handler. | |
| 494 TEST_F(ProfileSyncServiceTest, UpdateRegisteredInvalidationIdsPersistence) { | |
| 495 harness_.StartSyncService(); | |
| 496 | |
| 497 syncer::ObjectIdSet ids; | |
| 498 ids.insert(invalidation::ObjectId(3, "id3")); | |
| 499 const syncer::ObjectIdInvalidationMap& states = | |
| 500 syncer::ObjectIdSetToInvalidationMap(ids, "payload"); | |
| 501 | |
| 502 syncer::FakeInvalidationHandler handler; | |
| 503 | |
| 504 harness_.service->RegisterInvalidationHandler(&handler); | |
| 505 harness_.service->UpdateRegisteredInvalidationIds(&handler, ids); | |
| 506 | |
| 507 harness_.service->StopAndSuppress(); | |
| 508 harness_.service->UnsuppressAndStart(); | |
| 509 | |
| 510 SyncBackendHostForProfileSyncTest* const backend = | |
| 511 harness_.service->GetBackendForTest(); | |
| 512 | |
| 513 backend->EmitOnInvalidatorStateChange(syncer::INVALIDATIONS_ENABLED); | |
| 514 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); | |
| 515 | |
| 516 backend->EmitOnIncomingInvalidation(states); | |
| 517 EXPECT_THAT(states, Eq(handler.GetLastInvalidationMap())); | |
| 518 | |
| 519 backend->EmitOnInvalidatorStateChange(syncer::TRANSIENT_INVALIDATION_ERROR); | |
| 520 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR, | |
| 521 handler.GetInvalidatorState()); | |
| 522 | |
| 523 harness_.service->UnregisterInvalidationHandler(&handler); | |
| 524 } | |
| 525 | |
| 526 // Thin Invalidator wrapper around ProfileSyncService. | |
| 527 class ProfileSyncServiceInvalidator : public syncer::Invalidator { | |
| 528 public: | |
| 529 explicit ProfileSyncServiceInvalidator(ProfileSyncService* service) | |
| 530 : service_(service) {} | |
| 531 | |
| 532 virtual ~ProfileSyncServiceInvalidator() {} | |
| 533 | |
| 534 // Invalidator implementation. | |
| 535 virtual void RegisterHandler(syncer::InvalidationHandler* handler) OVERRIDE { | |
| 536 service_->RegisterInvalidationHandler(handler); | |
| 537 } | |
| 538 | |
| 539 virtual void UpdateRegisteredIds(syncer::InvalidationHandler* handler, | |
| 540 const syncer::ObjectIdSet& ids) OVERRIDE { | |
| 541 service_->UpdateRegisteredInvalidationIds(handler, ids); | |
| 542 } | |
| 543 | |
| 544 virtual void UnregisterHandler( | |
| 545 syncer::InvalidationHandler* handler) OVERRIDE { | |
| 546 service_->UnregisterInvalidationHandler(handler); | |
| 547 } | |
| 548 | |
| 549 virtual void Acknowledge(const invalidation::ObjectId& id, | |
| 550 const syncer::AckHandle& ack_handle) OVERRIDE { | |
| 551 // Do nothing. | |
| 552 } | |
| 553 | |
| 554 virtual syncer::InvalidatorState GetInvalidatorState() const OVERRIDE { | |
| 555 return service_->GetInvalidatorState(); | |
| 556 } | |
| 557 | |
| 558 virtual void UpdateCredentials( | |
| 559 const std::string& email, const std::string& token) OVERRIDE { | |
| 560 // Do nothing. | |
| 561 } | |
| 562 | |
| 563 virtual void SendInvalidation( | |
| 564 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE { | |
| 565 // Do nothing. | |
| 566 } | |
| 567 | |
| 568 private: | |
| 569 ProfileSyncService* const service_; | |
| 570 | |
| 571 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceInvalidator); | |
| 572 }; | |
| 573 | |
| 574 } // namespace | 463 } // namespace |
| 575 | |
| 576 | |
| 577 // ProfileSyncServiceInvalidatorTestDelegate has to be visible from | |
| 578 // the syncer namespace (where InvalidatorTest lives). | |
| 579 class ProfileSyncServiceInvalidatorTestDelegate { | |
| 580 public: | |
| 581 ProfileSyncServiceInvalidatorTestDelegate() {} | |
| 582 | |
| 583 ~ProfileSyncServiceInvalidatorTestDelegate() { | |
| 584 DestroyInvalidator(); | |
| 585 } | |
| 586 | |
| 587 void CreateInvalidator( | |
| 588 const std::string& invalidation_client_id, | |
| 589 const std::string& initial_state, | |
| 590 const base::WeakPtr<syncer::InvalidationStateTracker>& | |
| 591 invalidation_state_tracker) { | |
| 592 DCHECK(!invalidator_.get()); | |
| 593 harness_.SetUp(); | |
| 594 harness_.StartSyncService(); | |
| 595 invalidator_.reset( | |
| 596 new ProfileSyncServiceInvalidator(harness_.service.get())); | |
| 597 } | |
| 598 | |
| 599 ProfileSyncServiceInvalidator* GetInvalidator() { | |
| 600 return invalidator_.get(); | |
| 601 } | |
| 602 | |
| 603 void DestroyInvalidator() { | |
| 604 invalidator_.reset(); | |
| 605 harness_.TearDown(); | |
| 606 } | |
| 607 | |
| 608 void WaitForInvalidator() { | |
| 609 // Do nothing. | |
| 610 } | |
| 611 | |
| 612 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) { | |
| 613 harness_.service->GetBackendForTest()->EmitOnInvalidatorStateChange(state); | |
| 614 } | |
| 615 | |
| 616 void TriggerOnIncomingInvalidation( | |
| 617 const syncer::ObjectIdInvalidationMap& invalidation_map) { | |
| 618 harness_.service->GetBackendForTest()->EmitOnIncomingInvalidation( | |
| 619 invalidation_map); | |
| 620 } | |
| 621 | |
| 622 private: | |
| 623 ProfileSyncServiceTestHarness harness_; | |
| 624 scoped_ptr<ProfileSyncServiceInvalidator> invalidator_; | |
| 625 }; | |
| 626 | |
| 627 } // namespace browser_sync | 464 } // namespace browser_sync |
| 628 | |
| 629 namespace syncer { | |
| 630 namespace { | |
| 631 | |
| 632 // ProfileSyncService should behave just like an invalidator. | |
| 633 INSTANTIATE_TYPED_TEST_CASE_P( | |
| 634 ProfileSyncServiceInvalidatorTest, InvalidatorTest, | |
| 635 ::browser_sync::ProfileSyncServiceInvalidatorTestDelegate); | |
| 636 | |
| 637 } // namespace | |
| 638 } // namespace syncer | |
| OLD | NEW |