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