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
{
23 MOCK_METHOD0(SyncStartupCompleted
, void(void));
24 MOCK_METHOD0(SyncStartupFailed
, void(void));
27 class SyncStartupTrackerTest
: public testing::Test
{
29 SyncStartupTrackerTest() :
30 no_error_(GoogleServiceAuthError::NONE
) {
32 virtual void SetUp() OVERRIDE
{
33 profile_
.reset(new TestingProfile());
34 mock_pss_
= static_cast<ProfileSyncServiceMock
*>(
35 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
37 ProfileSyncServiceMock::BuildMockProfileSyncService
));
39 // Make gmock not spam the output with information about these uninteresting
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 virtual void TearDown() OVERRIDE
{
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));
64 content::TestBrowserThreadBundle thread_bundle_
;
65 GoogleServiceAuthError no_error_
;
66 scoped_ptr
<TestingProfile
> profile_
;
67 ProfileSyncServiceMock
* mock_pss_
;
68 MockObserver observer_
;
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_
);
79 TEST_F(SyncStartupTrackerTest
, SyncNotSignedIn
) {
80 // Make sure that we get a SyncStartupFailed() callback if sync is not logged
82 EXPECT_CALL(*mock_pss_
, sync_initialized()).WillRepeatedly(Return(false));
83 EXPECT_CALL(*mock_pss_
, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
85 EXPECT_CALL(observer_
, SyncStartupFailed());
86 SyncStartupTracker
tracker(profile_
.get(), &observer_
);
89 TEST_F(SyncStartupTrackerTest
, SyncAuthError
) {
90 // Make sure that we get a SyncStartupFailed() callback if sync gets an auth
92 EXPECT_CALL(*mock_pss_
, sync_initialized()).WillRepeatedly(Return(false));
93 EXPECT_CALL(*mock_pss_
, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
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_
);
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();
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_
);
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(
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();
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_
);
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(
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();