Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / signin / signin_manager_unittest.cc
blobdaacd63225eda604ad2124ec78772728e36090bf
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.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_profile_oauth2_token_service.h"
37 #include "components/signin/core/browser/profile_oauth2_token_service.h"
38 #include "components/signin/core/browser/test_signin_client.h"
39 #include "content/public/browser/child_process_security_policy.h"
40 #include "content/public/browser/notification_source.h"
41 #include "content/public/test/test_browser_thread_bundle.h"
42 #include "google_apis/gaia/gaia_constants.h"
43 #include "google_apis/gaia/gaia_urls.h"
44 #include "net/cookies/cookie_monster.h"
45 #include "net/url_request/test_url_fetcher_factory.h"
46 #include "net/url_request/url_request.h"
47 #include "net/url_request/url_request_context_getter.h"
48 #include "net/url_request/url_request_status.h"
50 #include "testing/gmock/include/gmock/gmock.h"
51 #include "testing/gtest/include/gtest/gtest.h"
53 namespace {
55 scoped_ptr<KeyedService> SigninManagerBuild(content::BrowserContext* context) {
56 Profile* profile = static_cast<Profile*>(context);
57 scoped_ptr<SigninManager> service(new SigninManager(
58 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile),
59 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
60 AccountTrackerServiceFactory::GetForProfile(profile),
61 GaiaCookieManagerServiceFactory::GetForProfile(profile)));
62 service->Initialize(NULL);
63 return service.Pass();
66 class TestSigninManagerObserver : public SigninManagerBase::Observer {
67 public:
68 TestSigninManagerObserver() : num_failed_signins_(0),
69 num_successful_signins_(0),
70 num_signouts_(0) {
73 ~TestSigninManagerObserver() override {}
75 int num_failed_signins_;
76 int num_successful_signins_;
77 int num_signouts_;
79 private:
80 // SigninManagerBase::Observer:
81 void GoogleSigninFailed(const GoogleServiceAuthError& error) override {
82 num_failed_signins_++;
85 void GoogleSigninSucceeded(const std::string& account_id,
86 const std::string& username,
87 const std::string& password) override {
88 num_successful_signins_++;
91 void GoogleSignedOut(const std::string& account_id,
92 const std::string& username) override {
93 num_signouts_++;
97 } // namespace
100 class SigninManagerTest : public testing::Test {
101 public:
102 SigninManagerTest() : manager_(NULL) {}
103 ~SigninManagerTest() override {}
105 void SetUp() override {
106 manager_ = NULL;
107 prefs_.reset(new TestingPrefServiceSimple);
108 chrome::RegisterLocalState(prefs_->registry());
109 TestingBrowserProcess::GetGlobal()->SetLocalState(
110 prefs_.get());
111 TestingProfile::Builder builder;
112 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
113 BuildFakeProfileOAuth2TokenService);
114 builder.AddTestingFactory(ChromeSigninClientFactory::GetInstance(),
115 signin::BuildTestSigninClient);
116 builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
117 SigninManagerBuild);
118 builder.AddTestingFactory(AccountFetcherServiceFactory::GetInstance(),
119 FakeAccountFetcherService::BuildForTests);
120 profile_ = builder.Build();
122 TestSigninClient* client = static_cast<TestSigninClient*>(
123 ChromeSigninClientFactory::GetForProfile(profile()));
124 client->SetURLRequestContext(profile_->GetRequestContext());
127 void TearDown() override {
128 if (manager_)
129 manager_->RemoveObserver(&test_observer_);
131 // Destroy the SigninManager here, because it relies on profile() which is
132 // freed in the base class.
133 if (naked_manager_) {
134 naked_manager_->Shutdown();
135 naked_manager_.reset(NULL);
137 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL);
139 // Manually destroy PrefService and Profile so that they are shutdown
140 // in the correct order. Both need to be destroyed before the
141 // |thread_bundle_| member.
142 profile_.reset();
143 prefs_.reset(); // LocalState needs to outlive the profile.
146 TestingProfile* profile() { return profile_.get(); }
148 TestSigninClient* signin_client() {
149 return static_cast<TestSigninClient*>(
150 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile()));
153 // Seed the account tracker with information from logged in user. Normally
154 // this is done by UI code before calling SigninManager. Returns the string
155 // to use as the account_id.
156 std::string AddToAccountTracker(const std::string& gaia_id,
157 const std::string& email) {
158 AccountTrackerService* service =
159 AccountTrackerServiceFactory::GetForProfile(profile());
160 service->SeedAccountInfo(gaia_id, email);
161 return service->PickAccountIdForAccount(gaia_id, email);
164 // Sets up the signin manager as a service if other code will try to get it as
165 // a PKS.
166 void SetUpSigninManagerAsService() {
167 DCHECK(!manager_);
168 DCHECK(!naked_manager_);
169 manager_ = static_cast<SigninManager*>(
170 SigninManagerFactory::GetForProfile(profile()));
171 manager_->AddObserver(&test_observer_);
174 // Create a naked signin manager if integration with PKSs is not needed.
175 void CreateNakedSigninManager() {
176 DCHECK(!manager_);
177 naked_manager_.reset(new SigninManager(
178 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile()),
179 ProfileOAuth2TokenServiceFactory::GetForProfile(profile()),
180 AccountTrackerServiceFactory::GetForProfile(profile()),
181 GaiaCookieManagerServiceFactory::GetForProfile(profile())));
183 manager_ = naked_manager_.get();
184 manager_->AddObserver(&test_observer_);
187 // Shuts down |manager_|.
188 void ShutDownManager() {
189 DCHECK(manager_);
190 manager_->RemoveObserver(&test_observer_);
191 manager_->Shutdown();
192 if (naked_manager_)
193 naked_manager_.reset(NULL);
194 manager_ = NULL;
197 void ExpectSignInWithRefreshTokenSuccess() {
198 EXPECT_TRUE(manager_->IsAuthenticated());
199 EXPECT_FALSE(manager_->GetAuthenticatedAccountId().empty());
200 EXPECT_FALSE(manager_->GetAuthenticatedUsername().empty());
202 ProfileOAuth2TokenService* token_service =
203 ProfileOAuth2TokenServiceFactory::GetForProfile(profile());
204 EXPECT_TRUE(token_service->RefreshTokenIsAvailable(
205 manager_->GetAuthenticatedAccountId()));
207 // Should go into token service and stop.
208 EXPECT_EQ(1, test_observer_.num_successful_signins_);
209 EXPECT_EQ(0, test_observer_.num_failed_signins_);
212 void CompleteSigninCallback(const std::string& oauth_token) {
213 oauth_tokens_fetched_.push_back(oauth_token);
214 manager_->CompletePendingSignin();
217 content::TestBrowserThreadBundle thread_bundle_;
218 net::TestURLFetcherFactory factory_;
219 scoped_ptr<SigninManager> naked_manager_;
220 SigninManager* manager_;
221 TestSigninManagerObserver test_observer_;
222 scoped_ptr<TestingProfile> profile_;
223 std::vector<std::string> oauth_tokens_fetched_;
224 scoped_ptr<TestingPrefServiceSimple> prefs_;
225 std::vector<std::string> cookies_;
228 TEST_F(SigninManagerTest, SignInWithRefreshToken) {
229 SetUpSigninManagerAsService();
230 EXPECT_FALSE(manager_->IsAuthenticated());
232 std::string account_id = AddToAccountTracker("gaia_id", "user@gmail.com");
233 manager_->StartSignInWithRefreshToken(
234 "rt",
235 "gaia_id",
236 "user@gmail.com",
237 "password",
238 SigninManager::OAuthTokenFetchedCallback());
240 ExpectSignInWithRefreshTokenSuccess();
242 // Should persist across resets.
243 ShutDownManager();
244 CreateNakedSigninManager();
245 manager_->Initialize(NULL);
246 EXPECT_EQ(account_id, manager_->GetAuthenticatedAccountId());
249 TEST_F(SigninManagerTest, SignInWithRefreshTokenCallbackComplete) {
250 SetUpSigninManagerAsService();
251 EXPECT_FALSE(manager_->IsAuthenticated());
253 // Since the password is empty, must verify the gaia cookies first.
254 SigninManager::OAuthTokenFetchedCallback callback =
255 base::Bind(&SigninManagerTest::CompleteSigninCallback,
256 base::Unretained(this));
257 manager_->StartSignInWithRefreshToken(
258 "rt",
259 "gaia_id",
260 "user@gmail.com",
261 "password",
262 callback);
264 ExpectSignInWithRefreshTokenSuccess();
265 ASSERT_EQ(1U, oauth_tokens_fetched_.size());
266 EXPECT_EQ(oauth_tokens_fetched_[0], "rt");
269 TEST_F(SigninManagerTest, SignInWithRefreshTokenCallsPostSignout) {
270 SetUpSigninManagerAsService();
271 EXPECT_FALSE(manager_->IsAuthenticated());
273 std::string gaia_id = "12345";
274 std::string email = "user@google.com";
276 AccountTrackerServiceFactory::GetForProfile(profile())->
277 SeedAccountInfo(gaia_id, email);
278 FakeAccountFetcherService* account_fetcher_service =
279 static_cast<FakeAccountFetcherService*>(
280 AccountFetcherServiceFactory::GetForProfile(profile()));
281 account_fetcher_service->EnableNetworkFetches();
283 ASSERT_TRUE(signin_client()->get_signed_in_password().empty());
285 manager_->StartSignInWithRefreshToken(
286 "rt1",
287 gaia_id,
288 email,
289 "password",
290 SigninManager::OAuthTokenFetchedCallback());
292 // PostSignedIn is not called until the AccountTrackerService returns.
293 ASSERT_EQ("", signin_client()->get_signed_in_password());
295 account_fetcher_service->FakeUserInfoFetchSuccess(email,
296 gaia_id,
297 "google.com",
298 "full_name",
299 "given_name",
300 "locale",
301 "http://www.google.com");
303 // AccountTracker and SigninManager are both done and PostSignedIn was called.
304 ASSERT_EQ("password", signin_client()->get_signed_in_password());
306 ExpectSignInWithRefreshTokenSuccess();
310 TEST_F(SigninManagerTest, SignOut) {
311 SetUpSigninManagerAsService();
312 manager_->StartSignInWithRefreshToken(
313 "rt",
314 "gaia_id",
315 "user@gmail.com",
316 "password",
317 SigninManager::OAuthTokenFetchedCallback());
318 manager_->SignOut(signin_metrics::SIGNOUT_TEST);
319 EXPECT_FALSE(manager_->IsAuthenticated());
320 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty());
321 EXPECT_TRUE(manager_->GetAuthenticatedAccountId().empty());
322 // Should not be persisted anymore
323 ShutDownManager();
324 CreateNakedSigninManager();
325 manager_->Initialize(NULL);
326 EXPECT_FALSE(manager_->IsAuthenticated());
327 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty());
328 EXPECT_TRUE(manager_->GetAuthenticatedAccountId().empty());
331 TEST_F(SigninManagerTest, SignOutWhileProhibited) {
332 SetUpSigninManagerAsService();
333 EXPECT_FALSE(manager_->IsAuthenticated());
334 EXPECT_TRUE(manager_->GetAuthenticatedUsername().empty());
335 EXPECT_TRUE(manager_->GetAuthenticatedAccountId().empty());
337 manager_->SetAuthenticatedAccountInfo("gaia_id", "user@gmail.com");
338 manager_->ProhibitSignout(true);
339 manager_->SignOut(signin_metrics::SIGNOUT_TEST);
340 EXPECT_TRUE(manager_->IsAuthenticated());
341 manager_->ProhibitSignout(false);
342 manager_->SignOut(signin_metrics::SIGNOUT_TEST);
343 EXPECT_FALSE(manager_->IsAuthenticated());
346 TEST_F(SigninManagerTest, Prohibited) {
347 g_browser_process->local_state()->SetString(
348 prefs::kGoogleServicesUsernamePattern, ".*@google.com");
349 CreateNakedSigninManager();
350 manager_->Initialize(g_browser_process->local_state());
351 EXPECT_TRUE(manager_->IsAllowedUsername("test@google.com"));
352 EXPECT_TRUE(manager_->IsAllowedUsername("happy@google.com"));
353 EXPECT_FALSE(manager_->IsAllowedUsername("test@invalid.com"));
354 EXPECT_FALSE(manager_->IsAllowedUsername("test@notgoogle.com"));
355 EXPECT_FALSE(manager_->IsAllowedUsername(std::string()));
358 TEST_F(SigninManagerTest, TestAlternateWildcard) {
359 // Test to make sure we accept "*@google.com" as a pattern (treat it as if
360 // the admin entered ".*@google.com").
361 g_browser_process->local_state()->SetString(
362 prefs::kGoogleServicesUsernamePattern, "*@google.com");
363 CreateNakedSigninManager();
364 manager_->Initialize(g_browser_process->local_state());
365 EXPECT_TRUE(manager_->IsAllowedUsername("test@google.com"));
366 EXPECT_TRUE(manager_->IsAllowedUsername("happy@google.com"));
367 EXPECT_FALSE(manager_->IsAllowedUsername("test@invalid.com"));
368 EXPECT_FALSE(manager_->IsAllowedUsername("test@notgoogle.com"));
369 EXPECT_FALSE(manager_->IsAllowedUsername(std::string()));
372 TEST_F(SigninManagerTest, ProhibitedAtStartup) {
373 std::string account_id = AddToAccountTracker("gaia_id", "user@gmail.com");
374 profile()->GetPrefs()->SetString(prefs::kGoogleServicesAccountId, account_id);
375 g_browser_process->local_state()->SetString(
376 prefs::kGoogleServicesUsernamePattern, ".*@google.com");
377 CreateNakedSigninManager();
378 manager_->Initialize(g_browser_process->local_state());
379 // Currently signed in user is prohibited by policy, so should be signed out.
380 EXPECT_EQ("", manager_->GetAuthenticatedUsername());
381 EXPECT_EQ("", manager_->GetAuthenticatedAccountId());
384 TEST_F(SigninManagerTest, ProhibitedAfterStartup) {
385 std::string account_id = AddToAccountTracker("gaia_id", "user@gmail.com");
386 profile()->GetPrefs()->SetString(prefs::kGoogleServicesAccountId, account_id);
387 CreateNakedSigninManager();
388 manager_->Initialize(g_browser_process->local_state());
389 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername());
390 EXPECT_EQ(account_id, manager_->GetAuthenticatedAccountId());
391 // Update the profile - user should be signed out.
392 g_browser_process->local_state()->SetString(
393 prefs::kGoogleServicesUsernamePattern, ".*@google.com");
394 EXPECT_EQ("", manager_->GetAuthenticatedUsername());
395 EXPECT_EQ("", manager_->GetAuthenticatedAccountId());
398 TEST_F(SigninManagerTest, ExternalSignIn) {
399 CreateNakedSigninManager();
400 manager_->Initialize(g_browser_process->local_state());
401 EXPECT_EQ("", manager_->GetAuthenticatedUsername());
402 EXPECT_EQ("", manager_->GetAuthenticatedAccountId());
403 EXPECT_EQ(0, test_observer_.num_successful_signins_);
405 std::string account_id = AddToAccountTracker("gaia_id", "user@gmail.com");
406 manager_->OnExternalSigninCompleted("user@gmail.com");
407 EXPECT_EQ(1, test_observer_.num_successful_signins_);
408 EXPECT_EQ(0, test_observer_.num_failed_signins_);
409 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername());
410 EXPECT_EQ(account_id, manager_->GetAuthenticatedAccountId());
413 TEST_F(SigninManagerTest, SigninNotAllowed) {
414 std::string user("user@google.com");
415 profile()->GetPrefs()->SetString(prefs::kGoogleServicesAccountId, user);
416 profile()->GetPrefs()->SetBoolean(prefs::kSigninAllowed, false);
417 CreateNakedSigninManager();
418 AddToAccountTracker("gaia_id", user);
419 manager_->Initialize(g_browser_process->local_state());
420 // Currently signing in is prohibited by policy, so should be signed out.
421 EXPECT_EQ("", manager_->GetAuthenticatedUsername());
422 EXPECT_EQ("", manager_->GetAuthenticatedAccountId());
425 TEST_F(SigninManagerTest, UpgradeToNewPrefs) {
426 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
427 "user@gmail.com");
428 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUserAccountId,
429 "account_id");
430 CreateNakedSigninManager();
431 manager_->Initialize(g_browser_process->local_state());
432 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedUsername());
434 AccountTrackerService* service =
435 AccountTrackerServiceFactory::GetForProfile(profile());
436 if (service->GetMigrationState() ==
437 AccountTrackerService::MIGRATION_NOT_STARTED) {
438 // TODO(rogerta): until the migration to gaia id, the account id will remain
439 // the old username.
440 EXPECT_EQ("user@gmail.com", manager_->GetAuthenticatedAccountId());
441 EXPECT_EQ("user@gmail.com",
442 profile()->GetPrefs()->GetString(prefs::kGoogleServicesAccountId));
443 } else {
444 EXPECT_EQ("account_id", manager_->GetAuthenticatedAccountId());
445 EXPECT_EQ("account_id", profile()->GetPrefs()->GetString(
446 prefs::kGoogleServicesAccountId));
448 EXPECT_EQ("",
449 profile()->GetPrefs()->GetString(prefs::kGoogleServicesUsername));
451 // Make sure account tracker was updated.
452 AccountTrackerService::AccountInfo info = service->GetAccountInfo(
453 manager_->GetAuthenticatedAccountId());
454 EXPECT_EQ("user@gmail.com", info.email);
455 EXPECT_EQ("account_id", info.gaia);
458 TEST_F(SigninManagerTest, CanonicalizesPrefs) {
459 AccountTrackerService* service =
460 AccountTrackerServiceFactory::GetForProfile(profile());
462 // This unit test is not needed after migrating to gaia id.
463 if (service->GetMigrationState() ==
464 AccountTrackerService::MIGRATION_NOT_STARTED) {
465 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
466 "user.C@gmail.com");
468 CreateNakedSigninManager();
469 manager_->Initialize(g_browser_process->local_state());
470 EXPECT_EQ("user.C@gmail.com", manager_->GetAuthenticatedUsername());
472 // TODO(rogerta): until the migration to gaia id, the account id will remain
473 // the old username.
474 EXPECT_EQ("userc@gmail.com", manager_->GetAuthenticatedAccountId());
475 EXPECT_EQ("userc@gmail.com", profile()->GetPrefs()->GetString(
476 prefs::kGoogleServicesAccountId));
477 EXPECT_EQ("",
478 profile()->GetPrefs()->GetString(prefs::kGoogleServicesUsername));
480 // Make sure account tracker has a canonicalized username.
481 AccountTrackerService::AccountInfo info =
482 service->GetAccountInfo(manager_->GetAuthenticatedAccountId());
483 EXPECT_EQ("user.C@gmail.com", info.email);
484 EXPECT_EQ("userc@gmail.com", info.account_id);
488 TEST_F(SigninManagerTest, GaiaIdMigration) {
489 AccountTrackerService* tracker =
490 AccountTrackerServiceFactory::GetForProfile(profile());
491 if (tracker->GetMigrationState() !=
492 AccountTrackerService::MIGRATION_NOT_STARTED) {
493 std::string email = "user@gmail.com";
494 std::string gaia_id = "account_gaia_id";
496 CreateNakedSigninManager();
498 PrefService* client_prefs = signin_client()->GetPrefs();
499 client_prefs->SetInteger(prefs::kAccountIdMigrationState,
500 AccountTrackerService::MIGRATION_NOT_STARTED);
501 ListPrefUpdate update(client_prefs,
502 AccountTrackerService::kAccountInfoPref);
503 update->Clear();
504 base::DictionaryValue* dict = new base::DictionaryValue();
505 update->Append(dict);
506 dict->SetString("account_id", base::UTF8ToUTF16(email));
507 dict->SetString("email", base::UTF8ToUTF16(email));
508 dict->SetString("gaia", base::UTF8ToUTF16(gaia_id));
510 tracker->Shutdown();
511 tracker->Initialize(signin_client());
513 client_prefs->SetString(prefs::kGoogleServicesAccountId, email);
514 manager_->Initialize(g_browser_process->local_state());
516 EXPECT_EQ(gaia_id, manager_->GetAuthenticatedAccountId());
517 EXPECT_EQ(gaia_id, profile()->GetPrefs()->GetString(
518 prefs::kGoogleServicesAccountId));
522 TEST_F(SigninManagerTest, VeryOldProfileGaiaIdMigration) {
523 AccountTrackerService* tracker =
524 AccountTrackerServiceFactory::GetForProfile(profile());
525 if (tracker->GetMigrationState() !=
526 AccountTrackerService::MIGRATION_NOT_STARTED) {
527 std::string email = "user@gmail.com";
528 std::string gaia_id = "account_gaia_id";
530 CreateNakedSigninManager();
532 PrefService* client_prefs = signin_client()->GetPrefs();
533 client_prefs->SetInteger(prefs::kAccountIdMigrationState,
534 AccountTrackerService::MIGRATION_NOT_STARTED);
535 ListPrefUpdate update(client_prefs,
536 AccountTrackerService::kAccountInfoPref);
537 update->Clear();
538 base::DictionaryValue* dict = new base::DictionaryValue();
539 update->Append(dict);
540 dict->SetString("account_id", base::UTF8ToUTF16(email));
541 dict->SetString("email", base::UTF8ToUTF16(email));
542 dict->SetString("gaia", base::UTF8ToUTF16(gaia_id));
544 tracker->Shutdown();
545 tracker->Initialize(signin_client());
547 client_prefs->ClearPref(prefs::kGoogleServicesAccountId);
548 client_prefs->SetString(prefs::kGoogleServicesUsername, email);
549 manager_->Initialize(g_browser_process->local_state());
551 EXPECT_EQ(gaia_id, manager_->GetAuthenticatedAccountId());
552 EXPECT_EQ(gaia_id, profile()->GetPrefs()->GetString(
553 prefs::kGoogleServicesAccountId));
557 TEST_F(SigninManagerTest, GaiaIdMigrationCrashInTheMiddle) {
558 AccountTrackerService* tracker =
559 AccountTrackerServiceFactory::GetForProfile(profile());
560 if (tracker->GetMigrationState() !=
561 AccountTrackerService::MIGRATION_NOT_STARTED) {
562 std::string email = "user@gmail.com";
563 std::string gaia_id = "account_gaia_id";
565 CreateNakedSigninManager();
567 PrefService* client_prefs = signin_client()->GetPrefs();
568 client_prefs->SetInteger(prefs::kAccountIdMigrationState,
569 AccountTrackerService::MIGRATION_NOT_STARTED);
570 ListPrefUpdate update(client_prefs,
571 AccountTrackerService::kAccountInfoPref);
572 update->Clear();
573 base::DictionaryValue* dict = new base::DictionaryValue();
574 update->Append(dict);
575 dict->SetString("account_id", base::UTF8ToUTF16(email));
576 dict->SetString("email", base::UTF8ToUTF16(email));
577 dict->SetString("gaia", base::UTF8ToUTF16(gaia_id));
579 tracker->Shutdown();
580 tracker->Initialize(signin_client());
582 client_prefs->SetString(prefs::kGoogleServicesAccountId, gaia_id);
583 manager_->Initialize(g_browser_process->local_state());
585 EXPECT_EQ(gaia_id, manager_->GetAuthenticatedAccountId());
586 EXPECT_EQ(gaia_id, profile()->GetPrefs()->GetString(
587 prefs::kGoogleServicesAccountId));
589 base::RunLoop().RunUntilIdle();
590 EXPECT_EQ(AccountTrackerService::MIGRATION_DONE,
591 tracker->GetMigrationState());