| OLD | NEW |
| 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 "chrome/browser/ui/webui/sync_setup_handler.h" | 5 #include "chrome/browser/ui/webui/sync_setup_handler.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 class SyncSetupHandlerTest : public testing::Test { | 276 class SyncSetupHandlerTest : public testing::Test { |
| 277 public: | 277 public: |
| 278 SyncSetupHandlerTest() : error_(GoogleServiceAuthError::NONE) {} | 278 SyncSetupHandlerTest() : error_(GoogleServiceAuthError::NONE) {} |
| 279 virtual void SetUp() OVERRIDE { | 279 virtual void SetUp() OVERRIDE { |
| 280 error_ = GoogleServiceAuthError::AuthErrorNone(); | 280 error_ = GoogleServiceAuthError::AuthErrorNone(); |
| 281 profile_.reset(ProfileSyncServiceMock::MakeSignedInTestingProfile()); | 281 profile_.reset(ProfileSyncServiceMock::MakeSignedInTestingProfile()); |
| 282 | 282 |
| 283 mock_signin_ = static_cast<SigninManagerBase*>( | 283 mock_signin_ = static_cast<SigninManagerBase*>( |
| 284 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 284 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| 285 profile_.get(), FakeSigninManagerBase::Build)); | 285 profile_.get(), FakeSigninManagerBase::Build)); |
| 286 | 286 profile_->GetPrefs()->SetString( |
| 287 prefs::kGoogleServicesUsername, GetTestUser()); |
| 288 mock_signin_->Initialize(profile_.get(), NULL); |
| 287 mock_pss_ = static_cast<ProfileSyncServiceMock*>( | 289 mock_pss_ = static_cast<ProfileSyncServiceMock*>( |
| 288 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 290 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 289 profile_.get(), | 291 profile_.get(), |
| 290 ProfileSyncServiceMock::BuildMockProfileSyncService)); | 292 ProfileSyncServiceMock::BuildMockProfileSyncService)); |
| 291 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error_)); | 293 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error_)); |
| 292 ON_CALL(*mock_pss_, GetPassphraseType()).WillByDefault( | 294 ON_CALL(*mock_pss_, GetPassphraseType()).WillByDefault( |
| 293 Return(syncer::IMPLICIT_PASSPHRASE)); | 295 Return(syncer::IMPLICIT_PASSPHRASE)); |
| 294 ON_CALL(*mock_pss_, GetPassphraseTime()).WillByDefault( | 296 ON_CALL(*mock_pss_, GetPassphraseTime()).WillByDefault( |
| 295 Return(base::Time())); | 297 Return(base::Time())); |
| 296 ON_CALL(*mock_pss_, GetExplicitPassphraseTime()).WillByDefault( | 298 ON_CALL(*mock_pss_, GetExplicitPassphraseTime()).WillByDefault( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 316 } | 318 } |
| 317 | 319 |
| 318 void SetupInitializedProfileSyncService() { | 320 void SetupInitializedProfileSyncService() { |
| 319 // An initialized ProfileSyncService will have already completed sync setup | 321 // An initialized ProfileSyncService will have already completed sync setup |
| 320 // and will have an initialized sync backend. | 322 // and will have an initialized sync backend. |
| 321 if (!mock_signin_->IsInitialized()) { | 323 if (!mock_signin_->IsInitialized()) { |
| 322 profile_->GetPrefs()->SetString( | 324 profile_->GetPrefs()->SetString( |
| 323 prefs::kGoogleServicesUsername, kTestUser); | 325 prefs::kGoogleServicesUsername, kTestUser); |
| 324 mock_signin_->Initialize(profile_.get(), NULL); | 326 mock_signin_->Initialize(profile_.get(), NULL); |
| 325 } | 327 } |
| 326 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()) | |
| 327 .WillRepeatedly(Return(true)); | |
| 328 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()) | |
| 329 .WillRepeatedly(Return(true)); | |
| 330 EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted()) | |
| 331 .WillRepeatedly(Return(true)); | |
| 332 EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(true)); | 328 EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(true)); |
| 333 } | 329 } |
| 334 | 330 |
| 335 void ExpectConfig() { | 331 void ExpectConfig() { |
| 336 ASSERT_EQ(1U, web_ui_.call_data().size()); | 332 ASSERT_EQ(1U, web_ui_.call_data().size()); |
| 337 const TestWebUI::CallData& data = web_ui_.call_data()[0]; | 333 const TestWebUI::CallData& data = web_ui_.call_data()[0]; |
| 338 EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data.function_name); | 334 EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data.function_name); |
| 339 std::string page; | 335 std::string page; |
| 340 ASSERT_TRUE(data.arg1->GetAsString(&page)); | 336 ASSERT_TRUE(data.arg1->GetAsString(&page)); |
| 341 EXPECT_EQ(page, "configure"); | 337 EXPECT_EQ(page, "configure"); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 367 } | 363 } |
| 368 | 364 |
| 369 // It's difficult to notify sync listeners when using a ProfileSyncServiceMock | 365 // It's difficult to notify sync listeners when using a ProfileSyncServiceMock |
| 370 // so this helper routine dispatches an OnStateChanged() notification to the | 366 // so this helper routine dispatches an OnStateChanged() notification to the |
| 371 // SyncStartupTracker. | 367 // SyncStartupTracker. |
| 372 void NotifySyncListeners() { | 368 void NotifySyncListeners() { |
| 373 if (handler_->sync_startup_tracker_) | 369 if (handler_->sync_startup_tracker_) |
| 374 handler_->sync_startup_tracker_->OnStateChanged(); | 370 handler_->sync_startup_tracker_->OnStateChanged(); |
| 375 } | 371 } |
| 376 | 372 |
| 373 virtual std::string GetTestUser() { |
| 374 return std::string(kTestUser); |
| 375 } |
| 376 |
| 377 content::TestBrowserThreadBundle thread_bundle_; | 377 content::TestBrowserThreadBundle thread_bundle_; |
| 378 scoped_ptr<Profile> profile_; | 378 scoped_ptr<Profile> profile_; |
| 379 ProfileSyncServiceMock* mock_pss_; | 379 ProfileSyncServiceMock* mock_pss_; |
| 380 GoogleServiceAuthError error_; | 380 GoogleServiceAuthError error_; |
| 381 SigninManagerBase* mock_signin_; | 381 SigninManagerBase* mock_signin_; |
| 382 TestWebUI web_ui_; | 382 TestWebUI web_ui_; |
| 383 scoped_ptr<TestingSyncSetupHandler> handler_; | 383 scoped_ptr<TestingSyncSetupHandler> handler_; |
| 384 }; | 384 }; |
| 385 | 385 |
| 386 class SyncSetupHandlerFirstSigninTest : public SyncSetupHandlerTest { |
| 387 virtual std::string GetTestUser() OVERRIDE { return std::string(); } |
| 388 }; |
| 389 |
| 386 TEST_F(SyncSetupHandlerTest, Basic) { | 390 TEST_F(SyncSetupHandlerTest, Basic) { |
| 387 } | 391 } |
| 388 | 392 |
| 389 #if !defined(OS_CHROMEOS) | 393 #if !defined(OS_CHROMEOS) |
| 390 TEST_F(SyncSetupHandlerTest, DisplayBasicLogin) { | 394 TEST_F(SyncSetupHandlerFirstSigninTest, DisplayBasicLogin) { |
| 391 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()) | 395 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()) |
| 392 .WillRepeatedly(Return(false)); | 396 .WillRepeatedly(Return(false)); |
| 393 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()) | 397 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()) |
| 394 .WillRepeatedly(Return(false)); | 398 .WillRepeatedly(Return(false)); |
| 395 EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted()) | 399 EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted()) |
| 396 .WillRepeatedly(Return(false)); | 400 .WillRepeatedly(Return(false)); |
| 397 handler_->HandleStartSignin(NULL); | 401 handler_->HandleStartSignin(NULL); |
| 398 | 402 |
| 399 // Sync setup hands off control to the gaia login tab. | 403 // Sync setup hands off control to the gaia login tab. |
| 400 EXPECT_EQ(NULL, | 404 EXPECT_EQ(NULL, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 421 // We expect a call to SyncSetupOverlay.showSyncSetupPage. | 425 // We expect a call to SyncSetupOverlay.showSyncSetupPage. |
| 422 ASSERT_EQ(1U, web_ui_.call_data().size()); | 426 ASSERT_EQ(1U, web_ui_.call_data().size()); |
| 423 const TestWebUI::CallData& data = web_ui_.call_data()[0]; | 427 const TestWebUI::CallData& data = web_ui_.call_data()[0]; |
| 424 EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data.function_name); | 428 EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data.function_name); |
| 425 | 429 |
| 426 ASSERT_FALSE(handler_->is_configuring_sync()); | 430 ASSERT_FALSE(handler_->is_configuring_sync()); |
| 427 EXPECT_EQ(NULL, | 431 EXPECT_EQ(NULL, |
| 428 LoginUIServiceFactory::GetForProfile( | 432 LoginUIServiceFactory::GetForProfile( |
| 429 profile_.get())->current_login_ui()); | 433 profile_.get())->current_login_ui()); |
| 430 } | 434 } |
| 431 #endif | 435 #endif // !defined(OS_CHROMEOS) |
| 432 | 436 |
| 433 // Verifies that the handler correctly handles a cancellation when | 437 // Verifies that the handler correctly handles a cancellation when |
| 434 // it is displaying the spinner to the user. | 438 // it is displaying the spinner to the user. |
| 435 TEST_F(SyncSetupHandlerTest, DisplayConfigureWithBackendDisabledAndCancel) { | 439 TEST_F(SyncSetupHandlerTest, DisplayConfigureWithBackendDisabledAndCancel) { |
| 436 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()) | 440 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()) |
| 437 .WillRepeatedly(Return(true)); | 441 .WillRepeatedly(Return(true)); |
| 438 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, kTestUser); | |
| 439 mock_signin_->Initialize(profile_.get(), NULL); | |
| 440 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()) | 442 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()) |
| 441 .WillRepeatedly(Return(true)); | 443 .WillRepeatedly(Return(true)); |
| 442 EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted()) | 444 EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted()) |
| 443 .WillRepeatedly(Return(false)); | 445 .WillRepeatedly(Return(false)); |
| 444 error_ = GoogleServiceAuthError::AuthErrorNone(); | 446 error_ = GoogleServiceAuthError::AuthErrorNone(); |
| 445 EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false)); | 447 EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false)); |
| 446 | 448 |
| 447 // We're simulating a user setting up sync, which would cause the backend to | 449 // We're simulating a user setting up sync, which would cause the backend to |
| 448 // kick off initialization, but not download user data types. The sync | 450 // kick off initialization, but not download user data types. The sync |
| 449 // backend will try to download control data types (e.g encryption info), but | 451 // backend will try to download control data types (e.g encryption info), but |
| 450 // that won't finish for this test as we're simulating cancelling while the | 452 // that won't finish for this test as we're simulating cancelling while the |
| 451 // spinner is showing. | 453 // spinner is showing. |
| 452 handler_->HandleShowSetupUI(NULL); | 454 handler_->HandleShowSetupUI(NULL); |
| 453 | 455 |
| 454 EXPECT_EQ(handler_.get(), | 456 EXPECT_EQ(handler_.get(), |
| 455 LoginUIServiceFactory::GetForProfile( | 457 LoginUIServiceFactory::GetForProfile( |
| 456 profile_.get())->current_login_ui()); | 458 profile_.get())->current_login_ui()); |
| 457 | 459 |
| 458 ExpectSpinnerAndClose(); | 460 ExpectSpinnerAndClose(); |
| 459 } | 461 } |
| 460 | 462 |
| 461 // Verifies that the handler correctly transitions from showing the spinner | 463 // Verifies that the handler correctly transitions from showing the spinner |
| 462 // to showing a configuration page when sync setup completes successfully. | 464 // to showing a configuration page when sync setup completes successfully. |
| 463 TEST_F(SyncSetupHandlerTest, | 465 TEST_F(SyncSetupHandlerTest, |
| 464 DisplayConfigureWithBackendDisabledAndSyncStartupCompleted) { | 466 DisplayConfigureWithBackendDisabledAndSyncStartupCompleted) { |
| 465 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()) | 467 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()) |
| 466 .WillRepeatedly(Return(true)); | 468 .WillRepeatedly(Return(true)); |
| 467 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, kTestUser); | |
| 468 mock_signin_->Initialize(profile_.get(), NULL); | |
| 469 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()) | 469 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()) |
| 470 .WillRepeatedly(Return(true)); | 470 .WillRepeatedly(Return(true)); |
| 471 EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted()) | 471 EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted()) |
| 472 .WillRepeatedly(Return(false)); | 472 .WillRepeatedly(Return(false)); |
| 473 error_ = GoogleServiceAuthError::AuthErrorNone(); | 473 error_ = GoogleServiceAuthError::AuthErrorNone(); |
| 474 // Sync backend is stopped initially, and will start up. | 474 // Sync backend is stopped initially, and will start up. |
| 475 EXPECT_CALL(*mock_pss_, sync_initialized()) | 475 EXPECT_CALL(*mock_pss_, sync_initialized()) |
| 476 .WillRepeatedly(Return(false)); | 476 .WillRepeatedly(Return(false)); |
| 477 SetDefaultExpectationsForConfigPage(); | 477 SetDefaultExpectationsForConfigPage(); |
| 478 | 478 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 | 513 |
| 514 // Verifies the case where the user cancels after the sync backend has | 514 // Verifies the case where the user cancels after the sync backend has |
| 515 // initialized (meaning it already transitioned from the spinner to a proper | 515 // initialized (meaning it already transitioned from the spinner to a proper |
| 516 // configuration page, tested by | 516 // configuration page, tested by |
| 517 // DisplayConfigureWithBackendDisabledAndSigninSuccess), but before the user | 517 // DisplayConfigureWithBackendDisabledAndSigninSuccess), but before the user |
| 518 // before the user has continued on. | 518 // before the user has continued on. |
| 519 TEST_F(SyncSetupHandlerTest, | 519 TEST_F(SyncSetupHandlerTest, |
| 520 DisplayConfigureWithBackendDisabledAndCancelAfterSigninSuccess) { | 520 DisplayConfigureWithBackendDisabledAndCancelAfterSigninSuccess) { |
| 521 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()) | 521 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()) |
| 522 .WillRepeatedly(Return(true)); | 522 .WillRepeatedly(Return(true)); |
| 523 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, kTestUser); | |
| 524 mock_signin_->Initialize(profile_.get(), NULL); | |
| 525 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()) | 523 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()) |
| 526 .WillRepeatedly(Return(true)); | 524 .WillRepeatedly(Return(true)); |
| 527 EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted()) | 525 EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted()) |
| 528 .WillRepeatedly(Return(false)); | 526 .WillRepeatedly(Return(false)); |
| 529 error_ = GoogleServiceAuthError::AuthErrorNone(); | 527 error_ = GoogleServiceAuthError::AuthErrorNone(); |
| 530 EXPECT_CALL(*mock_pss_, sync_initialized()) | 528 EXPECT_CALL(*mock_pss_, sync_initialized()) |
| 531 .WillOnce(Return(false)) | 529 .WillOnce(Return(false)) |
| 532 .WillRepeatedly(Return(true)); | 530 .WillRepeatedly(Return(true)); |
| 533 SetDefaultExpectationsForConfigPage(); | 531 SetDefaultExpectationsForConfigPage(); |
| 534 handler_->OpenSyncSetup(); | 532 handler_->OpenSyncSetup(); |
| 535 | 533 |
| 536 // It's important to tell sync the user cancelled the setup flow before we | 534 // It's important to tell sync the user cancelled the setup flow before we |
| 537 // tell it we're through with the setup progress. | 535 // tell it we're through with the setup progress. |
| 538 testing::InSequence seq; | 536 testing::InSequence seq; |
| 539 EXPECT_CALL(*mock_pss_, DisableForUser()); | 537 EXPECT_CALL(*mock_pss_, DisableForUser()); |
| 540 EXPECT_CALL(*mock_pss_, SetSetupInProgress(false)); | 538 EXPECT_CALL(*mock_pss_, SetSetupInProgress(false)); |
| 541 | 539 |
| 542 handler_->CloseSyncSetup(); | 540 handler_->CloseSyncSetup(); |
| 543 EXPECT_EQ(NULL, | 541 EXPECT_EQ(NULL, |
| 544 LoginUIServiceFactory::GetForProfile( | 542 LoginUIServiceFactory::GetForProfile( |
| 545 profile_.get())->current_login_ui()); | 543 profile_.get())->current_login_ui()); |
| 546 } | 544 } |
| 547 | 545 |
| 548 TEST_F(SyncSetupHandlerTest, | 546 TEST_F(SyncSetupHandlerTest, |
| 549 DisplayConfigureWithBackendDisabledAndSigninFailed) { | 547 DisplayConfigureWithBackendDisabledAndSigninFailed) { |
| 550 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()) | 548 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()) |
| 551 .WillRepeatedly(Return(true)); | 549 .WillRepeatedly(Return(true)); |
| 552 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, kTestUser); | |
| 553 mock_signin_->Initialize(profile_.get(), NULL); | |
| 554 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()) | 550 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()) |
| 555 .WillRepeatedly(Return(true)); | 551 .WillRepeatedly(Return(true)); |
| 556 EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted()) | 552 EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted()) |
| 557 .WillRepeatedly(Return(false)); | 553 .WillRepeatedly(Return(false)); |
| 558 error_ = GoogleServiceAuthError::AuthErrorNone(); | 554 error_ = GoogleServiceAuthError::AuthErrorNone(); |
| 559 EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false)); | 555 EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false)); |
| 560 | 556 |
| 561 handler_->OpenSyncSetup(); | 557 handler_->OpenSyncSetup(); |
| 562 const TestWebUI::CallData& data = web_ui_.call_data()[0]; | 558 const TestWebUI::CallData& data = web_ui_.call_data()[0]; |
| 563 EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data.function_name); | 559 EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data.function_name); |
| (...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1041 | 1037 |
| 1042 // This should display the sync setup dialog (not login). | 1038 // This should display the sync setup dialog (not login). |
| 1043 handler_->OpenSyncSetup(); | 1039 handler_->OpenSyncSetup(); |
| 1044 | 1040 |
| 1045 ExpectConfig(); | 1041 ExpectConfig(); |
| 1046 const TestWebUI::CallData& data = web_ui_.call_data()[0]; | 1042 const TestWebUI::CallData& data = web_ui_.call_data()[0]; |
| 1047 base::DictionaryValue* dictionary; | 1043 base::DictionaryValue* dictionary; |
| 1048 ASSERT_TRUE(data.arg2->GetAsDictionary(&dictionary)); | 1044 ASSERT_TRUE(data.arg2->GetAsDictionary(&dictionary)); |
| 1049 CheckBool(dictionary, "encryptAllData", true); | 1045 CheckBool(dictionary, "encryptAllData", true); |
| 1050 } | 1046 } |
| OLD | NEW |