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

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

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

Powered by Google App Engine
This is Rietveld 408576698