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/extensions/extension_special_storage_policy.h"
10 #include "chrome/browser/prefs/pref_service_syncable.h"
11 #include "chrome/browser/profiles/profile_info_cache.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/test/base/testing_browser_process.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 const std::string kGuestProfileName
= "Guest";
20 class ProfileManager
: public ::ProfileManagerWithoutInit
{
22 explicit ProfileManager(const base::FilePath
& user_data_dir
)
23 : ::ProfileManagerWithoutInit(user_data_dir
) {}
26 virtual Profile
* CreateProfileHelper(
27 const base::FilePath
& file_path
) OVERRIDE
{
28 return new TestingProfile(file_path
);
32 } // namespace testing
34 TestingProfileManager::TestingProfileManager(TestingBrowserProcess
* process
)
35 : called_set_up_(false),
36 browser_process_(process
),
37 local_state_(process
) {
40 TestingProfileManager::~TestingProfileManager() {
41 // Destroying this class also destroys the LocalState, so make sure the
42 // associated ProfileManager is also destroyed.
43 browser_process_
->SetProfileManager(NULL
);
46 bool TestingProfileManager::SetUp() {
48 return called_set_up_
;
51 TestingProfile
* TestingProfileManager::CreateTestingProfile(
52 const std::string
& profile_name
,
53 scoped_ptr
<PrefServiceSyncable
> prefs
,
54 const base::string16
& user_name
,
56 const std::string
& managed_user_id
,
57 const TestingProfile::TestingFactories
& factories
) {
58 DCHECK(called_set_up_
);
60 // Create a path for the profile based on the name.
61 base::FilePath
profile_path(profiles_dir_
.path());
62 profile_path
= profile_path
.AppendASCII(profile_name
);
64 // Create the profile and register it.
65 TestingProfile::Builder builder
;
66 builder
.SetPath(profile_path
);
67 builder
.SetPrefService(prefs
.Pass());
68 builder
.SetManagedUserId(managed_user_id
);
70 for (TestingProfile::TestingFactories::const_iterator it
= factories
.begin();
71 it
!= factories
.end(); ++it
) {
72 builder
.AddTestingFactory(it
->first
, it
->second
);
75 TestingProfile
* profile
= builder
.Build().release();
76 profile
->set_profile_name(profile_name
);
77 profile_manager_
->AddProfile(profile
); // Takes ownership.
79 // Update the user metadata.
80 ProfileInfoCache
& cache
= profile_manager_
->GetProfileInfoCache();
81 size_t index
= cache
.GetIndexOfProfileWithPath(profile_path
);
82 cache
.SetAvatarIconOfProfileAtIndex(index
, avatar_id
);
83 cache
.SetManagedUserIdOfProfileAtIndex(index
, managed_user_id
);
84 // SetNameOfProfileAtIndex may reshuffle the list of profiles, so we do it
86 cache
.SetNameOfProfileAtIndex(index
, user_name
);
88 testing_profiles_
.insert(std::make_pair(profile_name
, profile
));
93 TestingProfile
* TestingProfileManager::CreateTestingProfile(
94 const std::string
& name
) {
95 DCHECK(called_set_up_
);
96 return CreateTestingProfile(name
, scoped_ptr
<PrefServiceSyncable
>(),
97 base::UTF8ToUTF16(name
), 0, std::string(),
98 TestingProfile::TestingFactories());
101 TestingProfile
* TestingProfileManager::CreateGuestProfile() {
102 DCHECK(called_set_up_
);
104 // Create the profile and register it.
105 TestingProfile::Builder builder
;
106 builder
.SetGuestSession();
107 builder
.SetPath(ProfileManager::GetGuestProfilePath());
109 // Add the guest profile to the profile manager, but not to the info cache.
110 TestingProfile
* profile
= builder
.Build().release();
111 profile
->set_profile_name(kGuestProfileName
);
112 profile_manager_
->AddProfile(profile
); // Takes ownership.
113 profile_manager_
->SetGuestProfilePrefs(profile
);
115 testing_profiles_
.insert(std::make_pair(kGuestProfileName
, profile
));
120 void TestingProfileManager::DeleteTestingProfile(const std::string
& name
) {
121 DCHECK(called_set_up_
);
123 TestingProfilesMap::iterator it
= testing_profiles_
.find(name
);
124 DCHECK(it
!= testing_profiles_
.end());
126 TestingProfile
* profile
= it
->second
;
128 ProfileInfoCache
& cache
= profile_manager_
->GetProfileInfoCache();
129 cache
.DeleteProfileFromCache(profile
->GetPath());
131 profile_manager_
->profiles_info_
.erase(profile
->GetPath());
134 void TestingProfileManager::DeleteGuestProfile() {
135 DCHECK(called_set_up_
);
137 TestingProfilesMap::iterator it
= testing_profiles_
.find(kGuestProfileName
);
138 DCHECK(it
!= testing_profiles_
.end());
140 profile_manager_
->profiles_info_
.erase(ProfileManager::GetGuestProfilePath());
143 void TestingProfileManager::DeleteProfileInfoCache() {
144 profile_manager_
->profile_info_cache_
.reset(NULL
);
147 void TestingProfileManager::SetLoggedIn(bool logged_in
) {
148 profile_manager_
->logged_in_
= logged_in
;
151 const base::FilePath
& TestingProfileManager::profiles_dir() {
152 DCHECK(called_set_up_
);
153 return profiles_dir_
.path();
156 ProfileManager
* TestingProfileManager::profile_manager() {
157 DCHECK(called_set_up_
);
158 return profile_manager_
;
161 ProfileInfoCache
* TestingProfileManager::profile_info_cache() {
162 DCHECK(called_set_up_
);
163 return &profile_manager_
->GetProfileInfoCache();
166 void TestingProfileManager::SetUpInternal() {
167 ASSERT_FALSE(browser_process_
->profile_manager())
168 << "ProfileManager already exists";
170 // Set up the directory for profiles.
171 ASSERT_TRUE(profiles_dir_
.CreateUniqueTempDir());
173 profile_manager_
= new testing::ProfileManager(profiles_dir_
.path());
174 browser_process_
->SetProfileManager(profile_manager_
); // Takes ownership.
176 called_set_up_
= true;