| 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 SetBuildOnlyFakeInvalidatorsForTest(true); |
| 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 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 EXPECT_TRUE(harness_.service->sync_initialized()); | 349 EXPECT_TRUE(harness_.service->sync_initialized()); |
| 351 js_controller->RemoveJsEventHandler(&event_handler); | 350 js_controller->RemoveJsEventHandler(&event_handler); |
| 352 } | 351 } |
| 353 | 352 |
| 354 TEST_F(ProfileSyncServiceTest, JsControllerProcessJsMessageBasic) { | 353 TEST_F(ProfileSyncServiceTest, JsControllerProcessJsMessageBasic) { |
| 355 harness_.StartSyncService(); | 354 harness_.StartSyncService(); |
| 356 | 355 |
| 357 StrictMock<syncer::MockJsReplyHandler> reply_handler; | 356 StrictMock<syncer::MockJsReplyHandler> reply_handler; |
| 358 | 357 |
| 359 ListValue arg_list1; | 358 ListValue arg_list1; |
| 360 arg_list1.Append(Value::CreateStringValue("TRANSIENT_INVALIDATION_ERROR")); | 359 arg_list1.Append(Value::CreateStringValue("INVALIDATIONS_ENABLED")); |
| 361 syncer::JsArgList args1(&arg_list1); | 360 syncer::JsArgList args1(&arg_list1); |
| 362 EXPECT_CALL(reply_handler, | 361 EXPECT_CALL(reply_handler, |
| 363 HandleJsReply("getNotificationState", HasArgs(args1))); | 362 HandleJsReply("getNotificationState", HasArgs(args1))); |
| 364 | 363 |
| 365 { | 364 { |
| 366 base::WeakPtr<syncer::JsController> js_controller = | 365 base::WeakPtr<syncer::JsController> js_controller = |
| 367 harness_.service->GetJsController(); | 366 harness_.service->GetJsController(); |
| 368 js_controller->ProcessJsMessage("getNotificationState", args1, | 367 js_controller->ProcessJsMessage("getNotificationState", args1, |
| 369 reply_handler.AsWeakHandle()); | 368 reply_handler.AsWeakHandle()); |
| 370 } | 369 } |
| 371 | 370 |
| 372 // This forces the sync thread to process the message and reply. | 371 // This forces the sync thread to process the message and reply. |
| 373 harness_.TearDown(); | 372 harness_.TearDown(); |
| 374 } | 373 } |
| 375 | 374 |
| 376 TEST_F(ProfileSyncServiceTest, | 375 TEST_F(ProfileSyncServiceTest, |
| 377 JsControllerProcessJsMessageBasicDelayedBackendInitialization) { | 376 JsControllerProcessJsMessageBasicDelayedBackendInitialization) { |
| 378 harness_.StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, | 377 harness_.StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, |
| 379 syncer::STORAGE_IN_MEMORY); | 378 syncer::STORAGE_IN_MEMORY); |
| 380 | 379 |
| 381 StrictMock<syncer::MockJsReplyHandler> reply_handler; | 380 StrictMock<syncer::MockJsReplyHandler> reply_handler; |
| 382 | 381 |
| 383 ListValue arg_list1; | 382 ListValue arg_list1; |
| 384 arg_list1.Append(Value::CreateStringValue("TRANSIENT_INVALIDATION_ERROR")); | 383 arg_list1.Append(Value::CreateStringValue("INVALIDATIONS_ENABLED")); |
| 385 syncer::JsArgList args1(&arg_list1); | 384 syncer::JsArgList args1(&arg_list1); |
| 386 EXPECT_CALL(reply_handler, | 385 EXPECT_CALL(reply_handler, |
| 387 HandleJsReply("getNotificationState", HasArgs(args1))); | 386 HandleJsReply("getNotificationState", HasArgs(args1))); |
| 388 | 387 |
| 389 { | 388 { |
| 390 base::WeakPtr<syncer::JsController> js_controller = | 389 base::WeakPtr<syncer::JsController> js_controller = |
| 391 harness_.service->GetJsController(); | 390 harness_.service->GetJsController(); |
| 392 js_controller->ProcessJsMessage("getNotificationState", | 391 js_controller->ProcessJsMessage("getNotificationState", |
| 393 args1, reply_handler.AsWeakHandle()); | 392 args1, reply_handler.AsWeakHandle()); |
| 394 } | 393 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 // these tests). The sync_backend_host will notice this and inform the profile | 459 // these tests). The sync_backend_host will notice this and inform the profile |
| 461 // sync service of the failure to initialize the backed. | 460 // sync service of the failure to initialize the backed. |
| 462 TEST_F(ProfileSyncServiceTest, FailToDownloadControlTypes) { | 461 TEST_F(ProfileSyncServiceTest, FailToDownloadControlTypes) { |
| 463 harness_.StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, | 462 harness_.StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, |
| 464 syncer::STORAGE_IN_MEMORY); | 463 syncer::STORAGE_IN_MEMORY); |
| 465 | 464 |
| 466 // The backend is not ready. Ensure the PSS knows this. | 465 // The backend is not ready. Ensure the PSS knows this. |
| 467 EXPECT_FALSE(harness_.service->sync_initialized()); | 466 EXPECT_FALSE(harness_.service->sync_initialized()); |
| 468 } | 467 } |
| 469 | 468 |
| 470 // Register a handler with the ProfileSyncService, and disable and | |
| 471 // reenable sync. The handler should get notified of the state | |
| 472 // changes. | |
| 473 // Flaky on all platforms. http://crbug.com/154491 | |
| 474 TEST_F(ProfileSyncServiceTest, DISABLED_DisableInvalidationsOnStop) { | |
| 475 harness_.StartSyncServiceAndSetInitialSyncEnded( | |
| 476 true, true, true, true, syncer::STORAGE_IN_MEMORY); | |
| 477 | |
| 478 syncer::FakeInvalidationHandler handler; | |
| 479 harness_.service->RegisterInvalidationHandler(&handler); | |
| 480 | |
| 481 SyncBackendHostForProfileSyncTest* const backend = | |
| 482 harness_.service->GetBackendForTest(); | |
| 483 | |
| 484 backend->EmitOnInvalidatorStateChange(syncer::INVALIDATIONS_ENABLED); | |
| 485 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); | |
| 486 | |
| 487 harness_.service->StopAndSuppress(); | |
| 488 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR, | |
| 489 handler.GetInvalidatorState()); | |
| 490 | |
| 491 harness_.service->UnsuppressAndStart(); | |
| 492 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); | |
| 493 | |
| 494 harness_.service->UnregisterInvalidationHandler(&handler); | |
| 495 } | |
| 496 | |
| 497 // Register for some IDs with the ProfileSyncService, restart sync, | |
| 498 // and trigger some invalidation messages. They should still be | |
| 499 // received by the handler. | |
| 500 TEST_F(ProfileSyncServiceTest, UpdateRegisteredInvalidationIdsPersistence) { | |
| 501 harness_.StartSyncService(); | |
| 502 | |
| 503 syncer::ObjectIdSet ids; | |
| 504 ids.insert(invalidation::ObjectId(3, "id3")); | |
| 505 const syncer::ObjectIdInvalidationMap& states = | |
| 506 syncer::ObjectIdSetToInvalidationMap(ids, "payload"); | |
| 507 | |
| 508 syncer::FakeInvalidationHandler handler; | |
| 509 | |
| 510 harness_.service->RegisterInvalidationHandler(&handler); | |
| 511 harness_.service->UpdateRegisteredInvalidationIds(&handler, ids); | |
| 512 | |
| 513 harness_.service->StopAndSuppress(); | |
| 514 harness_.service->UnsuppressAndStart(); | |
| 515 | |
| 516 SyncBackendHostForProfileSyncTest* const backend = | |
| 517 harness_.service->GetBackendForTest(); | |
| 518 | |
| 519 backend->EmitOnInvalidatorStateChange(syncer::INVALIDATIONS_ENABLED); | |
| 520 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); | |
| 521 | |
| 522 backend->EmitOnIncomingInvalidation(states); | |
| 523 EXPECT_THAT(states, Eq(handler.GetLastInvalidationMap())); | |
| 524 | |
| 525 backend->EmitOnInvalidatorStateChange(syncer::TRANSIENT_INVALIDATION_ERROR); | |
| 526 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR, | |
| 527 handler.GetInvalidatorState()); | |
| 528 | |
| 529 harness_.service->UnregisterInvalidationHandler(&handler); | |
| 530 } | |
| 531 | |
| 532 // Thin Invalidator wrapper around ProfileSyncService. | |
| 533 class ProfileSyncServiceInvalidator : public syncer::Invalidator { | |
| 534 public: | |
| 535 explicit ProfileSyncServiceInvalidator(ProfileSyncService* service) | |
| 536 : service_(service) {} | |
| 537 | |
| 538 virtual ~ProfileSyncServiceInvalidator() {} | |
| 539 | |
| 540 // Invalidator implementation. | |
| 541 virtual void RegisterHandler(syncer::InvalidationHandler* handler) OVERRIDE { | |
| 542 service_->RegisterInvalidationHandler(handler); | |
| 543 } | |
| 544 | |
| 545 virtual void UpdateRegisteredIds(syncer::InvalidationHandler* handler, | |
| 546 const syncer::ObjectIdSet& ids) OVERRIDE { | |
| 547 service_->UpdateRegisteredInvalidationIds(handler, ids); | |
| 548 } | |
| 549 | |
| 550 virtual void UnregisterHandler( | |
| 551 syncer::InvalidationHandler* handler) OVERRIDE { | |
| 552 service_->UnregisterInvalidationHandler(handler); | |
| 553 } | |
| 554 | |
| 555 virtual void Acknowledge(const invalidation::ObjectId& id, | |
| 556 const syncer::AckHandle& ack_handle) OVERRIDE { | |
| 557 // Do nothing. | |
| 558 } | |
| 559 | |
| 560 virtual syncer::InvalidatorState GetInvalidatorState() const OVERRIDE { | |
| 561 return service_->GetInvalidatorState(); | |
| 562 } | |
| 563 | |
| 564 virtual void UpdateCredentials( | |
| 565 const std::string& email, const std::string& token) OVERRIDE { | |
| 566 // Do nothing. | |
| 567 } | |
| 568 | |
| 569 virtual void SendInvalidation( | |
| 570 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE { | |
| 571 // Do nothing. | |
| 572 } | |
| 573 | |
| 574 private: | |
| 575 ProfileSyncService* const service_; | |
| 576 | |
| 577 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceInvalidator); | |
| 578 }; | |
| 579 | |
| 580 } // namespace | 469 } // namespace |
| 581 | |
| 582 | |
| 583 // ProfileSyncServiceInvalidatorTestDelegate has to be visible from | |
| 584 // the syncer namespace (where InvalidatorTest lives). | |
| 585 class ProfileSyncServiceInvalidatorTestDelegate { | |
| 586 public: | |
| 587 ProfileSyncServiceInvalidatorTestDelegate() {} | |
| 588 | |
| 589 ~ProfileSyncServiceInvalidatorTestDelegate() { | |
| 590 DestroyInvalidator(); | |
| 591 } | |
| 592 | |
| 593 void CreateInvalidator( | |
| 594 const std::string& invalidation_client_id, | |
| 595 const std::string& initial_state, | |
| 596 const base::WeakPtr<syncer::InvalidationStateTracker>& | |
| 597 invalidation_state_tracker) { | |
| 598 DCHECK(!invalidator_.get()); | |
| 599 harness_.SetUp(); | |
| 600 harness_.StartSyncService(); | |
| 601 invalidator_.reset( | |
| 602 new ProfileSyncServiceInvalidator(harness_.service.get())); | |
| 603 } | |
| 604 | |
| 605 ProfileSyncServiceInvalidator* GetInvalidator() { | |
| 606 return invalidator_.get(); | |
| 607 } | |
| 608 | |
| 609 void DestroyInvalidator() { | |
| 610 invalidator_.reset(); | |
| 611 harness_.TearDown(); | |
| 612 } | |
| 613 | |
| 614 void WaitForInvalidator() { | |
| 615 // Do nothing. | |
| 616 } | |
| 617 | |
| 618 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) { | |
| 619 harness_.service->GetBackendForTest()->EmitOnInvalidatorStateChange(state); | |
| 620 } | |
| 621 | |
| 622 void TriggerOnIncomingInvalidation( | |
| 623 const syncer::ObjectIdInvalidationMap& invalidation_map) { | |
| 624 harness_.service->GetBackendForTest()->EmitOnIncomingInvalidation( | |
| 625 invalidation_map); | |
| 626 } | |
| 627 | |
| 628 private: | |
| 629 ProfileSyncServiceTestHarness harness_; | |
| 630 scoped_ptr<ProfileSyncServiceInvalidator> invalidator_; | |
| 631 }; | |
| 632 | |
| 633 } // namespace browser_sync | 470 } // namespace browser_sync |
| 634 | |
| 635 namespace syncer { | |
| 636 namespace { | |
| 637 | |
| 638 // ProfileSyncService should behave just like an invalidator. | |
| 639 INSTANTIATE_TYPED_TEST_CASE_P( | |
| 640 ProfileSyncServiceInvalidatorTest, InvalidatorTest, | |
| 641 ::browser_sync::ProfileSyncServiceInvalidatorTestDelegate); | |
| 642 | |
| 643 } // namespace | |
| 644 } // namespace syncer | |
| OLD | NEW |