Home | History | Annotate | Download | only in sync
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/memory/scoped_ptr.h"
      6 #include "chrome/browser/sync/profile_sync_service_factory.h"
      7 #include "chrome/browser/sync/profile_sync_service_mock.h"
      8 #include "chrome/browser/sync/sync_startup_tracker.h"
      9 #include "content/public/test/test_browser_thread_bundle.h"
     10 #include "testing/gmock/include/gmock/gmock.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 using ::testing::_;
     14 using ::testing::AnyNumber;
     15 using ::testing::Mock;
     16 using ::testing::Return;
     17 using ::testing::ReturnRef;
     18 
     19 namespace {
     20 
     21 class MockObserver : public SyncStartupTracker::Observer {
     22  public:
     23   MOCK_METHOD0(SyncStartupCompleted, void(void));
     24   MOCK_METHOD0(SyncStartupFailed, void(void));
     25 };
     26 
     27 class SyncStartupTrackerTest : public testing::Test {
     28  public:
     29   SyncStartupTrackerTest() :
     30       no_error_(GoogleServiceAuthError::NONE) {
     31   }
     32   virtual void SetUp() OVERRIDE {
     33     profile_.reset(new TestingProfile());
     34     mock_pss_ = static_cast<ProfileSyncServiceMock*>(
     35         ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
     36             profile_.get(),
     37             ProfileSyncServiceMock::BuildMockProfileSyncService));
     38 
     39     // Make gmock not spam the output with information about these uninteresting
     40     // calls.
     41     EXPECT_CALL(*mock_pss_, AddObserver(_)).Times(AnyNumber());
     42     EXPECT_CALL(*mock_pss_, RemoveObserver(_)).Times(AnyNumber());
     43     EXPECT_CALL(*mock_pss_, GetAuthError()).
     44         WillRepeatedly(ReturnRef(no_error_));
     45     ON_CALL(*mock_pss_, GetRegisteredDataTypes())
     46         .WillByDefault(Return(syncer::ModelTypeSet()));
     47     mock_pss_->Initialize();
     48   }
     49 
     50   virtual void TearDown() OVERRIDE {
     51     profile_.reset();
     52   }
     53 
     54   void SetupNonInitializedPSS() {
     55     EXPECT_CALL(*mock_pss_, GetAuthError())
     56         .WillRepeatedly(ReturnRef(no_error_));
     57     EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false));
     58     EXPECT_CALL(*mock_pss_, HasUnrecoverableError())
     59         .WillRepeatedly(Return(false));
     60     EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
     61         .WillRepeatedly(Return(true));
     62   }
     63 
     64   content::TestBrowserThreadBundle thread_bundle_;
     65   GoogleServiceAuthError no_error_;
     66   scoped_ptr<TestingProfile> profile_;
     67   ProfileSyncServiceMock* mock_pss_;
     68   MockObserver observer_;
     69 };
     70 
     71 TEST_F(SyncStartupTrackerTest, SyncAlreadyInitialized) {
     72   EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(true));
     73   EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
     74       .WillRepeatedly(Return(true));
     75   EXPECT_CALL(observer_, SyncStartupCompleted());
     76   SyncStartupTracker tracker(profile_.get(), &observer_);
     77 }
     78 
     79 TEST_F(SyncStartupTrackerTest, SyncNotSignedIn) {
     80   // Make sure that we get a SyncStartupFailed() callback if sync is not logged
     81   // in.
     82   EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false));
     83   EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
     84       Return(false));
     85   EXPECT_CALL(observer_, SyncStartupFailed());
     86   SyncStartupTracker tracker(profile_.get(), &observer_);
     87 }
     88 
     89 TEST_F(SyncStartupTrackerTest, SyncAuthError) {
     90   // Make sure that we get a SyncStartupFailed() callback if sync gets an auth
     91   // error.
     92   EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false));
     93   EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
     94       Return(true));
     95   GoogleServiceAuthError error(
     96       GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
     97   EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
     98   EXPECT_CALL(observer_, SyncStartupFailed());
     99   SyncStartupTracker tracker(profile_.get(), &observer_);
    100 }
    101 
    102 TEST_F(SyncStartupTrackerTest, SyncDelayedInitialization) {
    103   // Non-initialized PSS should result in no callbacks to the observer.
    104   SetupNonInitializedPSS();
    105   EXPECT_CALL(observer_, SyncStartupCompleted()).Times(0);
    106   EXPECT_CALL(observer_, SyncStartupFailed()).Times(0);
    107   SyncStartupTracker tracker(profile_.get(), &observer_);
    108   Mock::VerifyAndClearExpectations(&observer_);
    109   // Now, mark the PSS as initialized.
    110   EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(true));
    111   EXPECT_CALL(observer_, SyncStartupCompleted());
    112   tracker.OnStateChanged();
    113 }
    114 
    115 TEST_F(SyncStartupTrackerTest, SyncDelayedAuthError) {
    116   // Non-initialized PSS should result in no callbacks to the observer.
    117   SetupNonInitializedPSS();
    118   EXPECT_CALL(observer_, SyncStartupCompleted()).Times(0);
    119   EXPECT_CALL(observer_, SyncStartupFailed()).Times(0);
    120   SyncStartupTracker tracker(profile_.get(), &observer_);
    121   Mock::VerifyAndClearExpectations(&observer_);
    122   Mock::VerifyAndClearExpectations(mock_pss_);
    123 
    124   // Now, mark the PSS as having an auth error.
    125   EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false));
    126   EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
    127       Return(true));
    128   GoogleServiceAuthError error(
    129       GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
    130   EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
    131   EXPECT_CALL(observer_, SyncStartupFailed());
    132   tracker.OnStateChanged();
    133 }
    134 
    135 TEST_F(SyncStartupTrackerTest, SyncDelayedUnrecoverableError) {
    136   // Non-initialized PSS should result in no callbacks to the observer.
    137   SetupNonInitializedPSS();
    138   EXPECT_CALL(observer_, SyncStartupCompleted()).Times(0);
    139   EXPECT_CALL(observer_, SyncStartupFailed()).Times(0);
    140   SyncStartupTracker tracker(profile_.get(), &observer_);
    141   Mock::VerifyAndClearExpectations(&observer_);
    142   Mock::VerifyAndClearExpectations(mock_pss_);
    143 
    144   // Now, mark the PSS as having an unrecoverable error.
    145   EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false));
    146   EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
    147       Return(true));
    148   GoogleServiceAuthError error(
    149       GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
    150   EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
    151   EXPECT_CALL(observer_, SyncStartupFailed());
    152   tracker.OnStateChanged();
    153 }
    154 
    155 }  // namespace
    156