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 "chrome/test/base/testing_profile_manager.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/profiles/profile_info_cache.h"
10 #include "chrome/browser/profiles/profile_manager.h"
11 #include "chrome/common/chrome_constants.h"
12 #include "chrome/test/base/testing_browser_process.h"
13 #include "components/syncable_prefs/pref_service_syncable.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 #if defined(OS_CHROMEOS)
17 #include "chrome/browser/chromeos/profiles/profile_helper.h"
20 const char kGuestProfileName
[] = "Guest";
21 const char kSystemProfileName
[] = "System";
25 class ProfileManager
: public ::ProfileManagerWithoutInit
{
27 explicit ProfileManager(const base::FilePath
& user_data_dir
)
28 : ::ProfileManagerWithoutInit(user_data_dir
) {}
31 Profile
* CreateProfileHelper(const base::FilePath
& file_path
) override
{
32 return new TestingProfile(file_path
);
36 } // namespace testing
38 TestingProfileManager::TestingProfileManager(TestingBrowserProcess
* process
)
39 : called_set_up_(false),
40 browser_process_(process
),
41 local_state_(process
),
42 profile_manager_(NULL
) {
45 TestingProfileManager::~TestingProfileManager() {
46 // Destroying this class also destroys the LocalState, so make sure the
47 // associated ProfileManager is also destroyed.
48 browser_process_
->SetProfileManager(NULL
);
51 bool TestingProfileManager::SetUp() {
53 return called_set_up_
;
56 TestingProfile
* TestingProfileManager::CreateTestingProfile(
57 const std::string
& profile_name
,
58 scoped_ptr
<syncable_prefs::PrefServiceSyncable
> prefs
,
59 const base::string16
& user_name
,
61 const std::string
& supervised_user_id
,
62 const TestingProfile::TestingFactories
& factories
) {
63 DCHECK(called_set_up_
);
65 // Create a path for the profile based on the name.
66 base::FilePath
profile_path(profiles_dir_
.path());
67 #if defined(OS_CHROMEOS)
68 if (profile_name
!= chrome::kInitialProfile
) {
70 profile_path
.Append(chromeos::ProfileHelper::Get()->GetUserProfileDir(
71 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(
74 profile_path
= profile_path
.AppendASCII(profile_name
);
77 profile_path
= profile_path
.AppendASCII(profile_name
);
80 // Create the profile and register it.
81 TestingProfile::Builder builder
;
82 builder
.SetPath(profile_path
);
83 builder
.SetPrefService(prefs
.Pass());
84 builder
.SetSupervisedUserId(supervised_user_id
);
86 for (TestingProfile::TestingFactories::const_iterator it
= factories
.begin();
87 it
!= factories
.end(); ++it
) {
88 builder
.AddTestingFactory(it
->first
, it
->second
);
91 TestingProfile
* profile
= builder
.Build().release();
92 profile
->set_profile_name(profile_name
);
93 profile_manager_
->AddProfile(profile
); // Takes ownership.
95 // Update the user metadata.
96 ProfileInfoCache
& cache
= profile_manager_
->GetProfileInfoCache();
97 size_t index
= cache
.GetIndexOfProfileWithPath(profile_path
);
98 cache
.SetAvatarIconOfProfileAtIndex(index
, avatar_id
);
99 cache
.SetSupervisedUserIdOfProfileAtIndex(index
, supervised_user_id
);
100 // SetNameOfProfileAtIndex may reshuffle the list of profiles, so we do it
102 cache
.SetNameOfProfileAtIndex(index
, user_name
);
104 testing_profiles_
.insert(std::make_pair(profile_name
, profile
));
109 TestingProfile
* TestingProfileManager::CreateTestingProfile(
110 const std::string
& name
) {
111 DCHECK(called_set_up_
);
112 return CreateTestingProfile(name
,
113 scoped_ptr
<syncable_prefs::PrefServiceSyncable
>(),
114 base::UTF8ToUTF16(name
), 0, std::string(),
115 TestingProfile::TestingFactories());
118 TestingProfile
* TestingProfileManager::CreateGuestProfile() {
119 DCHECK(called_set_up_
);
121 // Create the profile and register it.
122 TestingProfile::Builder builder
;
123 builder
.SetGuestSession();
124 builder
.SetPath(ProfileManager::GetGuestProfilePath());
126 // Add the guest profile to the profile manager, but not to the info cache.
127 TestingProfile
* profile
= builder
.Build().release();
128 profile
->set_profile_name(kGuestProfileName
);
130 // Set up a profile with an off the record profile.
131 TestingProfile::Builder().BuildIncognito(profile
);
133 profile_manager_
->AddProfile(profile
); // Takes ownership.
134 profile_manager_
->SetNonPersonalProfilePrefs(profile
);
136 testing_profiles_
.insert(std::make_pair(kGuestProfileName
, profile
));
141 TestingProfile
* TestingProfileManager::CreateSystemProfile() {
142 DCHECK(called_set_up_
);
144 // Create the profile and register it.
145 TestingProfile::Builder builder
;
146 builder
.SetPath(ProfileManager::GetSystemProfilePath());
148 // Add the system profile to the profile manager, but not to the info cache.
149 TestingProfile
* profile
= builder
.Build().release();
150 profile
->set_profile_name(kSystemProfileName
);
152 profile_manager_
->AddProfile(profile
); // Takes ownership.
154 testing_profiles_
.insert(std::make_pair(kSystemProfileName
, profile
));
159 void TestingProfileManager::DeleteTestingProfile(const std::string
& name
) {
160 DCHECK(called_set_up_
);
162 TestingProfilesMap::iterator it
= testing_profiles_
.find(name
);
163 DCHECK(it
!= testing_profiles_
.end());
165 TestingProfile
* profile
= it
->second
;
167 ProfileInfoCache
& cache
= profile_manager_
->GetProfileInfoCache();
168 cache
.DeleteProfileFromCache(profile
->GetPath());
170 profile_manager_
->profiles_info_
.erase(profile
->GetPath());
172 testing_profiles_
.erase(it
);
175 void TestingProfileManager::DeleteAllTestingProfiles() {
176 for (TestingProfilesMap::iterator it
= testing_profiles_
.begin();
177 it
!= testing_profiles_
.end(); ++it
) {
178 TestingProfile
* profile
= it
->second
;
179 ProfileInfoCache
& cache
= profile_manager_
->GetProfileInfoCache();
180 cache
.DeleteProfileFromCache(profile
->GetPath());
182 testing_profiles_
.clear();
186 void TestingProfileManager::DeleteGuestProfile() {
187 DCHECK(called_set_up_
);
189 TestingProfilesMap::iterator it
= testing_profiles_
.find(kGuestProfileName
);
190 DCHECK(it
!= testing_profiles_
.end());
192 profile_manager_
->profiles_info_
.erase(ProfileManager::GetGuestProfilePath());
195 void TestingProfileManager::DeleteSystemProfile() {
196 DCHECK(called_set_up_
);
198 TestingProfilesMap::iterator it
= testing_profiles_
.find(kSystemProfileName
);
199 DCHECK(it
!= testing_profiles_
.end());
201 profile_manager_
->profiles_info_
.erase(
202 ProfileManager::GetSystemProfilePath());
205 void TestingProfileManager::DeleteProfileInfoCache() {
206 profile_manager_
->profile_info_cache_
.reset(NULL
);
209 void TestingProfileManager::SetLoggedIn(bool logged_in
) {
210 profile_manager_
->logged_in_
= logged_in
;
213 void TestingProfileManager::UpdateLastUser(Profile
* last_active
) {
214 #if !defined(OS_ANDROID) && !defined(OS_IOS)
215 profile_manager_
->UpdateLastUser(last_active
);
219 const base::FilePath
& TestingProfileManager::profiles_dir() {
220 DCHECK(called_set_up_
);
221 return profiles_dir_
.path();
224 ProfileManager
* TestingProfileManager::profile_manager() {
225 DCHECK(called_set_up_
);
226 return profile_manager_
;
229 ProfileInfoCache
* TestingProfileManager::profile_info_cache() {
230 DCHECK(called_set_up_
);
231 return &profile_manager_
->GetProfileInfoCache();
234 void TestingProfileManager::SetUpInternal() {
235 ASSERT_FALSE(browser_process_
->profile_manager())
236 << "ProfileManager already exists";
238 // Set up the directory for profiles.
239 ASSERT_TRUE(profiles_dir_
.CreateUniqueTempDir());
241 profile_manager_
= new testing::ProfileManager(profiles_dir_
.path());
242 browser_process_
->SetProfileManager(profile_manager_
); // Takes ownership.
244 profile_manager_
->GetProfileInfoCache().
245 set_disable_avatar_download_for_testing(true);
246 called_set_up_
= true;