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 "chrome/test/base/testing_profile.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 class ProfileManager : public ::ProfileManagerWithoutInit {
21 explicit ProfileManager(const base::FilePath& user_data_dir)
22 : ::ProfileManagerWithoutInit(user_data_dir) {}
25 virtual Profile* CreateProfileHelper(
26 const base::FilePath& file_path) OVERRIDE {
27 return new TestingProfile(file_path);
31 } // namespace testing
33 TestingProfileManager::TestingProfileManager(TestingBrowserProcess* process)
34 : called_set_up_(false),
35 browser_process_(process),
36 local_state_(process) {
39 TestingProfileManager::~TestingProfileManager() {
42 bool TestingProfileManager::SetUp() {
44 return called_set_up_;
47 TestingProfile* TestingProfileManager::CreateTestingProfile(
48 const std::string& profile_name,
49 scoped_ptr<PrefServiceSyncable> prefs,
50 const string16& user_name,
52 const std::string& managed_user_id) {
53 DCHECK(called_set_up_);
55 // Create a path for the profile based on the name.
56 base::FilePath profile_path(profiles_dir_.path());
57 profile_path = profile_path.AppendASCII(profile_name);
59 // Create the profile and register it.
60 TestingProfile::Builder builder;
61 builder.SetPath(profile_path);
62 builder.SetPrefService(prefs.Pass());
63 builder.SetManagedUserId(managed_user_id);
65 TestingProfile* profile = builder.Build().release();
66 profile_manager_->AddProfile(profile); // Takes ownership.
68 // Update the user metadata.
69 ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
70 size_t index = cache.GetIndexOfProfileWithPath(profile_path);
71 cache.SetAvatarIconOfProfileAtIndex(index, avatar_id);
72 cache.SetManagedUserIdOfProfileAtIndex(index, managed_user_id);
73 // SetNameOfProfileAtIndex may reshuffle the list of profiles, so we do it
75 cache.SetNameOfProfileAtIndex(index, user_name);
77 testing_profiles_.insert(std::make_pair(profile_name, profile));
82 TestingProfile* TestingProfileManager::CreateTestingProfile(
83 const std::string& name) {
84 DCHECK(called_set_up_);
85 return CreateTestingProfile(name, scoped_ptr<PrefServiceSyncable>(),
86 UTF8ToUTF16(name), 0, std::string());
89 void TestingProfileManager::DeleteTestingProfile(const std::string& name) {
90 DCHECK(called_set_up_);
92 TestingProfilesMap::iterator it = testing_profiles_.find(name);
93 DCHECK(it != testing_profiles_.end());
95 TestingProfile* profile = it->second;
97 ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
98 cache.DeleteProfileFromCache(profile->GetPath());
100 profile_manager_->profiles_info_.erase(profile->GetPath());
103 void TestingProfileManager::DeleteProfileInfoCache() {
104 profile_manager_->profile_info_cache_.reset(NULL);
107 void TestingProfileManager::SetLoggedIn(bool logged_in) {
108 profile_manager_->logged_in_ = logged_in;
111 const base::FilePath& TestingProfileManager::profiles_dir() {
112 DCHECK(called_set_up_);
113 return profiles_dir_.path();
116 ProfileManager* TestingProfileManager::profile_manager() {
117 DCHECK(called_set_up_);
118 return profile_manager_;
121 ProfileInfoCache* TestingProfileManager::profile_info_cache() {
122 DCHECK(called_set_up_);
123 return &profile_manager_->GetProfileInfoCache();
126 void TestingProfileManager::SetUpInternal() {
127 ASSERT_FALSE(browser_process_->profile_manager())
128 << "ProfileManager already exists";
130 // Set up the directory for profiles.
131 ASSERT_TRUE(profiles_dir_.CreateUniqueTempDir());
133 profile_manager_ = new testing::ProfileManager(profiles_dir_.path());
134 browser_process_->SetProfileManager(profile_manager_); // Takes ownership.
136 called_set_up_ = true;