Fix issue with browser action toolbar putting all extension icons in overflow once...
[chromium-blink-merge.git] / chrome / test / base / testing_profile_manager.cc
blobcd3eb5d31884c28c154035e616a4dd4ace359bde
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";
18 namespace testing {
20 class ProfileManager : public ::ProfileManagerWithoutInit {
21 public:
22 explicit ProfileManager(const base::FilePath& user_data_dir)
23 : ::ProfileManagerWithoutInit(user_data_dir) {}
25 protected:
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() {
47 SetUpInternal();
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,
55 int avatar_id,
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
85 // last.
86 cache.SetNameOfProfileAtIndex(index, user_name);
88 testing_profiles_.insert(std::make_pair(profile_name, profile));
90 return 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 // Set up a profile with an off the record profile.
105 TestingProfile::Builder otr_builder;
106 otr_builder.SetIncognito();
107 scoped_ptr<TestingProfile> otr_profile(otr_builder.Build());
109 // Create the profile and register it.
110 TestingProfile::Builder builder;
111 builder.SetGuestSession();
112 builder.SetPath(ProfileManager::GetGuestProfilePath());
114 // Add the guest profile to the profile manager, but not to the info cache.
115 TestingProfile* profile = builder.Build().release();
116 profile->set_profile_name(kGuestProfileName);
118 otr_profile->SetOriginalProfile(profile);
119 profile->SetOffTheRecordProfile(otr_profile.PassAs<Profile>());
120 profile_manager_->AddProfile(profile); // Takes ownership.
121 profile_manager_->SetGuestProfilePrefs(profile);
123 testing_profiles_.insert(std::make_pair(kGuestProfileName, profile));
125 return profile;
128 void TestingProfileManager::DeleteTestingProfile(const std::string& name) {
129 DCHECK(called_set_up_);
131 TestingProfilesMap::iterator it = testing_profiles_.find(name);
132 DCHECK(it != testing_profiles_.end());
134 TestingProfile* profile = it->second;
136 ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
137 cache.DeleteProfileFromCache(profile->GetPath());
139 profile_manager_->profiles_info_.erase(profile->GetPath());
142 void TestingProfileManager::DeleteGuestProfile() {
143 DCHECK(called_set_up_);
145 TestingProfilesMap::iterator it = testing_profiles_.find(kGuestProfileName);
146 DCHECK(it != testing_profiles_.end());
148 profile_manager_->profiles_info_.erase(ProfileManager::GetGuestProfilePath());
151 void TestingProfileManager::DeleteProfileInfoCache() {
152 profile_manager_->profile_info_cache_.reset(NULL);
155 void TestingProfileManager::SetLoggedIn(bool logged_in) {
156 profile_manager_->logged_in_ = logged_in;
159 const base::FilePath& TestingProfileManager::profiles_dir() {
160 DCHECK(called_set_up_);
161 return profiles_dir_.path();
164 ProfileManager* TestingProfileManager::profile_manager() {
165 DCHECK(called_set_up_);
166 return profile_manager_;
169 ProfileInfoCache* TestingProfileManager::profile_info_cache() {
170 DCHECK(called_set_up_);
171 return &profile_manager_->GetProfileInfoCache();
174 void TestingProfileManager::SetUpInternal() {
175 ASSERT_FALSE(browser_process_->profile_manager())
176 << "ProfileManager already exists";
178 // Set up the directory for profiles.
179 ASSERT_TRUE(profiles_dir_.CreateUniqueTempDir());
181 profile_manager_ = new testing::ProfileManager(profiles_dir_.path());
182 browser_process_->SetProfileManager(profile_manager_); // Takes ownership.
184 called_set_up_ = true;