| 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/sync/sync_setup_wizard.h" | 5 #include "chrome/browser/sync/sync_setup_wizard.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" | 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 12 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
| 13 #include "chrome/browser/prefs/testing_pref_store.h" | 13 #include "chrome/browser/prefs/testing_pref_store.h" |
| 14 #include "chrome/browser/signin/signin_manager_fake.h" | 14 #include "chrome/browser/signin/signin_manager_fake.h" |
| 15 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" | 15 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" |
| 16 #include "chrome/browser/sync/profile_sync_service.h" | 16 #include "chrome/browser/sync/profile_sync_service.h" |
| 17 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 17 #include "chrome/browser/sync/sync_setup_flow.h" | 18 #include "chrome/browser/sync/sync_setup_flow.h" |
| 18 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/browser_list.h" | 20 #include "chrome/browser/ui/browser_list.h" |
| 20 #include "chrome/browser/ui/webui/options/options_sync_setup_handler.h" | 21 #include "chrome/browser/ui/webui/options/options_sync_setup_handler.h" |
| 21 #include "chrome/common/net/gaia/google_service_auth_error.h" | 22 #include "chrome/common/net/gaia/google_service_auth_error.h" |
| 22 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 23 #include "chrome/test/base/browser_with_test_window_test.h" | 24 #include "chrome/test/base/browser_with_test_window_test.h" |
| 24 #include "chrome/test/base/test_browser_window.h" | 25 #include "chrome/test/base/test_browser_window.h" |
| 25 #include "chrome/test/base/testing_profile.h" | 26 #include "chrome/test/base/testing_profile.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 84 |
| 84 AuthError auth_error_; | 85 AuthError auth_error_; |
| 85 std::string username_; | 86 std::string username_; |
| 86 std::string password_; | 87 std::string password_; |
| 87 std::string captcha_; | 88 std::string captcha_; |
| 88 }; | 89 }; |
| 89 | 90 |
| 90 // A PSS subtype to inject. | 91 // A PSS subtype to inject. |
| 91 class ProfileSyncServiceForWizardTest : public ProfileSyncService { | 92 class ProfileSyncServiceForWizardTest : public ProfileSyncService { |
| 92 public: | 93 public: |
| 93 ProfileSyncServiceForWizardTest(Profile* profile, | 94 virtual ~ProfileSyncServiceForWizardTest() {} |
| 94 ProfileSyncService::StartBehavior behavior) | 95 |
| 95 : ProfileSyncService(NULL, profile, NULL, behavior), | 96 static ProfileKeyedService* BuildManual(Profile* profile) { |
| 96 user_cancelled_dialog_(false), | 97 return new ProfileSyncServiceForWizardTest(profile, |
| 97 is_using_secondary_passphrase_(false), | 98 ProfileSyncService::MANUAL_START); |
| 98 encrypt_everything_(false) { | |
| 99 signin_ = &mock_signin_; | |
| 100 ResetTestStats(); | |
| 101 } | 99 } |
| 102 | 100 |
| 103 virtual ~ProfileSyncServiceForWizardTest() {} | 101 static ProfileKeyedService* BuildAuto(Profile* profile) { |
| 102 return new ProfileSyncServiceForWizardTest(profile, |
| 103 ProfileSyncService::AUTO_START); |
| 104 } |
| 104 | 105 |
| 105 virtual void OnUserChoseDatatypes( | 106 virtual void OnUserChoseDatatypes( |
| 106 bool sync_everything, syncable::ModelTypeSet chosen_types) OVERRIDE { | 107 bool sync_everything, syncable::ModelTypeSet chosen_types) OVERRIDE { |
| 107 user_chose_data_types_ = true; | 108 user_chose_data_types_ = true; |
| 108 chosen_data_types_ = chosen_types; | 109 chosen_data_types_ = chosen_types; |
| 109 } | 110 } |
| 110 | 111 |
| 111 virtual void OnUserCancelledDialog() OVERRIDE { | 112 virtual void OnUserCancelledDialog() OVERRIDE { |
| 112 user_cancelled_dialog_ = true; | 113 user_cancelled_dialog_ = true; |
| 113 } | 114 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 SigninManagerMock mock_signin_; | 172 SigninManagerMock mock_signin_; |
| 172 bool user_cancelled_dialog_; | 173 bool user_cancelled_dialog_; |
| 173 bool user_chose_data_types_; | 174 bool user_chose_data_types_; |
| 174 bool keep_everything_synced_; | 175 bool keep_everything_synced_; |
| 175 bool is_using_secondary_passphrase_; | 176 bool is_using_secondary_passphrase_; |
| 176 bool encrypt_everything_; | 177 bool encrypt_everything_; |
| 177 syncable::ModelTypeSet chosen_data_types_; | 178 syncable::ModelTypeSet chosen_data_types_; |
| 178 std::string passphrase_; | 179 std::string passphrase_; |
| 179 | 180 |
| 180 private: | 181 private: |
| 182 ProfileSyncServiceForWizardTest(Profile* profile, |
| 183 ProfileSyncService::StartBehavior behavior) |
| 184 : ProfileSyncService(NULL, profile, NULL, behavior), |
| 185 user_cancelled_dialog_(false), |
| 186 is_using_secondary_passphrase_(false), |
| 187 encrypt_everything_(false) { |
| 188 signin_ = &mock_signin_; |
| 189 ResetTestStats(); |
| 190 } |
| 191 |
| 181 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceForWizardTest); | 192 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceForWizardTest); |
| 182 }; | 193 }; |
| 183 | 194 |
| 184 class TestingProfileWithSyncService : public TestingProfile { | |
| 185 public: | |
| 186 explicit TestingProfileWithSyncService( | |
| 187 ProfileSyncService::StartBehavior behavior) { | |
| 188 sync_service_.reset(new ProfileSyncServiceForWizardTest( | |
| 189 this, behavior)); | |
| 190 } | |
| 191 | |
| 192 virtual ProfileSyncService* GetProfileSyncService() { | |
| 193 return sync_service_.get(); | |
| 194 } | |
| 195 private: | |
| 196 ProfileSyncComponentsFactoryMock factory_; | |
| 197 scoped_ptr<ProfileSyncService> sync_service_; | |
| 198 }; | |
| 199 | |
| 200 // TODO(jhawkins): Subclass Browser (specifically, ShowOptionsTab) and inject it | 195 // TODO(jhawkins): Subclass Browser (specifically, ShowOptionsTab) and inject it |
| 201 // here to test the visibility of the Sync UI. | 196 // here to test the visibility of the Sync UI. |
| 202 class SyncSetupWizardTest : public BrowserWithTestWindowTest { | 197 class SyncSetupWizardTest : public BrowserWithTestWindowTest { |
| 203 public: | 198 public: |
| 204 SyncSetupWizardTest() | 199 SyncSetupWizardTest() |
| 205 : wizard_(NULL), | 200 : wizard_(NULL), |
| 206 service_(NULL), | 201 service_(NULL), |
| 207 flow_(NULL) {} | 202 flow_(NULL) {} |
| 208 virtual ~SyncSetupWizardTest() {} | 203 virtual ~SyncSetupWizardTest() {} |
| 209 virtual TestingProfile* BuildProfile() { | 204 virtual TestingProfile* BuildProfile() { |
| 210 return new TestingProfileWithSyncService( | 205 TestingProfile* profile = new TestingProfile(); |
| 211 ProfileSyncService::MANUAL_START); | 206 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory(profile, |
| 207 ProfileSyncServiceForWizardTest::BuildManual); |
| 208 return profile; |
| 212 } | 209 } |
| 213 virtual void SetUp() { | 210 virtual void SetUp() { |
| 214 set_profile(BuildProfile()); | 211 set_profile(BuildProfile()); |
| 215 profile()->CreateBookmarkModel(false); | 212 profile()->CreateBookmarkModel(false); |
| 216 // Wait for the bookmarks model to load. | 213 // Wait for the bookmarks model to load. |
| 217 profile()->BlockUntilBookmarkModelLoaded(); | 214 profile()->BlockUntilBookmarkModelLoaded(); |
| 218 PrefService* prefs = profile()->GetPrefs(); | 215 PrefService* prefs = profile()->GetPrefs(); |
| 219 prefs->SetString(prefs::kGoogleServicesUsername, kTestUser); | 216 prefs->SetString(prefs::kGoogleServicesUsername, kTestUser); |
| 220 | 217 |
| 221 set_browser(new Browser(Browser::TYPE_TABBED, profile())); | 218 set_browser(new Browser(Browser::TYPE_TABBED, profile())); |
| 222 browser()->SetWindowForTesting(window()); | 219 browser()->SetWindowForTesting(window()); |
| 223 BrowserList::SetLastActive(browser()); | 220 BrowserList::SetLastActive(browser()); |
| 224 service_ = static_cast<ProfileSyncServiceForWizardTest*>( | 221 service_ = static_cast<ProfileSyncServiceForWizardTest*>( |
| 225 profile()->GetProfileSyncService()); | 222 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile())); |
| 226 wizard_ = service_->GetWizard(); | 223 wizard_ = service_->GetWizard(); |
| 227 } | 224 } |
| 228 | 225 |
| 229 virtual void TearDown() { | 226 virtual void TearDown() { |
| 230 wizard_ = NULL; | 227 wizard_ = NULL; |
| 231 service_ = NULL; | 228 service_ = NULL; |
| 232 flow_ = NULL; | 229 flow_ = NULL; |
| 233 } | 230 } |
| 234 | 231 |
| 235 protected: | 232 protected: |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 AttachSyncSetupHandler(); | 604 AttachSyncSetupHandler(); |
| 608 EXPECT_EQ(SyncSetupWizard::GAIA_LOGIN, flow_->current_state_); | 605 EXPECT_EQ(SyncSetupWizard::GAIA_LOGIN, flow_->current_state_); |
| 609 EXPECT_EQ(SyncSetupWizard::DONE, flow_->end_state_); | 606 EXPECT_EQ(SyncSetupWizard::DONE, flow_->end_state_); |
| 610 CloseSetupUI(); | 607 CloseSetupUI(); |
| 611 EXPECT_FALSE(wizard_->IsVisible()); | 608 EXPECT_FALSE(wizard_->IsVisible()); |
| 612 } | 609 } |
| 613 | 610 |
| 614 class SyncSetupWizardCrosTest : public SyncSetupWizardTest { | 611 class SyncSetupWizardCrosTest : public SyncSetupWizardTest { |
| 615 public: | 612 public: |
| 616 virtual TestingProfile* BuildProfile() { | 613 virtual TestingProfile* BuildProfile() { |
| 617 TestingProfile* profile = | 614 TestingProfile* profile = new TestingProfile(); |
| 618 new TestingProfileWithSyncService(ProfileSyncService::AUTO_START); | 615 ProfileSyncServiceFactory* f = ProfileSyncServiceFactory::GetInstance(); |
| 619 profile->GetProfileSyncService()->signin()->SetAuthenticatedUsername( | 616 f->SetTestingFactory(profile, ProfileSyncServiceForWizardTest::BuildAuto); |
| 620 kTestUser); | 617 f->GetForProfile(profile)->signin()->SetAuthenticatedUsername(kTestUser); |
| 621 return profile; | 618 return profile; |
| 622 } | 619 } |
| 623 }; | 620 }; |
| 624 | 621 |
| 625 // Tests a scenario where sync is disabled on chrome os on startup due to | 622 // Tests a scenario where sync is disabled on chrome os on startup due to |
| 626 // an auth error (application specific password is needed). | 623 // an auth error (application specific password is needed). |
| 627 TEST_F(SyncSetupWizardCrosTest, CrosAuthSetup) { | 624 TEST_F(SyncSetupWizardCrosTest, CrosAuthSetup) { |
| 628 wizard_->Step(SyncSetupWizard::GAIA_LOGIN); | 625 wizard_->Step(SyncSetupWizard::GAIA_LOGIN); |
| 629 | 626 |
| 630 AttachSyncSetupHandler(); | 627 AttachSyncSetupHandler(); |
| 631 EXPECT_EQ(SyncSetupWizard::GAIA_SUCCESS, flow_->end_state_); | 628 EXPECT_EQ(SyncSetupWizard::GAIA_SUCCESS, flow_->end_state_); |
| 632 | 629 |
| 633 DictionaryValue dialog_args; | 630 DictionaryValue dialog_args; |
| 634 flow_->GetArgsForGaiaLogin(&dialog_args); | 631 flow_->GetArgsForGaiaLogin(&dialog_args); |
| 635 EXPECT_EQ(4U, dialog_args.size()); | 632 EXPECT_EQ(4U, dialog_args.size()); |
| 636 std::string actual_user; | 633 std::string actual_user; |
| 637 dialog_args.GetString("user", &actual_user); | 634 dialog_args.GetString("user", &actual_user); |
| 638 EXPECT_EQ(kTestUser, actual_user); | 635 EXPECT_EQ(kTestUser, actual_user); |
| 639 int error = -1; | 636 int error = -1; |
| 640 dialog_args.GetInteger("error", &error); | 637 dialog_args.GetInteger("error", &error); |
| 641 EXPECT_EQ(0, error); | 638 EXPECT_EQ(0, error); |
| 642 bool editable = true; | 639 bool editable = true; |
| 643 dialog_args.GetBoolean("editable_user", &editable); | 640 dialog_args.GetBoolean("editable_user", &editable); |
| 644 EXPECT_FALSE(editable); | 641 EXPECT_FALSE(editable); |
| 645 wizard_->Step(SyncSetupWizard::GAIA_SUCCESS); | 642 wizard_->Step(SyncSetupWizard::GAIA_SUCCESS); |
| 646 EXPECT_TRUE(service_->user_cancelled_dialog_); | 643 EXPECT_TRUE(service_->user_cancelled_dialog_); |
| 647 EXPECT_TRUE(service_->profile()->GetPrefs()->GetBoolean( | 644 EXPECT_TRUE(service_->profile()->GetPrefs()->GetBoolean( |
| 648 prefs::kSyncHasSetupCompleted)); | 645 prefs::kSyncHasSetupCompleted)); |
| 649 } | 646 } |
| OLD | NEW |