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

Side by Side Diff: chrome/browser/sync/profile_sync_service_unittest.cc

Issue 17911005: Merge 208347 "Revert 208315 "Make use of InvalidationService"" (Closed) Base URL: svn://svn.chromium.org/chrome/branches/1547/src/
Patch Set: Created 7 years, 5 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 | Annotate | Revision Log
OLDNEW
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"
12 #include "chrome/browser/signin/signin_manager.h" 11 #include "chrome/browser/signin/signin_manager.h"
13 #include "chrome/browser/signin/signin_manager_factory.h" 12 #include "chrome/browser/signin/signin_manager_factory.h"
14 #include "chrome/browser/signin/token_service.h" 13 #include "chrome/browser/signin/token_service.h"
15 #include "chrome/browser/signin/token_service_factory.h" 14 #include "chrome/browser/signin/token_service_factory.h"
16 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" 15 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h"
17 #include "chrome/browser/sync/glue/data_type_controller.h" 16 #include "chrome/browser/sync/glue/data_type_controller.h"
18 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" 17 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
19 #include "chrome/browser/sync/test_profile_sync_service.h" 18 #include "chrome/browser/sync/test_profile_sync_service.h"
20 #include "chrome/common/chrome_version_info.h" 19 #include "chrome/common/chrome_version_info.h"
21 #include "chrome/common/pref_names.h" 20 #include "chrome/common/pref_names.h"
22 #include "chrome/test/base/testing_pref_service_syncable.h" 21 #include "chrome/test/base/testing_pref_service_syncable.h"
23 #include "chrome/test/base/testing_profile.h" 22 #include "chrome/test/base/testing_profile.h"
24 #include "content/public/test/test_browser_thread.h" 23 #include "content/public/test/test_browser_thread.h"
25 #include "google/cacheinvalidation/include/types.h" 24 #include "google/cacheinvalidation/include/types.h"
26 #include "google_apis/gaia/gaia_constants.h" 25 #include "google_apis/gaia/gaia_constants.h"
27 #include "sync/js/js_arg_list.h" 26 #include "sync/js/js_arg_list.h"
28 #include "sync/js/js_event_details.h" 27 #include "sync/js/js_event_details.h"
29 #include "sync/js/js_test_util.h" 28 #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"
30 #include "testing/gmock/include/gmock/gmock.h" 33 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
32 35
33 // TODO(akalin): Add tests here that exercise the whole 36 // TODO(akalin): Add tests here that exercise the whole
34 // ProfileSyncService/SyncBackendHost stack while mocking out as 37 // ProfileSyncService/SyncBackendHost stack while mocking out as
35 // little as possible. 38 // little as possible.
36 39
37 namespace browser_sync { 40 namespace browser_sync {
38 41
39 namespace { 42 namespace {
(...skipping 14 matching lines...) Expand all
54 file_thread_(BrowserThread::FILE), 57 file_thread_(BrowserThread::FILE),
55 io_thread_(BrowserThread::IO) {} 58 io_thread_(BrowserThread::IO) {}
56 59
57 ~ProfileSyncServiceTestHarness() {} 60 ~ProfileSyncServiceTestHarness() {}
58 61
59 void SetUp() { 62 void SetUp() {
60 file_thread_.Start(); 63 file_thread_.Start();
61 io_thread_.StartIOThread(); 64 io_thread_.StartIOThread();
62 profile.reset(new TestingProfile()); 65 profile.reset(new TestingProfile());
63 profile->CreateRequestContext(); 66 profile->CreateRequestContext();
64 invalidation::InvalidationServiceFactory::GetInstance()->
65 SetBuildOnlyFakeInvalidatorsForTest(true);
66 } 67 }
67 68
68 void TearDown() { 69 void TearDown() {
69 // Kill the service before the profile. 70 // Kill the service before the profile.
70 if (service) { 71 if (service) {
71 service->Shutdown(); 72 service->Shutdown();
72 } 73 }
73 service.reset(); 74 service.reset();
74 profile.reset(); 75 profile.reset();
75 // Pump messages posted by the sync thread (which may end up 76 // Pump messages posted by the sync thread (which may end up
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 EXPECT_TRUE(harness_.service->sync_initialized()); 350 EXPECT_TRUE(harness_.service->sync_initialized());
350 js_controller->RemoveJsEventHandler(&event_handler); 351 js_controller->RemoveJsEventHandler(&event_handler);
351 } 352 }
352 353
353 TEST_F(ProfileSyncServiceTest, JsControllerProcessJsMessageBasic) { 354 TEST_F(ProfileSyncServiceTest, JsControllerProcessJsMessageBasic) {
354 harness_.StartSyncService(); 355 harness_.StartSyncService();
355 356
356 StrictMock<syncer::MockJsReplyHandler> reply_handler; 357 StrictMock<syncer::MockJsReplyHandler> reply_handler;
357 358
358 ListValue arg_list1; 359 ListValue arg_list1;
359 arg_list1.Append(Value::CreateStringValue("INVALIDATIONS_ENABLED")); 360 arg_list1.Append(Value::CreateStringValue("TRANSIENT_INVALIDATION_ERROR"));
360 syncer::JsArgList args1(&arg_list1); 361 syncer::JsArgList args1(&arg_list1);
361 EXPECT_CALL(reply_handler, 362 EXPECT_CALL(reply_handler,
362 HandleJsReply("getNotificationState", HasArgs(args1))); 363 HandleJsReply("getNotificationState", HasArgs(args1)));
363 364
364 { 365 {
365 base::WeakPtr<syncer::JsController> js_controller = 366 base::WeakPtr<syncer::JsController> js_controller =
366 harness_.service->GetJsController(); 367 harness_.service->GetJsController();
367 js_controller->ProcessJsMessage("getNotificationState", args1, 368 js_controller->ProcessJsMessage("getNotificationState", args1,
368 reply_handler.AsWeakHandle()); 369 reply_handler.AsWeakHandle());
369 } 370 }
370 371
371 // This forces the sync thread to process the message and reply. 372 // This forces the sync thread to process the message and reply.
372 harness_.TearDown(); 373 harness_.TearDown();
373 } 374 }
374 375
375 TEST_F(ProfileSyncServiceTest, 376 TEST_F(ProfileSyncServiceTest,
376 JsControllerProcessJsMessageBasicDelayedBackendInitialization) { 377 JsControllerProcessJsMessageBasicDelayedBackendInitialization) {
377 harness_.StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, 378 harness_.StartSyncServiceAndSetInitialSyncEnded(true, false, false, true,
378 syncer::STORAGE_IN_MEMORY); 379 syncer::STORAGE_IN_MEMORY);
379 380
380 StrictMock<syncer::MockJsReplyHandler> reply_handler; 381 StrictMock<syncer::MockJsReplyHandler> reply_handler;
381 382
382 ListValue arg_list1; 383 ListValue arg_list1;
383 arg_list1.Append(Value::CreateStringValue("INVALIDATIONS_ENABLED")); 384 arg_list1.Append(Value::CreateStringValue("TRANSIENT_INVALIDATION_ERROR"));
384 syncer::JsArgList args1(&arg_list1); 385 syncer::JsArgList args1(&arg_list1);
385 EXPECT_CALL(reply_handler, 386 EXPECT_CALL(reply_handler,
386 HandleJsReply("getNotificationState", HasArgs(args1))); 387 HandleJsReply("getNotificationState", HasArgs(args1)));
387 388
388 { 389 {
389 base::WeakPtr<syncer::JsController> js_controller = 390 base::WeakPtr<syncer::JsController> js_controller =
390 harness_.service->GetJsController(); 391 harness_.service->GetJsController();
391 js_controller->ProcessJsMessage("getNotificationState", 392 js_controller->ProcessJsMessage("getNotificationState",
392 args1, reply_handler.AsWeakHandle()); 393 args1, reply_handler.AsWeakHandle());
393 } 394 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 // these tests). The sync_backend_host will notice this and inform the profile 460 // these tests). The sync_backend_host will notice this and inform the profile
460 // sync service of the failure to initialize the backed. 461 // sync service of the failure to initialize the backed.
461 TEST_F(ProfileSyncServiceTest, FailToDownloadControlTypes) { 462 TEST_F(ProfileSyncServiceTest, FailToDownloadControlTypes) {
462 harness_.StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, 463 harness_.StartSyncServiceAndSetInitialSyncEnded(false, true, true, true,
463 syncer::STORAGE_IN_MEMORY); 464 syncer::STORAGE_IN_MEMORY);
464 465
465 // The backend is not ready. Ensure the PSS knows this. 466 // The backend is not ready. Ensure the PSS knows this.
466 EXPECT_FALSE(harness_.service->sync_initialized()); 467 EXPECT_FALSE(harness_.service->sync_initialized());
467 } 468 }
468 469
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
469 } // namespace 580 } // 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
470 } // namespace browser_sync 633 } // 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
OLDNEW
« no previous file with comments | « chrome/browser/sync/profile_sync_service_typed_url_unittest.cc ('k') | chrome/browser/sync/test/integration/sync_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698