Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(475)

Side by Side Diff: chrome/browser/profiles/profile_manager_unittest.cc

Issue 1913373002: Revert "Refactor ProfileInfoCache in most of c/b/profiles" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 MockObserver* mock_observer) { 157 MockObserver* mock_observer) {
158 manager->CreateProfileAsync( 158 manager->CreateProfileAsync(
159 temp_dir_.path().AppendASCII(name), 159 temp_dir_.path().AppendASCII(name),
160 base::Bind(&MockObserver::OnProfileCreated, 160 base::Bind(&MockObserver::OnProfileCreated,
161 base::Unretained(mock_observer)), 161 base::Unretained(mock_observer)),
162 base::UTF8ToUTF16(name), 162 base::UTF8ToUTF16(name),
163 profiles::GetDefaultAvatarIconUrl(0), 163 profiles::GetDefaultAvatarIconUrl(0),
164 is_supervised ? "Dummy ID" : std::string()); 164 is_supervised ? "Dummy ID" : std::string());
165 } 165 }
166 166
167 // Helper function to add a profile with |profile_name| to |profile_manager|'s 167 // Helper function to add a profile with |profile_name| to
168 // ProfileAttributesStorage, and return the profile created. 168 // |profile_manager|'s ProfileInfoCache, and return the profile created.
169 Profile* AddProfileToStorage(ProfileManager* profile_manager, 169 Profile* AddProfileToCache(ProfileManager* profile_manager,
170 const std::string& path_suffix, 170 const std::string& path_suffix,
171 const base::string16& profile_name) { 171 const base::string16& profile_name) {
172 ProfileAttributesStorage& storage = 172 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
173 profile_manager->GetProfileAttributesStorage(); 173 size_t num_profiles = cache.GetNumberOfProfiles();
174 size_t num_profiles = storage.GetNumberOfProfiles();
175 base::FilePath path = temp_dir_.path().AppendASCII(path_suffix); 174 base::FilePath path = temp_dir_.path().AppendASCII(path_suffix);
176 storage.AddProfile(path, profile_name, std::string(), base::string16(), 0, 175 cache.AddProfileToCache(path, profile_name,
177 std::string()); 176 std::string(), base::string16(), 0, std::string());
178 EXPECT_EQ(num_profiles + 1u, storage.GetNumberOfProfiles()); 177 EXPECT_EQ(num_profiles + 1, cache.GetNumberOfProfiles());
179 return profile_manager->GetProfile(path); 178 return profile_manager->GetProfile(path);
180 } 179 }
181 180
182 #if defined(OS_CHROMEOS) 181 #if defined(OS_CHROMEOS)
183 // Helper function to register an user with id |user_id| and create profile 182 // Helper function to register an user with id |user_id| and create profile
184 // with a correct path. 183 // with a correct path.
185 void RegisterUser(const std::string& user_id) { 184 void RegisterUser(const std::string& user_id) {
186 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get(); 185 chromeos::ProfileHelper* profile_helper = chromeos::ProfileHelper::Get();
187 const std::string user_id_hash = 186 const std::string user_id_hash =
188 profile_helper->GetUserIdHashByUserIdForTesting(user_id); 187 profile_helper->GetUserIdHashByUserIdForTesting(user_id);
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 } 408 }
410 409
411 TEST_F(ProfileManagerTest, CreateProfileAsyncCheckOmitted) { 410 TEST_F(ProfileManagerTest, CreateProfileAsyncCheckOmitted) {
412 std::string name = "0 Supervised Profile"; 411 std::string name = "0 Supervised Profile";
413 412
414 MockObserver mock_observer; 413 MockObserver mock_observer;
415 EXPECT_CALL(mock_observer, OnProfileCreated( 414 EXPECT_CALL(mock_observer, OnProfileCreated(
416 testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); 415 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
417 416
418 ProfileManager* profile_manager = g_browser_process->profile_manager(); 417 ProfileManager* profile_manager = g_browser_process->profile_manager();
419 ProfileAttributesStorage& storage = 418 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
420 profile_manager->GetProfileAttributesStorage(); 419 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
421 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
422 420
423 CreateProfileAsync(profile_manager, name, true, &mock_observer); 421 CreateProfileAsync(profile_manager, name, true, &mock_observer);
424 base::RunLoop().RunUntilIdle(); 422 base::RunLoop().RunUntilIdle();
425 423
426 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); 424 EXPECT_EQ(1u, cache.GetNumberOfProfiles());
427 // Supervised profiles should start out omitted from the profile list. 425 // Supervised profiles should start out omitted from the profile list.
428 EXPECT_TRUE(storage.GetAllProfilesAttributesSortedByName()[0u]->IsOmitted()); 426 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0));
429 427
430 name = "1 Regular Profile"; 428 name = "1 Regular Profile";
431 CreateProfileAsync(profile_manager, name, false, &mock_observer); 429 CreateProfileAsync(profile_manager, name, false, &mock_observer);
432 base::RunLoop().RunUntilIdle(); 430 base::RunLoop().RunUntilIdle();
433 431
434 EXPECT_EQ(2u, storage.GetNumberOfProfiles()); 432 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
435 // Non-supervised profiles should be included in the profile list. 433 // Non-supervised profiles should be included in the profile list.
436 EXPECT_FALSE(storage.GetAllProfilesAttributesSortedByName()[1u]->IsOmitted()); 434 EXPECT_FALSE(cache.IsOmittedProfileAtIndex(1));
437 } 435 }
438 436
439 TEST_F(ProfileManagerTest, AddProfileToStorageCheckOmitted) { 437 TEST_F(ProfileManagerTest, AddProfileToCacheCheckOmitted) {
440 ProfileManager* profile_manager = g_browser_process->profile_manager(); 438 ProfileManager* profile_manager = g_browser_process->profile_manager();
441 ProfileAttributesStorage& storage = 439 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
442 profile_manager->GetProfileAttributesStorage(); 440 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
443 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
444 441
445 const base::FilePath supervised_path = 442 const base::FilePath supervised_path =
446 temp_dir_.path().AppendASCII("Supervised"); 443 temp_dir_.path().AppendASCII("Supervised");
447 TestingProfile* supervised_profile = 444 TestingProfile* supervised_profile =
448 new TestingProfile(supervised_path, NULL); 445 new TestingProfile(supervised_path, NULL);
449 supervised_profile->GetPrefs()->SetString(prefs::kSupervisedUserId, "An ID"); 446 supervised_profile->GetPrefs()->SetString(prefs::kSupervisedUserId, "An ID");
450 447
451 // RegisterTestingProfile adds the profile to the cache and takes ownership. 448 // RegisterTestingProfile adds the profile to the cache and takes ownership.
452 profile_manager->RegisterTestingProfile(supervised_profile, true, false); 449 profile_manager->RegisterTestingProfile(supervised_profile, true, false);
453 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); 450 EXPECT_EQ(1u, cache.GetNumberOfProfiles());
454 EXPECT_TRUE(storage.GetAllProfilesAttributesSortedByName()[0u]->IsOmitted()); 451 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(0));
455 452
456 const base::FilePath nonsupervised_path = temp_dir_.path().AppendASCII( 453 const base::FilePath nonsupervised_path = temp_dir_.path().AppendASCII(
457 "Non-Supervised"); 454 "Non-Supervised");
458 TestingProfile* nonsupervised_profile = new TestingProfile(nonsupervised_path, 455 TestingProfile* nonsupervised_profile = new TestingProfile(nonsupervised_path,
459 NULL); 456 NULL);
460 profile_manager->RegisterTestingProfile(nonsupervised_profile, true, false); 457 profile_manager->RegisterTestingProfile(nonsupervised_profile, true, false);
461 458
462 EXPECT_EQ(2u, storage.GetNumberOfProfiles()); 459 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
463 ProfileAttributesEntry* entry; 460 size_t supervised_index = cache.GetIndexOfProfileWithPath(supervised_path);
464 ASSERT_TRUE(storage.GetProfileAttributesWithPath(supervised_path, &entry)); 461 EXPECT_TRUE(cache.IsOmittedProfileAtIndex(supervised_index));
465 EXPECT_TRUE(entry->IsOmitted()); 462 size_t nonsupervised_index =
466 463 cache.GetIndexOfProfileWithPath(nonsupervised_path);
467 ASSERT_TRUE(storage.GetProfileAttributesWithPath(nonsupervised_path, &entry)); 464 EXPECT_FALSE(cache.IsOmittedProfileAtIndex(nonsupervised_index));
468 EXPECT_FALSE(entry->IsOmitted());
469 } 465 }
470 466
471 TEST_F(ProfileManagerTest, GetGuestProfilePath) { 467 TEST_F(ProfileManagerTest, GetGuestProfilePath) {
472 base::FilePath guest_path = ProfileManager::GetGuestProfilePath(); 468 base::FilePath guest_path = ProfileManager::GetGuestProfilePath();
473 base::FilePath expected_path = temp_dir_.path(); 469 base::FilePath expected_path = temp_dir_.path();
474 expected_path = expected_path.Append(chrome::kGuestProfileDir); 470 expected_path = expected_path.Append(chrome::kGuestProfileDir);
475 EXPECT_EQ(expected_path, guest_path); 471 EXPECT_EQ(expected_path, guest_path);
476 } 472 }
477 473
478 TEST_F(ProfileManagerTest, GetSystemProfilePath) { 474 TEST_F(ProfileManagerTest, GetSystemProfilePath) {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 537
542 Profile* last_used_profile = ProfileManager::GetLastUsedProfile(); 538 Profile* last_used_profile = ProfileManager::GetLastUsedProfile();
543 EXPECT_TRUE(last_used_profile->IsOffTheRecord()); 539 EXPECT_TRUE(last_used_profile->IsOffTheRecord());
544 540
545 EXPECT_TRUE(last_used_profile->IsSameProfile(active_profile)); 541 EXPECT_TRUE(last_used_profile->IsSameProfile(active_profile));
546 } 542 }
547 #endif 543 #endif
548 544
549 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) { 545 TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) {
550 ProfileManager* profile_manager = g_browser_process->profile_manager(); 546 ProfileManager* profile_manager = g_browser_process->profile_manager();
551 ProfileAttributesStorage& storage = 547 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
552 profile_manager->GetProfileAttributesStorage();
553 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, 548 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
554 new base::FundamentalValue(true)); 549 new base::FundamentalValue(true));
555 550
556 // Setting a pref which is not applicable to a system (i.e., Android in this 551 // Setting a pref which is not applicable to a system (i.e., Android in this
557 // case) does not necessarily create it. Don't bother continuing with the 552 // case) does not necessarily create it. Don't bother continuing with the
558 // test if this pref doesn't exist because it will not load the profiles if 553 // test if this pref doesn't exist because it will not load the profiles if
559 // it cannot verify that the pref for background mode is enabled. 554 // it cannot verify that the pref for background mode is enabled.
560 if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled)) 555 if (!local_state_.Get()->HasPrefPath(prefs::kBackgroundModeEnabled))
561 return; 556 return;
562 557
563 EXPECT_EQ(0u, storage.GetNumberOfProfiles()); 558 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
564 559 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
565 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_1"), 560 ASCIIToUTF16("name_1"), "12345", base::string16(), 0,
566 ASCIIToUTF16("name_1"), "12345", base::string16(), 0, std::string()); 561 std::string());
567 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_2"), 562 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
568 ASCIIToUTF16("name_2"), "23456", base::string16(), 0, std::string()); 563 ASCIIToUTF16("name_2"), "23456", base::string16(), 0,
569 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_3"), 564 std::string());
570 ASCIIToUTF16("name_3"), "34567", base::string16(), 0, std::string()); 565 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"),
571 566 ASCIIToUTF16("name_3"), "34567", base::string16(), 0,
572 EXPECT_EQ(3u, storage.GetNumberOfProfiles()); 567 std::string());
573 568 cache.SetBackgroundStatusOfProfileAtIndex(0, true);
574 std::vector<ProfileAttributesEntry*> entries = 569 cache.SetBackgroundStatusOfProfileAtIndex(2, true);
575 storage.GetAllProfilesAttributes(); 570 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
576 entries[0u]->SetBackgroundStatus(true);
577 entries[2u]->SetBackgroundStatus(true);
578 571
579 profile_manager->AutoloadProfiles(); 572 profile_manager->AutoloadProfiles();
580 573
581 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); 574 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
582 } 575 }
583 576
584 TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) { 577 TEST_F(ProfileManagerTest, DoNotAutoloadProfilesIfBackgroundModeOff) {
585 ProfileManager* profile_manager = g_browser_process->profile_manager(); 578 ProfileManager* profile_manager = g_browser_process->profile_manager();
586 ProfileAttributesStorage& storage = 579 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
587 profile_manager->GetProfileAttributesStorage();
588 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled, 580 local_state_.Get()->SetUserPref(prefs::kBackgroundModeEnabled,
589 new base::FundamentalValue(false)); 581 new base::FundamentalValue(false));
590 582
591 EXPECT_EQ(0u, storage.GetNumberOfProfiles()); 583 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
592 584 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"),
593 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_1"), 585 ASCIIToUTF16("name_1"), "12345", base::string16(), 0,
594 ASCIIToUTF16("name_1"), "12345", base::string16(), 0, std::string()); 586 std::string());
595 storage.AddProfile(profile_manager->user_data_dir().AppendASCII("path_2"), 587 cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"),
596 ASCIIToUTF16("name_2"), "23456", base::string16(), 0, std::string()); 588 ASCIIToUTF16("name_2"), "23456", base::string16(), 0,
597 589 std::string());
598 EXPECT_EQ(2u, storage.GetNumberOfProfiles()); 590 cache.SetBackgroundStatusOfProfileAtIndex(0, false);
599 591 cache.SetBackgroundStatusOfProfileAtIndex(1, true);
600 std::vector<ProfileAttributesEntry*> entries = 592 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
601 storage.GetAllProfilesAttributes();
602 entries[0u]->SetBackgroundStatus(false);
603 entries[1u]->SetBackgroundStatus(true);
604 593
605 profile_manager->AutoloadProfiles(); 594 profile_manager->AutoloadProfiles();
606 595
607 EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size()); 596 EXPECT_EQ(0u, profile_manager->GetLoadedProfiles().size());
608 } 597 }
609 598
610 TEST_F(ProfileManagerTest, InitProfileUserPrefs) { 599 TEST_F(ProfileManagerTest, InitProfileUserPrefs) {
611 base::FilePath dest_path = temp_dir_.path(); 600 base::FilePath dest_path = temp_dir_.path();
612 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); 601 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
613 602
(...skipping 17 matching lines...) Expand all
631 avatar_index)); 620 avatar_index));
632 } 621 }
633 622
634 // Tests that a new profile's entry in the profile info cache is setup with the 623 // Tests that a new profile's entry in the profile info cache is setup with the
635 // same values that are in the profile prefs. 624 // same values that are in the profile prefs.
636 TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) { 625 TEST_F(ProfileManagerTest, InitProfileInfoCacheForAProfile) {
637 base::FilePath dest_path = temp_dir_.path(); 626 base::FilePath dest_path = temp_dir_.path();
638 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); 627 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
639 628
640 ProfileManager* profile_manager = g_browser_process->profile_manager(); 629 ProfileManager* profile_manager = g_browser_process->profile_manager();
630 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
641 631
642 // Successfully create the profile 632 // Successfully create the profile
643 Profile* profile = profile_manager->GetProfile(dest_path); 633 Profile* profile = profile_manager->GetProfile(dest_path);
644 ASSERT_TRUE(profile); 634 ASSERT_TRUE(profile);
645 635
646 std::string profile_name = 636 std::string profile_name =
647 profile->GetPrefs()->GetString(prefs::kProfileName); 637 profile->GetPrefs()->GetString(prefs::kProfileName);
648 size_t avatar_index = 638 size_t avatar_index =
649 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex); 639 profile->GetPrefs()->GetInteger(prefs::kProfileAvatarIndex);
650 640
651 ProfileAttributesEntry* entry; 641 size_t profile_index = cache.GetIndexOfProfileWithPath(dest_path);
652 ASSERT_TRUE(profile_manager->GetProfileAttributesStorage().
653 GetProfileAttributesWithPath(dest_path, &entry));
654 642
655 // Check if the profile prefs are the same as the cache prefs 643 // Check if the profile prefs are the same as the cache prefs
656 EXPECT_EQ(profile_name, base::UTF16ToUTF8(entry->GetName())); 644 EXPECT_EQ(profile_name,
657 EXPECT_EQ(avatar_index, entry->GetAvatarIconIndex()); 645 base::UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_index)));
646 EXPECT_EQ(avatar_index,
647 cache.GetAvatarIconIndexOfProfileAtIndex(profile_index));
658 } 648 }
659 649
660 TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) { 650 TEST_F(ProfileManagerTest, GetLastUsedProfileAllowedByPolicy) {
661 ProfileManager* profile_manager = g_browser_process->profile_manager(); 651 ProfileManager* profile_manager = g_browser_process->profile_manager();
662 ASSERT_TRUE(profile_manager); 652 ASSERT_TRUE(profile_manager);
663 653
664 #if defined(OS_CHROMEOS) 654 #if defined(OS_CHROMEOS)
665 // On CrOS, profile returned by GetLastUsedProfile is a singin profile that 655 // On CrOS, profile returned by GetLastUsedProfile is a singin profile that
666 // is forced to be incognito. That's why we need to create at least one user 656 // is forced to be incognito. That's why we need to create at least one user
667 // to get a regular profile. 657 // to get a regular profile.
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
959 browser3.reset(); 949 browser3.reset();
960 950
961 last_opened_profiles = profile_manager->GetLastOpenedProfiles(); 951 last_opened_profiles = profile_manager->GetLastOpenedProfiles();
962 ASSERT_EQ(1U, last_opened_profiles.size()); 952 ASSERT_EQ(1U, last_opened_profiles.size());
963 EXPECT_EQ(normal_profile, last_opened_profiles[0]); 953 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
964 } 954 }
965 955
966 TEST_F(ProfileManagerTest, CleanUpEphemeralProfiles) { 956 TEST_F(ProfileManagerTest, CleanUpEphemeralProfiles) {
967 // Create two profiles, one of them ephemeral. 957 // Create two profiles, one of them ephemeral.
968 ProfileManager* profile_manager = g_browser_process->profile_manager(); 958 ProfileManager* profile_manager = g_browser_process->profile_manager();
969 ProfileAttributesStorage& storage = 959 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
970 profile_manager->GetProfileAttributesStorage(); 960 ASSERT_EQ(0u, cache.GetNumberOfProfiles());
971 ASSERT_EQ(0u, storage.GetNumberOfProfiles());
972 961
973 const std::string profile_name1 = "Homer"; 962 const std::string profile_name1 = "Homer";
974 base::FilePath path1 = 963 base::FilePath path1 = temp_dir_.path().AppendASCII(profile_name1);
975 profile_manager->user_data_dir().AppendASCII(profile_name1); 964 cache.AddProfileToCache(path1, base::UTF8ToUTF16(profile_name1),
976 storage.AddProfile(path1, base::UTF8ToUTF16(profile_name1), std::string(), 965 std::string(), base::UTF8ToUTF16(profile_name1), 0,
977 base::UTF8ToUTF16(profile_name1), 0, std::string()); 966 std::string());
978 storage.GetAllProfilesAttributes()[0u]->SetIsEphemeral(true); 967 cache.SetProfileIsEphemeralAtIndex(0, true);
979 ASSERT_TRUE(base::CreateDirectory(path1)); 968 ASSERT_TRUE(base::CreateDirectory(path1));
980 969
981 const std::string profile_name2 = "Marge"; 970 const std::string profile_name2 = "Marge";
982 base::FilePath path2 = 971 base::FilePath path2 = temp_dir_.path().AppendASCII(profile_name2);
983 profile_manager->user_data_dir().AppendASCII(profile_name2); 972 cache.AddProfileToCache(path2, base::UTF8ToUTF16(profile_name2),
984 storage.AddProfile(path2, base::UTF8ToUTF16(profile_name2), std::string(), 973 std::string(), base::UTF8ToUTF16(profile_name2), 0,
985 base::UTF8ToUTF16(profile_name2), 0, std::string()); 974 std::string());
986 ASSERT_EQ(2u, storage.GetNumberOfProfiles()); 975 ASSERT_EQ(2u, cache.GetNumberOfProfiles());
987 ASSERT_TRUE(base::CreateDirectory(path2)); 976 ASSERT_TRUE(base::CreateDirectory(path2));
988 977
989 // Set the active profile. 978 // Set the active profile.
990 PrefService* local_state = g_browser_process->local_state(); 979 PrefService* local_state = g_browser_process->local_state();
991 local_state->SetString(prefs::kProfileLastUsed, profile_name1); 980 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
992 981
993 profile_manager->CleanUpEphemeralProfiles(); 982 profile_manager->CleanUpEphemeralProfiles();
994 base::RunLoop().RunUntilIdle(); 983 base::RunLoop().RunUntilIdle();
995 984
996 // The ephemeral profile should be deleted, and the last used profile set to 985 // The ephemeral profile should be deleted, and the last used profile set to
997 // the other one. 986 // the other one.
998 EXPECT_FALSE(base::DirectoryExists(path1)); 987 EXPECT_FALSE(base::DirectoryExists(path1));
999 EXPECT_TRUE(base::DirectoryExists(path2)); 988 EXPECT_TRUE(base::DirectoryExists(path2));
1000 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); 989 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1001 ASSERT_EQ(1u, storage.GetNumberOfProfiles()); 990 ASSERT_EQ(1u, cache.GetNumberOfProfiles());
1002 991
1003 // Mark the remaining profile ephemeral and clean up. 992 // Mark the remaining profile ephemeral and clean up.
1004 storage.GetAllProfilesAttributes()[0u]->SetIsEphemeral(true); 993 cache.SetProfileIsEphemeralAtIndex(0, true);
1005 profile_manager->CleanUpEphemeralProfiles(); 994 profile_manager->CleanUpEphemeralProfiles();
1006 base::RunLoop().RunUntilIdle(); 995 base::RunLoop().RunUntilIdle();
1007 996
1008 // The profile should be deleted, and the last used profile set to a new one. 997 // The profile should be deleted, and the last used profile set to a new one.
1009 EXPECT_FALSE(base::DirectoryExists(path2)); 998 EXPECT_FALSE(base::DirectoryExists(path2));
1010 EXPECT_EQ(0u, storage.GetNumberOfProfiles()); 999 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1011 EXPECT_EQ("Profile 1", local_state->GetString(prefs::kProfileLastUsed)); 1000 EXPECT_EQ("Profile 1", local_state->GetString(prefs::kProfileLastUsed));
1012 } 1001 }
1013 1002
1014 TEST_F(ProfileManagerTest, ActiveProfileDeleted) { 1003 TEST_F(ProfileManagerTest, ActiveProfileDeleted) {
1015 ProfileManager* profile_manager = g_browser_process->profile_manager(); 1004 ProfileManager* profile_manager = g_browser_process->profile_manager();
1016 ASSERT_TRUE(profile_manager); 1005 ASSERT_TRUE(profile_manager);
1017 1006
1018 // Create and load two profiles. 1007 // Create and load two profiles.
1019 const std::string profile_name1 = "New Profile 1"; 1008 const std::string profile_name1 = "New Profile 1";
1020 const std::string profile_name2 = "New Profile 2"; 1009 const std::string profile_name2 = "New Profile 2";
1021 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1); 1010 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1022 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2); 1011 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1023 1012
1024 MockObserver mock_observer; 1013 MockObserver mock_observer;
1025 EXPECT_CALL(mock_observer, OnProfileCreated( 1014 EXPECT_CALL(mock_observer, OnProfileCreated(
1026 testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); 1015 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
1027 1016
1028 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); 1017 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1029 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer); 1018 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
1030 base::RunLoop().RunUntilIdle(); 1019 base::RunLoop().RunUntilIdle();
1031 1020
1032 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); 1021 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
1033 EXPECT_EQ(2u, profile_manager->GetProfileAttributesStorage(). 1022 EXPECT_EQ(2u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
1034 GetNumberOfProfiles());
1035 1023
1036 // Set the active profile. 1024 // Set the active profile.
1037 PrefService* local_state = g_browser_process->local_state(); 1025 PrefService* local_state = g_browser_process->local_state();
1038 local_state->SetString(prefs::kProfileLastUsed, profile_name1); 1026 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
1039 1027
1040 // Delete the active profile. 1028 // Delete the active profile.
1041 profile_manager->ScheduleProfileForDeletion(dest_path1, 1029 profile_manager->ScheduleProfileForDeletion(dest_path1,
1042 ProfileManager::CreateCallback()); 1030 ProfileManager::CreateCallback());
1043 // Spin the message loop so that all the callbacks can finish running. 1031 // Spin the message loop so that all the callbacks can finish running.
1044 base::RunLoop().RunUntilIdle(); 1032 base::RunLoop().RunUntilIdle();
1045 1033
1046 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); 1034 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1047 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); 1035 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1048 } 1036 }
1049 1037
1050 TEST_F(ProfileManagerTest, LastProfileDeleted) { 1038 TEST_F(ProfileManagerTest, LastProfileDeleted) {
1051 ProfileManager* profile_manager = g_browser_process->profile_manager(); 1039 ProfileManager* profile_manager = g_browser_process->profile_manager();
1052 ASSERT_TRUE(profile_manager); 1040 ASSERT_TRUE(profile_manager);
1053 ProfileAttributesStorage& storage =
1054 profile_manager->GetProfileAttributesStorage();
1055 1041
1056 // Create and load a profile. 1042 // Create and load a profile.
1057 const std::string profile_name1 = "New Profile 1"; 1043 const std::string profile_name1 = "New Profile 1";
1058 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1); 1044 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1059 1045
1060 MockObserver mock_observer; 1046 MockObserver mock_observer;
1061 EXPECT_CALL(mock_observer, OnProfileCreated( 1047 EXPECT_CALL(mock_observer, OnProfileCreated(
1062 testing::NotNull(), NotFail())).Times(testing::AtLeast(1)); 1048 testing::NotNull(), NotFail())).Times(testing::AtLeast(1));
1063 1049
1064 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); 1050 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1065 base::RunLoop().RunUntilIdle(); 1051 base::RunLoop().RunUntilIdle();
1066 1052
1067 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); 1053 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
1068 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); 1054 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
1069 1055
1070 // Set it as the active profile. 1056 // Set it as the active profile.
1071 PrefService* local_state = g_browser_process->local_state(); 1057 PrefService* local_state = g_browser_process->local_state();
1072 local_state->SetString(prefs::kProfileLastUsed, profile_name1); 1058 local_state->SetString(prefs::kProfileLastUsed, profile_name1);
1073 1059
1074 // Delete the active profile. 1060 // Delete the active profile.
1075 profile_manager->ScheduleProfileForDeletion(dest_path1, 1061 profile_manager->ScheduleProfileForDeletion(dest_path1,
1076 ProfileManager::CreateCallback()); 1062 ProfileManager::CreateCallback());
1077 // Spin the message loop so that all the callbacks can finish running. 1063 // Spin the message loop so that all the callbacks can finish running.
1078 base::RunLoop().RunUntilIdle(); 1064 base::RunLoop().RunUntilIdle();
1079 1065
1080 // A new profile should have been created 1066 // A new profile should have been created
1081 const std::string profile_name2 = "Profile 1"; 1067 const std::string profile_name2 = "Profile 1";
1082 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2); 1068 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1083 1069
1084 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath()); 1070 EXPECT_EQ(dest_path2, profile_manager->GetLastUsedProfile()->GetPath());
1085 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed)); 1071 EXPECT_EQ(profile_name2, local_state->GetString(prefs::kProfileLastUsed));
1086 EXPECT_EQ(dest_path2, storage.GetAllProfilesAttributes()[0u]->GetPath()); 1072 EXPECT_EQ(dest_path2,
1073 profile_manager->GetProfileInfoCache().GetPathOfProfileAtIndex(0));
1087 } 1074 }
1088 1075
1089 TEST_F(ProfileManagerTest, LastProfileDeletedWithGuestActiveProfile) { 1076 TEST_F(ProfileManagerTest, LastProfileDeletedWithGuestActiveProfile) {
1090 ProfileManager* profile_manager = g_browser_process->profile_manager(); 1077 ProfileManager* profile_manager = g_browser_process->profile_manager();
1091 ASSERT_TRUE(profile_manager); 1078 ASSERT_TRUE(profile_manager);
1092 ProfileAttributesStorage& storage =
1093 profile_manager->GetProfileAttributesStorage();
1094 1079
1095 // Create and load a profile. 1080 // Create and load a profile.
1096 const std::string profile_name1 = "New Profile 1"; 1081 const std::string profile_name1 = "New Profile 1";
1097 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1); 1082 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1098 1083
1099 MockObserver mock_observer; 1084 MockObserver mock_observer;
1100 EXPECT_CALL(mock_observer, OnProfileCreated( 1085 EXPECT_CALL(mock_observer, OnProfileCreated(
1101 testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); 1086 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1102 1087
1103 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); 1088 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1104 base::RunLoop().RunUntilIdle(); 1089 base::RunLoop().RunUntilIdle();
1105 1090
1106 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); 1091 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
1107 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); 1092 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
1108 1093
1109 // Create the profile and register it. 1094 // Create the profile and register it.
1110 const std::string guest_profile_name = 1095 const std::string guest_profile_name =
1111 ProfileManager::GetGuestProfilePath().BaseName().MaybeAsASCII(); 1096 ProfileManager::GetGuestProfilePath().BaseName().MaybeAsASCII();
1112 1097
1113 TestingProfile::Builder builder; 1098 TestingProfile::Builder builder;
1114 builder.SetGuestSession(); 1099 builder.SetGuestSession();
1115 builder.SetPath(ProfileManager::GetGuestProfilePath()); 1100 builder.SetPath(ProfileManager::GetGuestProfilePath());
1116 TestingProfile* guest_profile = builder.Build().release(); 1101 TestingProfile* guest_profile = builder.Build().release();
1117 guest_profile->set_profile_name(guest_profile_name); 1102 guest_profile->set_profile_name(guest_profile_name);
1118 // Registering the profile passes ownership to the ProfileManager. 1103 // Registering the profile passes ownership to the ProfileManager.
1119 profile_manager->RegisterTestingProfile(guest_profile, false, false); 1104 profile_manager->RegisterTestingProfile(guest_profile, false, false);
1120 1105
1121 // The Guest profile does not get added to the ProfileAttributesStorage. 1106 // The Guest profile does not get added to the ProfileInfoCache.
1122 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); 1107 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
1123 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); 1108 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
1124 1109
1125 // Set the Guest profile as the active profile. 1110 // Set the Guest profile as the active profile.
1126 PrefService* local_state = g_browser_process->local_state(); 1111 PrefService* local_state = g_browser_process->local_state();
1127 local_state->SetString(prefs::kProfileLastUsed, guest_profile_name); 1112 local_state->SetString(prefs::kProfileLastUsed, guest_profile_name);
1128 1113
1129 // Delete the other profile. 1114 // Delete the other profile.
1130 profile_manager->ScheduleProfileForDeletion(dest_path1, 1115 profile_manager->ScheduleProfileForDeletion(dest_path1,
1131 ProfileManager::CreateCallback()); 1116 ProfileManager::CreateCallback());
1132 // Spin the message loop so that all the callbacks can finish running. 1117 // Spin the message loop so that all the callbacks can finish running.
1133 base::RunLoop().RunUntilIdle(); 1118 base::RunLoop().RunUntilIdle();
1134 1119
1135 // A new profile should have been created. 1120 // A new profile should have been created.
1136 const std::string profile_name2 = "Profile 1"; 1121 const std::string profile_name2 = "Profile 1";
1137 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2); 1122 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1138 1123
1139 EXPECT_EQ(3u, profile_manager->GetLoadedProfiles().size()); 1124 EXPECT_EQ(3u, profile_manager->GetLoadedProfiles().size());
1140 EXPECT_EQ(1u, storage.GetNumberOfProfiles()); 1125 EXPECT_EQ(1u, profile_manager->GetProfileInfoCache().GetNumberOfProfiles());
1141 EXPECT_EQ(dest_path2, storage.GetAllProfilesAttributes()[0u]->GetPath()); 1126 EXPECT_EQ(dest_path2,
1127 profile_manager->GetProfileInfoCache().GetPathOfProfileAtIndex(0));
1142 } 1128 }
1143 1129
1144 TEST_F(ProfileManagerTest, ProfileDisplayNameResetsDefaultName) { 1130 TEST_F(ProfileManagerTest, ProfileDisplayNameResetsDefaultName) {
1145 if (!profiles::IsMultipleProfilesEnabled()) 1131 if (!profiles::IsMultipleProfilesEnabled())
1146 return; 1132 return;
1147 1133
1148 ProfileManager* profile_manager = g_browser_process->profile_manager(); 1134 ProfileManager* profile_manager = g_browser_process->profile_manager();
1149 ProfileAttributesStorage& storage = 1135 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1150 profile_manager->GetProfileAttributesStorage(); 1136 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1151 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
1152 1137
1153 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME. 1138 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1154 const base::string16 default_profile_name = 1139 const base::string16 default_profile_name =
1155 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); 1140 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1156 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u); 1141 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1157 Profile* profile1 = AddProfileToStorage(profile_manager, 1142 Profile* profile1 = AddProfileToCache(profile_manager,
1158 "path_1", profile_name1); 1143 "path_1", profile_name1);
1159 EXPECT_EQ(default_profile_name, 1144 EXPECT_EQ(default_profile_name,
1160 profiles::GetAvatarNameForProfile(profile1->GetPath())); 1145 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1161 1146
1162 // Multiple profiles means displaying the actual profile names. 1147 // Multiple profiles means displaying the actual profile names.
1163 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u); 1148 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1164 Profile* profile2 = AddProfileToStorage(profile_manager, 1149 Profile* profile2 = AddProfileToCache(profile_manager,
1165 "path_2", profile_name2); 1150 "path_2", profile_name2);
1166 EXPECT_EQ(profile_name1, 1151 EXPECT_EQ(profile_name1,
1167 profiles::GetAvatarNameForProfile(profile1->GetPath())); 1152 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1168 EXPECT_EQ(profile_name2, 1153 EXPECT_EQ(profile_name2,
1169 profiles::GetAvatarNameForProfile(profile2->GetPath())); 1154 profiles::GetAvatarNameForProfile(profile2->GetPath()));
1170 1155
1171 // Deleting a profile means returning to the default name. 1156 // Deleting a profile means returning to the default name.
1172 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(), 1157 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1173 ProfileManager::CreateCallback()); 1158 ProfileManager::CreateCallback());
1174 // Spin the message loop so that all the callbacks can finish running. 1159 // Spin the message loop so that all the callbacks can finish running.
1175 base::RunLoop().RunUntilIdle(); 1160 base::RunLoop().RunUntilIdle();
1176 EXPECT_EQ(default_profile_name, 1161 EXPECT_EQ(default_profile_name,
1177 profiles::GetAvatarNameForProfile(profile1->GetPath())); 1162 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1178 } 1163 }
1179 1164
1180 TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesCustomName) { 1165 TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesCustomName) {
1181 if (!profiles::IsMultipleProfilesEnabled()) 1166 if (!profiles::IsMultipleProfilesEnabled())
1182 return; 1167 return;
1183 1168
1184 ProfileManager* profile_manager = g_browser_process->profile_manager(); 1169 ProfileManager* profile_manager = g_browser_process->profile_manager();
1185 ProfileAttributesStorage& storage = 1170 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1186 profile_manager->GetProfileAttributesStorage(); 1171 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1187 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
1188 1172
1189 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME. 1173 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1190 const base::string16 default_profile_name = 1174 const base::string16 default_profile_name =
1191 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); 1175 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1192 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u); 1176 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1193 Profile* profile1 = AddProfileToStorage(profile_manager, 1177 Profile* profile1 = AddProfileToCache(profile_manager,
1194 "path_1", profile_name1); 1178 "path_1", profile_name1);
1195 EXPECT_EQ(default_profile_name, 1179 EXPECT_EQ(default_profile_name,
1196 profiles::GetAvatarNameForProfile(profile1->GetPath())); 1180 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1197 ASSERT_EQ(1u, storage.GetNumberOfProfiles());
1198 1181
1199 // We should display custom names for local profiles. 1182 // We should display custom names for local profiles.
1200 const base::string16 custom_profile_name = ASCIIToUTF16("Batman"); 1183 const base::string16 custom_profile_name = ASCIIToUTF16("Batman");
1201 ProfileAttributesEntry* entry = storage.GetAllProfilesAttributes()[0u]; 1184 cache.SetNameOfProfileAtIndex(0, custom_profile_name);
1202 entry->SetName(custom_profile_name); 1185 cache.SetProfileIsUsingDefaultNameAtIndex(0, false);
1203 entry->SetIsUsingDefaultName(false); 1186 EXPECT_EQ(custom_profile_name, cache.GetNameOfProfileAtIndex(0));
1204 EXPECT_EQ(custom_profile_name, entry->GetName());
1205 EXPECT_EQ(custom_profile_name, 1187 EXPECT_EQ(custom_profile_name,
1206 profiles::GetAvatarNameForProfile(profile1->GetPath())); 1188 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1207 1189
1208 // Multiple profiles means displaying the actual profile names. 1190 // Multiple profiles means displaying the actual profile names.
1209 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u); 1191 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1210 Profile* profile2 = AddProfileToStorage(profile_manager, 1192 Profile* profile2 = AddProfileToCache(profile_manager,
1211 "path_2", profile_name2); 1193 "path_2", profile_name2);
1212 EXPECT_EQ(custom_profile_name, 1194 EXPECT_EQ(custom_profile_name,
1213 profiles::GetAvatarNameForProfile(profile1->GetPath())); 1195 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1214 EXPECT_EQ(profile_name2, 1196 EXPECT_EQ(profile_name2,
1215 profiles::GetAvatarNameForProfile(profile2->GetPath())); 1197 profiles::GetAvatarNameForProfile(profile2->GetPath()));
1216 1198
1217 // Deleting a profile means returning to the original, custom name. 1199 // Deleting a profile means returning to the original, custom name.
1218 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(), 1200 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1219 ProfileManager::CreateCallback()); 1201 ProfileManager::CreateCallback());
1220 // Spin the message loop so that all the callbacks can finish running. 1202 // Spin the message loop so that all the callbacks can finish running.
1221 base::RunLoop().RunUntilIdle(); 1203 base::RunLoop().RunUntilIdle();
1222 EXPECT_EQ(custom_profile_name, 1204 EXPECT_EQ(custom_profile_name,
1223 profiles::GetAvatarNameForProfile(profile1->GetPath())); 1205 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1224 } 1206 }
1225 1207
1226 TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesSignedInName) { 1208 TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesSignedInName) {
1227 if (!profiles::IsMultipleProfilesEnabled()) 1209 if (!profiles::IsMultipleProfilesEnabled())
1228 return; 1210 return;
1229 1211
1230 ProfileManager* profile_manager = g_browser_process->profile_manager(); 1212 ProfileManager* profile_manager = g_browser_process->profile_manager();
1231 ProfileAttributesStorage& storage = 1213 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1232 profile_manager->GetProfileAttributesStorage(); 1214 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1233 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
1234 1215
1235 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME. 1216 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1236 const base::string16 default_profile_name = 1217 const base::string16 default_profile_name =
1237 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); 1218 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1238 const base::string16 profile_name1 = storage.ChooseNameForNewProfile(0u); 1219 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1239 Profile* profile1 = AddProfileToStorage(profile_manager, 1220 Profile* profile1 = AddProfileToCache(profile_manager,
1240 "path_1", profile_name1); 1221 "path_1", profile_name1);
1241 EXPECT_EQ(default_profile_name, 1222 EXPECT_EQ(default_profile_name,
1242 profiles::GetAvatarNameForProfile(profile1->GetPath())); 1223 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1243 1224
1244 ProfileAttributesEntry* entry = storage.GetAllProfilesAttributes()[0u];
1245 // For a signed in profile with a default name we still display 1225 // For a signed in profile with a default name we still display
1246 // IDS_SINGLE_PROFILE_DISPLAY_NAME. 1226 // IDS_SINGLE_PROFILE_DISPLAY_NAME.
1247 entry->SetAuthInfo("12345", ASCIIToUTF16("user@gmail.com")); 1227 cache.SetAuthInfoOfProfileAtIndex(0, "12345", ASCIIToUTF16("user@gmail.com"));
1248 EXPECT_EQ(profile_name1, entry->GetName()); 1228 EXPECT_EQ(profile_name1, cache.GetNameOfProfileAtIndex(0));
1249 EXPECT_EQ(default_profile_name, 1229 EXPECT_EQ(default_profile_name,
1250 profiles::GetAvatarNameForProfile(profile1->GetPath())); 1230 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1251 1231
1252 // For a signed in profile with a non-default Gaia given name we display the 1232 // For a signed in profile with a non-default Gaia given name we display the
1253 // Gaia given name. 1233 // Gaia given name.
1254 entry->SetAuthInfo("12345", ASCIIToUTF16("user@gmail.com")); 1234 cache.SetAuthInfoOfProfileAtIndex(0, "12345", ASCIIToUTF16("user@gmail.com"));
1255 const base::string16 gaia_given_name(ASCIIToUTF16("given name")); 1235 const base::string16 gaia_given_name(ASCIIToUTF16("given name"));
1256 entry->SetGAIAGivenName(gaia_given_name); 1236 cache.SetGAIAGivenNameOfProfileAtIndex(0, gaia_given_name);
1257 EXPECT_EQ(gaia_given_name, entry->GetName()); 1237 EXPECT_EQ(gaia_given_name, cache.GetNameOfProfileAtIndex(0));
1258 EXPECT_EQ(gaia_given_name, 1238 EXPECT_EQ(gaia_given_name,
1259 profiles::GetAvatarNameForProfile(profile1->GetPath())); 1239 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1260 1240
1261 // Multiple profiles means displaying the actual profile names. 1241 // Multiple profiles means displaying the actual profile names.
1262 const base::string16 profile_name2 = storage.ChooseNameForNewProfile(1u); 1242 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1263 Profile* profile2 = AddProfileToStorage(profile_manager, 1243 Profile* profile2 = AddProfileToCache(profile_manager,
1264 "path_2", profile_name2); 1244 "path_2", profile_name2);
1265 EXPECT_EQ(gaia_given_name, 1245 EXPECT_EQ(gaia_given_name,
1266 profiles::GetAvatarNameForProfile(profile1->GetPath())); 1246 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1267 EXPECT_EQ(profile_name2, 1247 EXPECT_EQ(profile_name2,
1268 profiles::GetAvatarNameForProfile(profile2->GetPath())); 1248 profiles::GetAvatarNameForProfile(profile2->GetPath()));
1269 1249
1270 // Deleting a profile means returning to the original, actual profile name. 1250 // Deleting a profile means returning to the original, actual profile name.
1271 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(), 1251 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1272 ProfileManager::CreateCallback()); 1252 ProfileManager::CreateCallback());
1273 // Spin the message loop so that all the callbacks can finish running. 1253 // Spin the message loop so that all the callbacks can finish running.
1274 base::RunLoop().RunUntilIdle(); 1254 base::RunLoop().RunUntilIdle();
1275 EXPECT_EQ(gaia_given_name, 1255 EXPECT_EQ(gaia_given_name,
1276 profiles::GetAvatarNameForProfile(profile1->GetPath())); 1256 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1277 } 1257 }
1278 1258
1279 TEST_F(ProfileManagerTest, ProfileDisplayNameIsEmailIfDefaultName) { 1259 TEST_F(ProfileManagerTest, ProfileDisplayNameIsEmailIfDefaultName) {
1280 if (!profiles::IsMultipleProfilesEnabled()) 1260 if (!profiles::IsMultipleProfilesEnabled())
1281 return; 1261 return;
1282 1262
1283 ProfileManager* profile_manager = g_browser_process->profile_manager(); 1263 ProfileManager* profile_manager = g_browser_process->profile_manager();
1284 ProfileAttributesStorage& storage = 1264 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1285 profile_manager->GetProfileAttributesStorage(); 1265 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1286 EXPECT_EQ(0u, storage.GetNumberOfProfiles());
1287 1266
1288 // Create two signed in profiles, with both new and legacy default names, and 1267 // Create two signed in profiles, with both new and legacy default names, and
1289 // a profile with a custom name. 1268 // a profile with a custom name.
1290 Profile* profile1 = AddProfileToStorage(profile_manager, "path_1", 1269 Profile* profile1 = AddProfileToCache(
1291 ASCIIToUTF16("Person 1")); 1270 profile_manager, "path_1", ASCIIToUTF16("Person 1"));
1292 Profile* profile2 = AddProfileToStorage(profile_manager, "path_2", 1271 Profile* profile2 = AddProfileToCache(
1293 ASCIIToUTF16("Default Profile")); 1272 profile_manager, "path_2", ASCIIToUTF16("Default Profile"));
1294 const base::string16 profile_name3(ASCIIToUTF16("Batman")); 1273 const base::string16 profile_name3(ASCIIToUTF16("Batman"));
1295 Profile* profile3 = AddProfileToStorage(profile_manager, "path_3", 1274 Profile* profile3 = AddProfileToCache(
1296 profile_name3); 1275 profile_manager, "path_3", profile_name3);
1297 EXPECT_EQ(3u, storage.GetNumberOfProfiles()); 1276 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
1298 1277
1299 // Sign in all profiles, and make sure they do not have a Gaia name set. 1278 // Sign in all profiles, and make sure they do not have a Gaia name set.
1300 const base::string16 email1(ASCIIToUTF16("user1@gmail.com")); 1279 const base::string16 email1(ASCIIToUTF16("user1@gmail.com"));
1301 const base::string16 email2(ASCIIToUTF16("user2@gmail.com")); 1280 const base::string16 email2(ASCIIToUTF16("user2@gmail.com"));
1302 const base::string16 email3(ASCIIToUTF16("user3@gmail.com")); 1281 const base::string16 email3(ASCIIToUTF16("user3@gmail.com"));
1303 1282
1304 ProfileAttributesEntry* entry; 1283 int index = cache.GetIndexOfProfileWithPath(profile1->GetPath());
1305 1284 cache.SetAuthInfoOfProfileAtIndex(index, "12345", email1);
1306 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile1->GetPath(), 1285 cache.SetGAIAGivenNameOfProfileAtIndex(index, base::string16());
1307 &entry)); 1286 cache.SetGAIANameOfProfileAtIndex(index, base::string16());
1308 entry->SetAuthInfo("12345", email1);
1309 entry->SetGAIAGivenName(base::string16());
1310 entry->SetGAIAName(base::string16());
1311 1287
1312 // This may resort the cache, so be extra cautious to use the right profile. 1288 // This may resort the cache, so be extra cautious to use the right profile.
1313 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile2->GetPath(), 1289 index = cache.GetIndexOfProfileWithPath(profile2->GetPath());
1314 &entry)); 1290 cache.SetAuthInfoOfProfileAtIndex(index, "23456", email2);
1315 entry->SetAuthInfo("23456", email2); 1291 cache.SetGAIAGivenNameOfProfileAtIndex(index, base::string16());
1316 entry->SetGAIAGivenName(base::string16()); 1292 cache.SetGAIANameOfProfileAtIndex(index, base::string16());
1317 entry->SetGAIAName(base::string16());
1318 1293
1319 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile3->GetPath(), 1294 index = cache.GetIndexOfProfileWithPath(profile3->GetPath());
1320 &entry)); 1295 cache.SetAuthInfoOfProfileAtIndex(index, "34567", email3);
1321 entry->SetAuthInfo("34567", email3); 1296 cache.SetGAIAGivenNameOfProfileAtIndex(index, base::string16());
1322 entry->SetGAIAGivenName(base::string16()); 1297 cache.SetGAIANameOfProfileAtIndex(index, base::string16());
1323 entry->SetGAIAName(base::string16());
1324 1298
1325 // The profiles with default names should display the email address. 1299 // The profiles with default names should display the email address.
1326 EXPECT_EQ(email1, profiles::GetAvatarNameForProfile(profile1->GetPath())); 1300 EXPECT_EQ(email1, profiles::GetAvatarNameForProfile(profile1->GetPath()));
1327 EXPECT_EQ(email2, profiles::GetAvatarNameForProfile(profile2->GetPath())); 1301 EXPECT_EQ(email2, profiles::GetAvatarNameForProfile(profile2->GetPath()));
1328 1302
1329 // The profile with the custom name should display that. 1303 // The profile with the custom name should display that.
1330 EXPECT_EQ(profile_name3, 1304 EXPECT_EQ(profile_name3,
1331 profiles::GetAvatarNameForProfile(profile3->GetPath())); 1305 profiles::GetAvatarNameForProfile(profile3->GetPath()));
1332 1306
1333 // Adding a Gaia name to a profile that previously had a default name should 1307 // Adding a Gaia name to a profile that previously had a default name should
1334 // start displaying it. 1308 // start displaying it.
1335 const base::string16 gaia_given_name(ASCIIToUTF16("Robin")); 1309 const base::string16 gaia_given_name(ASCIIToUTF16("Robin"));
1336 ASSERT_TRUE(storage.GetProfileAttributesWithPath(profile1->GetPath(), 1310 cache.SetGAIAGivenNameOfProfileAtIndex(
1337 &entry)); 1311 cache.GetIndexOfProfileWithPath(profile1->GetPath()), gaia_given_name);
1338 entry->SetGAIAGivenName(gaia_given_name);
1339 EXPECT_EQ(gaia_given_name, 1312 EXPECT_EQ(gaia_given_name,
1340 profiles::GetAvatarNameForProfile(profile1->GetPath())); 1313 profiles::GetAvatarNameForProfile(profile1->GetPath()));
1341 } 1314 }
1342 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS) 1315 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
1343 1316
1344 #if defined(OS_MACOSX) 1317 #if defined(OS_MACOSX)
1345 // These tests are for a Mac-only code path that assumes the browser 1318 // These tests are for a Mac-only code path that assumes the browser
1346 // process isn't killed when all browser windows are closed. 1319 // process isn't killed when all browser windows are closed.
1347 TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) { 1320 TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) {
1348 ProfileManager* profile_manager = g_browser_process->profile_manager(); 1321 ProfileManager* profile_manager = g_browser_process->profile_manager();
1349 ASSERT_TRUE(profile_manager); 1322 ASSERT_TRUE(profile_manager);
1350 1323
1351 // Create and load one profile, and just create a second profile. 1324 // Create and load one profile, and just create a second profile.
1352 const std::string profile_name1 = "New Profile 1"; 1325 const std::string profile_name1 = "New Profile 1";
1353 const std::string profile_name2 = "New Profile 2"; 1326 const std::string profile_name2 = "New Profile 2";
1354 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1); 1327 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
1355 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2); 1328 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1356 1329
1357 MockObserver mock_observer; 1330 MockObserver mock_observer;
1358 EXPECT_CALL(mock_observer, OnProfileCreated( 1331 EXPECT_CALL(mock_observer, OnProfileCreated(
1359 testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); 1332 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1360 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); 1333 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1361 base::RunLoop().RunUntilIdle(); 1334 base::RunLoop().RunUntilIdle();
1362 1335
1363 // Track the profile, but don't load it. 1336 // Track the profile, but don't load it.
1364 ProfileAttributesStorage& storage = 1337 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1365 profile_manager->GetProfileAttributesStorage(); 1338 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2), "23456",
1366 storage.AddProfile(dest_path2, ASCIIToUTF16(profile_name2), "23456", 1339 base::string16(), 0, std::string());
1367 base::string16(), 0, std::string());
1368 base::RunLoop().RunUntilIdle(); 1340 base::RunLoop().RunUntilIdle();
1369 1341
1370 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); 1342 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
1371 EXPECT_EQ(2u, storage.GetNumberOfProfiles()); 1343 EXPECT_EQ(2u, cache.GetNumberOfProfiles());
1372 1344
1373 // Set the active profile. 1345 // Set the active profile.
1374 PrefService* local_state = g_browser_process->local_state(); 1346 PrefService* local_state = g_browser_process->local_state();
1375 local_state->SetString(prefs::kProfileLastUsed, 1347 local_state->SetString(prefs::kProfileLastUsed,
1376 dest_path1.BaseName().MaybeAsASCII()); 1348 dest_path1.BaseName().MaybeAsASCII());
1377 1349
1378 // Delete the active profile. This should switch and load the unloaded 1350 // Delete the active profile. This should switch and load the unloaded
1379 // profile. 1351 // profile.
1380 profile_manager->ScheduleProfileForDeletion(dest_path1, 1352 profile_manager->ScheduleProfileForDeletion(dest_path1,
1381 ProfileManager::CreateCallback()); 1353 ProfileManager::CreateCallback());
(...skipping 21 matching lines...) Expand all
1403 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2); 1375 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
1404 base::FilePath dest_path3 = temp_dir_.path().AppendASCII(profile_name3); 1376 base::FilePath dest_path3 = temp_dir_.path().AppendASCII(profile_name3);
1405 1377
1406 MockObserver mock_observer; 1378 MockObserver mock_observer;
1407 EXPECT_CALL(mock_observer, OnProfileCreated( 1379 EXPECT_CALL(mock_observer, OnProfileCreated(
1408 testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); 1380 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
1409 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); 1381 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
1410 base::RunLoop().RunUntilIdle(); 1382 base::RunLoop().RunUntilIdle();
1411 1383
1412 // Create the other profiles, but don't load them. Assign a fake avatar icon 1384 // Create the other profiles, but don't load them. Assign a fake avatar icon
1413 // to ensure that profiles in the profile attributes storage are sorted by the 1385 // to ensure that profiles in the info cache are sorted by the profile name,
1414 // profile name, and not randomly by the avatar name. 1386 // and not randomly by the avatar name.
1415 ProfileAttributesStorage& storage = 1387 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1416 profile_manager->GetProfileAttributesStorage(); 1388 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
1417 storage.AddProfile(dest_path2, ASCIIToUTF16(profile_name2), "23456", 1389 "23456", ASCIIToUTF16(profile_name2), 1,
1418 ASCIIToUTF16(profile_name2), 1, std::string()); 1390 std::string());
1419 storage.AddProfile(dest_path3, ASCIIToUTF16(profile_name3), "34567", 1391 cache.AddProfileToCache(dest_path3, ASCIIToUTF16(profile_name3),
1420 ASCIIToUTF16(profile_name3), 2, std::string()); 1392 "34567", ASCIIToUTF16(profile_name3), 2,
1393 std::string());
1421 1394
1422 base::RunLoop().RunUntilIdle(); 1395 base::RunLoop().RunUntilIdle();
1423 1396
1424 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size()); 1397 EXPECT_EQ(1u, profile_manager->GetLoadedProfiles().size());
1425 EXPECT_EQ(3u, storage.GetNumberOfProfiles()); 1398 EXPECT_EQ(3u, cache.GetNumberOfProfiles());
1426 1399
1427 // Set the active profile. 1400 // Set the active profile.
1428 PrefService* local_state = g_browser_process->local_state(); 1401 PrefService* local_state = g_browser_process->local_state();
1429 local_state->SetString(prefs::kProfileLastUsed, 1402 local_state->SetString(prefs::kProfileLastUsed,
1430 dest_path1.BaseName().MaybeAsASCII()); 1403 dest_path1.BaseName().MaybeAsASCII());
1431 1404
1432 // Delete the active profile, Profile1. 1405 // Delete the active profile, Profile1.
1433 // This will post a CreateProfileAsync message, that tries to load Profile2, 1406 // This will post a CreateProfileAsync message, that tries to load Profile2,
1434 // which checks that the profile is not being deleted, and then calls back 1407 // which checks that the profile is not being deleted, and then calls back
1435 // FinishDeletingProfile for Profile1. 1408 // FinishDeletingProfile for Profile1.
1436 // Try to break this flow by setting the active profile to Profile2 in the 1409 // Try to break this flow by setting the active profile to Profile2 in the
1437 // middle (so after the first posted message), and trying to delete Profile2, 1410 // middle (so after the first posted message), and trying to delete Profile2,
1438 // so that the ProfileManager has to look for a different profile to load. 1411 // so that the ProfileManager has to look for a different profile to load.
1439 profile_manager->ScheduleProfileForDeletion(dest_path1, 1412 profile_manager->ScheduleProfileForDeletion(dest_path1,
1440 ProfileManager::CreateCallback()); 1413 ProfileManager::CreateCallback());
1441 local_state->SetString(prefs::kProfileLastUsed, 1414 local_state->SetString(prefs::kProfileLastUsed,
1442 dest_path2.BaseName().MaybeAsASCII()); 1415 dest_path2.BaseName().MaybeAsASCII());
1443 profile_manager->ScheduleProfileForDeletion(dest_path2, 1416 profile_manager->ScheduleProfileForDeletion(dest_path2,
1444 ProfileManager::CreateCallback()); 1417 ProfileManager::CreateCallback());
1445 // Spin the message loop so that all the callbacks can finish running. 1418 // Spin the message loop so that all the callbacks can finish running.
1446 base::RunLoop().RunUntilIdle(); 1419 base::RunLoop().RunUntilIdle();
1447 1420
1448 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath()); 1421 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath());
1449 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed)); 1422 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed));
1450 } 1423 }
1451 #endif // !defined(OS_MACOSX) 1424 #endif // !defined(OS_MACOSX)
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_manager_browsertest.cc ('k') | chrome/browser/profiles/profile_metrics.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698