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"
14 using ::testing::AnyNumber
;
15 using ::testing::Mock
;
16 using ::testing::Return
;
17 using ::testing::ReturnRef
;
21 class MockObserver
: public SyncStartupTracker::Observer
{
26 MOCK_METHOD0(SyncStartupCompleted
, void(void));
27 MOCK_METHOD0(SyncStartupFailed
, void(void));
30 class SyncStartupTrackerTest
: public testing::Test
{
32 SyncStartupTrackerTest() :
33 no_error_(GoogleServiceAuthError::NONE
) {
35 virtual void SetUp() OVERRIDE
{
36 profile_
.reset(new TestingProfile());
37 mock_pss_
= static_cast<ProfileSyncServiceMock
*>(
38 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
40 ProfileSyncServiceMock::BuildMockProfileSyncService
));
42 // Make gmock not spam the output with information about these uninteresting
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_
));
49 mock_pss_
->Initialize();
52 virtual void TearDown() OVERRIDE
{
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));
66 content::TestBrowserThreadBundle thread_bundle_
;
67 GoogleServiceAuthError no_error_
;
68 scoped_ptr
<TestingProfile
> profile_
;
69 ProfileSyncServiceMock
* mock_pss_
;
70 MockObserver observer_
;
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_
);
81 TEST_F(SyncStartupTrackerTest
, SyncNotSignedIn
) {
82 // Make sure that we get a SyncStartupFailed() callback if sync is not logged
84 EXPECT_CALL(*mock_pss_
, sync_initialized()).WillRepeatedly(Return(false));
85 EXPECT_CALL(*mock_pss_
, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
87 EXPECT_CALL(observer_
, SyncStartupFailed());
88 SyncStartupTracker
tracker(profile_
.get(), &observer_
);
91 TEST_F(SyncStartupTrackerTest
, SyncAuthError
) {
92 // Make sure that we get a SyncStartupFailed() callback if sync gets an auth
94 EXPECT_CALL(*mock_pss_
, sync_initialized()).WillRepeatedly(Return(false));
95 EXPECT_CALL(*mock_pss_
, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
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_
);
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();
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_
);
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(
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();
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_
);
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(
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();