MD Downloads: UI review feedback
[chromium-blink-merge.git] / chrome / browser / signin / signin_manager_unittest.cc
blob745e1bad7ed1c2583327c29094f3b60f8357f741
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
3 // found in the LICENSE file.
5 #include "components/signin/core/browser/signin_manager.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/prefs/pref_registry_simple.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/prefs/scoped_user_pref_update.h"
16 #include "base/prefs/testing_pref_service.h"
17 #include "base/run_loop.h"
18 #include "base/strings/stringprintf.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/chrome_notification_types.h"
21 #include "chrome/browser/prefs/browser_prefs.h"
22 #include "chrome/browser/signin/account_fetcher_service_factory.h"
23 #include "chrome/browser/signin/account_tracker_service_factory.h"
24 #include "chrome/browser/signin/chrome_signin_client_factory.h"
25 #include "chrome/browser/signin/fake_account_fetcher_service_builder.h"
26 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
27 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
28 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
29 #include "chrome/browser/signin/signin_manager_factory.h"
30 #include "chrome/browser/signin/test_signin_client_builder.h"
31 #include "chrome/common/pref_names.h"
32 #include "chrome/common/url_constants.h"
33 #include "chrome/test/base/testing_browser_process.h"
34 #include "chrome/test/base/testing_profile.h"
35 #include "components/signin/core/browser/account_tracker_service.h"
36 #include "components/signin/core/browser/fake_account_fetcher_service.h"
37 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
38 #include "components/signin/core/browser/profile_oauth2_token_service.h"
39 #include "components/signin/core/browser/test_signin_client.h"
40 #include "content/public/browser/child_process_security_policy.h"
41 #include "content/public/browser/notification_source.h"
42 #include "content/public/test/test_browser_thread_bundle.h"
43 #include "google_apis/gaia/gaia_constants.h"
44 #include "google_apis/gaia/gaia_urls.h"
45 #include "net/cookies/cookie_monster.h"
46 #include "net/url_request/test_url_fetcher_factory.h"
47 #include "net/url_request/url_request.h"
48 #include "net/url_request/url_request_context_getter.h"
49 #include "net/url_request/url_request_status.h"
51 #include "testing/gmock/include/gmock/gmock.h"
52 #include "testing/gtest/include/gtest/gtest.h"
54 namespace {
56 scoped_ptr<KeyedService> SigninManagerBuild(content::BrowserContext* context) {
57 Profile* profile = static_cast<Profile*>(context);
58 scoped_ptr<SigninManager> service(new SigninManager(
59 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile),
60 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
61 AccountTrackerServiceFactory::GetForProfile(profile),
62 GaiaCookieManagerServiceFactory::GetForProfile(profile)));
63 service->Initialize(NULL);
64 return service.Pass();
67 class TestSigninManagerObserver : public SigninManagerBase::Observer {
68 public:
69 TestSigninManagerObserver() : num_failed_signins_(0),
70 num_successful_signins_(0),
71 num_signouts_(0) {
74 ~TestSigninManagerObserver() override {}
76 int num_failed_signins_;
77 int num_successful_signins_;
78 int num_signouts_;
80 private:
81 // SigninManagerBase::Observer:
82 void GoogleSigninFailed(const GoogleServiceAuthError& error) override {
83 num_failed_signins_++;
86 void GoogleSigninSucceeded(const std::string& account_id,
87 const std::string& username,
88 const std::string& password) override {
89 num_successful_signins_++;
92 void GoogleSignedOut(const std::string& account_id,
93 const std::string& username) override {
94 num_signouts_++;
98 } // namespace
101 class SigninManagerTest : public testing::Test {
102 public:
103 SigninManagerTest() : manager_(NULL) {}
104 ~SigninManagerTest() override {}
106 void SetUp() override {
107 manager_ = NULL;
108 prefs_.reset(new TestingPrefServiceSimple);
109 chrome::RegisterLocalState(prefs_->registry());
110 TestingBrowserProcess::GetGlobal()->SetLocalState(
111 prefs_.get());
112 TestingProfile::Builder builder;
113 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
114 BuildFakeProfileOAuth2TokenService);
115 builder.AddTestingFactory(ChromeSigninClientFactory::GetInstance(),
116 signin::BuildTestSigninClient);
117 builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
118 SigninManagerBuild);
119 builder.AddTestingFactory(AccountFetcherServiceFactory::GetInstance(),
120 FakeAccountFetcherServiceBuilder::BuildForTests);
121 profile_ = builder.Build();
123 TestSigninClient* client = static_cast<TestSigninClient*>(
124 ChromeSigninClientFactory::GetForProfile(profile()));
125 client->SetURLRequestContext(profile_->GetRequestContext());
128 void TearDown() override {
129 if (manager_)
130 manager_->RemoveObserver(&test_observer_);
132 // Destroy the SigninManager here, because it relies on profile() which is
133 // freed in the base class.
134 if (naked_manager_) {
135 naked_manager_->Shutdown();
136 naked_manager_.reset(NULL);
138 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL);
140 // Manually destroy PrefService and Profile so that they are shutdown
141 // in the correct order. Both need to be destroyed before the
142 // |thread_bundle_| member.
143 profile_.reset();
144 prefs_.reset(); // LocalState needs to outlive the profile.
147 TestingProfile* profile() { return profile_.get(); }
149 TestSigninClient* signin_client() {
150 return static_cast<TestSigninClient*>(
151 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile()));
154 // Seed the account tracker with information from logged in user. Normally
155 // this is done by UI code before calling SigninManager. Returns the string
156 // to use as the account_id.
157 std::string AddToAccountTracker(const std::string& gaia_id,
158 const std::string& email) {
159 AccountTrackerService* service =
160 AccountTrackerServiceFactory::GetForProfile(profile());
161 service->SeedAccountInfo(gaia_id, email);
162 return service->PickAccountIdForAccount(gaia_id, email);
165 // Sets up the signin manager as a service if other code will try to get it as
166 // a PKS.
167 void SetUpSigninManagerAsService() {
168 DCHECK(!manager_);
169 DCHECK(!naked_manager_);
170 manager_ = static_cast<SigninManager*>(
171 SigninManagerFactory::GetForProfile(profile()));
172 manager_->AddObserver(&test_observer_);
175 // Create a naked signin manager if integration with PKSs is not needed.
176 void CreateNakedSigninManager() {
177 DCHECK(!manager_);
178 naked_manager_.reset(new SigninManager(
179 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile()),
180 ProfileOAuth2TokenServiceFactory::GetForProfile(profile()),
181 AccountTrackerServiceFactory::GetForProfile(profile()),
182 GaiaCookieManagerServiceFactory::GetForProfile(profile())));
184 manager_ = naked_manager_.get();
185 manager_->AddObserver(&test_observer_);
188 // Shuts down |manager_|.
189 void ShutDownManager() {
190 DCHECK(manager_);
191 manager_->RemoveObserver(&test_observer_);
192 manager_->Shutdown();
193 if (naked_manager_)
194 naked_manager_.reset(NULL);
195 manager_ = NULL;
198 void ExpectSignInWithRefreshTokenSuccess() {
199 EXPECT_TRUE(manager_->IsAuthenticated());
200 EXPECT_FALSE(manager_->GetAuthenticatedAccountId().empty());
201 EXPECT_FALSE(manager_->GetAuthenticatedAccountInfo().email.empty());
203 ProfileOAuth2TokenService* token_service =
204 ProfileOAuth2TokenServiceFactory::GetForProfile(profile());
205 EXPECT_TRUE(token_service->RefreshTokenIsAvailable(
206 manager_->GetAuthenticatedAccountId()));
208 // Should go into token service and stop.
209 EXPECT_EQ(1, test_observer_.num_successful_signins_);
210 EXPECT_EQ(0, test_observer_.num_failed_signins_);
213 void CompleteSigninCallback(const std::string& oauth_token) {
214 oauth_tokens_fetched_.push_back(oauth_token);
215 manager_->CompletePendingSignin();
218 content::TestBrowserThreadBundle thread_bundle_;
219 net::TestURLFetcherFactory factory_;
220 scoped_ptr<SigninManager> naked_manager_;
221 SigninManager* manager_;
222 TestSigninManagerObserver test_observer_;
223 scoped_ptr<TestingProfile> profile_;
224 std::vector<std::string> oauth_tokens_fetched_;
225 scoped_ptr<TestingPrefServiceSimple> prefs_;
226 std::vector<std::string> cookies_;
229 TEST_F(SigninManagerTest, SignInWithRefreshToken) {
230 SetUpSigninManagerAsService();
231 EXPECT_FALSE(manager_->IsAuthenticated());
233 std::string account_id = AddToAccountTracker("gaia_id", "user@gmail.com");
234 manager_->StartSignInWithRefreshToken(
235 "rt",
236 "gaia_id",
237 "user@gmail.com",
238 "password",
239 SigninManager::OAuthTokenFetchedCallback());
241 ExpectSignInWithRefreshTokenSuccess();
243 // Should persist across resets.
244 ShutDownManager();
245 CreateNakedSigninManager();
246 manager_->Initialize(NULL);
247 EXPECT_EQ(account_id, manager_->GetAuthenticatedAccountId());
250 TEST_F(SigninManagerTest, SignInWithRefreshTokenCallbackComplete) {
251 SetUpSigninManagerAsService();
252 EXPECT_FALSE(manager_->IsAuthenticated());
254 // Since the password is empty, must verify the gaia cookies first.
255 SigninManager::OAuthTokenFetchedCallback callback =
256 base::Bind(&SigninManagerTest::CompleteSigninCallback,
257 base::Unretained(this));
258 manager_->StartSignInWithRefreshToken(
259 "rt",
260 "gaia_id",
261 "user@gmail.com",
262 "password",
263 callback);
265 ExpectSignInWithRefreshTokenSuccess();
266 ASSERT_EQ(1U, oauth_tokens_fetched_.size());
267 EXPECT_EQ(oauth_tokens_fetched_[0], "rt");
270 TEST_F(SigninManagerTest, SignInWithRefreshTokenCallsPostSignout) {
271 SetUpSigninManagerAsService();
272 EXPECT_FALSE(manager_->IsAuthenticated());
274 std::string gaia_id = "12345";
275 std::string email = "user@google.com";
277 AccountTrackerService* account_tracker_service =
278 AccountTrackerServiceFactory::GetForProfile(profile());
279 account_tracker_service->SeedAccountInfo(gaia_id, email);
280 FakeAccountFetcherService* account_fetcher_service =
281 static_cast<FakeAccountFetcherService*>(
282 AccountFetcherServiceFactory::GetForProfile(profile()));
283 account_fetcher_service->EnableNetworkFetches();
285 ASSERT_TRUE(signin_client()->get_signed_in_password().empty());
287 manager_->StartSignInWithRefreshToken(
288 "rt1",
289 gaia_id,
290 email,
291 "password",
292 SigninManager::OAuthTokenFetchedCallback());
294 // PostSignedIn is not called until the AccountTrackerService returns.
295 ASSERT_EQ("", signin_client()->get_signed_in_password());
297 account_fetcher_service->FakeUserInfoFetchSuccess(
298 account_tracker_service->PickAccountIdForAccount(gaia_id, email), email,
299 gaia_id, "google.com", "full_name", "given_name", "locale",
300 "http://www.google.com");
302 // AccountTracker and SigninManager are both done and PostSignedIn was called.
303 ASSERT_EQ("password", signin_client()->get_signed_in_password());
305 ExpectSignInWithRefreshTokenSuccess();
308 TEST_F(SigninManagerTest, SignOut) {
309 SetUpSigninManagerAsService();
310 manager_->StartSignInWithRefreshToken(
311 "rt",
312 "gaia_id",
313 "user@gmail.com",
314 "password",
315 SigninManager::OAuthTokenFetchedCallback());
316 manager_->SignOut(signin_metrics::SIGNOUT_TEST);
317 EXPECT_FALSE(manager_->IsAuthenticated());
318 EXPECT_TRUE(manager_->GetAuthenticatedAccountInfo().email.empty());
319 EXPECT_TRUE(manager_->GetAuthenticatedAccountId().empty());
320 // Should not be persisted anymore
321 ShutDownManager();
322 CreateNakedSigninManager();
323 manager_->Initialize(NULL);
324 EXPECT_FALSE(manager_->IsAuthenticated());
325 EXPECT_TRUE(manager_->GetAuthenticatedAccountInfo().email.empty());
326 EXPECT_TRUE(manager_->GetAuthenticatedAccountId().empty());
329 TEST_F(SigninManagerTest, SignOutWhileProhibited) {
330 SetUpSigninManagerAsService();
331 EXPECT_FALSE(manager_->IsAuthenticated());
332 EXPECT_TRUE(manager_->GetAuthenticatedAccountInfo().email.empty());
333 EXPECT_TRUE(manager_->GetAuthenticatedAccountId().empty());
335 manager_->SetAuthenticatedAccountInfo("gaia_id", "user@gmail.com");
336 manager_->ProhibitSignout(true);
337 manager_->SignOut(signin_metrics::SIGNOUT_TEST);
338 EXPECT_TRUE(manager_->IsAuthenticated());
339 manager_->ProhibitSignout(false);
340 manager_->SignOut(signin_metrics::SIGNOUT_TEST);
341 EXPECT_FALSE(manager_->IsAuthenticated());
344 TEST_F(SigninManagerTest, Prohibited) {
345 g_browser_process->local_state()->SetString(
346 prefs::kGoogleServicesUsernamePattern, ".*@google.com");
347 CreateNakedSigninManager();
348 manager_->Initialize(g_browser_process->local_state());
349 EXPECT_TRUE(manager_->IsAllowedUsername("test@google.com"));
350 EXPECT_TRUE(manager_->IsAllowedUsername("happy@google.com"));
351 EXPECT_FALSE(manager_->IsAllowedUsername("test@invalid.com"));
352 EXPECT_FALSE(manager_->IsAllowedUsername("test@notgoogle.com"));
353 EXPECT_FALSE(manager_->IsAllowedUsername(std::string()));
356 TEST_F(SigninManagerTest, TestAlternateWildcard) {
357 // Test to make sure we accept "*@google.com" as a pattern (treat it as if
358 // the admin entered ".*@google.com").
359 g_browser_process->local_state()->SetString(
360 prefs::kGoogleServicesUsernamePattern, "*@google.com");
361 CreateNakedSigninManager();
362 manager_->Initialize(g_browser_process->local_state());
363 EXPECT_TRUE(manager_->IsAllowedUsername("test@google.com"));
364 EXPECT_TRUE(manager_->IsAllowedUsername("happy@google.com"));
365 EXPECT_FALSE(manager_->IsAllowedUsername("test@invalid.com"));
366 EXPECT_FALSE(manager_->IsAllowedUsername("test@notgoogle.com"));
367 EXPECT_FALSE(manager_->IsAllowedUsername(std::string()));
370 TEST_F(SigninManagerTest, ProhibitedAtStartup) {
371 std::string account_id = AddToAccountTracker("gaia_id", "user@gmail.com");
372 profile()->GetPrefs()->SetString(prefs::kGoogleServicesAccountId, account_id);
373 g_browser_process->local_state()->SetString(
374 prefs::kGoogleServicesUsernamePattern, ".*@google.com");
375 CreateNakedSigninManager();
376 manager_->Initialize(g_browser_process->local_state());
377 // Currently signed in user is prohibited by policy, so should be signed out.
378 EXPECT_EQ("", manager_->GetAuthenticatedAccountInfo().email);
379 EXPECT_EQ("", manager_->GetAuthenticatedAccountId());
382 TEST_F(SigninManagerTest, ProhibitedAfterStartup) {
383 std::string account_id = AddToAccountTracker("gaia_id", "user@gmail.com");
384 profile()->GetPrefs()->SetString(prefs::kGoogleServicesAccountId, account_id);
385 CreateNakedSigninManager();
386 manager_->Initialize(g_browser_process->local_state());
387 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedAccountInfo().email);
388 EXPECT_EQ(account_id, manager_->GetAuthenticatedAccountId());
389 // Update the profile - user should be signed out.
390 g_browser_process->local_state()->SetString(
391 prefs::kGoogleServicesUsernamePattern, ".*@google.com");
392 EXPECT_EQ("", manager_->GetAuthenticatedAccountInfo().email);
393 EXPECT_EQ("", manager_->GetAuthenticatedAccountId());
396 TEST_F(SigninManagerTest, ExternalSignIn) {
397 CreateNakedSigninManager();
398 manager_->Initialize(g_browser_process->local_state());
399 EXPECT_EQ("", manager_->GetAuthenticatedAccountInfo().email);
400 EXPECT_EQ("", manager_->GetAuthenticatedAccountId());
401 EXPECT_EQ(0, test_observer_.num_successful_signins_);
403 std::string account_id = AddToAccountTracker("gaia_id", "user@gmail.com");
404 manager_->OnExternalSigninCompleted("user@gmail.com");
405 EXPECT_EQ(1, test_observer_.num_successful_signins_);
406 EXPECT_EQ(0, test_observer_.num_failed_signins_);
407 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedAccountInfo().email);
408 EXPECT_EQ(account_id, manager_->GetAuthenticatedAccountId());
411 TEST_F(SigninManagerTest, SigninNotAllowed) {
412 std::string user("user@google.com");
413 profile()->GetPrefs()->SetString(prefs::kGoogleServicesAccountId, user);
414 profile()->GetPrefs()->SetBoolean(prefs::kSigninAllowed, false);
415 CreateNakedSigninManager();
416 AddToAccountTracker("gaia_id", user);
417 manager_->Initialize(g_browser_process->local_state());
418 // Currently signing in is prohibited by policy, so should be signed out.
419 EXPECT_EQ("", manager_->GetAuthenticatedAccountInfo().email);
420 EXPECT_EQ("", manager_->GetAuthenticatedAccountId());
423 TEST_F(SigninManagerTest, UpgradeToNewPrefs) {
424 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
425 "user@gmail.com");
426 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUserAccountId,
427 "account_id");
428 CreateNakedSigninManager();
429 manager_->Initialize(g_browser_process->local_state());
430 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedAccountInfo().email);
432 AccountTrackerService* service =
433 AccountTrackerServiceFactory::GetForProfile(profile());
434 if (service->GetMigrationState() ==
435 AccountTrackerService::MIGRATION_NOT_STARTED) {
436 // TODO(rogerta): until the migration to gaia id, the account id will remain
437 // the old username.
438 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedAccountId());
439 EXPECT_EQ("user@gmail.com",
440 profile()->GetPrefs()->GetString(prefs::kGoogleServicesAccountId));
441 } else {
442 EXPECT_EQ("account_id", manager_->GetAuthenticatedAccountId());
443 EXPECT_EQ("account_id", profile()->GetPrefs()->GetString(
444 prefs::kGoogleServicesAccountId));
446 EXPECT_EQ("",
447 profile()->GetPrefs()->GetString(prefs::kGoogleServicesUsername));
449 // Make sure account tracker was updated.
450 AccountInfo info =
451 service->GetAccountInfo(manager_->GetAuthenticatedAccountId());
452 EXPECT_EQ("user@gmail.com", info.email);
453 EXPECT_EQ("account_id", info.gaia);
456 TEST_F(SigninManagerTest, CanonicalizesPrefs) {
457 AccountTrackerService* service =
458 AccountTrackerServiceFactory::GetForProfile(profile());
460 // This unit test is not needed after migrating to gaia id.
461 if (service->GetMigrationState() ==
462 AccountTrackerService::MIGRATION_NOT_STARTED) {
463 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
464 "user.C@gmail.com");
466 CreateNakedSigninManager();
467 manager_->Initialize(g_browser_process->local_state());
468 EXPECT_EQ("user.C@gmail.com",
469 manager_->GetAuthenticatedAccountInfo().email);
471 // TODO(rogerta): until the migration to gaia id, the account id will remain
472 // the old username.
473 EXPECT_EQ("userc@gmail.com", manager_->GetAuthenticatedAccountId());
474 EXPECT_EQ("userc@gmail.com", profile()->GetPrefs()->GetString(
475 prefs::kGoogleServicesAccountId));
476 EXPECT_EQ("",
477 profile()->GetPrefs()->GetString(prefs::kGoogleServicesUsername));
479 // Make sure account tracker has a canonicalized username.
480 AccountInfo info =
481 service->GetAccountInfo(manager_->GetAuthenticatedAccountId());
482 EXPECT_EQ("user.C@gmail.com", info.email);
483 EXPECT_EQ("userc@gmail.com", info.account_id);
487 TEST_F(SigninManagerTest, GaiaIdMigration) {
488 AccountTrackerService* tracker =
489 AccountTrackerServiceFactory::GetForProfile(profile());
490 if (tracker->GetMigrationState() !=
491 AccountTrackerService::MIGRATION_NOT_STARTED) {
492 std::string email = "user@gmail.com";
493 std::string gaia_id = "account_gaia_id";
495 CreateNakedSigninManager();
497 PrefService* client_prefs = signin_client()->GetPrefs();
498 client_prefs->SetInteger(prefs::kAccountIdMigrationState,
499 AccountTrackerService::MIGRATION_NOT_STARTED);
500 ListPrefUpdate update(client_prefs,
501 AccountTrackerService::kAccountInfoPref);
502 update->Clear();
503 base::DictionaryValue* dict = new base::DictionaryValue();
504 update->Append(dict);
505 dict->SetString("account_id", base::UTF8ToUTF16(email));
506 dict->SetString("email", base::UTF8ToUTF16(email));
507 dict->SetString("gaia", base::UTF8ToUTF16(gaia_id));
509 tracker->Shutdown();
510 tracker->Initialize(signin_client());
512 client_prefs->SetString(prefs::kGoogleServicesAccountId, email);
513 manager_->Initialize(g_browser_process->local_state());
515 EXPECT_EQ(gaia_id, manager_->GetAuthenticatedAccountId());
516 EXPECT_EQ(gaia_id, profile()->GetPrefs()->GetString(
517 prefs::kGoogleServicesAccountId));
521 TEST_F(SigninManagerTest, VeryOldProfileGaiaIdMigration) {
522 AccountTrackerService* tracker =
523 AccountTrackerServiceFactory::GetForProfile(profile());
524 if (tracker->GetMigrationState() !=
525 AccountTrackerService::MIGRATION_NOT_STARTED) {
526 std::string email = "user@gmail.com";
527 std::string gaia_id = "account_gaia_id";
529 CreateNakedSigninManager();
531 PrefService* client_prefs = signin_client()->GetPrefs();
532 client_prefs->SetInteger(prefs::kAccountIdMigrationState,
533 AccountTrackerService::MIGRATION_NOT_STARTED);
534 ListPrefUpdate update(client_prefs,
535 AccountTrackerService::kAccountInfoPref);
536 update->Clear();
537 base::DictionaryValue* dict = new base::DictionaryValue();
538 update->Append(dict);
539 dict->SetString("account_id", base::UTF8ToUTF16(email));
540 dict->SetString("email", base::UTF8ToUTF16(email));
541 dict->SetString("gaia", base::UTF8ToUTF16(gaia_id));
543 tracker->Shutdown();
544 tracker->Initialize(signin_client());
546 client_prefs->ClearPref(prefs::kGoogleServicesAccountId);
547 client_prefs->SetString(prefs::kGoogleServicesUsername, email);
548 manager_->Initialize(g_browser_process->local_state());
550 EXPECT_EQ(gaia_id, manager_->GetAuthenticatedAccountId());
551 EXPECT_EQ(gaia_id, profile()->GetPrefs()->GetString(
552 prefs::kGoogleServicesAccountId));
556 TEST_F(SigninManagerTest, GaiaIdMigrationCrashInTheMiddle) {
557 AccountTrackerService* tracker =
558 AccountTrackerServiceFactory::GetForProfile(profile());
559 if (tracker->GetMigrationState() !=
560 AccountTrackerService::MIGRATION_NOT_STARTED) {
561 std::string email = "user@gmail.com";
562 std::string gaia_id = "account_gaia_id";
564 CreateNakedSigninManager();
566 PrefService* client_prefs = signin_client()->GetPrefs();
567 client_prefs->SetInteger(prefs::kAccountIdMigrationState,
568 AccountTrackerService::MIGRATION_NOT_STARTED);
569 ListPrefUpdate update(client_prefs,
570 AccountTrackerService::kAccountInfoPref);
571 update->Clear();
572 base::DictionaryValue* dict = new base::DictionaryValue();
573 update->Append(dict);
574 dict->SetString("account_id", base::UTF8ToUTF16(email));
575 dict->SetString("email", base::UTF8ToUTF16(email));
576 dict->SetString("gaia", base::UTF8ToUTF16(gaia_id));
578 tracker->Shutdown();
579 tracker->Initialize(signin_client());
581 client_prefs->SetString(prefs::kGoogleServicesAccountId, gaia_id);
582 manager_->Initialize(g_browser_process->local_state());
584 EXPECT_EQ(gaia_id, manager_->GetAuthenticatedAccountId());
585 EXPECT_EQ(gaia_id, profile()->GetPrefs()->GetString(
586 prefs::kGoogleServicesAccountId));
588 base::RunLoop().RunUntilIdle();
589 EXPECT_EQ(AccountTrackerService::MIGRATION_DONE,
590 tracker->GetMigrationState());