Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: chrome/browser/sync/profile_sync_service_startup_unittest.cc

Issue 11348220: sync: centralize sync startup decisions in TryStart. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: take 2 Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "testing/gtest/include/gtest/gtest.h" 5 #include "testing/gtest/include/gtest/gtest.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "chrome/browser/prefs/pref_service.h" 9 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/browser/signin/signin_manager.h" 10 #include "chrome/browser/signin/signin_manager.h"
(...skipping 30 matching lines...) Expand all
41 service->OnConfigureStart(); 41 service->OnConfigureStart();
42 } 42 }
43 43
44 ACTION_P2(InvokeOnConfigureDone, pss, result) { 44 ACTION_P2(InvokeOnConfigureDone, pss, result) {
45 TestProfileSyncService* service = static_cast<TestProfileSyncService*>(pss); 45 TestProfileSyncService* service = static_cast<TestProfileSyncService*>(pss);
46 DataTypeManager::ConfigureResult configure_result = 46 DataTypeManager::ConfigureResult configure_result =
47 static_cast<DataTypeManager::ConfigureResult>(result); 47 static_cast<DataTypeManager::ConfigureResult>(result);
48 service->OnConfigureDone(configure_result); 48 service->OnConfigureDone(configure_result);
49 } 49 }
50 50
51 class FakeTokenService : public TokenService {
52 public:
53 FakeTokenService() {}
54 virtual ~FakeTokenService() {}
55
56 virtual void LoadTokensFromDB() OVERRIDE {
57 TokenService::set_tokens_loaded(true);
58 content::NotificationService::current()->Notify(
59 chrome::NOTIFICATION_TOKEN_LOADING_FINISHED,
60 content::Source<TokenService>(this),
61 content::NotificationService::NoDetails());
62 }
63 };
64
51 // TODO(chron): Test not using cros_user flag and use signin_ 65 // TODO(chron): Test not using cros_user flag and use signin_
52 class ProfileSyncServiceStartupTest : public testing::Test { 66 class ProfileSyncServiceStartupTest : public testing::Test {
53 public: 67 public:
54 ProfileSyncServiceStartupTest() 68 ProfileSyncServiceStartupTest()
55 : ui_thread_(BrowserThread::UI, &ui_loop_), 69 : ui_thread_(BrowserThread::UI, &ui_loop_),
56 db_thread_(BrowserThread::DB), 70 db_thread_(BrowserThread::DB),
57 file_thread_(BrowserThread::FILE), 71 file_thread_(BrowserThread::FILE),
58 io_thread_(BrowserThread::IO), 72 io_thread_(BrowserThread::IO),
59 profile_(new TestingProfile) {} 73 profile_(new TestingProfile) {}
60 74
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 service_->SetSetupInProgress(true); 183 service_->SetSetupInProgress(true);
170 service_->signin()->StartSignIn("test_user", "", "", ""); 184 service_->signin()->StartSignIn("test_user", "", "", "");
171 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( 185 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
172 GaiaConstants::kSyncService, "sync_token"); 186 GaiaConstants::kSyncService, "sync_token");
173 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( 187 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
174 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token"); 188 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
175 service_->SetSetupInProgress(false); 189 service_->SetSetupInProgress(false);
176 EXPECT_TRUE(service_->ShouldPushChanges()); 190 EXPECT_TRUE(service_->ShouldPushChanges());
177 } 191 }
178 192
193 ProfileKeyedService* BuildFakeTokenService(Profile* profile) {
194 return new FakeTokenService();
195 }
196
179 TEST_F(ProfileSyncServiceStartupTest, StartNoCredentials) { 197 TEST_F(ProfileSyncServiceStartupTest, StartNoCredentials) {
180 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); 198 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
181 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0); 199 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0);
200 TokenService* token_service = static_cast<TokenService*>(
201 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse(
202 profile_.get(), BuildFakeTokenService));
182 203
183 // We've never completed startup. 204 // We've never completed startup.
184 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); 205 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted);
185 // Make sure SigninManager doesn't think we're signed in (undoes the call to 206 // Make sure SigninManager doesn't think we're signed in (undoes the call to
186 // SetAuthenticatedUsername() in CreateSyncService()). 207 // SetAuthenticatedUsername() in CreateSyncService()).
187 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); 208 SigninManagerFactory::GetForProfile(profile_.get())->SignOut();
188 209
189 // Should not actually start, rather just clean things up and wait 210 // Should not actually start, rather just clean things up and wait
190 // to be enabled. 211 // to be enabled.
191 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 212 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
192 service_->Initialize(); 213 service_->Initialize();
193 EXPECT_FALSE(service_->GetBackendForTest()); 214 EXPECT_FALSE(service_->GetBackendForTest());
194 215
195 // Preferences should be back to defaults. 216 // Preferences should be back to defaults.
196 EXPECT_EQ(0, profile_->GetPrefs()->GetInt64(prefs::kSyncLastSyncedTime)); 217 EXPECT_EQ(0, profile_->GetPrefs()->GetInt64(prefs::kSyncLastSyncedTime));
197 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(prefs::kSyncHasSetupCompleted)); 218 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(prefs::kSyncHasSetupCompleted));
198 Mock::VerifyAndClearExpectations(data_type_manager); 219 Mock::VerifyAndClearExpectations(data_type_manager);
199 220
200 // Then start things up. 221 // Then start things up.
201 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(1); 222 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(1);
202 EXPECT_CALL(*data_type_manager, state()). 223 EXPECT_CALL(*data_type_manager, state()).
203 WillOnce(Return(DataTypeManager::CONFIGURED)). 224 WillOnce(Return(DataTypeManager::CONFIGURED)).
204 WillOnce(Return(DataTypeManager::CONFIGURED)); 225 WillOnce(Return(DataTypeManager::CONFIGURED));
205 EXPECT_CALL(*data_type_manager, Stop()).Times(1); 226 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
206 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 227 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
207 228
208 service_->SetSetupInProgress(true); 229 service_->SetSetupInProgress(true);
209 service_->signin()->StartSignIn("test_user", "", "", ""); 230 service_->signin()->StartSignIn("test_user", "", "", "");
210 // NOTE: Unlike StartFirstTime, this test does not issue any auth tokens. 231 // NOTE: Unlike StartFirstTime, this test does not issue any auth tokens.
211 content::NotificationService::current()->Notify( 232 token_service->LoadTokensFromDB();
212 chrome::NOTIFICATION_TOKEN_LOADING_FINISHED,
213 content::Source<TokenService>(
214 TokenServiceFactory::GetForProfile(profile_.get())),
215 content::NotificationService::NoDetails());
216 service_->SetSetupInProgress(false); 233 service_->SetSetupInProgress(false);
217 // Backend should initialize using a bogus GAIA token for credentials. 234 // Backend should initialize using a bogus GAIA token for credentials.
218 EXPECT_TRUE(service_->ShouldPushChanges()); 235 EXPECT_TRUE(service_->ShouldPushChanges());
219 } 236 }
220 237
221 TEST_F(ProfileSyncServiceStartupCrosTest, StartCrosNoCredentials) { 238 TEST_F(ProfileSyncServiceStartupCrosTest, StartCrosNoCredentials) {
222 EXPECT_CALL(*factory_mock(), CreateDataTypeManager(_, _, _, _)).Times(0); 239 EXPECT_CALL(*factory_mock(), CreateDataTypeManager(_, _, _, _)).Times(0);
223 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); 240 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted);
224 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 241 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
242 TokenService* token_service = static_cast<TokenService*>(
243 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse(
244 profile_.get(), BuildFakeTokenService));
225 245
226 service_->Initialize(); 246 service_->Initialize();
227 // Sync should not start because there are no tokens yet. 247 // Sync should not start because there are no tokens yet.
228 EXPECT_FALSE(service_->ShouldPushChanges()); 248 EXPECT_FALSE(service_->ShouldPushChanges());
229 EXPECT_FALSE(service_->GetBackendForTest()); 249 EXPECT_FALSE(service_->GetBackendForTest());
230 content::NotificationService::current()->Notify( 250 token_service->LoadTokensFromDB();
231 chrome::NOTIFICATION_TOKEN_LOADING_FINISHED,
232 content::Source<TokenService>(
233 TokenServiceFactory::GetForProfile(profile_.get())),
234 content::NotificationService::NoDetails());
235 service_->SetSetupInProgress(false); 251 service_->SetSetupInProgress(false);
236 // Sync should not start because there are still no tokens. 252 // Sync should not start because there are still no tokens.
237 EXPECT_FALSE(service_->ShouldPushChanges()); 253 EXPECT_FALSE(service_->ShouldPushChanges());
238 EXPECT_FALSE(service_->GetBackendForTest()); 254 EXPECT_FALSE(service_->GetBackendForTest());
239 } 255 }
240 256
241 TEST_F(ProfileSyncServiceStartupCrosTest, StartFirstTime) { 257 TEST_F(ProfileSyncServiceStartupCrosTest, StartFirstTime) {
242 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); 258 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
243 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); 259 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted);
244 EXPECT_CALL(*data_type_manager, Configure(_, _)); 260 EXPECT_CALL(*data_type_manager, Configure(_, _));
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 416
401 // Preload the tokens. 417 // Preload the tokens.
402 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( 418 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
403 GaiaConstants::kSyncService, "sync_token"); 419 GaiaConstants::kSyncService, "sync_token");
404 service_->fail_initial_download(); 420 service_->fail_initial_download();
405 421
406 service_->Initialize(); 422 service_->Initialize();
407 EXPECT_FALSE(service_->sync_initialized()); 423 EXPECT_FALSE(service_->sync_initialized());
408 EXPECT_FALSE(service_->GetBackendForTest()); 424 EXPECT_FALSE(service_->GetBackendForTest());
409 } 425 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698