Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / sync / sync_startup_tracker_unittest.cc
blobcd49e63c29047f5f976cf092403089aa14c09563
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.
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"
13 using ::testing::_;
14 using ::testing::AnyNumber;
15 using ::testing::Mock;
16 using ::testing::Return;
17 using ::testing::ReturnRef;
19 namespace {
21 class MockObserver : public SyncStartupTracker::Observer {
22 public:
23 MOCK_METHOD0(SyncStartupCompleted, void(void));
24 MOCK_METHOD0(SyncStartupFailed, void(void));
27 class SyncStartupTrackerTest : public testing::Test {
28 public:
29 SyncStartupTrackerTest() :
30 no_error_(GoogleServiceAuthError::NONE) {
32 void SetUp() override {
33 profile_.reset(new TestingProfile());
34 mock_pss_ = static_cast<ProfileSyncServiceMock*>(
35 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
36 profile_.get(),
37 ProfileSyncServiceMock::BuildMockProfileSyncService));
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();
50 void TearDown() override { profile_.reset(); }
52 void SetupNonInitializedPSS() {
53 EXPECT_CALL(*mock_pss_, GetAuthError())
54 .WillRepeatedly(ReturnRef(no_error_));
55 EXPECT_CALL(*mock_pss_, IsBackendInitialized())
56 .WillRepeatedly(Return(false));
57 EXPECT_CALL(*mock_pss_, HasUnrecoverableError())
58 .WillRepeatedly(Return(false));
59 EXPECT_CALL(*mock_pss_, CanSyncStart()).WillRepeatedly(Return(true));
62 content::TestBrowserThreadBundle thread_bundle_;
63 GoogleServiceAuthError no_error_;
64 scoped_ptr<TestingProfile> profile_;
65 ProfileSyncServiceMock* mock_pss_;
66 MockObserver observer_;
69 TEST_F(SyncStartupTrackerTest, SyncAlreadyInitialized) {
70 EXPECT_CALL(*mock_pss_, IsBackendInitialized()).WillRepeatedly(Return(true));
71 EXPECT_CALL(*mock_pss_, CanSyncStart()).WillRepeatedly(Return(true));
72 EXPECT_CALL(observer_, SyncStartupCompleted());
73 SyncStartupTracker tracker(profile_.get(), &observer_);
76 TEST_F(SyncStartupTrackerTest, SyncNotSignedIn) {
77 // Make sure that we get a SyncStartupFailed() callback if sync is not logged
78 // in.
79 EXPECT_CALL(*mock_pss_, IsBackendInitialized()).WillRepeatedly(Return(false));
80 EXPECT_CALL(*mock_pss_, CanSyncStart()).WillRepeatedly(Return(false));
81 EXPECT_CALL(observer_, SyncStartupFailed());
82 SyncStartupTracker tracker(profile_.get(), &observer_);
85 TEST_F(SyncStartupTrackerTest, SyncAuthError) {
86 // Make sure that we get a SyncStartupFailed() callback if sync gets an auth
87 // error.
88 EXPECT_CALL(*mock_pss_, IsBackendInitialized()).WillRepeatedly(Return(false));
89 EXPECT_CALL(*mock_pss_, CanSyncStart()).WillRepeatedly(Return(true));
90 GoogleServiceAuthError error(
91 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
92 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
93 EXPECT_CALL(observer_, SyncStartupFailed());
94 SyncStartupTracker tracker(profile_.get(), &observer_);
97 TEST_F(SyncStartupTrackerTest, SyncDelayedInitialization) {
98 // Non-initialized PSS should result in no callbacks to the observer.
99 SetupNonInitializedPSS();
100 EXPECT_CALL(observer_, SyncStartupCompleted()).Times(0);
101 EXPECT_CALL(observer_, SyncStartupFailed()).Times(0);
102 SyncStartupTracker tracker(profile_.get(), &observer_);
103 Mock::VerifyAndClearExpectations(&observer_);
104 // Now, mark the PSS as initialized.
105 EXPECT_CALL(*mock_pss_, IsBackendInitialized()).WillRepeatedly(Return(true));
106 EXPECT_CALL(observer_, SyncStartupCompleted());
107 tracker.OnStateChanged();
110 TEST_F(SyncStartupTrackerTest, SyncDelayedAuthError) {
111 // Non-initialized PSS should result in no callbacks to the observer.
112 SetupNonInitializedPSS();
113 EXPECT_CALL(observer_, SyncStartupCompleted()).Times(0);
114 EXPECT_CALL(observer_, SyncStartupFailed()).Times(0);
115 SyncStartupTracker tracker(profile_.get(), &observer_);
116 Mock::VerifyAndClearExpectations(&observer_);
117 Mock::VerifyAndClearExpectations(mock_pss_);
119 // Now, mark the PSS as having an auth error.
120 EXPECT_CALL(*mock_pss_, IsBackendInitialized()).WillRepeatedly(Return(false));
121 EXPECT_CALL(*mock_pss_, CanSyncStart()).WillRepeatedly(Return(true));
122 GoogleServiceAuthError error(
123 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
124 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
125 EXPECT_CALL(observer_, SyncStartupFailed());
126 tracker.OnStateChanged();
129 TEST_F(SyncStartupTrackerTest, SyncDelayedUnrecoverableError) {
130 // Non-initialized PSS should result in no callbacks to the observer.
131 SetupNonInitializedPSS();
132 EXPECT_CALL(observer_, SyncStartupCompleted()).Times(0);
133 EXPECT_CALL(observer_, SyncStartupFailed()).Times(0);
134 SyncStartupTracker tracker(profile_.get(), &observer_);
135 Mock::VerifyAndClearExpectations(&observer_);
136 Mock::VerifyAndClearExpectations(mock_pss_);
138 // Now, mark the PSS as having an unrecoverable error.
139 EXPECT_CALL(*mock_pss_, IsBackendInitialized()).WillRepeatedly(Return(false));
140 EXPECT_CALL(*mock_pss_, CanSyncStart()).WillRepeatedly(Return(true));
141 GoogleServiceAuthError error(
142 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
143 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
144 EXPECT_CALL(observer_, SyncStartupFailed());
145 tracker.OnStateChanged();
148 } // namespace