| OLD | NEW |
| (Empty) | |
| 1 // Use of this source code is governed by a BSD-style license that can be |
| 2 // found in the LICENSE file. |
| 3 |
| 4 #ifdef CHROME_PERSONALIZATION |
| 5 |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 |
| 8 #include "base/json_writer.h" |
| 9 #include "chrome/browser/browser.h" |
| 10 #include "chrome/browser/browser_list.h" |
| 11 #include "chrome/browser/sync/personalization.h" |
| 12 #include "chrome/browser/sync/profile_sync_service.h" |
| 13 #include "chrome/browser/views/sync/sync_setup_flow.h" |
| 14 #include "chrome/browser/views/sync/sync_setup_wizard.h" |
| 15 #include "chrome/test/browser_with_test_window_test.h" |
| 16 #include "chrome/test/testing_profile.h" |
| 17 #include "chrome/test/test_browser_window.h" |
| 18 |
| 19 static const char* kTestUser = "chrome.p13n.test@gmail.com"; |
| 20 static const char* kTestPassword = "g00gl3g00gl3"; |
| 21 |
| 22 // A PSS subtype to inject. |
| 23 class ProfileSyncServiceForWizardTest : public ProfileSyncService { |
| 24 public: |
| 25 ProfileSyncServiceForWizardTest(Profile* profile) |
| 26 : ProfileSyncService(profile), user_accepted_merge_and_sync_(false), |
| 27 user_cancelled_dialog_(false) { |
| 28 } |
| 29 |
| 30 virtual void OnUserSubmittedAuth(const std::string& username, |
| 31 const std::string& password) { |
| 32 username_ = username; |
| 33 password_ = password; |
| 34 } |
| 35 virtual void OnUserAcceptedMergeAndSync() { |
| 36 user_accepted_merge_and_sync_ = true; |
| 37 } |
| 38 virtual void OnUserCancelledDialog() { |
| 39 user_cancelled_dialog_ = true; |
| 40 } |
| 41 |
| 42 virtual string16 GetAuthenticatedUsername() const { |
| 43 return UTF8ToWide(username_); |
| 44 } |
| 45 |
| 46 void set_auth_state(const std::string& last_email, AuthErrorState state) { |
| 47 last_attempted_user_email_ = last_email; |
| 48 last_auth_error_ = state; |
| 49 } |
| 50 |
| 51 void ResetTestStats() { |
| 52 username_.clear(); |
| 53 password_.clear(); |
| 54 user_accepted_merge_and_sync_ = false; |
| 55 user_cancelled_dialog_ = false; |
| 56 } |
| 57 ~ProfileSyncServiceForWizardTest() { } |
| 58 std::string username_; |
| 59 std::string password_; |
| 60 bool user_accepted_merge_and_sync_; |
| 61 bool user_cancelled_dialog_; |
| 62 }; |
| 63 |
| 64 class ProfilePersonalizationTestImpl : public ProfilePersonalization { |
| 65 public: |
| 66 ProfilePersonalizationTestImpl(Profile* p) |
| 67 : sync_service_(new ProfileSyncServiceForWizardTest(p)) { |
| 68 } |
| 69 virtual ProfileSyncService* sync_service() { return sync_service_.get(); } |
| 70 private: |
| 71 scoped_ptr<ProfileSyncService> sync_service_; |
| 72 }; |
| 73 |
| 74 class TestingProfileWithPersonalization : public TestingProfile { |
| 75 public: |
| 76 TestingProfileWithPersonalization() { |
| 77 personalization_.reset(new ProfilePersonalizationTestImpl(this)); |
| 78 } |
| 79 |
| 80 virtual ProfilePersonalization* GetProfilePersonalization() { |
| 81 return personalization_.get(); |
| 82 } |
| 83 private: |
| 84 scoped_ptr<ProfilePersonalization> personalization_; |
| 85 }; |
| 86 |
| 87 class TestBrowserWindowForWizardTest : public TestBrowserWindow { |
| 88 public: |
| 89 explicit TestBrowserWindowForWizardTest(Browser* browser) |
| 90 : TestBrowserWindow(browser), flow_(NULL), |
| 91 was_show_html_dialog_called_(false) { |
| 92 } |
| 93 |
| 94 // We intercept this call to hijack the flow created and then use it to |
| 95 // drive the wizard as if we were the HTML page. |
| 96 virtual void ShowHTMLDialog(HtmlDialogUIDelegate* delegate, |
| 97 gfx::NativeWindow parent_window) { |
| 98 flow_ = static_cast<SyncSetupFlow*>(delegate); |
| 99 was_show_html_dialog_called_ = true; |
| 100 } |
| 101 |
| 102 bool TestAndResetWasShowHTMLDialogCalled() { |
| 103 bool ret = was_show_html_dialog_called_; |
| 104 was_show_html_dialog_called_ = false; |
| 105 return ret; |
| 106 } |
| 107 |
| 108 SyncSetupFlow* flow_; |
| 109 private: |
| 110 bool was_show_html_dialog_called_; |
| 111 }; |
| 112 |
| 113 class SyncSetupWizardTest : public BrowserWithTestWindowTest { |
| 114 public: |
| 115 SyncSetupWizardTest() : test_window_(NULL), wizard_(NULL) { } |
| 116 virtual ~SyncSetupWizardTest() { } |
| 117 virtual void SetUp() { |
| 118 set_profile(new TestingProfileWithPersonalization()); |
| 119 profile()->CreateBookmarkModel(false); |
| 120 // Wait for the bookmarks model to load. |
| 121 profile()->BlockUntilBookmarkModelLoaded(); |
| 122 profile()->GetPrefs()->RegisterBooleanPref(prefs::kSyncHasSetupCompleted, |
| 123 false); |
| 124 set_browser(new Browser(Browser::TYPE_NORMAL, profile())); |
| 125 test_window_ = new TestBrowserWindowForWizardTest(browser()); |
| 126 set_window(test_window_); |
| 127 browser()->set_window(window()); |
| 128 BrowserList::SetLastActive(browser()); |
| 129 service_ = static_cast<ProfileSyncServiceForWizardTest*>( |
| 130 profile()->GetProfilePersonalization()->sync_service()); |
| 131 wizard_.reset(new SyncSetupWizard(service_)); |
| 132 } |
| 133 |
| 134 virtual void TearDown() { |
| 135 test_window_ = NULL; |
| 136 service_ = NULL; |
| 137 wizard_.reset(); |
| 138 } |
| 139 |
| 140 TestBrowserWindowForWizardTest* test_window_; |
| 141 scoped_ptr<SyncSetupWizard> wizard_; |
| 142 ProfileSyncServiceForWizardTest* service_; |
| 143 }; |
| 144 |
| 145 TEST_F(SyncSetupWizardTest, InitialStepLogin) { |
| 146 DictionaryValue dialog_args; |
| 147 SyncSetupFlow::GetArgsForGaiaLogin(service_, &dialog_args); |
| 148 std::string json_start_args; |
| 149 JSONWriter::Write(&dialog_args, false, &json_start_args); |
| 150 ListValue credentials; |
| 151 std::string auth = "{\"user\":\""; |
| 152 auth += std::string(kTestUser) + "\",\"pass\":\""; |
| 153 auth += std::string(kTestPassword) + "\"}"; |
| 154 credentials.Append(new StringValue(auth)); |
| 155 |
| 156 EXPECT_FALSE(wizard_->IsVisible()); |
| 157 EXPECT_FALSE(test_window_->flow_); |
| 158 wizard_->Step(SyncSetupWizard::GAIA_LOGIN); |
| 159 |
| 160 EXPECT_TRUE(wizard_->IsVisible()); |
| 161 EXPECT_TRUE(test_window_->TestAndResetWasShowHTMLDialogCalled()); |
| 162 EXPECT_EQ(SyncSetupWizard::GAIA_LOGIN, test_window_->flow_->current_state_); |
| 163 EXPECT_EQ(SyncSetupWizard::DONE, test_window_->flow_->end_state_); |
| 164 EXPECT_EQ(json_start_args, test_window_->flow_->dialog_start_args_); |
| 165 |
| 166 // Simulate the user submitting credentials. |
| 167 test_window_->flow_->flow_handler_->HandleSubmitAuth(&credentials); |
| 168 EXPECT_TRUE(wizard_->IsVisible()); |
| 169 EXPECT_EQ(SyncSetupWizard::GAIA_LOGIN, test_window_->flow_->current_state_); |
| 170 EXPECT_EQ(kTestUser, service_->username_); |
| 171 EXPECT_EQ(kTestPassword, service_->password_); |
| 172 EXPECT_FALSE(service_->user_accepted_merge_and_sync_); |
| 173 EXPECT_FALSE(service_->user_cancelled_dialog_); |
| 174 service_->ResetTestStats(); |
| 175 |
| 176 // Simulate failed credentials. |
| 177 service_->set_auth_state(kTestUser, AUTH_ERROR_INVALID_GAIA_CREDENTIALS); |
| 178 wizard_->Step(SyncSetupWizard::GAIA_LOGIN); |
| 179 EXPECT_TRUE(wizard_->IsVisible()); |
| 180 EXPECT_FALSE(test_window_->TestAndResetWasShowHTMLDialogCalled()); |
| 181 EXPECT_EQ(SyncSetupWizard::GAIA_LOGIN, test_window_->flow_->current_state_); |
| 182 dialog_args.Clear(); |
| 183 SyncSetupFlow::GetArgsForGaiaLogin(service_, &dialog_args); |
| 184 EXPECT_EQ(2, dialog_args.GetSize()); |
| 185 std::string actual_user; |
| 186 dialog_args.GetString(L"user", &actual_user); |
| 187 EXPECT_EQ(kTestUser, actual_user); |
| 188 int error = -1; |
| 189 dialog_args.GetInteger(L"error", &error); |
| 190 EXPECT_EQ(static_cast<int>(AUTH_ERROR_INVALID_GAIA_CREDENTIALS), error); |
| 191 service_->set_auth_state(kTestUser, AUTH_ERROR_NONE); |
| 192 |
| 193 // Simulate success. |
| 194 wizard_->Step(SyncSetupWizard::GAIA_SUCCESS); |
| 195 EXPECT_TRUE(wizard_->IsVisible()); |
| 196 EXPECT_FALSE(test_window_->TestAndResetWasShowHTMLDialogCalled()); |
| 197 EXPECT_EQ(SyncSetupWizard::GAIA_SUCCESS, test_window_->flow_->current_state_); |
| 198 |
| 199 wizard_->Step(SyncSetupWizard::DONE); // No merge and sync. |
| 200 EXPECT_TRUE(wizard_->IsVisible()); |
| 201 EXPECT_FALSE(test_window_->TestAndResetWasShowHTMLDialogCalled()); |
| 202 EXPECT_EQ(SyncSetupWizard::DONE, test_window_->flow_->current_state_); |
| 203 } |
| 204 |
| 205 TEST_F(SyncSetupWizardTest, InitialStepMergeAndSync) { |
| 206 wizard_->Step(SyncSetupWizard::GAIA_LOGIN); |
| 207 EXPECT_TRUE(wizard_->IsVisible()); |
| 208 EXPECT_TRUE(test_window_->TestAndResetWasShowHTMLDialogCalled()); |
| 209 EXPECT_EQ(SyncSetupWizard::DONE, test_window_->flow_->end_state_); |
| 210 |
| 211 wizard_->Step(SyncSetupWizard::GAIA_SUCCESS); |
| 212 wizard_->Step(SyncSetupWizard::MERGE_AND_SYNC); |
| 213 EXPECT_TRUE(wizard_->IsVisible()); |
| 214 EXPECT_FALSE(test_window_->TestAndResetWasShowHTMLDialogCalled()); |
| 215 EXPECT_EQ(SyncSetupWizard::MERGE_AND_SYNC, |
| 216 test_window_->flow_->current_state_); |
| 217 |
| 218 test_window_->flow_->flow_handler_->HandleSubmitMergeAndSync(NULL); |
| 219 EXPECT_TRUE(wizard_->IsVisible()); |
| 220 EXPECT_EQ(SyncSetupWizard::MERGE_AND_SYNC, |
| 221 test_window_->flow_->current_state_); |
| 222 EXPECT_EQ(std::string(), service_->username_); |
| 223 EXPECT_EQ(std::string(), service_->password_); |
| 224 EXPECT_TRUE(service_->user_accepted_merge_and_sync_); |
| 225 EXPECT_FALSE(service_->user_cancelled_dialog_); |
| 226 service_->ResetTestStats(); |
| 227 wizard_->Step(SyncSetupWizard::DONE); // No merge and sync. |
| 228 EXPECT_TRUE(wizard_->IsVisible()); |
| 229 EXPECT_FALSE(test_window_->TestAndResetWasShowHTMLDialogCalled()); |
| 230 EXPECT_EQ(SyncSetupWizard::DONE, test_window_->flow_->current_state_); |
| 231 } |
| 232 |
| 233 TEST_F(SyncSetupWizardTest, DialogCancelled) { |
| 234 wizard_->Step(SyncSetupWizard::GAIA_LOGIN); |
| 235 test_window_->flow_->OnDialogClosed(""); |
| 236 EXPECT_FALSE(wizard_->IsVisible()); |
| 237 EXPECT_TRUE(service_->user_cancelled_dialog_); |
| 238 EXPECT_EQ(std::string(), service_->username_); |
| 239 EXPECT_EQ(std::string(), service_->password_); |
| 240 EXPECT_FALSE(service_->user_accepted_merge_and_sync_); |
| 241 |
| 242 wizard_->Step(SyncSetupWizard::GAIA_LOGIN); |
| 243 EXPECT_TRUE(wizard_->IsVisible()); |
| 244 EXPECT_TRUE(test_window_->TestAndResetWasShowHTMLDialogCalled()); |
| 245 wizard_->Step(SyncSetupWizard::GAIA_LOGIN); |
| 246 EXPECT_FALSE(test_window_->TestAndResetWasShowHTMLDialogCalled()); |
| 247 |
| 248 wizard_->Step(SyncSetupWizard::MERGE_AND_SYNC); |
| 249 test_window_->flow_->OnDialogClosed(""); |
| 250 EXPECT_FALSE(wizard_->IsVisible()); |
| 251 EXPECT_TRUE(service_->user_cancelled_dialog_); |
| 252 EXPECT_EQ(std::string(), service_->username_); |
| 253 EXPECT_EQ(std::string(), service_->password_); |
| 254 EXPECT_FALSE(service_->user_accepted_merge_and_sync_); |
| 255 } |
| 256 |
| 257 TEST_F(SyncSetupWizardTest, InvalidTransitions) { |
| 258 wizard_->Step(SyncSetupWizard::GAIA_SUCCESS); |
| 259 EXPECT_FALSE(wizard_->IsVisible()); |
| 260 EXPECT_FALSE(test_window_->TestAndResetWasShowHTMLDialogCalled()); |
| 261 |
| 262 wizard_->Step(SyncSetupWizard::DONE); |
| 263 EXPECT_FALSE(wizard_->IsVisible()); |
| 264 EXPECT_FALSE(test_window_->TestAndResetWasShowHTMLDialogCalled()); |
| 265 |
| 266 wizard_->Step(SyncSetupWizard::GAIA_LOGIN); |
| 267 wizard_->Step(SyncSetupWizard::MERGE_AND_SYNC); |
| 268 EXPECT_EQ(SyncSetupWizard::GAIA_LOGIN, test_window_->flow_->current_state_); |
| 269 |
| 270 wizard_->Step(SyncSetupWizard::DONE); |
| 271 EXPECT_EQ(SyncSetupWizard::GAIA_LOGIN, test_window_->flow_->current_state_); |
| 272 |
| 273 wizard_->Step(SyncSetupWizard::GAIA_SUCCESS); |
| 274 wizard_->Step(SyncSetupWizard::MERGE_AND_SYNC); |
| 275 EXPECT_EQ(SyncSetupWizard::MERGE_AND_SYNC, |
| 276 test_window_->flow_->current_state_); |
| 277 |
| 278 wizard_->Step(SyncSetupWizard::GAIA_SUCCESS); |
| 279 EXPECT_EQ(SyncSetupWizard::MERGE_AND_SYNC, |
| 280 test_window_->flow_->current_state_); |
| 281 } |
| 282 |
| 283 TEST_F(SyncSetupWizardTest, FullSuccessfulRunSetsPref) { |
| 284 wizard_->Step(SyncSetupWizard::GAIA_LOGIN); |
| 285 wizard_->Step(SyncSetupWizard::GAIA_SUCCESS); |
| 286 wizard_->Step(SyncSetupWizard::MERGE_AND_SYNC); |
| 287 wizard_->Step(SyncSetupWizard::DONE); |
| 288 test_window_->flow_->OnDialogClosed(""); |
| 289 EXPECT_FALSE(wizard_->IsVisible()); |
| 290 EXPECT_TRUE(service_->profile()->GetPrefs()->GetBoolean( |
| 291 prefs::kSyncHasSetupCompleted)); |
| 292 } |
| 293 |
| 294 TEST_F(SyncSetupWizardTest, DiscreteRun) { |
| 295 DictionaryValue dialog_args; |
| 296 // For a discrete run, we need to have ran through setup once. |
| 297 wizard_->Step(SyncSetupWizard::GAIA_LOGIN); |
| 298 wizard_->Step(SyncSetupWizard::GAIA_SUCCESS); |
| 299 wizard_->Step(SyncSetupWizard::MERGE_AND_SYNC); |
| 300 wizard_->Step(SyncSetupWizard::DONE); |
| 301 test_window_->flow_->OnDialogClosed(""); |
| 302 EXPECT_TRUE(test_window_->TestAndResetWasShowHTMLDialogCalled()); |
| 303 |
| 304 wizard_->Step(SyncSetupWizard::GAIA_LOGIN); |
| 305 EXPECT_EQ(SyncSetupWizard::GAIA_SUCCESS, test_window_->flow_->end_state_); |
| 306 |
| 307 service_->set_auth_state(kTestUser, AUTH_ERROR_INVALID_GAIA_CREDENTIALS); |
| 308 wizard_->Step(SyncSetupWizard::GAIA_LOGIN); |
| 309 EXPECT_TRUE(wizard_->IsVisible()); |
| 310 SyncSetupFlow::GetArgsForGaiaLogin(service_, &dialog_args); |
| 311 EXPECT_EQ(2, dialog_args.GetSize()); |
| 312 std::string actual_user; |
| 313 dialog_args.GetString(L"user", &actual_user); |
| 314 EXPECT_EQ(kTestUser, actual_user); |
| 315 int error = -1; |
| 316 dialog_args.GetInteger(L"error", &error); |
| 317 EXPECT_EQ(static_cast<int>(AUTH_ERROR_INVALID_GAIA_CREDENTIALS), error); |
| 318 service_->set_auth_state(kTestUser, AUTH_ERROR_NONE); |
| 319 |
| 320 wizard_->Step(SyncSetupWizard::GAIA_SUCCESS); |
| 321 EXPECT_TRUE(test_window_->TestAndResetWasShowHTMLDialogCalled()); |
| 322 } |
| 323 |
| 324 #endif // CHROME_PERSONALIZATION |
| OLD | NEW |