Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / sync / sync_startup_tracker_unittest.cc
blob8b1d907bce830f0998c2259e7b768ad04a25d561
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 virtual 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 virtual void TearDown() override {
51 profile_.reset();
54 void SetupNonInitializedPSS() {
55 EXPECT_CALL(*mock_pss_, GetAuthError())
56 .WillRepeatedly(ReturnRef(no_error_));
57 EXPECT_CALL(*mock_pss_, backend_initialized())
58 .WillRepeatedly(Return(false));
59 EXPECT_CALL(*mock_pss_, HasUnrecoverableError())
60 .WillRepeatedly(Return(false));
61 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
62 .WillRepeatedly(Return(true));
65 content::TestBrowserThreadBundle thread_bundle_;
66 GoogleServiceAuthError no_error_;
67 scoped_ptr<TestingProfile> profile_;
68 ProfileSyncServiceMock* mock_pss_;
69 MockObserver observer_;
72 TEST_F(SyncStartupTrackerTest, SyncAlreadyInitialized) {
73 EXPECT_CALL(*mock_pss_, backend_initialized()).WillRepeatedly(Return(true));
74 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
75 .WillRepeatedly(Return(true));
76 EXPECT_CALL(observer_, SyncStartupCompleted());
77 SyncStartupTracker tracker(profile_.get(), &observer_);
80 TEST_F(SyncStartupTrackerTest, SyncNotSignedIn) {
81 // Make sure that we get a SyncStartupFailed() callback if sync is not logged
82 // in.
83 EXPECT_CALL(*mock_pss_, backend_initialized()).WillRepeatedly(Return(false));
84 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
85 Return(false));
86 EXPECT_CALL(observer_, SyncStartupFailed());
87 SyncStartupTracker tracker(profile_.get(), &observer_);
90 TEST_F(SyncStartupTrackerTest, SyncAuthError) {
91 // Make sure that we get a SyncStartupFailed() callback if sync gets an auth
92 // error.
93 EXPECT_CALL(*mock_pss_, backend_initialized()).WillRepeatedly(Return(false));
94 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
95 Return(true));
96 GoogleServiceAuthError error(
97 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
98 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
99 EXPECT_CALL(observer_, SyncStartupFailed());
100 SyncStartupTracker tracker(profile_.get(), &observer_);
103 TEST_F(SyncStartupTrackerTest, SyncDelayedInitialization) {
104 // Non-initialized PSS should result in no callbacks to the observer.
105 SetupNonInitializedPSS();
106 EXPECT_CALL(observer_, SyncStartupCompleted()).Times(0);
107 EXPECT_CALL(observer_, SyncStartupFailed()).Times(0);
108 SyncStartupTracker tracker(profile_.get(), &observer_);
109 Mock::VerifyAndClearExpectations(&observer_);
110 // Now, mark the PSS as initialized.
111 EXPECT_CALL(*mock_pss_, backend_initialized()).WillRepeatedly(Return(true));
112 EXPECT_CALL(observer_, SyncStartupCompleted());
113 tracker.OnStateChanged();
116 TEST_F(SyncStartupTrackerTest, SyncDelayedAuthError) {
117 // Non-initialized PSS should result in no callbacks to the observer.
118 SetupNonInitializedPSS();
119 EXPECT_CALL(observer_, SyncStartupCompleted()).Times(0);
120 EXPECT_CALL(observer_, SyncStartupFailed()).Times(0);
121 SyncStartupTracker tracker(profile_.get(), &observer_);
122 Mock::VerifyAndClearExpectations(&observer_);
123 Mock::VerifyAndClearExpectations(mock_pss_);
125 // Now, mark the PSS as having an auth error.
126 EXPECT_CALL(*mock_pss_, backend_initialized()).WillRepeatedly(Return(false));
127 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
128 Return(true));
129 GoogleServiceAuthError error(
130 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
131 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
132 EXPECT_CALL(observer_, SyncStartupFailed());
133 tracker.OnStateChanged();
136 TEST_F(SyncStartupTrackerTest, SyncDelayedUnrecoverableError) {
137 // Non-initialized PSS should result in no callbacks to the observer.
138 SetupNonInitializedPSS();
139 EXPECT_CALL(observer_, SyncStartupCompleted()).Times(0);
140 EXPECT_CALL(observer_, SyncStartupFailed()).Times(0);
141 SyncStartupTracker tracker(profile_.get(), &observer_);
142 Mock::VerifyAndClearExpectations(&observer_);
143 Mock::VerifyAndClearExpectations(mock_pss_);
145 // Now, mark the PSS as having an unrecoverable error.
146 EXPECT_CALL(*mock_pss_, backend_initialized()).WillRepeatedly(Return(false));
147 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
148 Return(true));
149 GoogleServiceAuthError error(
150 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
151 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
152 EXPECT_CALL(observer_, SyncStartupFailed());
153 tracker.OnStateChanged();
156 } // namespace