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";
24 class ProfileManager
: public ::ProfileManagerWithoutInit
{
26 explicit ProfileManager(const base::FilePath
& user_data_dir
)
27 : ::ProfileManagerWithoutInit(user_data_dir
) {}
30 Profile
* CreateProfileHelper(const base::FilePath
& file_path
) override
{
31 return new TestingProfile(file_path
);
35 } // namespace testing
37 TestingProfileManager::TestingProfileManager(TestingBrowserProcess
* process
)
38 : called_set_up_(false),
39 browser_process_(process
),
40 local_state_(process
),
41 profile_manager_(NULL
) {
44 TestingProfileManager::~TestingProfileManager() {
45 // Destroying this class also destroys the LocalState, so make sure the
46 // associated ProfileManager is also destroyed.
47 browser_process_
->SetProfileManager(NULL
);
50 bool TestingProfileManager::SetUp() {
52 return called_set_up_
;
55 TestingProfile
* TestingProfileManager::CreateTestingProfile(
56 const std::string
& profile_name
,
57 scoped_ptr
<PrefServiceSyncable
> prefs
,
58 const base::string16
& user_name
,
60 const std::string
& supervised_user_id
,
61 const TestingProfile::TestingFactories
& factories
) {
62 DCHECK(called_set_up_
);
64 // Create a path for the profile based on the name.
65 base::FilePath
profile_path(profiles_dir_
.path());
66 #if defined(OS_CHROMEOS)
67 if (profile_name
!= chrome::kInitialProfile
) {
69 profile_path
.Append(chromeos::ProfileHelper::Get()->GetUserProfileDir(
70 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(
73 profile_path
= profile_path
.AppendASCII(profile_name
);
76 profile_path
= profile_path
.AppendASCII(profile_name
);
79 // Create the profile and register it.
80 TestingProfile::Builder builder
;
81 builder
.SetPath(profile_path
);
82 builder
.SetPrefService(prefs
.Pass());
83 builder
.SetSupervisedUserId(supervised_user_id
);
85 for (TestingProfile::TestingFactories::const_iterator it
= factories
.begin();
86 it
!= factories
.end(); ++it
) {
87 builder
.AddTestingFactory(it
->first
, it
->second
);
90 TestingProfile
* profile
= builder
.Build().release();
91 profile
->set_profile_name(profile_name
);
92 profile_manager_
->AddProfile(profile
); // Takes ownership.
94 // Update the user metadata.
95 ProfileInfoCache
& cache
= profile_manager_
->GetProfileInfoCache();
96 size_t index
= cache
.GetIndexOfProfileWithPath(profile_path
);
97 cache
.SetAvatarIconOfProfileAtIndex(index
, avatar_id
);
98 cache
.SetSupervisedUserIdOfProfileAtIndex(index
, supervised_user_id
);
99 // SetNameOfProfileAtIndex may reshuffle the list of profiles, so we do it
101 cache
.SetNameOfProfileAtIndex(index
, user_name
);
103 testing_profiles_
.insert(std::make_pair(profile_name
, profile
));
108 TestingProfile
* TestingProfileManager::CreateTestingProfile(
109 const std::string
& name
) {
110 DCHECK(called_set_up_
);
111 return CreateTestingProfile(name
, scoped_ptr
<PrefServiceSyncable
>(),
112 base::UTF8ToUTF16(name
), 0, std::string(),
113 TestingProfile::TestingFactories());
116 TestingProfile
* TestingProfileManager::CreateGuestProfile() {
117 DCHECK(called_set_up_
);
119 // Create the profile and register it.
120 TestingProfile::Builder builder
;
121 builder
.SetGuestSession();
122 builder
.SetPath(ProfileManager::GetGuestProfilePath());
124 // Add the guest profile to the profile manager, but not to the info cache.
125 TestingProfile
* profile
= builder
.Build().release();
126 profile
->set_profile_name(kGuestProfileName
);
128 // Set up a profile with an off the record profile.
129 TestingProfile::Builder().BuildIncognito(profile
);
131 profile_manager_
->AddProfile(profile
); // Takes ownership.
132 profile_manager_
->SetGuestProfilePrefs(profile
);
134 testing_profiles_
.insert(std::make_pair(kGuestProfileName
, profile
));
139 void TestingProfileManager::DeleteTestingProfile(const std::string
& name
) {
140 DCHECK(called_set_up_
);
142 TestingProfilesMap::iterator it
= testing_profiles_
.find(name
);
143 DCHECK(it
!= testing_profiles_
.end());
145 TestingProfile
* profile
= it
->second
;
147 ProfileInfoCache
& cache
= profile_manager_
->GetProfileInfoCache();
148 cache
.DeleteProfileFromCache(profile
->GetPath());
150 profile_manager_
->profiles_info_
.erase(profile
->GetPath());
153 void TestingProfileManager::DeleteAllTestingProfiles() {
154 for (TestingProfilesMap::iterator it
= testing_profiles_
.begin();
155 it
!= testing_profiles_
.end(); ++it
) {
156 TestingProfile
* profile
= it
->second
;
157 ProfileInfoCache
& cache
= profile_manager_
->GetProfileInfoCache();
158 cache
.DeleteProfileFromCache(profile
->GetPath());
160 testing_profiles_
.clear();
164 void TestingProfileManager::DeleteGuestProfile() {
165 DCHECK(called_set_up_
);
167 TestingProfilesMap::iterator it
= testing_profiles_
.find(kGuestProfileName
);
168 DCHECK(it
!= testing_profiles_
.end());
170 profile_manager_
->profiles_info_
.erase(ProfileManager::GetGuestProfilePath());
173 void TestingProfileManager::DeleteProfileInfoCache() {
174 profile_manager_
->profile_info_cache_
.reset(NULL
);
177 void TestingProfileManager::SetLoggedIn(bool logged_in
) {
178 profile_manager_
->logged_in_
= logged_in
;
181 void TestingProfileManager::UpdateLastUser(Profile
* last_active
) {
182 #if !defined(OS_ANDROID) && !defined(OS_IOS)
183 profile_manager_
->UpdateLastUser(last_active
);
187 const base::FilePath
& TestingProfileManager::profiles_dir() {
188 DCHECK(called_set_up_
);
189 return profiles_dir_
.path();
192 ProfileManager
* TestingProfileManager::profile_manager() {
193 DCHECK(called_set_up_
);
194 return profile_manager_
;
197 ProfileInfoCache
* TestingProfileManager::profile_info_cache() {
198 DCHECK(called_set_up_
);
199 return &profile_manager_
->GetProfileInfoCache();
202 void TestingProfileManager::SetUpInternal() {
203 ASSERT_FALSE(browser_process_
->profile_manager())
204 << "ProfileManager already exists";
206 // Set up the directory for profiles.
207 ASSERT_TRUE(profiles_dir_
.CreateUniqueTempDir());
209 profile_manager_
= new testing::ProfileManager(profiles_dir_
.path());
210 browser_process_
->SetProfileManager(profile_manager_
); // Takes ownership.
212 called_set_up_
= true;