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   MockObserver() {}
     24   ~MockObserver() {}
     25 
     26   MOCK_METHOD0(SyncStartupCompleted, void(void));
     27   MOCK_METHOD0(SyncStartupFailed, void(void));
     28 };
     29 
     30 class SyncStartupTrackerTest : public testing::Test {
     31  public:
     32   SyncStartupTrackerTest() :
     33       no_error_(GoogleServiceAuthError::NONE) {
     34   }
     35   virtual void SetUp() OVERRIDE {
     36     profile_.reset(new TestingProfile());
     37     mock_pss_ = static_cast<ProfileSyncServiceMock*>(
     38         ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
     39             profile_.get(),
     40             ProfileSyncServiceMock::BuildMockProfileSyncService));
     41 
     42     // Make gmock not spam the output with information about these uninteresting
     43     // calls.
     44     EXPECT_CALL(*mock_pss_, AddObserver(_)).Times(AnyNumber());
     45     EXPECT_CALL(*mock_pss_, RemoveObserver(_)).Times(AnyNumber());
     46     EXPECT_CALL(*mock_pss_, GetAuthError()).
     47         WillRepeatedly(ReturnRef(no_error_));
     48 
     49     mock_pss_->Initialize();
     50   }
     51 
     52   virtual void TearDown() OVERRIDE {
     53     profile_.reset();
     54   }
     55 
     56   void SetupNonInitializedPSS() {
     57     EXPECT_CALL(*mock_pss_, GetAuthError())
     58         .WillRepeatedly(ReturnRef(no_error_));
     59     EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false));
     60     EXPECT_CALL(*mock_pss_, HasUnrecoverableError())
     61         .WillRepeatedly(Return(false));
     62     EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
     63         .WillRepeatedly(Return(true));
     64   }
     65 
     66   content::TestBrowserThreadBundle thread_bundle_;
     67   GoogleServiceAuthError no_error_;
     68   scoped_ptr<TestingProfile> profile_;
     69   ProfileSyncServiceMock* mock_pss_;
     70   MockObserver observer_;
     71 };
     72 
     73 TEST_F(SyncStartupTrackerTest, SyncAlreadyInitialized) {
     74   EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(true));
     75   EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
     76       .WillRepeatedly(Return(true));
     77   EXPECT_CALL(observer_, SyncStartupCompleted());
     78   SyncStartupTracker tracker(profile_.get(), &observer_);
     79 }
     80 
     81 TEST_F(SyncStartupTrackerTest, SyncNotSignedIn) {
     82   // Make sure that we get a SyncStartupFailed() callback if sync is not logged
     83   // in.
     84   EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false));
     85   EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
     86       Return(false));
     87   EXPECT_CALL(observer_, SyncStartupFailed());
     88   SyncStartupTracker tracker(profile_.get(), &observer_);
     89 }
     90 
     91 TEST_F(SyncStartupTrackerTest, SyncAuthError) {
     92   // Make sure that we get a SyncStartupFailed() callback if sync gets an auth
     93   // error.
     94   EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false));
     95   EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
     96       Return(true));
     97   GoogleServiceAuthError error(
     98       GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
     99   EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
    100   EXPECT_CALL(observer_, SyncStartupFailed());
    101   SyncStartupTracker tracker(profile_.get(), &observer_);
    102 }
    103 
    104 TEST_F(SyncStartupTrackerTest, SyncDelayedInitialization) {
    105   // Non-initialized PSS should result in no callbacks to the observer.
    106   SetupNonInitializedPSS();
    107   EXPECT_CALL(observer_, SyncStartupCompleted()).Times(0);
    108   EXPECT_CALL(observer_, SyncStartupFailed()).Times(0);
    109   SyncStartupTracker tracker(profile_.get(), &observer_);
    110   Mock::VerifyAndClearExpectations(&observer_);
    111   // Now, mark the PSS as initialized.
    112   EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(true));
    113   EXPECT_CALL(observer_, SyncStartupCompleted());
    114   tracker.OnStateChanged();
    115 }
    116 
    117 TEST_F(SyncStartupTrackerTest, SyncDelayedAuthError) {
    118   // Non-initialized PSS should result in no callbacks to the observer.
    119   SetupNonInitializedPSS();
    120   EXPECT_CALL(observer_, SyncStartupCompleted()).Times(0);
    121   EXPECT_CALL(observer_, SyncStartupFailed()).Times(0);
    122   SyncStartupTracker tracker(profile_.get(), &observer_);
    123   Mock::VerifyAndClearExpectations(&observer_);
    124   Mock::VerifyAndClearExpectations(mock_pss_);
    125 
    126   // Now, mark the PSS as having an auth error.
    127   EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false));
    128   EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
    129       Return(true));
    130   GoogleServiceAuthError error(
    131       GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
    132   EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
    133   EXPECT_CALL(observer_, SyncStartupFailed());
    134   tracker.OnStateChanged();
    135 }
    136 
    137 TEST_F(SyncStartupTrackerTest, SyncDelayedUnrecoverableError) {
    138   // Non-initialized PSS should result in no callbacks to the observer.
    139   SetupNonInitializedPSS();
    140   EXPECT_CALL(observer_, SyncStartupCompleted()).Times(0);
    141   EXPECT_CALL(observer_, SyncStartupFailed()).Times(0);
    142   SyncStartupTracker tracker(profile_.get(), &observer_);
    143   Mock::VerifyAndClearExpectations(&observer_);
    144   Mock::VerifyAndClearExpectations(mock_pss_);
    145 
    146   // Now, mark the PSS as having an unrecoverable error.
    147   EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false));
    148   EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
    149       Return(true));
    150   GoogleServiceAuthError error(
    151       GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
    152   EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
    153   EXPECT_CALL(observer_, SyncStartupFailed());
    154   tracker.OnStateChanged();
    155 }
    156 
    157 }  // namespace
    158