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 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 void TearDown() override
{ profile_
.reset(); }
52 void SetupNonInitializedPSS() {
53 EXPECT_CALL(*mock_pss_
, GetAuthError())
54 .WillRepeatedly(ReturnRef(no_error_
));
55 EXPECT_CALL(*mock_pss_
, backend_initialized())
56 .WillRepeatedly(Return(false));
57 EXPECT_CALL(*mock_pss_
, HasUnrecoverableError())
58 .WillRepeatedly(Return(false));
59 EXPECT_CALL(*mock_pss_
, IsSyncEnabledAndLoggedIn())
60 .WillRepeatedly(Return(true));
63 content::TestBrowserThreadBundle thread_bundle_
;
64 GoogleServiceAuthError no_error_
;
65 scoped_ptr
<TestingProfile
> profile_
;
66 ProfileSyncServiceMock
* mock_pss_
;
67 MockObserver observer_
;
70 TEST_F(SyncStartupTrackerTest
, SyncAlreadyInitialized
) {
71 EXPECT_CALL(*mock_pss_
, backend_initialized()).WillRepeatedly(Return(true));
72 EXPECT_CALL(*mock_pss_
, IsSyncEnabledAndLoggedIn())
73 .WillRepeatedly(Return(true));
74 EXPECT_CALL(observer_
, SyncStartupCompleted());
75 SyncStartupTracker
tracker(profile_
.get(), &observer_
);
78 TEST_F(SyncStartupTrackerTest
, SyncNotSignedIn
) {
79 // Make sure that we get a SyncStartupFailed() callback if sync is not logged
81 EXPECT_CALL(*mock_pss_
, backend_initialized()).WillRepeatedly(Return(false));
82 EXPECT_CALL(*mock_pss_
, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
84 EXPECT_CALL(observer_
, SyncStartupFailed());
85 SyncStartupTracker
tracker(profile_
.get(), &observer_
);
88 TEST_F(SyncStartupTrackerTest
, SyncAuthError
) {
89 // Make sure that we get a SyncStartupFailed() callback if sync gets an auth
91 EXPECT_CALL(*mock_pss_
, backend_initialized()).WillRepeatedly(Return(false));
92 EXPECT_CALL(*mock_pss_
, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
94 GoogleServiceAuthError
error(
95 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
96 EXPECT_CALL(*mock_pss_
, GetAuthError()).WillRepeatedly(ReturnRef(error
));
97 EXPECT_CALL(observer_
, SyncStartupFailed());
98 SyncStartupTracker
tracker(profile_
.get(), &observer_
);
101 TEST_F(SyncStartupTrackerTest
, SyncDelayedInitialization
) {
102 // Non-initialized PSS should result in no callbacks to the observer.
103 SetupNonInitializedPSS();
104 EXPECT_CALL(observer_
, SyncStartupCompleted()).Times(0);
105 EXPECT_CALL(observer_
, SyncStartupFailed()).Times(0);
106 SyncStartupTracker
tracker(profile_
.get(), &observer_
);
107 Mock::VerifyAndClearExpectations(&observer_
);
108 // Now, mark the PSS as initialized.
109 EXPECT_CALL(*mock_pss_
, backend_initialized()).WillRepeatedly(Return(true));
110 EXPECT_CALL(observer_
, SyncStartupCompleted());
111 tracker
.OnStateChanged();
114 TEST_F(SyncStartupTrackerTest
, SyncDelayedAuthError
) {
115 // Non-initialized PSS should result in no callbacks to the observer.
116 SetupNonInitializedPSS();
117 EXPECT_CALL(observer_
, SyncStartupCompleted()).Times(0);
118 EXPECT_CALL(observer_
, SyncStartupFailed()).Times(0);
119 SyncStartupTracker
tracker(profile_
.get(), &observer_
);
120 Mock::VerifyAndClearExpectations(&observer_
);
121 Mock::VerifyAndClearExpectations(mock_pss_
);
123 // Now, mark the PSS as having an auth error.
124 EXPECT_CALL(*mock_pss_
, backend_initialized()).WillRepeatedly(Return(false));
125 EXPECT_CALL(*mock_pss_
, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
127 GoogleServiceAuthError
error(
128 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
129 EXPECT_CALL(*mock_pss_
, GetAuthError()).WillRepeatedly(ReturnRef(error
));
130 EXPECT_CALL(observer_
, SyncStartupFailed());
131 tracker
.OnStateChanged();
134 TEST_F(SyncStartupTrackerTest
, SyncDelayedUnrecoverableError
) {
135 // Non-initialized PSS should result in no callbacks to the observer.
136 SetupNonInitializedPSS();
137 EXPECT_CALL(observer_
, SyncStartupCompleted()).Times(0);
138 EXPECT_CALL(observer_
, SyncStartupFailed()).Times(0);
139 SyncStartupTracker
tracker(profile_
.get(), &observer_
);
140 Mock::VerifyAndClearExpectations(&observer_
);
141 Mock::VerifyAndClearExpectations(mock_pss_
);
143 // Now, mark the PSS as having an unrecoverable error.
144 EXPECT_CALL(*mock_pss_
, backend_initialized()).WillRepeatedly(Return(false));
145 EXPECT_CALL(*mock_pss_
, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
147 GoogleServiceAuthError
error(
148 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
149 EXPECT_CALL(*mock_pss_
, GetAuthError()).WillRepeatedly(ReturnRef(error
));
150 EXPECT_CALL(observer_
, SyncStartupFailed());
151 tracker
.OnStateChanged();