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/common/chrome_constants.h"
14 #include "chrome/test/base/testing_browser_process.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 #if defined(OS_CHROMEOS)
18 #include "chrome/browser/chromeos/profiles/profile_helper.h"
21 const std::string kGuestProfileName = "Guest";
25 class ProfileManager : public ::ProfileManagerWithoutInit {
27 explicit ProfileManager(const base::FilePath& user_data_dir)
28 : ::ProfileManagerWithoutInit(user_data_dir) {}
31 virtual Profile* CreateProfileHelper(
32 const base::FilePath& file_path) OVERRIDE {
33 return new TestingProfile(file_path);
37 } // namespace testing
39 TestingProfileManager::TestingProfileManager(TestingBrowserProcess* process)
40 : called_set_up_(false),
41 browser_process_(process),
42 local_state_(process),
43 profile_manager_(NULL) {
46 TestingProfileManager::~TestingProfileManager() {
47 // Destroying this class also destroys the LocalState, so make sure the
48 // associated ProfileManager is also destroyed.
49 browser_process_->SetProfileManager(NULL);
52 bool TestingProfileManager::SetUp() {
54 return called_set_up_;
57 TestingProfile* TestingProfileManager::CreateTestingProfile(
58 const std::string& profile_name,
59 scoped_ptr<PrefServiceSyncable> prefs,
60 const base::string16& user_name,
62 const std::string& supervised_user_id,
63 const TestingProfile::TestingFactories& factories) {
64 DCHECK(called_set_up_);
66 // Create a path for the profile based on the name.
67 base::FilePath profile_path(profiles_dir_.path());
68 #if defined(OS_CHROMEOS)
69 if (profile_name != chrome::kInitialProfile) {
71 profile_path.Append(chromeos::ProfileHelper::Get()->GetUserProfileDir(
72 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(
75 profile_path = profile_path.AppendASCII(profile_name);
78 profile_path = profile_path.AppendASCII(profile_name);
81 // Create the profile and register it.
82 TestingProfile::Builder builder;
83 builder.SetPath(profile_path);
84 builder.SetPrefService(prefs.Pass());
85 builder.SetSupervisedUserId(supervised_user_id);
87 for (TestingProfile::TestingFactories::const_iterator it = factories.begin();
88 it != factories.end(); ++it) {
89 builder.AddTestingFactory(it->first, it->second);
92 TestingProfile* profile = builder.Build().release();
93 profile->set_profile_name(profile_name);
94 profile_manager_->AddProfile(profile); // Takes ownership.
96 // Update the user metadata.
97 ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
98 size_t index = cache.GetIndexOfProfileWithPath(profile_path);
99 cache.SetAvatarIconOfProfileAtIndex(index, avatar_id);
100 cache.SetSupervisedUserIdOfProfileAtIndex(index, supervised_user_id);
101 // SetNameOfProfileAtIndex may reshuffle the list of profiles, so we do it
103 cache.SetNameOfProfileAtIndex(index, user_name);
105 testing_profiles_.insert(std::make_pair(profile_name, profile));
110 TestingProfile* TestingProfileManager::CreateTestingProfile(
111 const std::string& name) {
112 DCHECK(called_set_up_);
113 return CreateTestingProfile(name, scoped_ptr<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_->SetGuestProfilePrefs(profile);
136 testing_profiles_.insert(std::make_pair(kGuestProfileName, profile));
141 void TestingProfileManager::DeleteTestingProfile(const std::string& name) {
142 DCHECK(called_set_up_);
144 TestingProfilesMap::iterator it = testing_profiles_.find(name);
145 DCHECK(it != testing_profiles_.end());
147 TestingProfile* profile = it->second;
149 ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
150 cache.DeleteProfileFromCache(profile->GetPath());
152 profile_manager_->profiles_info_.erase(profile->GetPath());
155 void TestingProfileManager::DeleteAllTestingProfiles() {
156 for (TestingProfilesMap::iterator it = testing_profiles_.begin();
157 it != testing_profiles_.end(); ++it) {
158 TestingProfile* profile = it->second;
159 ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
160 cache.DeleteProfileFromCache(profile->GetPath());
162 testing_profiles_.clear();
166 void TestingProfileManager::DeleteGuestProfile() {
167 DCHECK(called_set_up_);
169 TestingProfilesMap::iterator it = testing_profiles_.find(kGuestProfileName);
170 DCHECK(it != testing_profiles_.end());
172 profile_manager_->profiles_info_.erase(ProfileManager::GetGuestProfilePath());
175 void TestingProfileManager::DeleteProfileInfoCache() {
176 profile_manager_->profile_info_cache_.reset(NULL);
179 void TestingProfileManager::SetLoggedIn(bool logged_in) {
180 profile_manager_->logged_in_ = logged_in;
183 const base::FilePath& TestingProfileManager::profiles_dir() {
184 DCHECK(called_set_up_);
185 return profiles_dir_.path();
188 ProfileManager* TestingProfileManager::profile_manager() {
189 DCHECK(called_set_up_);
190 return profile_manager_;
193 ProfileInfoCache* TestingProfileManager::profile_info_cache() {
194 DCHECK(called_set_up_);
195 return &profile_manager_->GetProfileInfoCache();
198 void TestingProfileManager::SetUpInternal() {
199 ASSERT_FALSE(browser_process_->profile_manager())
200 << "ProfileManager already exists";
202 // Set up the directory for profiles.
203 ASSERT_TRUE(profiles_dir_.CreateUniqueTempDir());
205 profile_manager_ = new testing::ProfileManager(profiles_dir_.path());
206 browser_process_->SetProfileManager(profile_manager_); // Takes ownership.
208 called_set_up_ = true;