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_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" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 file_thread_(BrowserThread::FILE), | 60 file_thread_(BrowserThread::FILE), |
60 io_thread_(BrowserThread::IO) {} | 61 io_thread_(BrowserThread::IO) {} |
61 | 62 |
62 ~ProfileSyncServiceTestHarness() {} | 63 ~ProfileSyncServiceTestHarness() {} |
63 | 64 |
64 void SetUp() { | 65 void SetUp() { |
65 file_thread_.Start(); | 66 file_thread_.Start(); |
66 io_thread_.StartIOThread(); | 67 io_thread_.StartIOThread(); |
67 profile.reset(new TestingProfile()); | 68 profile.reset(new TestingProfile()); |
68 profile->CreateRequestContext(); | 69 profile->CreateRequestContext(); |
| 70 InvalidationServiceFactory::GetInstance()->SetTestingFactory( |
| 71 profile.get(), InvalidationServiceFactory::BuildTestServiceInstanceFor); |
69 | 72 |
70 // We need to set the user agent before the backend host can call | 73 // We need to set the user agent before the backend host can call |
71 // webkit_glue::GetUserAgent(). | 74 // webkit_glue::GetUserAgent(). |
72 webkit_glue::SetUserAgent(content::GetContentClient()->GetUserAgent(), | 75 webkit_glue::SetUserAgent(content::GetContentClient()->GetUserAgent(), |
73 false); | 76 false); |
74 } | 77 } |
75 | 78 |
76 void TearDown() { | 79 void TearDown() { |
77 // Kill the service before the profile. | 80 // Kill the service before the profile. |
78 if (service.get()) { | 81 if (service.get()) { |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 // these tests). The sync_backend_host will notice this and inform the profile | 458 // these tests). The sync_backend_host will notice this and inform the profile |
456 // sync service of the failure to initialize the backed. | 459 // sync service of the failure to initialize the backed. |
457 TEST_F(ProfileSyncServiceTest, FailToDownloadControlTypes) { | 460 TEST_F(ProfileSyncServiceTest, FailToDownloadControlTypes) { |
458 harness_.StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, | 461 harness_.StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, |
459 syncer::STORAGE_IN_MEMORY); | 462 syncer::STORAGE_IN_MEMORY); |
460 | 463 |
461 // The backend is not ready. Ensure the PSS knows this. | 464 // The backend is not ready. Ensure the PSS knows this. |
462 EXPECT_FALSE(harness_.service->sync_initialized()); | 465 EXPECT_FALSE(harness_.service->sync_initialized()); |
463 } | 466 } |
464 | 467 |
465 // Register a handler with the ProfileSyncService, and disable and | |
466 // reenable sync. The handler should get notified of the state | |
467 // changes. | |
468 // Flaky on all platforms. http://crbug.com/154491 | |
469 TEST_F(ProfileSyncServiceTest, DISABLED_DisableInvalidationsOnStop) { | |
470 harness_.StartSyncServiceAndSetInitialSyncEnded( | |
471 true, true, true, true, syncer::STORAGE_IN_MEMORY); | |
472 | |
473 syncer::FakeInvalidationHandler handler; | |
474 harness_.service->RegisterInvalidationHandler(&handler); | |
475 | |
476 SyncBackendHostForProfileSyncTest* const backend = | |
477 harness_.service->GetBackendForTest(); | |
478 | |
479 backend->EmitOnInvalidatorStateChange(syncer::INVALIDATIONS_ENABLED); | |
480 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); | |
481 | |
482 harness_.service->StopAndSuppress(); | |
483 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR, | |
484 handler.GetInvalidatorState()); | |
485 | |
486 harness_.service->UnsuppressAndStart(); | |
487 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); | |
488 | |
489 harness_.service->UnregisterInvalidationHandler(&handler); | |
490 } | |
491 | |
492 // Register for some IDs with the ProfileSyncService, restart sync, | |
493 // and trigger some invalidation messages. They should still be | |
494 // received by the handler. | |
495 TEST_F(ProfileSyncServiceTest, UpdateRegisteredInvalidationIdsPersistence) { | |
496 harness_.StartSyncService(); | |
497 | |
498 syncer::ObjectIdSet ids; | |
499 ids.insert(invalidation::ObjectId(3, "id3")); | |
500 const syncer::ObjectIdInvalidationMap& states = | |
501 syncer::ObjectIdSetToInvalidationMap(ids, "payload"); | |
502 | |
503 syncer::FakeInvalidationHandler handler; | |
504 | |
505 harness_.service->RegisterInvalidationHandler(&handler); | |
506 harness_.service->UpdateRegisteredInvalidationIds(&handler, ids); | |
507 | |
508 harness_.service->StopAndSuppress(); | |
509 harness_.service->UnsuppressAndStart(); | |
510 | |
511 SyncBackendHostForProfileSyncTest* const backend = | |
512 harness_.service->GetBackendForTest(); | |
513 | |
514 backend->EmitOnInvalidatorStateChange(syncer::INVALIDATIONS_ENABLED); | |
515 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); | |
516 | |
517 backend->EmitOnIncomingInvalidation(states); | |
518 EXPECT_THAT(states, Eq(handler.GetLastInvalidationMap())); | |
519 | |
520 backend->EmitOnInvalidatorStateChange(syncer::TRANSIENT_INVALIDATION_ERROR); | |
521 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR, | |
522 handler.GetInvalidatorState()); | |
523 | |
524 harness_.service->UnregisterInvalidationHandler(&handler); | |
525 } | |
526 | |
527 // Thin Invalidator wrapper around ProfileSyncService. | |
528 class ProfileSyncServiceInvalidator : public syncer::Invalidator { | |
529 public: | |
530 explicit ProfileSyncServiceInvalidator(ProfileSyncService* service) | |
531 : service_(service) {} | |
532 | |
533 virtual ~ProfileSyncServiceInvalidator() {} | |
534 | |
535 // Invalidator implementation. | |
536 virtual void RegisterHandler(syncer::InvalidationHandler* handler) OVERRIDE { | |
537 service_->RegisterInvalidationHandler(handler); | |
538 } | |
539 | |
540 virtual void UpdateRegisteredIds(syncer::InvalidationHandler* handler, | |
541 const syncer::ObjectIdSet& ids) OVERRIDE { | |
542 service_->UpdateRegisteredInvalidationIds(handler, ids); | |
543 } | |
544 | |
545 virtual void UnregisterHandler( | |
546 syncer::InvalidationHandler* handler) OVERRIDE { | |
547 service_->UnregisterInvalidationHandler(handler); | |
548 } | |
549 | |
550 virtual void Acknowledge(const invalidation::ObjectId& id, | |
551 const syncer::AckHandle& ack_handle) OVERRIDE { | |
552 // Do nothing. | |
553 } | |
554 | |
555 virtual syncer::InvalidatorState GetInvalidatorState() const OVERRIDE { | |
556 return service_->GetInvalidatorState(); | |
557 } | |
558 | |
559 virtual void UpdateCredentials( | |
560 const std::string& email, const std::string& token) OVERRIDE { | |
561 // Do nothing. | |
562 } | |
563 | |
564 virtual void SendInvalidation( | |
565 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE { | |
566 // Do nothing. | |
567 } | |
568 | |
569 private: | |
570 ProfileSyncService* const service_; | |
571 | |
572 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceInvalidator); | |
573 }; | |
574 | |
575 } // namespace | 468 } // namespace |
576 | |
577 | |
578 // ProfileSyncServiceInvalidatorTestDelegate has to be visible from | |
579 // the syncer namespace (where InvalidatorTest lives). | |
580 class ProfileSyncServiceInvalidatorTestDelegate { | |
581 public: | |
582 ProfileSyncServiceInvalidatorTestDelegate() {} | |
583 | |
584 ~ProfileSyncServiceInvalidatorTestDelegate() { | |
585 DestroyInvalidator(); | |
586 } | |
587 | |
588 void CreateInvalidator( | |
589 const std::string& invalidation_client_id, | |
590 const std::string& initial_state, | |
591 const base::WeakPtr<syncer::InvalidationStateTracker>& | |
592 invalidation_state_tracker) { | |
593 DCHECK(!invalidator_.get()); | |
594 harness_.SetUp(); | |
595 harness_.StartSyncService(); | |
596 invalidator_.reset( | |
597 new ProfileSyncServiceInvalidator(harness_.service.get())); | |
598 } | |
599 | |
600 ProfileSyncServiceInvalidator* GetInvalidator() { | |
601 return invalidator_.get(); | |
602 } | |
603 | |
604 void DestroyInvalidator() { | |
605 invalidator_.reset(); | |
606 harness_.TearDown(); | |
607 } | |
608 | |
609 void WaitForInvalidator() { | |
610 // Do nothing. | |
611 } | |
612 | |
613 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) { | |
614 harness_.service->GetBackendForTest()->EmitOnInvalidatorStateChange(state); | |
615 } | |
616 | |
617 void TriggerOnIncomingInvalidation( | |
618 const syncer::ObjectIdInvalidationMap& invalidation_map) { | |
619 harness_.service->GetBackendForTest()->EmitOnIncomingInvalidation( | |
620 invalidation_map); | |
621 } | |
622 | |
623 private: | |
624 ProfileSyncServiceTestHarness harness_; | |
625 scoped_ptr<ProfileSyncServiceInvalidator> invalidator_; | |
626 }; | |
627 | |
628 } // namespace browser_sync | 469 } // namespace browser_sync |
629 | |
630 namespace syncer { | |
631 namespace { | |
632 | |
633 // ProfileSyncService should behave just like an invalidator. | |
634 INSTANTIATE_TYPED_TEST_CASE_P( | |
635 ProfileSyncServiceInvalidatorTest, InvalidatorTest, | |
636 ::browser_sync::ProfileSyncServiceInvalidatorTestDelegate); | |
637 | |
638 } // namespace | |
639 } // namespace syncer | |
OLD | NEW |