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/prefs/pref_service_syncable.h"
10 #include "chrome/browser/profiles/profile_info_cache.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/common/chrome_constants.h"
13 #include "chrome/test/base/testing_browser_process.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
<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
, scoped_ptr
<PrefServiceSyncable
>(),
113 base::UTF8ToUTF16(name
), 0, std::string(),
114 TestingProfile::TestingFactories());
117 TestingProfile
* TestingProfileManager::CreateGuestProfile() {
118 DCHECK(called_set_up_
);
120 // Create the profile and register it.
121 TestingProfile::Builder builder
;
122 builder
.SetGuestSession();
123 builder
.SetPath(ProfileManager::GetGuestProfilePath());
125 // Add the guest profile to the profile manager, but not to the info cache.
126 TestingProfile
* profile
= builder
.Build().release();
127 profile
->set_profile_name(kGuestProfileName
);
129 // Set up a profile with an off the record profile.
130 TestingProfile::Builder().BuildIncognito(profile
);
132 profile_manager_
->AddProfile(profile
); // Takes ownership.
133 profile_manager_
->SetGuestProfilePrefs(profile
);
135 testing_profiles_
.insert(std::make_pair(kGuestProfileName
, profile
));
140 TestingProfile
* TestingProfileManager::CreateSystemProfile() {
141 DCHECK(called_set_up_
);
143 // Create the profile and register it.
144 TestingProfile::Builder builder
;
145 builder
.SetPath(ProfileManager::GetSystemProfilePath());
147 // Add the system profile to the profile manager, but not to the info cache.
148 TestingProfile
* profile
= builder
.Build().release();
149 profile
->set_profile_name(kSystemProfileName
);
151 profile_manager_
->AddProfile(profile
); // Takes ownership.
153 testing_profiles_
.insert(std::make_pair(kSystemProfileName
, profile
));
158 void TestingProfileManager::DeleteTestingProfile(const std::string
& name
) {
159 DCHECK(called_set_up_
);
161 TestingProfilesMap::iterator it
= testing_profiles_
.find(name
);
162 DCHECK(it
!= testing_profiles_
.end());
164 TestingProfile
* profile
= it
->second
;
166 ProfileInfoCache
& cache
= profile_manager_
->GetProfileInfoCache();
167 cache
.DeleteProfileFromCache(profile
->GetPath());
169 profile_manager_
->profiles_info_
.erase(profile
->GetPath());
171 testing_profiles_
.erase(it
);
174 void TestingProfileManager::DeleteAllTestingProfiles() {
175 for (TestingProfilesMap::iterator it
= testing_profiles_
.begin();
176 it
!= testing_profiles_
.end(); ++it
) {
177 TestingProfile
* profile
= it
->second
;
178 ProfileInfoCache
& cache
= profile_manager_
->GetProfileInfoCache();
179 cache
.DeleteProfileFromCache(profile
->GetPath());
181 testing_profiles_
.clear();
185 void TestingProfileManager::DeleteGuestProfile() {
186 DCHECK(called_set_up_
);
188 TestingProfilesMap::iterator it
= testing_profiles_
.find(kGuestProfileName
);
189 DCHECK(it
!= testing_profiles_
.end());
191 profile_manager_
->profiles_info_
.erase(ProfileManager::GetGuestProfilePath());
194 void TestingProfileManager::DeleteSystemProfile() {
195 DCHECK(called_set_up_
);
197 TestingProfilesMap::iterator it
= testing_profiles_
.find(kSystemProfileName
);
198 DCHECK(it
!= testing_profiles_
.end());
200 profile_manager_
->profiles_info_
.erase(
201 ProfileManager::GetSystemProfilePath());
204 void TestingProfileManager::DeleteProfileInfoCache() {
205 profile_manager_
->profile_info_cache_
.reset(NULL
);
208 void TestingProfileManager::SetLoggedIn(bool logged_in
) {
209 profile_manager_
->logged_in_
= logged_in
;
212 void TestingProfileManager::UpdateLastUser(Profile
* last_active
) {
213 #if !defined(OS_ANDROID) && !defined(OS_IOS)
214 profile_manager_
->UpdateLastUser(last_active
);
218 const base::FilePath
& TestingProfileManager::profiles_dir() {
219 DCHECK(called_set_up_
);
220 return profiles_dir_
.path();
223 ProfileManager
* TestingProfileManager::profile_manager() {
224 DCHECK(called_set_up_
);
225 return profile_manager_
;
228 ProfileInfoCache
* TestingProfileManager::profile_info_cache() {
229 DCHECK(called_set_up_
);
230 return &profile_manager_
->GetProfileInfoCache();
233 void TestingProfileManager::SetUpInternal() {
234 ASSERT_FALSE(browser_process_
->profile_manager())
235 << "ProfileManager already exists";
237 // Set up the directory for profiles.
238 ASSERT_TRUE(profiles_dir_
.CreateUniqueTempDir());
240 profile_manager_
= new testing::ProfileManager(profiles_dir_
.path());
241 browser_process_
->SetProfileManager(profile_manager_
); // Takes ownership.
243 called_set_up_
= true;