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

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

Issue 171523004: Stop using the old-style profile names when using --new-profile-management (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review comments Created 6 years, 10 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 | Annotate | Revision Log
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 <string> 5 #include <string>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "build/build_config.h" 14 #include "build/build_config.h"
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 15 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/chrome_notification_types.h" 17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/chromeos/settings/cros_settings.h" 18 #include "chrome/browser/chromeos/settings/cros_settings.h"
19 #include "chrome/browser/history/history_service.h" 19 #include "chrome/browser/history/history_service.h"
20 #include "chrome/browser/history/history_service_factory.h" 20 #include "chrome/browser/history/history_service_factory.h"
21 #include "chrome/browser/io_thread.h" 21 #include "chrome/browser/io_thread.h"
22 #include "chrome/browser/prefs/browser_prefs.h" 22 #include "chrome/browser/prefs/browser_prefs.h"
23 #include "chrome/browser/prefs/incognito_mode_prefs.h" 23 #include "chrome/browser/prefs/incognito_mode_prefs.h"
24 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/profiles/profile_info_cache.h" 25 #include "chrome/browser/profiles/profile_info_cache.h"
26 #include "chrome/browser/profiles/profile_manager.h" 26 #include "chrome/browser/profiles/profile_manager.h"
27 #include "chrome/browser/profiles/profiles_state.h"
27 #include "chrome/browser/ui/browser.h" 28 #include "chrome/browser/ui/browser.h"
28 #include "chrome/common/chrome_constants.h" 29 #include "chrome/common/chrome_constants.h"
29 #include "chrome/common/chrome_paths.h" 30 #include "chrome/common/chrome_paths.h"
30 #include "chrome/common/chrome_switches.h" 31 #include "chrome/common/chrome_switches.h"
31 #include "chrome/common/pref_names.h" 32 #include "chrome/common/pref_names.h"
32 #include "chrome/test/base/scoped_testing_local_state.h" 33 #include "chrome/test/base/scoped_testing_local_state.h"
33 #include "chrome/test/base/test_browser_window.h" 34 #include "chrome/test/base/test_browser_window.h"
34 #include "chrome/test/base/testing_browser_process.h" 35 #include "chrome/test/base/testing_browser_process.h"
35 #include "chrome/test/base/testing_profile.h" 36 #include "chrome/test/base/testing_profile.h"
36 #include "content/public/browser/notification_service.h" 37 #include "content/public/browser/notification_service.h"
37 #include "content/public/test/test_browser_thread_bundle.h" 38 #include "content/public/test/test_browser_thread_bundle.h"
39 #include "grit/generated_resources.h"
38 #include "testing/gmock/include/gmock/gmock.h" 40 #include "testing/gmock/include/gmock/gmock.h"
39 #include "testing/gtest/include/gtest/gtest.h" 41 #include "testing/gtest/include/gtest/gtest.h"
42 #include "ui/base/l10n/l10n_util.h"
40 43
41 #if defined(OS_CHROMEOS) 44 #if defined(OS_CHROMEOS)
42 #include "chrome/browser/chromeos/login/mock_user_manager.h" 45 #include "chrome/browser/chromeos/login/mock_user_manager.h"
43 #include "chrome/browser/chromeos/login/user_manager.h" 46 #include "chrome/browser/chromeos/login/user_manager.h"
44 #include "chrome/browser/chromeos/settings/cros_settings.h" 47 #include "chrome/browser/chromeos/settings/cros_settings.h"
45 #include "chrome/browser/chromeos/settings/device_settings_service.h" 48 #include "chrome/browser/chromeos/settings/device_settings_service.h"
46 #include "chromeos/chromeos_switches.h" 49 #include "chromeos/chromeos_switches.h"
47 #endif 50 #endif
48 51
49 using base::ASCIIToUTF16; 52 using base::ASCIIToUTF16;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 MockObserver* mock_observer) { 122 MockObserver* mock_observer) {
120 manager->CreateProfileAsync( 123 manager->CreateProfileAsync(
121 temp_dir_.path().AppendASCII(name), 124 temp_dir_.path().AppendASCII(name),
122 base::Bind(&MockObserver::OnProfileCreated, 125 base::Bind(&MockObserver::OnProfileCreated,
123 base::Unretained(mock_observer)), 126 base::Unretained(mock_observer)),
124 base::UTF8ToUTF16(name), 127 base::UTF8ToUTF16(name),
125 base::UTF8ToUTF16(ProfileInfoCache::GetDefaultAvatarIconUrl(0)), 128 base::UTF8ToUTF16(ProfileInfoCache::GetDefaultAvatarIconUrl(0)),
126 is_managed ? "Dummy ID" : std::string()); 129 is_managed ? "Dummy ID" : std::string());
127 } 130 }
128 131
132 // Helper function to add a profile with |profile_name| to
133 // |profile_manager|'s ProfileInfoCache, and return the profile created.
134 Profile* AddProfileToCache(ProfileManager* profile_manager,
135 const std::string& path_suffix,
136 const base::string16& profile_name) {
137 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
138 size_t num_profiles = cache.GetNumberOfProfiles();
139 base::FilePath path = temp_dir_.path().AppendASCII(path_suffix);
140 cache.AddProfileToCache(path, profile_name,
141 base::string16(), 0, std::string());
142 EXPECT_EQ(num_profiles + 1, cache.GetNumberOfProfiles());
143 return profile_manager->GetProfile(path);
144 }
145
129 #if defined(OS_CHROMEOS) 146 #if defined(OS_CHROMEOS)
130 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; 147 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
131 chromeos::ScopedTestCrosSettings test_cros_settings_; 148 chromeos::ScopedTestCrosSettings test_cros_settings_;
132 #endif 149 #endif
133 150
134 // The path to temporary directory used to contain the test operations. 151 // The path to temporary directory used to contain the test operations.
135 base::ScopedTempDir temp_dir_; 152 base::ScopedTempDir temp_dir_;
136 ScopedTestingLocalState local_state_; 153 ScopedTestingLocalState local_state_;
137 154
138 content::TestBrowserThreadBundle thread_bundle_; 155 content::TestBrowserThreadBundle thread_bundle_;
(...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after
828 EXPECT_EQ(normal_profile, last_opened_profiles[0]); 845 EXPECT_EQ(normal_profile, last_opened_profiles[0]);
829 } 846 }
830 847
831 TEST_F(ProfileManagerTest, ActiveProfileDeleted) { 848 TEST_F(ProfileManagerTest, ActiveProfileDeleted) {
832 ProfileManager* profile_manager = g_browser_process->profile_manager(); 849 ProfileManager* profile_manager = g_browser_process->profile_manager();
833 ASSERT_TRUE(profile_manager); 850 ASSERT_TRUE(profile_manager);
834 851
835 // Create and load two profiles. 852 // Create and load two profiles.
836 const std::string profile_name1 = "New Profile 1"; 853 const std::string profile_name1 = "New Profile 1";
837 const std::string profile_name2 = "New Profile 2"; 854 const std::string profile_name2 = "New Profile 2";
838 base::FilePath dest_path1 = 855 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
839 temp_dir_.path().AppendASCII(profile_name1); 856 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
840 base::FilePath dest_path2 =
841 temp_dir_.path().AppendASCII(profile_name2);
842 857
843 MockObserver mock_observer; 858 MockObserver mock_observer;
844 EXPECT_CALL(mock_observer, OnProfileCreated( 859 EXPECT_CALL(mock_observer, OnProfileCreated(
845 testing::NotNull(), NotFail())).Times(testing::AtLeast(3)); 860 testing::NotNull(), NotFail())).Times(testing::AtLeast(3));
846 861
847 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); 862 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
848 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer); 863 CreateProfileAsync(profile_manager, profile_name2, false, &mock_observer);
849 base::RunLoop().RunUntilIdle(); 864 base::RunLoop().RunUntilIdle();
850 865
851 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size()); 866 EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
(...skipping 17 matching lines...) Expand all
869 #if defined(OS_MACOSX) 884 #if defined(OS_MACOSX)
870 // These tests are for a Mac-only code path that assumes the browser 885 // These tests are for a Mac-only code path that assumes the browser
871 // process isn't killed when all browser windows are closed. 886 // process isn't killed when all browser windows are closed.
872 TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) { 887 TEST_F(ProfileManagerTest, ActiveProfileDeletedNeedsToLoadNextProfile) {
873 ProfileManager* profile_manager = g_browser_process->profile_manager(); 888 ProfileManager* profile_manager = g_browser_process->profile_manager();
874 ASSERT_TRUE(profile_manager); 889 ASSERT_TRUE(profile_manager);
875 890
876 // Create and load one profile, and just create a second profile. 891 // Create and load one profile, and just create a second profile.
877 const std::string profile_name1 = "New Profile 1"; 892 const std::string profile_name1 = "New Profile 1";
878 const std::string profile_name2 = "New Profile 2"; 893 const std::string profile_name2 = "New Profile 2";
879 base::FilePath dest_path1 = 894 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
880 temp_dir_.path().AppendASCII(profile_name1); 895 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
881 base::FilePath dest_path2 =
882 temp_dir_.path().AppendASCII(profile_name2);
883 896
884 MockObserver mock_observer; 897 MockObserver mock_observer;
885 EXPECT_CALL(mock_observer, OnProfileCreated( 898 EXPECT_CALL(mock_observer, OnProfileCreated(
886 testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); 899 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
887 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); 900 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
888 base::RunLoop().RunUntilIdle(); 901 base::RunLoop().RunUntilIdle();
889 902
890 // Track the profile, but don't load it. 903 // Track the profile, but don't load it.
891 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); 904 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
892 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2), 905 cache.AddProfileToCache(dest_path2, ASCIIToUTF16(profile_name2),
(...skipping 25 matching lines...) Expand all
918 // the next active profile has also been marked for deletion, so the 931 // the next active profile has also been marked for deletion, so the
919 // ProfileManager needs to recursively select a different next profile. 932 // ProfileManager needs to recursively select a different next profile.
920 TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) { 933 TEST_F(ProfileManagerTest, ActiveProfileDeletedNextProfileDeletedToo) {
921 ProfileManager* profile_manager = g_browser_process->profile_manager(); 934 ProfileManager* profile_manager = g_browser_process->profile_manager();
922 ASSERT_TRUE(profile_manager); 935 ASSERT_TRUE(profile_manager);
923 936
924 // Create and load one profile, and create two more profiles. 937 // Create and load one profile, and create two more profiles.
925 const std::string profile_name1 = "New Profile 1"; 938 const std::string profile_name1 = "New Profile 1";
926 const std::string profile_name2 = "New Profile 2"; 939 const std::string profile_name2 = "New Profile 2";
927 const std::string profile_name3 = "New Profile 3"; 940 const std::string profile_name3 = "New Profile 3";
928 base::FilePath dest_path1 = 941 base::FilePath dest_path1 = temp_dir_.path().AppendASCII(profile_name1);
929 temp_dir_.path().AppendASCII(profile_name1); 942 base::FilePath dest_path2 = temp_dir_.path().AppendASCII(profile_name2);
930 base::FilePath dest_path2 = 943 base::FilePath dest_path3 = temp_dir_.path().AppendASCII(profile_name3);
931 temp_dir_.path().AppendASCII(profile_name2);
932 base::FilePath dest_path3 =
933 temp_dir_.path().AppendASCII(profile_name3);
934 944
935 MockObserver mock_observer; 945 MockObserver mock_observer;
936 EXPECT_CALL(mock_observer, OnProfileCreated( 946 EXPECT_CALL(mock_observer, OnProfileCreated(
937 testing::NotNull(), NotFail())).Times(testing::AtLeast(2)); 947 testing::NotNull(), NotFail())).Times(testing::AtLeast(2));
938 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer); 948 CreateProfileAsync(profile_manager, profile_name1, false, &mock_observer);
939 base::RunLoop().RunUntilIdle(); 949 base::RunLoop().RunUntilIdle();
940 950
941 // Create the other profiles, but don't load them. Assign a fake avatar icon 951 // Create the other profiles, but don't load them. Assign a fake avatar icon
942 // to ensure that profiles in the info cache are sorted by the profile name, 952 // to ensure that profiles in the info cache are sorted by the profile name,
943 // and not randomly by the avatar name. 953 // and not randomly by the avatar name.
(...skipping 26 matching lines...) Expand all
970 dest_path2.BaseName().MaybeAsASCII()); 980 dest_path2.BaseName().MaybeAsASCII());
971 profile_manager->ScheduleProfileForDeletion(dest_path2, 981 profile_manager->ScheduleProfileForDeletion(dest_path2,
972 ProfileManager::CreateCallback()); 982 ProfileManager::CreateCallback());
973 // Spin the message loop so that all the callbacks can finish running. 983 // Spin the message loop so that all the callbacks can finish running.
974 base::RunLoop().RunUntilIdle(); 984 base::RunLoop().RunUntilIdle();
975 985
976 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath()); 986 EXPECT_EQ(dest_path3, profile_manager->GetLastUsedProfile()->GetPath());
977 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed)); 987 EXPECT_EQ(profile_name3, local_state->GetString(prefs::kProfileLastUsed));
978 } 988 }
979 #endif // !defined(OS_MACOSX) 989 #endif // !defined(OS_MACOSX)
990
991 TEST_F(ProfileManagerTest, ProfileDisplayNameResetsDefaultName) {
992 // The command line is reset at the end of every test by the test suite.
993 CommandLine::ForCurrentProcess()->AppendSwitch(
994 switches::kNewProfileManagement);
995
996 ProfileManager* profile_manager = g_browser_process->profile_manager();
997 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
998 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
999
1000 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1001 const base::string16 default_profile_name =
1002 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1003 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1004 Profile* profile1 = AddProfileToCache(profile_manager,
1005 "path_1", profile_name1);
1006 EXPECT_EQ(default_profile_name, profiles::GetAvatarNameForProfile(profile1));
1007
1008 // Multiple profiles means displaying the actual profile names.
1009 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1010 Profile* profile2 = AddProfileToCache(profile_manager,
1011 "path_2", profile_name2);
1012 EXPECT_EQ(profile_name1, profiles::GetAvatarNameForProfile(profile1));
1013 EXPECT_EQ(profile_name2, profiles::GetAvatarNameForProfile(profile2));
1014
1015 // Deleting a profile means returning to the default name.
1016 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1017 ProfileManager::CreateCallback());
1018 // Spin the message loop so that all the callbacks can finish running.
1019 base::RunLoop().RunUntilIdle();
1020 EXPECT_EQ(default_profile_name, profiles::GetAvatarNameForProfile(profile1));
1021 }
1022
1023 TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesCustomName) {
1024 // The command line is reset at the end of every test by the test suite.
1025 CommandLine::ForCurrentProcess()->AppendSwitch(
1026 switches::kNewProfileManagement);
1027
1028 ProfileManager* profile_manager = g_browser_process->profile_manager();
1029 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1030 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1031
1032 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1033 const base::string16 default_profile_name =
1034 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1035 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1036 Profile* profile1 = AddProfileToCache(profile_manager,
1037 "path_1", profile_name1);
1038 EXPECT_EQ(default_profile_name, profiles::GetAvatarNameForProfile(profile1));
1039
1040 // We should display custom names for local profiles.
1041 const base::string16 custom_profile_name = ASCIIToUTF16("Batman");
1042 cache.SetNameOfProfileAtIndex(0, custom_profile_name);
1043 EXPECT_EQ(custom_profile_name, cache.GetNameOfProfileAtIndex(0));
1044 EXPECT_EQ(custom_profile_name, profiles::GetAvatarNameForProfile(profile1));
1045
1046 // Multiple profiles means displaying the actual profile names.
1047 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1048 Profile* profile2 = AddProfileToCache(profile_manager,
1049 "path_2", profile_name2);
1050 EXPECT_EQ(custom_profile_name, profiles::GetAvatarNameForProfile(profile1));
1051 EXPECT_EQ(profile_name2, profiles::GetAvatarNameForProfile(profile2));
1052
1053 // Deleting a profile means returning to the original, custom name.
1054 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1055 ProfileManager::CreateCallback());
1056 // Spin the message loop so that all the callbacks can finish running.
1057 base::RunLoop().RunUntilIdle();
1058 EXPECT_EQ(custom_profile_name, profiles::GetAvatarNameForProfile(profile1));
1059 }
1060
1061 TEST_F(ProfileManagerTest, ProfileDisplayNamePreservesSignedInName) {
1062 // The command line is reset at the end of every test by the test suite.
1063 CommandLine::ForCurrentProcess()->AppendSwitch(
1064 switches::kNewProfileManagement);
1065
1066 ProfileManager* profile_manager = g_browser_process->profile_manager();
1067 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
1068 EXPECT_EQ(0u, cache.GetNumberOfProfiles());
1069
1070 // Only one local profile means we display IDS_SINGLE_PROFILE_DISPLAY_NAME.
1071 const base::string16 default_profile_name =
1072 l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
1073 const base::string16 profile_name1 = cache.ChooseNameForNewProfile(0);
1074 Profile* profile1 = AddProfileToCache(profile_manager,
1075 "path_1", profile_name1);
1076 EXPECT_EQ(default_profile_name, profiles::GetAvatarNameForProfile(profile1));
1077
1078 // We should display the actual profile name for signed in profiles.
1079 cache.SetUserNameOfProfileAtIndex(0, ASCIIToUTF16("user@gmail.com"));
1080 EXPECT_EQ(profile_name1, cache.GetNameOfProfileAtIndex(0));
1081 EXPECT_EQ(profile_name1, profiles::GetAvatarNameForProfile(profile1));
1082
1083 // Multiple profiles means displaying the actual profile names.
1084 const base::string16 profile_name2 = cache.ChooseNameForNewProfile(1);
1085 Profile* profile2 = AddProfileToCache(profile_manager,
1086 "path_2", profile_name2);
1087 EXPECT_EQ(profile_name1, profiles::GetAvatarNameForProfile(profile1));
1088 EXPECT_EQ(profile_name2, profiles::GetAvatarNameForProfile(profile2));
1089
1090 // Deleting a profile means returning to the original, actual profile name.
1091 profile_manager->ScheduleProfileForDeletion(profile2->GetPath(),
1092 ProfileManager::CreateCallback());
1093 // Spin the message loop so that all the callbacks can finish running.
1094 base::RunLoop().RunUntilIdle();
1095 EXPECT_EQ(profile_name1, profiles::GetAvatarNameForProfile(profile1));
1096 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698